-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[PhpUnitBridge] Replace ErrorAssert by @expectedDeprecation
#20255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,9 @@ class SymfonyTestsListener extends \PHPUnit_Framework_BaseTestListener | |
private $skippedFile = false; | ||
private $wasSkipped = array(); | ||
private $isSkipped = array(); | ||
private $expectedDeprecations; | ||
private $gatheredDeprecations; | ||
private $previousErrorHandler; | ||
|
||
/** | ||
* @param array $mockedNamespaces List of namespaces, indexed by mocked features (time-sensitive or dns-sensitive) | ||
|
@@ -142,7 +145,7 @@ public function addSkippedTest(\PHPUnit_Framework_Test $test, \Exception $e, $ti | |
public function startTest(\PHPUnit_Framework_Test $test) | ||
{ | ||
if (-2 < $this->state && $test instanceof \PHPUnit_Framework_TestCase) { | ||
$groups = \PHPUnit_Util_Test::getGroups(get_class($test), $test->getName()); | ||
$groups = \PHPUnit_Util_Test::getGroups(get_class($test), $test->getName(false)); | ||
|
||
if (in_array('time-sensitive', $groups, true)) { | ||
ClockMock::register(get_class($test)); | ||
|
@@ -151,13 +154,37 @@ public function startTest(\PHPUnit_Framework_Test $test) | |
if (in_array('dns-sensitive', $groups, true)) { | ||
DnsMock::register(get_class($test)); | ||
} | ||
|
||
$annotations = \PHPUnit_Util_Test::parseTestMethodAnnotations(get_class($test), $test->getName(false)); | ||
|
||
if (isset($annotations['class']['expectedDeprecation'])) { | ||
$test->getTestResultObject()->addError($test, new \PHPUnit_Framework_AssertionFailedError('`@expectedDeprecation` annotations are not allowed at the class level.'), 0); | ||
} | ||
if (isset($annotations['method']['expectedDeprecation'])) { | ||
if (!in_array('legacy', $groups, true)) { | ||
$test->getTestResultObject()->addError($test, new \PHPUnit_Framework_AssertionFailedError('Only tests with the `@group legacy` annotation can have `@expectedDeprecation`.'), 0); | ||
} | ||
$this->expectedDeprecations = $annotations['method']['expectedDeprecation']; | ||
$this->previousErrorHandler = set_error_handler(array($this, 'handleError')); | ||
} | ||
} | ||
} | ||
|
||
public function endTest(\PHPUnit_Framework_Test $test, $time) | ||
{ | ||
if ($this->expectedDeprecations) { | ||
restore_error_handler(); | ||
try { | ||
$prefix = "@expectedDeprecation:\n "; | ||
$test->assertStringMatchesFormat($prefix.implode("\n ", $this->expectedDeprecations), $prefix.implode("\n ", $this->gatheredDeprecations)); | ||
} catch (\PHPUnit_Framework_AssertionFailedError $e) { | ||
$test->getTestResultObject()->addFailure($test, $e, $time); | ||
} | ||
|
||
$this->expectedDeprecations = $this->gatheredDeprecations = $this->previousErrorHandler = null; | ||
} | ||
if (-2 < $this->state && $test instanceof \PHPUnit_Framework_TestCase) { | ||
$groups = \PHPUnit_Util_Test::getGroups(get_class($test), $test->getName()); | ||
$groups = \PHPUnit_Util_Test::getGroups(get_class($test), $test->getName(false)); | ||
|
||
if (in_array('time-sensitive', $groups, true)) { | ||
ClockMock::withClockMock(false); | ||
|
@@ -167,4 +194,17 @@ public function endTest(\PHPUnit_Framework_Test $test, $time) | |
} | ||
} | ||
} | ||
|
||
public function handleError($type, $msg, $file, $line, $context) | ||
{ | ||
if (E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) { | ||
$h = $this->previousErrorHandler; | ||
|
||
return $h ? $h($type, $msg, $file, $line, $context) : false; | ||
} | ||
if (error_reporting()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I disagree here: this practice makes the code more fragile, because it needs one more assumption to be correct. When I read this kind of code, I have to verify how |
||
$msg = 'Unsilenced deprecation: '.$msg; | ||
} | ||
$this->gatheredDeprecations[] = $msg; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This now differs in one detail from the current implementation: Previously, the order in which you declared multiple expected deprecations did not matter. Now you need to know in which order they will be triggered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine with me :)