Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0c95aad

Browse files
feature #32940 [PhpUnitBridge] Add polyfill for PhpUnit namespace (jderusse)
This PR was merged into the 4.4 branch. Discussion ---------- [PhpUnitBridge] Add polyfill for PhpUnit namespace | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? |no | Tests pass? | yes | Fixed tickets | #32844 | License | MIT | Doc PR | NA This PR provides PhpUnit >= 6 Namespaces class. This will simplify tests that have to be compatible with multiple version of PHPUnit ```diff - if (class_exists('PHPUnit_Foo')) { - PHPUnit_Foo::bar(): - } else { - \PHPUnit\Foo::bar(); - } + \PHPUnit\Foo::bar(); ``` WIP => waiting for #32941 to be green Commits ------- b7520f7 Add polyfill for PhpUnit namespace
2 parents f6ea704 + b7520f7 commit 0c95aad

12 files changed

+123
-73
lines changed

src/Symfony/Bridge/PhpUnit/CHANGELOG.md

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

77
* made the bridge act as a polyfill for newest PHPUnit features
88
* added `SetUpTearDownTrait` to allow working around the `void` return-type added by PHPUnit 8
9+
* added namespace aliases for PHPUnit < 6
910

1011
4.3.0
1112
-----

src/Symfony/Bridge/PhpUnit/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');

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php

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

1212
namespace Symfony\Bridge\PhpUnit;
1313

14+
use PHPUnit\Util\ErrorHandler;
1415
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Configuration;
1516
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Deprecation;
1617

@@ -48,7 +49,6 @@ class DeprecationErrorHandler
4849
];
4950

5051
private static $isRegistered = false;
51-
private static $utilPrefix;
5252

5353
/**
5454
* Registers and configures the deprecation handler.
@@ -72,15 +72,13 @@ public static function register($mode = 0)
7272
return;
7373
}
7474

75-
self::$utilPrefix = class_exists('PHPUnit_Util_ErrorHandler') ? 'PHPUnit_Util_' : 'PHPUnit\Util\\';
76-
7775
$handler = new self();
7876
$oldErrorHandler = set_error_handler([$handler, 'handleError']);
7977

8078
if (null !== $oldErrorHandler) {
8179
restore_error_handler();
8280

83-
if ([self::$utilPrefix.'ErrorHandler', 'handleError'] === $oldErrorHandler) {
81+
if ([ErrorHandler::class, 'handleError'] === $oldErrorHandler) {
8482
restore_error_handler();
8583
self::register($mode);
8684
}
@@ -100,12 +98,7 @@ public static function collectDeprecations($outputFile)
10098
return $previousErrorHandler($type, $msg, $file, $line, $context);
10199
}
102100

103-
static $autoload = true;
104-
105-
$ErrorHandler = class_exists('PHPUnit_Util_ErrorHandler', $autoload) ? 'PHPUnit_Util_ErrorHandler' : 'PHPUnit\Util\ErrorHandler';
106-
$autoload = false;
107-
108-
return $ErrorHandler::handleError($type, $msg, $file, $line, $context);
101+
return ErrorHandler::handleError($type, $msg, $file, $line, $context);
109102
}
110103

111104
$deprecations[] = [error_reporting(), $msg, $file];
@@ -122,9 +115,7 @@ public static function collectDeprecations($outputFile)
122115
public function handleError($type, $msg, $file, $line, $context = [])
123116
{
124117
if ((E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) || !$this->getConfiguration()->isEnabled()) {
125-
$ErrorHandler = self::$utilPrefix.'ErrorHandler';
126-
127-
return $ErrorHandler::handleError($type, $msg, $file, $line, $context);
118+
return ErrorHandler::handleError($type, $msg, $file, $line, $context);
128119
}
129120

130121
$deprecation = new Deprecation($msg, debug_backtrace(), $file);
@@ -140,7 +131,7 @@ public function handleError($type, $msg, $file, $line, $context = [])
140131

141132
if (0 !== error_reporting()) {
142133
$group = 'unsilenced';
143-
} elseif ($deprecation->isLegacy(self::$utilPrefix)) {
134+
} elseif ($deprecation->isLegacy()) {
144135
$group = 'legacy';
145136
} else {
146137
$group = [

src/Symfony/Bridge/PhpUnit/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
/**

src/Symfony/Bridge/PhpUnit/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)

src/Symfony/Bridge/PhpUnit/Legacy/PolyfillTestCaseTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/Symfony/Bridge/PhpUnit/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

src/Symfony/Bridge/PhpUnit/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');

src/Symfony/Bridge/PhpUnit/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';

src/Symfony/Bridge/PhpUnit/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(\class_exists('PHPUnit_Framework_Exception') ? 'PHPUnit_Framework_Exception' : '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);

src/Symfony/Bridge/PhpUnit/TextUI/Command.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\TextUI;
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\CommandForV5', 'Symfony\Bridge\PhpUnit\TextUI\Command');
1616
} else {
1717
class_alias('Symfony\Bridge\PhpUnit\Legacy\CommandForV6', 'Symfony\Bridge\PhpUnit\TextUI\Command');

0 commit comments

Comments
 (0)