10000 [PhpUnitBridge] Add polyfill for PhpUnit namespace by jderusse · Pull Request #32940 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PhpUnitBridge] Add polyfill for PhpUnit namespace #32940

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

Merged
merged 1 commit into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add polyfill for PhpUnit namespace
  • Loading branch information
jderusse committed Aug 5, 2019
commit b7520f738d7f2b9b166679830b0d60229c64635a
1 change: 1 addition & 0 deletions src/Symfony/Bridge/PhpUnit/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

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

4.3.0
-----
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/PhpUnit/CoverageListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Bridge\PhpUnit;

if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) {
if (version_compare(\PHPUnit\Runner\Version::id(), '6.0.0', '<')) {
class_alias('Symfony\Bridge\PhpUnit\Legacy\CoverageListenerForV5', 'Symfony\Bridge\PhpUnit\CoverageListener');
} elseif (version_compare(\PHPUnit\Runner\Version::id(), '7.0.0', '<')) {
class_alias('Symfony\Bridge\PhpUnit\Legacy\CoverageListenerForV6', 'Symfony\Bridge\PhpUnit\CoverageListener');
Expand Down
19 changes: 5 additions & 14 deletions src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bridge\PhpUnit;

use PHPUnit\Util\ErrorHandler;
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Configuration;
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Deprecation;

Expand Down Expand Up @@ -48,7 +49,6 @@ class DeprecationErrorHandler
];

private static $isRegistered = false;
private static $utilPrefix;

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

self::$utilPrefix = class_exists('PHPUnit_Util_ErrorHandler') ? 'PHPUnit_Util_' : 'PHPUnit\Util\\';

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

if (null !== $oldErrorHandler) {
restore_error_handler();

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

static $autoload = true;

$ErrorHandler = class_exists('PHPUnit_Util_ErrorHandler', $autoload) ? 'PHPUnit_Util_ErrorHandler' : 'PHPUnit\Util\ErrorHandler';
$autoload = false;

return $ErrorHandler::handleError($type, $msg, $file, $line, $context);
return ErrorHandler::handleError($type, $msg, $file, $line, $context);
}

$deprecations[] = [error_reporting(), $msg, $file];
Expand All @@ -122,9 +115,7 @@ public static function collectDeprecations($outputFile)
public function handleError($type, $msg, $file, $line, $context = [])
{
if ((E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) || !$this->getConfiguration()->isEnabled()) {
$ErrorHandler = self::$utilPrefix.'ErrorHandler';

return $ErrorHandler::handleError($type, $msg, $file, $line, $context);
return ErrorHandler::handleError($type, $msg, $file, $line, $context);
}

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

if (0 !== error_reporting()) {
$group = 'unsilenced';
} elseif ($deprecation->isLegacy(self::$utilPrefix)) {
} elseif ($deprecation->isLegacy()) {
$group = 'legacy';
} else {
$group = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bridge\PhpUnit\DeprecationErrorHandler;

use PHPUnit\Util\Test;
use Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerFor;

/**
Expand Down Expand Up @@ -156,17 +157,16 @@ public function getMessage()
*
* @return bool
*/
public function isLegacy($utilPrefix)
public function isLegacy()
{
$test = $utilPrefix.'Test';
$class = $this->originatingClass();
$method = $this->originatingMethod();

return 0 === strpos($method, 'testLegacy')
|| 0 === strpos($method, 'provideLegacy')
|| 0 === strpos($method, 'getLegacy')
|| strpos($class, '\Legacy')
|| \in_array('legacy', $test::getGroups($class, $method), true);
|| \in_array('legacy', Test::getGroups($class, $method), true);
}

/**
Expand Down
10 changes: 3 additions & 7 deletions src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Warning;
use PHPUnit\Util\Test;

/**
* PHP 5.3 compatible trait-like shared implementation.
Expand Down Expand Up @@ -65,12 +66,7 @@ public function startTest($test)
return;
}

$testClass = \PHPUnit\Util\Test::class;
if (!class_exists($testClass, false)) {
$testClass = \PHPUnit_Util_Test::class;
}

$r = new \ReflectionProperty($testClass, 'annotationCache');
$r = new \ReflectionProperty(Test::class, 'annotationCache');
$r->setAccessible(true);

$cache = $r->getValue();
Expand All @@ -79,7 +75,7 @@ public function startTest($test)
'covers' => \is_array($sutFqcn) ? $sutFqcn : array($sutFqcn),
),
));
$r->setValue($testClass, $cache);
$r->setValue(Test::class, $cache);
}

private function findSutFqcn($test)
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Bridge/PhpUnit/Legacy/PolyfillTestCaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function createPartialMock($originalClassName, array $methods)
*/
public function expectException($exception)
{
$property = new \ReflectionProperty(class_exists('PHPUnit_Framework_TestCase') ? 'PHPUnit_Framework_TestCase' : TestCase::class, 'expectedException');
$property = new \ReflectionProperty(TestCase::class, 'expectedException');
$property->setAccessible(true);
$property->setValue($this, $exception);
}
Expand All @@ -78,7 +78,7 @@ public function expectException($exception)
*/
public function expectExceptionCode($code)
{
$property = new \ReflectionProperty(class_exists('PHPUnit_Framework_TestCase') ? 'PHPUnit_Framework_TestCase' : TestCase::class, 'expectedExceptionCode');
$property = new \ReflectionProperty(TestCase::class, 'expectedExceptionCode');
$property->setAccessible(true);
$property->setValue($this, $code);
}
Expand All @@ -90,7 +90,7 @@ public function expectExceptionCode($code)
*/
public function expectExceptionMessage($message)
{
$property = new \ReflectionProperty(class_exists('PHPUnit_Framework_TestCase') ? 'PHPUnit_Framework_TestCase' : TestCase::class, 'expectedExceptionMessage');
$property = new \ReflectionProperty(TestCase::class, 'expectedExceptionMessage');
$property->setAccessible(true);
$property->setValue($this, $message);
}
Expand All @@ -102,7 +102,7 @@ public function expectExceptionMessage($message)
*/
public function expectExceptionMessageRegExp($messageRegExp)
{
$property = new \ReflectionProperty(class_exists('PHPUnit_Framework_TestCase') ? 'PHPUnit_Framework_TestCase' : TestCase::class, 'expectedExceptionMessageRegExp');
$property = new \ReflectionProperty(TestCase::class, 'expectedExceptionMessageRegExp');
$property->setAccessible(true);
$property->setValue($this, $messageRegExp);
}
Expand Down
52 changes: 13 additions & 39 deletions src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php
A851
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestSuite;
use PHPUnit\Runner\BaseTestRunner;
use PHPUnit\Util\Blacklist;
use PHPUnit\Util\Test;
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Bridge\PhpUnit\DnsMock;
use Symfony\Component\Debug\DebugClassLoader as LegacyDebugClassLoader;
Expand Down Expand Up @@ -48,11 +50,7 @@ class SymfonyTestsListenerTrait
*/
public function __construct(array $mockedNamespaces = array())
{
if (class_exists('PHPUnit_Util_Blacklist')) {
\PHPUnit_Util_Blacklist::$blacklistedClassNames['\Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait'] = 2;
} else {
Blacklist::$blacklistedClassNames['\Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait'] = 2;
}
Blacklist::$blacklistedClassNames['\Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait'] = 2;

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

Expand Down Expand Up @@ -113,19 +111,14 @@ public function globalListenerDisabled()

public function startTestSuite($suite)
{
if (class_exists('PHPUnit_Util_Blacklist', false)) {
$Test = 'PHPUnit_Util_Test';
} else {
$Test = 'PHPUnit\Util\Test';
}
$suiteName = $suite->getName();
$this->testsWithWarnings = array();

foreach ($suite->tests() as $test) {
if (!($test instanceof \PHPUnit\Framework\TestCase || $test instanceof TestCase)) {
continue;
}
if (null === $Test::getPreserveGlobalStateSettings(\get_class($test), $test->getName(false))) {
if (null === Test::getPreserveGlobalStateSettings(\get_class($test), $test->getName(false))) {
$test->setPreserveGlobalState(false);
}
}
Expand Down Expand Up @@ -157,12 +150,12 @@ public function startTestSuite($suite)
$testSuites = array($suite);
for ($i = 0; isset($testSuites[$i]); ++$i) {
foreach ($testSuites[$i]->tests() as $test) {
if ($test instanceof \PHPUnit_Framework_TestSuite || $test instanceof TestSuite) {
if ($test instanceof TestSuite) {
if (!class_exists($test->getName(), false)) {
$testSuites[] = $test;
continue;
}
$groups = $Test::getGroups($test->getName());
$groups = Test::getGroups($test->getName());
if (\in_array('time-sensitive', $groups, true)) {
ClockMock::register($test->getName());
}
Expand Down Expand Up @@ -213,14 +206,7 @@ public function startTest($test)
putenv('SYMFONY_DEPRECATIONS_SERIALIZE='.$this->runsInSeparateProcess);
}

if (class_exists('PHPUnit_Util_Blacklist', false)) {
$Test = 'PHPUnit_Util_Test';
$AssertionFailedError = 'PHPUnit_Framework_AssertionFailedError';
} else {
$Test = 'PHPUnit\Util\Test';
$AssertionFailedError = 'PHPUnit\Framework\AssertionFailedError';
}
$groups = $Test::getGroups(\get_class($test), $test->getName(false));
$groups = Test::getGroups(\get_class($test), $test->getName(false));

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

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

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

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

public function endTest($test, $time)
{
if (class_exists('PHPUnit_Util_Blacklist', false)) {
$Test = 'PHPUnit_Util_Test';
$BaseTestRunner = 'PHPUnit_Runner_BaseTestRunner';
$Warning = 'PHPUnit_Framework_Warning';
} else {
$Test = 'PHPUnit\Util\Test';
$BaseTestRunner = 'PHPUnit\Runner\BaseTestRunner';
$Warning = 'PHPUnit\Framework\Warning';
}
$className = \get_class($test);
$classGroups = $Test::getGroups($className);
$groups = $Test::getGroups($className, $test->getName(false));
$groups = Test::getGroups($className, $test->getName(false));

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

if ($this->expectedDeprecations) {
if (!\in_array($test->getStatus(), array($BaseTestRunner::STATUS_SKIPPED, $BaseTestRunner::STATUS_INCOMPLETE), true)) {
if (!\in_array($test->getStatus(), array(BaseTestRunner::STATUS_SKIPPED, BaseTestRunner::STATUS_INCOMPLETE), true)) {
$test->addToAssertionCount(\count($this->expectedDeprecations));
}

restore_error_handler();

if (!$errored && !\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");
} catch (AssertionFailedError $e) {
$test->getTestResultObject()->addFailure($test, $e, $time);
} catch (\PHPUnit_Framework_AssertionFailedError $e) {
$test->getTestResultObject()->addFailure($test, $e, $time);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/PhpUnit/SymfonyTestsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Bridge\PhpUnit;

if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) {
if (version_compare(\PHPUnit\Runner\Version::id(), '6.0.0', '<')) {
class_alias('Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerForV5', 'Symfony\Bridge\PhpUnit\SymfonyTestsListener');
} elseif (version_compare(\PHPUnit\Runner\Version::id(), '7.0.0', '<')) {
class_alias('Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerForV6', 'Symfony\Bridge\PhpUnit\SymfonyTestsListener');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

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

if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) {
if (version_compare(\PHPUnit\Runner\Version::id(), '6.0.0', '<')) {
require_once __DIR__.'/../../../../Legacy/CoverageListenerForV5.php';
} elseif (version_compare(\PHPUnit\Runner\Version::id(), '7.0.0', '<')) {
require_once __DIR__.'/../../../../Legacy/CoverageListenerForV6.php';
Expand Down
3 changes: 1 addition & 2 deletions src/Symfony/Bridge/PhpUnit/Tests/ProcessIsolationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public function testIsolation()

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

trigger_error('Test that PHPUnit\'s error handler fires.', E_USER_WARNING);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/PhpUnit/TextUI/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Bridge\PhpUnit\TextUI;

if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) {
if (version_compare(\PHPUnit\Runner\Version::id(), '6.0.0', '<')) {
class_alias('Symfony\Bridge\PhpUnit\Legacy\CommandForV5', 'Symfony\Bridge\PhpUnit\TextUI\Command');
} else {
class_alias('Symfony\Bridge\PhpUnit\Legacy\CommandForV6', 'Symfony\Bridge\PhpUnit\TextUI\Command');
Expand Down
Loading
0