Skip to content

Commit a7ab672

Browse files
Merge branch '4.4'
* 4.4: Minor fixes [Mailer] fixed dispatcher not available in Mailer [HttpClient] Minor fixes Use namespaced Phpunit classes Add polyfill for PhpUnit namespace [Messenger] Fixed ConsumeMessagesCommand configuration [Form] remove leftover int child phpdoc Support DateTimeInterface in IntlDateFormatter::format [PhpUnitBridge] fixed PHPUnit 8.3 compatibility: method handleError was renamed to __invoke [Yaml] Removed unused $nullAsTilde property [Security] add support for opportunistic password migrations [Lock] Legacy test should implement legacy interface fixed phpdocs Use PHPunit assertion [Intl] Order alpha2 to alpha3 mapping + phpdoc fixes
2 parents 6a73457 + 850e71f commit a7ab672

12 files changed

+147
-73
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CHANGELOG
1111

1212
* made the bridge act as a polyfill for newest PHPUnit features
1313
* added `SetUpTearDownTrait` to allow working around the `void` return-type added by PHPUnit 8
14+
* added namespace aliases for PHPUnit < 6
1415

1516
4.3.0
1617
-----

CoverageListener.php

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

1212
namespace Symfony\Bridge\PhpUnit;
1313

14-
if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) {
14+
if (version_compare(\PHPUnit\Runner\Version::id(), '6.0.0', '<')) {
1515
class_alias('Symfony\Bridge\PhpUnit\Legacy\CoverageListenerForV5', 'Symfony\Bridge\PhpUnit\CoverageListener');
1616
} elseif (version_compare(\PHPUnit\Runner\Version::id(), '7.0.0', '<')) {
1717
class_alias('Symfony\Bridge\PhpUnit\Legacy\CoverageListenerForV6', 'Symfony\Bridge\PhpUnit\CoverageListener');

DeprecationErrorHandler.php

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Bridge\PhpUnit;
1313

14+
use PHPUnit\Framework\TestResult;
15+
use PHPUnit\Util\ErrorHandler;
1416
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Configuration;
1517
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Deprecation;
1618

@@ -43,7 +45,7 @@ class DeprecationErrorHandler
4345
];
4446

4547
private static $isRegistered = false;
46-
private static $utilPrefix;
48+
private static $isAtLeastPhpUnit83;
4749

4850
/**
4951
* Registers and configures the deprecation handler.
@@ -67,15 +69,15 @@ public static function register($mode = 0)
6769
return;
6870
}
6971

70-
self::$utilPrefix = class_exists('PHPUnit_Util_ErrorHandler') ? 'PHPUnit_Util_' : 'PHPUnit\Util\\';
72+
self::$isAtLeastPhpUnit83 = !class_exists('PHPUnit_Util_ErrorHandler') && method_exists(ErrorHandler::class, '__invoke');
7173

7274
$handler = new self();
7375
$oldErrorHandler = set_error_handler([$handler, 'handleError']);
7476

7577
if (null !== $oldErrorHandler) {
7678
restore_error_handler();
7779

78-
if ([self::$utilPrefix.'ErrorHandler', 'handleError'] === $oldErrorHandler) {
80+
if ($oldErrorHandler instanceof ErrorHandler || [ErrorHandler::class, 'handleError'] === $oldErrorHandler) {
7981
restore_error_handler();
8082
self::register($mode);
8183
}
@@ -95,12 +97,7 @@ public static function collectDeprecations($outputFile)
9597
return $previousErrorHandler($type, $msg, $file, $line, $context);
9698
}
9799

98-
static $autoload = true;
99-
100-
$ErrorHandler = class_exists('PHPUnit_Util_ErrorHandler', $autoload) ? 'PHPUnit_Util_ErrorHandler' : 'PHPUnit\Util\ErrorHandler';
101-
$autoload = false;
102-
103-
return $ErrorHandler::handleError($type, $msg, $file, $line, $context);
100+
return \call_user_func(self::getPhpUnitErrorHandler(), $type, $msg, $file, $line, $context);
104101
}
105102

106103
$deprecations[] = [error_reporting(), $msg, $file];
@@ -117,9 +114,7 @@ public static function collectDeprecations($outputFile)
117114
public function handleError($type, $msg, $file, $line, $context = [])
118115
{
119116
if ((E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) || !$this->getConfiguration()->isEnabled()) {
120-
$ErrorHandler = self::$utilPrefix.'ErrorHandler';
121-
122-
return $ErrorHandler::handleError($type, $msg, $file, $line, $context);
117+
return \call_user_func(self::getPhpUnitErrorHandler(), $type, $msg, $file, $line, $context);
123118
}
124119

125120
$deprecation = new Deprecation($msg, debug_backtrace(), $file);
@@ -135,7 +130,7 @@ public function handleError($type, $msg, $file, $line, $context = [])
135130

136131
if (0 !== error_reporting()) {
137132
$group = 'unsilenced';
138-
} elseif ($deprecation->isLegacy(self::$utilPrefix)) {
133+
} elseif ($deprecation->isLegacy()) {
139134
$group = 'legacy';
140135
} else {
141136
$group = [
@@ -307,6 +302,26 @@ private function displayDeprecations($groups, $configuration)
307302
}
308303
}
309304

305+
private static function getPhpUnitErrorHandler()
306+
{
307+
if (!self::$isAtLeastPhpUnit83) {
308+
return 'PHPUnit\Util\ErrorHandler::handleError';
309+
}
310+
311+
foreach (debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) {
312+
if (isset($frame['object']) && $frame['object'] instanceof TestResult) {
313+
return new ErrorHandler(
314+
$frame['object']->getConvertDeprecationsToExceptions(),
315+
$frame['object']->getConvertErrorsToExceptions(),
316+
$frame['object']->getConvertNoticesToExceptions(),
317+
$frame['object']->getConvertWarningsToExceptions()
318+
);
319+
}
320+
}
321+
322+
return function () { return false; };
323+
}
324+
310325
/**
311326
* Returns true if STDOUT is defined and supports colorization.
312327
*

DeprecationErrorHandler/Deprecation.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bridge\PhpUnit\DeprecationErrorHandler;
1313

14+
use PHPUnit\Util\Test;
1415
use Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerFor;
1516

1617
/**
@@ -156,17 +157,16 @@ public function getMessage()
156157
*
157158
* @return bool
158159
*/
159-
public function isLegacy($utilPrefix)
160+
public function isLegacy()
160161
{
161-
$test = $utilPrefix.'Test';
162162
$class = $this->originatingClass();
163163
$method = $this->originatingMethod();
164164

165165
return 0 === strpos($method, 'testLegacy')
166166
|| 0 === strpos($method, 'provideLegacy')
167167
|| 0 === strpos($method, 'getLegacy')
168168
|| strpos($class, '\Legacy')
169-
|| \in_array('legacy', $test::getGroups($class, $method), true);
169+
|| \in_array('legacy', Test::getGroups($class, $method), true);
170170
}
171171

172172
/**

Legacy/CoverageListenerTrait.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use PHPUnit\Framework\Warning;
16+
use PHPUnit\Util\Test;
1617

1718
/**
1819
* PHP 5.3 compatible trait-like shared implementation.
@@ -65,12 +66,7 @@ public function startTest($test)
6566
return;
6667
}
6768

68-
$testClass = \PHPUnit\Util\Test::class;
69-
if (!class_exists($testClass, false)) {
70-
$testClass = \PHPUnit_Util_Test::class;
71-
}
72-
73-
$r = new \ReflectionProperty($testClass, 'annotationCache');
69+
$r = new \ReflectionProperty(Test::class, 'annotationCache');
7470
$r->setAccessible(true);
7571

7672
$cache = $r->getValue();
@@ -79,7 +75,7 @@ public function startTest($test)
7975
'covers' => \is_array($sutFqcn) ? $sutFqcn : array($sutFqcn),
8076
),
8177
));
82-
$r->setValue($testClass, $cache);
78+
$r->setValue(Test::class, $cache);
8379
}
8480

8581
private function findSutFqcn($test)

Legacy/PolyfillTestCaseTrait.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use PHPUnit\Framework\TestCase;
1616

1717
/**
18-
* This trait is @internal
18+
* This trait is @internal.
1919
*/
2020
trait PolyfillTestCaseTrait
2121
{
@@ -66,7 +66,7 @@ protected function createPartialMock($originalClassName, array $methods)
6666
*/
6767
public function expectException($exception)
6868
{
69-
$property = new \ReflectionProperty(class_exists('PHPUnit_Framework_TestCase') ? 'PHPUnit_Framework_TestCase' : TestCase::class, 'expectedException');
69+
$property = new \ReflectionProperty(TestCase::class, 'expectedException');
7070
$property->setAccessible(true);
7171
$property->setValue($this, $exception);
7272
}
@@ -78,7 +78,7 @@ public function expectException($exception)
7878
*/
7979
public function expectExceptionCode($code)
8080
{
81-
$property = new \ReflectionProperty(class_exists('PHPUnit_Framework_TestCase') ? 'PHPUnit_Framework_TestCase' : TestCase::class, 'expectedExceptionCode');
81+
$property = new \ReflectionProperty(TestCase::class, 'expectedExceptionCode');
8282
$property->setAccessible(true);
8383
$property->setValue($this, $code);
8484
}
@@ -90,7 +90,7 @@ public function expectExceptionCode($code)
9090
*/
9191
public function expectExceptionMessage($message)
9292
{
93-
$property = new \ReflectionProperty(class_exists('PHPUnit_Framework_TestCase') ? 'PHPUnit_Framework_TestCase' : TestCase::class, 'expectedExceptionMessage');
93+
$property = new \ReflectionProperty(TestCase::class, 'expectedExceptionMessage');
9494
$property->setAccessible(true);
9595
$property->setValue($this, $message);
9696
}
@@ -102,7 +102,7 @@ public function expectExceptionMessage($message)
102102
*/
103103
public function expectExceptionMessageRegExp($messageRegExp)
104104
{
105-
$property = new \ReflectionProperty(class_exists('PHPUnit_Framework_TestCase') ? 'PHPUnit_Framework_TestCase' : TestCase::class, 'expectedExceptionMessageRegExp');
105+
$property = new \ReflectionProperty(TestCase::class, 'expectedExceptionMessageRegExp');
106106
$property->setAccessible(true);
107107
$property->setValue($this, $messageRegExp);
108108
}

Legacy/SymfonyTestsListenerTrait.php

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
use PHPUnit\Framework\AssertionFailedError;
1616
use PHPUnit\Framework\TestCase;
1717
use PHPUnit\Framework\TestSuite;
18+
use PHPUnit\Runner\BaseTestRunner;
1819
use PHPUnit\Util\Blacklist;
20+
use PHPUnit\Util\Test;
1921
use Symfony\Bridge\PhpUnit\ClockMock;
2022
use Symfony\Bridge\PhpUnit\DnsMock;
2123
use Symfony\Component\Debug\DebugClassLoader as LegacyDebugClassLoader;
@@ -48,11 +50,7 @@ class SymfonyTestsListenerTrait
4850
*/
4951
public function __construct(array $mockedNamespaces = array())
5052
{
51-
if (class_exists('PHPUnit_Util_Blacklist')) {
52-
\PHPUnit_Util_Blacklist::$blacklistedClassNames['\Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait'] = 2;
53-
} else {
54-
Blacklist::$blacklistedClassNames['\Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait'] = 2;
55-
}
53+
Blacklist::$blacklistedClassNames['\Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait'] = 2;
5654

5755
$enableDebugClassLoader = class_exists(DebugClassLoader::class) || class_exists(LegacyDebugClassLoader::class);
5856

@@ -113,19 +111,14 @@ public function globalListenerDisabled()
113111

114112
public function startTestSuite($suite)
115113
{
116-
if (class_exists('PHPUnit_Util_Blacklist', false)) {
117-
$Test = 'PHPUnit_Util_Test';
118-
} else {
119-
$Test = 'PHPUnit\Util\Test';
120-
}
121114
$suiteName = $suite->getName();
122115
$this->testsWithWarnings = array();
123116

124117
foreach ($suite->tests() as $test) {
125118
if (!($test instanceof \PHPUnit\Framework\TestCase || $test instanceof TestCase)) {
126119
continue;
127120
}
128-
if (null === $Test::getPreserveGlobalStateSettings(\get_class($test), $test->getName(false))) {
121+
if (null === Test::getPreserveGlobalStateSettings(\get_class($test), $test->getName(false))) {
129122
$test->setPreserveGlobalState(false);
130123
}
131124
}
@@ -157,12 +150,12 @@ public function startTestSuite($suite)
157150
$testSuites = array($suite);
158151
for ($i = 0; isset($testSuites[$i]); ++$i) {
159152
foreach ($testSuites[$i]->tests() as $test) {
160-
if ($test instanceof \PHPUnit_Framework_TestSuite || $test instanceof TestSuite) {
153+
if ($test instanceof TestSuite) {
161154
if (!class_exists($test->getName(), false)) {
162155
$testSuites[] = $test;
163156
continue;
164157
}
165-
$groups = $Test::getGroups($test->getName());
158+
$groups = Test::getGroups($test->getName());
166159
if (\in_array('time-sensitive', $groups, true)) {
167160
ClockMock::register($test->getName());
168161
}
@@ -213,14 +206,7 @@ public function startTest($test)
213206
putenv('SYMFONY_DEPRECATIONS_SERIALIZE='.$this->runsInSeparateProcess);
214207
}
215208

216-
if (class_exists('PHPUnit_Util_Blacklist', false)) {
217-
$Test = 'PHPUnit_Util_Test';
218-
$AssertionFailedError = 'PHPUnit_Framework_AssertionFailedError';
219-
} else {
220-
$Test = 'PHPUnit\Util\Test';
221-
$AssertionFailedError = 'PHPUnit\Framework\AssertionFailedError';
222-
}
223-
$groups = $Test::getGroups(\get_class($test), $test->getName(false));
209+
$groups = Test::getGroups(\get_class($test), $test->getName(false));
224210

225211
if (!$this->runsInSeparateProcess) {
226212
if (\in_array('time-sensitive', $groups, true)) {
@@ -232,14 +218,14 @@ public function startTest($test)
232218
}
233219
}
234220

235-
$annotations = $Test::parseTestMethodAnnotations(\get_class($test), $test->getName(false));
221+
$annotations = Test::parseTestMethodAnnotations(\get_class($test), $test->getName(false));
236222

237223
if (isset($annotations['class']['expectedDeprecation'])) {
238-
$test->getTestResultObject()->addError($test, new $AssertionFailedError('`@expectedDeprecation` annotations are not allowed at the class level.'), 0);
224+
$test->getTestResultObject()->addError($test, new AssertionFailedError('`@expectedDeprecation` annotations are not allowed at the class level.'), 0);
239225
}
240226
if (isset($annotations['method']['expectedDeprecation'])) {
241227
if (!\in_array('legacy', $groups, true)) {
242-
$this->error = new $AssertionFailedError('Only tests with the `@group legacy` annotation can have `@expectedDeprecation`.');
228+
$this->error = new AssertionFailedError('Only tests with the `@group legacy` annotation can have `@expectedDeprecation`.');
243229
}
244230

245231
$test->getTestResultObject()->beStrictAboutTestsThatDoNotTestAnything(false);
@@ -259,18 +245,8 @@ public function addWarning($test, $e, $time)
259245

260246
public function endTest($test, $time)
261247
{
262-
if (class_exists('PHPUnit_Util_Blacklist', false)) {
263-
$Test = 'PHPUnit_Util_Test';
264-
$BaseTestRunner = 'PHPUnit_Runner_BaseTestRunner';
265-
$Warning = 'PHPUnit_Framework_Warning';
266-
} else {
267-
$Test = 'PHPUnit\Util\Test';
268-
$BaseTestRunner = 'PHPUnit\Runner\BaseTestRunner';
269-
$Warning = 'PHPUnit\Framework\Warning';
270-
}
271248
$className = \get_class($test);
272-
$classGroups = $Test::getGroups($className);
273-
$groups = $Test::getGroups($className, $test->getName(false));
249+
$groups = Test::getGroups($className, $test->getName(false));
274250

275251
if (null !== $this->reportUselessTests) {
276252
$test->getTestResultObject()->beStrictAboutTestsThatDoNotTestAnything($this->reportUselessTests);
@@ -299,20 +275,18 @@ public function endTest($test, $time)
299275
}
300276

301277
if ($this->expectedDeprecations) {
302-
if (!\in_array($test->getStatus(), array($BaseTestRunner::STATUS_SKIPPED, $BaseTestRunner::STATUS_INCOMPLETE), true)) {
278+
if (!\in_array($test->getStatus(), array(BaseTestRunner::STATUS_SKIPPED, BaseTestRunner::STATUS_INCOMPLETE), true)) {
303279
$test->addToAssertionCount(\count($this->expectedDeprecations));
304280
}
305281

306282
restore_error_handler();
307283

308-
if (!$errored && !\in_array($test->getStatus(), array($BaseTestRunner::STATUS_SKIPPED, $BaseTestRunner::STATUS_INCOMPLETE, $BaseTestRunner::STATUS_FAILURE, $BaseTestRunner::STATUS_ERROR), true)) {
284+
if (!$errored && !\in_array($test->getStatus(), array(BaseTestRunner::STATUS_SKIPPED, BaseTestRunner::STATUS_INCOMPLETE, BaseTestRunner::STATUS_FAILURE, BaseTestRunner::STATUS_ERROR), true)) {
309285
try {
310286
$prefix = "@expectedDeprecation:\n";
311287
$test->assertStringMatchesFormat($prefix.'%A '.implode("\n%A ", $this->expectedDeprecations)."\n%A", $prefix.' '.implode("\n ", $this->gatheredDeprecations)."\n");
312288
} catch (AssertionFailedError $e) {
313289
$test->getTestResultObject()->addFailure($test, $e, $time);
314-
} catch (\PHPUnit_Framework_AssertionFailedError $e) {
315-
$test->getTestResultObject()->addFailure($test, $e, $time);
316290
}
317291
}
318292

SymfonyTestsListener.php

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

1212
namespace Symfony\Bridge\PhpUnit;
1313

14-
if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) {
14+
if (version_compare(\PHPUnit\Runner\Version::id(), '6.0.0', '<')) {
1515
class_alias('Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerForV5', 'Symfony\Bridge\PhpUnit\SymfonyTestsListener');
1616
} elseif (version_compare(\PHPUnit\Runner\Version::id(), '7.0.0', '<')) {
1717
class_alias('Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerForV6', 'Symfony\Bridge\PhpUnit\SymfonyTestsListener');

Tests/Fixtures/coverage/tests/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
require __DIR__.'/../../../../Legacy/CoverageListenerTrait.php';
1616

17-
if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) {
17+
if (version_compare(\PHPUnit\Runner\Version::id(), '6.0.0', '<')) {
1818
require_once __DIR__.'/../../../../Legacy/CoverageListenerForV5.php';
1919
} elseif (version_compare(\PHPUnit\Runner\Version::id(), '7.0.0', '<')) {
2020
require_once __DIR__.'/../../../../Legacy/CoverageListenerForV6.php';

Tests/ProcessIsolationTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ public function testIsolation()
2424

2525
public function testCallingOtherErrorHandler()
2626
{
27-
$class = class_exists('PHPUnit\Framework\Exception') ? 'PHPUnit\Framework\Exception' : 'PHPUnit_Framework_Exception';
28-
$this->expectException($class);
27+
$this->expectException('PHPUnit\Framework\Exception');
2928
$this->expectExceptionMessage('Test that PHPUnit\'s error handler fires.');
3029

3130
trigger_error('Test that PHPUnit\'s error handler fires.', E_USER_WARNING);

0 commit comments

Comments
 (0)