matthiasnoback / behat-expect-exception
Installs: 63 693
Dependents: 1
Suggesters: 0
Security: 0
Stars: 22
Watchers: 3
Forks: 0
Open Issues: 0
Requires (Dev)
- php: ^7.2
- behat/behat: ^3.5
This package is auto-updated.
Last update: 2024-10-24 04:18:02 UTC
README
Installation
composer require --dev matthiasnoback/behat-expect-exception
Purpose
This library lets you run code in one step definition that is expected to thrown an exception, then in another step definition allows you to verify that the correct exception was caught. Just like with PHPUnit you can compare the type of the caught exception to the expected type, and you can check if the actual exception message contains a given string.
Usage example
use Behat\Behat\Context\Context; use BehatExpectException\ExpectException; final class FeatureContext implements Context { // Use this trait in your feature context: use ExpectException; /** * @When I try to make a reservation for :numberOfSeats seats */ public function iTryToMakeAReservation(int $numberOfSeats): void { /* * Catch an exception using $this->shouldFail(). * If the code in the callable doesn't throw an exception, shouldFail() * itself will throw an ExpectedAnException exception. */ $this->shouldFail( function () use ($numberOfSeats) { // This will throw a CouldNotMakeReservation exception: $this->reservationService()->makeReservation($numberOfSeats); } ); } /** * @Then I should see an error message saying: :message */ public function confirmCaughtExceptionMatchesExpectedTypeAndMessage(string $message): void { $this->assertCaughtExceptionMatches( CouldNotMakeReservation::class, $message ); } /** * @When I make a reservation for :numberOfSeats seats */ public function iMakeAReservation(int $numberOfSeats): void { /* * Catch a possible exception using $this->mayFail(). * If the code in the callable doesn't throw an exception, * then it's not a problem. mayFail() doesn't throw an * ExpectedAnException exception itself in that case. * You can still use assertCaughtExceptionMatches(), but * it will throw an ExpectedAnException if no exception was * caught. */ $this->mayFail( function () use ($numberOfSeats) { // This might throw a CouldNotMakeReservation exception: $this->reservationService()->makeReservation($numberOfSeats); } ); } }