Skip to content

Commit

Permalink
AssertionFailedWrappedError with test (#14)
Browse files Browse the repository at this point in the history
* Update and rename ExpectationFailedWrappedError.php to AssertionFailedWrappedError.php

* Update README.md

* Remove unneeded use statements in BehatProvidingTraitTest

* Create TestWrappingException.php

* Create AssertionFailedWrapperErrrorTest

* Remove stray brace in readme

* Provide code to parent in AssertionFailedError
  • Loading branch information
jonathanjfshaw authored Dec 19, 2022
1 parent 479d7b4 commit 8cc5345
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 10 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand Down
82 changes: 82 additions & 0 deletions tests/AssertionFailedWrapperErrorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace PHPUnitBehat\Tests;

use PHPUnit\Framework\TestCase;
use PHPUnitBehat\PHPUnit\Framework\AssertionFailedWrappedError;

/**
*
*/
class AssertionFailedWrappedErrorTest extends TestCase {

use TestDefinitionsTrait;

// This code using AssertionFailedWrappedError is taken from the project's readme.

use TestHelpersTrait {
assertBehatScenarioPassed as assertBehatScenarioPassedTrait;
}

public static function assertBehatScenarioPassed($scenarioResults, $scenario = NULL, $stepResults = [], $snippetGenerator = NULL, $environment = NULL, $message = '', $callHandler = '')
{
try {
self::assertBehatScenarioPassedTrait($scenarioResults, $scenario, $stepResults, $snippetGenerator, $environment, $message, $callHandler);
}
catch (TestWrappingException $e) {
throw new \PHPUnitBehat\PHPUnit\Framework\AssertionFailedWrappedError($e);
}
}

protected $testBehatFeature = <<<'FEATURE'
Feature: BehatScenarioTestingTrait
In order to test a feature
We need to able to test scenarios
Scenario: #0 Normal error
Given an error
Scenario: #1 Error to wrap
Given an error to wrap

FEATURE;


/**
* Test scenario "#0 Error"
*/
public function testError() {
$scenario = $this->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");
}

}
2 changes: 0 additions & 2 deletions tests/BehatProvidingTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions tests/TestWrappingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace PHPUnitBehat\Tests;

use Exception;

class TestWrappingException extends Exception {}

0 comments on commit 8cc5345

Please sign in to comment.