Skip to content

Commit 9104995

Browse files
[PhpUnitBridge] Drop ErrorAssert in favor of @expectedDeprecation
1 parent d5ab07f commit 9104995

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

SymfonyTestsListener.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class SymfonyTestsListener extends \PHPUnit_Framework_BaseTestListener
2525
private $skippedFile = false;
2626
private $wasSkipped = array();
2727
private $isSkipped = array();
28+
private $expectedDeprecations;
29+
private $gatheredDeprecations;
30+
private $previousErrorHandler;
2831

2932
/**
3033
* @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
142145
public function startTest(\PHPUnit_Framework_Test $test)
143146
{
144147
if (-2 < $this->state && $test instanceof \PHPUnit_Framework_TestCase) {
145-
$groups = \PHPUnit_Util_Test::getGroups(get_class($test), $test->getName());
148+
$groups = \PHPUnit_Util_Test::getGroups(get_class($test), $test->getName(false));
146149

147150
if (in_array('time-sensitive', $groups, true)) {
148151
ClockMock::register(get_class($test));
@@ -151,13 +154,37 @@ public function startTest(\PHPUnit_Framework_Test $test)
151154
if (in_array('dns-sensitive', $groups, true)) {
152155
DnsMock::register(get_class($test));
153156
}
157+
158+
$annotations = \PHPUnit_Util_Test::parseTestMethodAnnotations(get_class($test), $test->getName(false));
159+
160+
if (isset($annotations['class']['expectedDeprecation'])) {
161+
$test->getTestResultObject()->addError($test, new \PHPUnit_Framework_AssertionFailedError('`@expectedDeprecation` annotations are not allowed at the class level.'), 0);
162+
}
163+
if (isset($annotations['method']['expectedDeprecation'])) {
164+
if (!in_array('legacy', $groups, true)) {
165+
$test->getTestResultObject()->addError($test, new \PHPUnit_Framework_AssertionFailedError('Only tests with the `@group legacy` annotation can have `@expectedDeprecation`.'), 0);
166+
}
167+
$this->expectedDeprecations = $annotations['method']['expectedDeprecation'];
168+
$this->previousErrorHandler = set_error_handler(array($this, 'handleError'));
169+
}
154170
}
155171
}
156172

157173
public function endTest(\PHPUnit_Framework_Test $test, $time)
158174
{
175+
if ($this->expectedDeprecations) {
176+
restore_error_handler();
177+
try {
178+
$prefix = "@expectedDeprecation:\n ";
179+
$test->assertStringMatchesFormat($prefix.implode("\n ", $this->expectedDeprecations), $prefix.implode("\n ", $this->gatheredDeprecations));
180+
} catch (\PHPUnit_Framework_AssertionFailedError $e) {
181+
$test->getTestResultObject()->addFailure($test, $e, $time);
182+
}
183+
184+
$this->expectedDeprecations = $this->gatheredDeprecations = $this->previousErrorHandler = null;
185+
}
159186
if (-2 < $this->state && $test instanceof \PHPUnit_Framework_TestCase) {
160-
$groups = \PHPUnit_Util_Test::getGroups(get_class($test), $test->getName());
187+
$groups = \PHPUnit_Util_Test::getGroups(get_class($test), $test->getName(false));
161188

162189
if (in_array('time-sensitive', $groups, true)) {
163190
ClockMock::withClockMock(false);
@@ -167,4 +194,17 @@ public function endTest(\PHPUnit_Framework_Test $test, $time)
167194
}
168195
}
169196
}
197+
198+
public function handleError($type, $msg, $file, $line, $context)
199+
{
200+
if (E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) {
201+
$h = $this->previousErrorHandler;
202+
203+
return $h ? $h($type, $msg, $file, $line, $context) : false;
204+
}
205+
if (error_reporting()) {
206+
$msg = 'Unsilenced deprecation: '.$msg;
207+
}
208+
$this->gatheredDeprecations[] = $msg;
209+
}
170210
}

bin/simple-phpunit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

1313
// Please update when phpunit needs to be reinstalled with fresh deps:
14-
// Cache-Id-Version: 2016-09-12 09:00 UTC
14+
// Cache-Id-Version: 2016-10-20 14:00 UTC
1515

1616
error_reporting(-1);
1717

0 commit comments

Comments
 (0)