bug #22070 [PhpUnitBridge] add errors as late as possible (xabbuh)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[PhpUnitBridge] add errors as late as possible

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

PHPUnit may change the test's state after the listener's startTest()
method has been executed thus leading to broken test result output.

Commits
-------

167742e521 add errors as late as possible
This commit is contained in:
Fabien Potencier 2017-03-22 08:44:06 -07:00
commit 7c308be5de

View File

@ -39,6 +39,7 @@ class SymfonyTestsListenerTrait
private $previousErrorHandler;
private $testsWithWarnings;
private $reportUselessTests;
private $error;
/**
* @param array $mockedNamespaces List of namespaces, indexed by mocked features (time-sensitive or dns-sensitive)
@ -207,11 +208,11 @@ class SymfonyTestsListenerTrait
}
if (isset($annotations['method']['expectedDeprecation'])) {
if (!in_array('legacy', $groups, true)) {
$test->getTestResultObject()->addError($test, new $AssertionFailedError('Only tests with the `@group legacy` annotation can have `@expectedDeprecation`.'), 0);
} else {
$test->getTestResultObject()->beStrictAboutTestsThatDoNotTestAnything(false);
$this->error = new $AssertionFailedError('Only tests with the `@group legacy` annotation can have `@expectedDeprecation`.');
}
$test->getTestResultObject()->beStrictAboutTestsThatDoNotTestAnything(false);
$this->expectedDeprecations = $annotations['method']['expectedDeprecation'];
$this->previousErrorHandler = set_error_handler(array($this, 'handleError'));
}
@ -245,6 +246,17 @@ class SymfonyTestsListenerTrait
$this->reportUselessTests = null;
}
$errored = false;
if (null !== $this->error) {
if ($BaseTestRunner::STATUS_PASSED === $test->getStatus()) {
$test->getTestResultObject()->addError($test, $this->error, 0);
$errored = true;
}
$this->error = null;
}
if ($this->expectedDeprecations) {
if (!in_array($test->getStatus(), array($BaseTestRunner::STATUS_SKIPPED, $BaseTestRunner::STATUS_INCOMPLETE), true)) {
$test->addToAssertionCount(count($this->expectedDeprecations));
@ -252,7 +264,7 @@ class SymfonyTestsListenerTrait
restore_error_handler();
if (!in_array($test->getStatus(), array($BaseTestRunner::STATUS_SKIPPED, $BaseTestRunner::STATUS_INCOMPLETE, $BaseTestRunner::STATUS_FAILURE, $BaseTestRunner::STATUS_ERROR), true)) {
if (!$errored && !in_array($test->getStatus(), array($BaseTestRunner::STATUS_SKIPPED, $BaseTestRunner::STATUS_INCOMPLETE, $BaseTestRunner::STATUS_FAILURE, $BaseTestRunner::STATUS_ERROR), true)) {
try {
$prefix = "@expectedDeprecation:\n";
$test->assertStringMatchesFormat($prefix.'%A '.implode("\n%A ", $this->expectedDeprecations)."\n%A", $prefix.' '.implode("\n ", $this->gatheredDeprecations)."\n");