diff --git a/README.md b/README.md index 14406b2..809cf50 100644 --- a/README.md +++ b/README.md @@ -131,9 +131,9 @@ More complex possibilities exist; see https://phpunit.de/manual/6.5/en/textui.ht ## Failures and errors -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. +Phpunit will report any instance of AssertionFailedError 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 ExpectationFailedWrappedError: +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 AssertionFailedWrappedError: ``` use BehatTestTrait { @@ -146,10 +146,9 @@ 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\ExpectationFailedWrappedError($e); + throw new \PHPUnitBehat\PHPUnit\Framework\AssertionFailedWrappedError($e); } } -} ``` ## License diff --git a/src/PHPUnit/Framework/ExpectationFailedWrappedError.php b/src/PHPUnit/Framework/AssertionFailedWrappedError.php similarity index 76% rename from src/PHPUnit/Framework/ExpectationFailedWrappedError.php rename to src/PHPUnit/Framework/AssertionFailedWrappedError.php index 8858f8f..0c5429a 100644 --- a/src/PHPUnit/Framework/ExpectationFailedWrappedError.php +++ b/src/PHPUnit/Framework/AssertionFailedWrappedError.php @@ -11,11 +11,11 @@ * * @see \PHPUnit\Framework\ExceptionWrapper and \PHPUnit\Framework\AssertionFailedError */ -class ExpectationFailedWrappedError extends AssertionFailedError +class AssertionFailedWrappedError extends AssertionFailedError { /** - * The wrapped exception. + * The wrapped error. */ protected $wrapped; @@ -24,14 +24,14 @@ class ExpectationFailedWrappedError extends AssertionFailedError */ public function __construct(\Throwable $wrapped) { - parent::__construct($wrapped->getMessage(), NULL, $wrapped->getPrevious()); + parent::__construct($wrapped->getMessage(), $wrapped->getCode(), $wrapped->getPrevious()); $this->wrapped = $wrapped; } /** * @return string */ - public function __toString() + public function __toString(): string { $string = TestFailure::exceptionToString($this->wrapped); diff --git a/tests/AssertionFailedWrapperErrorTest.php b/tests/AssertionFailedWrapperErrorTest.php new file mode 100644 index 0000000..b391951 --- /dev/null +++ b/tests/AssertionFailedWrapperErrorTest.php @@ -0,0 +1,82 @@ +getTestBehatScenario(0); + $result = $this->executeTestBehatScenario($scenario); + $this->assertTestFailed($result); + $exceptionMessages = [ + "Scenario '#0 Error' had steps:", + "Failed: Given an error", + "A test error message", + ]; + $this->assertBehatScenarioAssertion($scenario, TestException::class, $exceptionMessages); + } + + /** + * Test scenario "#1 Error" + */ + public function testErrorToWrap() { + $scenario = $this->getTestBehatScenario(1); + $result = $this->executeTestBehatScenario($scenario); + $this->assertTestFailed($result); + $exceptionMessages = [ + "Scenario '#1 Error to wrap' had steps:", + "Failed: Given an error to wrap", + "A wrapped test error message", + ]; + $this->assertBehatScenarioAssertion($scenario, AssertionFailedWrappedError::class, $exceptionMessages); + } + + /** + * @Given an error to wrap + */ + public function anErrorToWrapStep() { + throw new TestWrappingException("A wrapped test error message"); + } + +} diff --git a/tests/BehatProvidingTraitTest.php b/tests/BehatProvidingTraitTest.php index e0808a9..4259518 100644 --- a/tests/BehatProvidingTraitTest.php +++ b/tests/BehatProvidingTraitTest.php @@ -3,8 +3,6 @@ namespace PHPUnitBehat\Tests; use PHPUnit\Framework\TestCase; -use PHPUnit\Framework\ExpectationFailedException; -use Behat\Testwork\Argument\Exception\UnknownParameterValueException; use PHPUnitBehat\TestTraits\BehatTestTrait; use Behat\Gherkin\Node\KeywordNodeInterface; use Behat\Gherkin\Node\ScenarioInterface; diff --git a/tests/TestWrappingException.php b/tests/TestWrappingException.php new file mode 100644 index 0000000..978d41d --- /dev/null +++ b/tests/TestWrappingException.php @@ -0,0 +1,7 @@ +