Skip to content

Commit

Permalink
Expectation failed wrapped error (#13)
Browse files Browse the repository at this point in the history
* ExpectationFailedWrappedException must extend AssertionFailedError

ExpectationFailedException has become final.

* Update README.md
  • Loading branch information
jonathanjfshaw authored Dec 16, 2022
1 parent 11b82a9 commit 479d7b4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ More complex possibilities exist; see https://phpunit.de/manual/6.5/en/textui.ht

Phpunit will report any instance of ExpectationFailedException as a failure and other exceptions as errors. In some circumstances you may wish to display certain exceptions as errors. For example, if you're using Mink for web assertions, you may want to report Mink's ExpectationExpection (which is thrown when web content does not match an expectation) as a failure not an error.

To achieve this, in your test's base class you can catch these errors when scenario results are examined, and rethrow them using the provided ExpectationFailedWrappedException:
To achieve this, in your test's base class you can catch these errors when scenario results are examined, and rethrow them using the provided ExpectationFailedWrappedError:

```
use BehatTestTrait {
Expand All @@ -146,15 +146,12 @@ To achieve this, in your test's base class you can catch these errors when scena
self::assertBehatScenarioPassedTrait($scenarioResults, $scenario, $stepResults, $snippetGenerator, $environment, $message, $callHandler);
}
catch (\Behat\Mink\Exception\ExpectationException $e) {
throw new \PHPUnitBehat\PHPUnit\Framework\ExpectationFailedWrappedException($e);
throw new \PHPUnitBehat\PHPUnit\Framework\ExpectationFailedWrappedError($e);
}
}
}
```

## Notes
The code has not been tested with Behat's 'Outline' scenarios, but it should work.

## License

MIT.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

namespace PHPUnitBehat\PHPUnit\Framework;

use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\TestFailure;
use PHPUnit\Util\Filter;
use PHPUnit\Framework\ExpectationFailedException;

/**
* Allows wrapping an exception as an expectation failure.
*
* @see \PHPUnit\Framework\ExceptionWrapper and \PHPUnit\Framework\Exception
* @see \PHPUnit\Framework\ExceptionWrapper and \PHPUnit\Framework\AssertionFailedError
*/
class ExpectationFailedWrappedException extends ExpectationFailedException
class ExpectationFailedWrappedError extends AssertionFailedError
{

/**
Expand All @@ -28,14 +28,6 @@ public function __construct(\Throwable $wrapped)
$this->wrapped = $wrapped;
}

/**
* @inheritDoc
*/
public function getComparisonFailure()
{
return NULL;
}

/**
* @return string
*/
Expand Down

0 comments on commit 479d7b4

Please sign in to comment.