diff --git a/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php b/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php index ca85d8aba5994..30aa65075330a 100644 --- a/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php +++ b/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php @@ -11,6 +11,14 @@ namespace Symfony\Bridge\PhpUnit; +if (class_exists('PHPUnit\Framework\Test')) { + use PHPUnit\Util\ErrorHandler; + use PHPUnit\Util\Test; +} else { + use \PHPUnit_Util_ErrorHandler as ErrorHandler; + use \PHPUnit_Util_Test as Test; +} + /** * Catch deprecation notices and print a summary report at the end of the test suite. * @@ -70,14 +78,14 @@ public static function register($mode = 0) $deprecationHandler = function ($type, $msg, $file, $line, $context) use (&$deprecations, $getMode) { $mode = $getMode(); if ((E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) || DeprecationErrorHandler::MODE_DISABLED === $mode) { - return \PHPUnit_Util_ErrorHandler::handleError($type, $msg, $file, $line, $context); + return ErrorHandler::handleError($type, $msg, $file, $line, $context); } $trace = debug_backtrace(true); $group = 'other'; $i = count($trace); - while (1 < $i && (!isset($trace[--$i]['class']) || ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_')))) { + while (1 < $i && (!isset($trace[--$i]['class']) || ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_') || 0 === strpos($trace[$i]['class'], 'PHPUnit\\')))) { // No-op } @@ -91,7 +99,7 @@ public static function register($mode = 0) || 0 === strpos($method, 'provideLegacy') || 0 === strpos($method, 'getLegacy') || strpos($class, '\Legacy') - || in_array('legacy', \PHPUnit_Util_Test::getGroups($class, $method), true) + || in_array('legacy', Test::getGroups($class, $method), true) ) { $group = 'legacy'; } else { @@ -128,7 +136,7 @@ public static function register($mode = 0) if (null !== $oldErrorHandler) { restore_error_handler(); - if (array('PHPUnit_Util_ErrorHandler', 'handleError') === $oldErrorHandler) { + if (array('PHPUnit_Util_ErrorHandler', 'handleError') === $oldErrorHandler || array('PHPUnit\Util\ErrorHandler', 'handleError') === $oldErrorHandler) { restore_error_handler(); self::register($mode); } diff --git a/src/Symfony/Bridge/PhpUnit/SymfonyTestsListener.php b/src/Symfony/Bridge/PhpUnit/SymfonyTestsListener.php index 339ff3f8d417e..b744ec9a9e7f2 100644 --- a/src/Symfony/Bridge/PhpUnit/SymfonyTestsListener.php +++ b/src/Symfony/Bridge/PhpUnit/SymfonyTestsListener.php @@ -12,13 +12,32 @@ namespace Symfony\Bridge\PhpUnit; use Doctrine\Common\Annotations\AnnotationRegistry; +if (class_exists('PHPUnit\Framework\Test')) { + use PHPUnit\Framework\AssertionFailedError; + use PHPUnit\Framework\Test; + use PHPUnit\Framework\TestCase; + use PHPUnit\Framework\TestSuite; + use PHPUnit\Util\Blacklist; + use PHPUnit\Util\Test as TestUtil; + use PHPUnit\Framework\Warning; +} else { + use \PHPUnit_Framework_AssertionFailedError as AssertionFailedError; + use \PHPUnit_Framework_BaseTestListener as BaseTestListener; + use \PHPUnit_Framework_Test as Test; + use \PHPUnit_Framework_TestCase as TestCase; + use \PHPUnit_Framework_TestSuite as TestSuite; + use \PHPUnit_Framework_Warning as Warning; + use \PHPUnit_Runner_BaseTestRunner as BaseTestRunner; + use \PHPUnit_Util_Blacklist as Blacklist; + use \PHPUnit_Util_Test as TestUtil; +} /** * Collects and replays skipped tests. * * @author Nicolas Grekas
*/ -class SymfonyTestsListener extends \PHPUnit_Framework_BaseTestListener +class SymfonyTestsListener extends BaseTestListener { private static $globallyEnabled = false; private $state = -1; @@ -35,7 +54,7 @@ class SymfonyTestsListener extends \PHPUnit_Framework_BaseTestListener */ public function __construct(array $mockedNamespaces = array()) { - \PHPUnit_Util_Blacklist::$blacklistedClassNames['\Symfony\Bridge\PhpUnit\SymfonyTestsListener'] = 1; + Blacklist::$blacklistedClassNames['\Symfony\Bridge\PhpUnit\SymfonyTestsListener'] = 1; $warn = false; foreach ($mockedNamespaces as $type => $namespaces) { @@ -75,7 +94,7 @@ public function __destruct() } } - public function startTestSuite(\PHPUnit_Framework_TestSuite $suite) + public function startTestSuite(TestSuite $suite) { $suiteName = $suite->getName(); $this->testsWithWarnings = array(); @@ -103,12 +122,12 @@ public function startTestSuite(\PHPUnit_Framework_TestSuite $suite) $testSuites = array($suite); for ($i = 0; isset($testSuites[$i]); ++$i) { foreach ($testSuites[$i]->tests() as $test) { - if ($test instanceof \PHPUnit_Framework_TestSuite) { + if ($test instanceof TestSuite) { if (!class_exists($test->getName(), false)) { $testSuites[] = $test; continue; } - $groups = \PHPUnit_Util_Test::getGroups($test->getName()); + $groups = TestUtil::getGroups($test->getName()); if (in_array('time-sensitive', $groups, true)) { ClockMock::register($test->getName()); } @@ -121,7 +140,7 @@ public function startTestSuite(\PHPUnit_Framework_TestSuite $suite) } elseif (2 === $this->state) { $skipped = array(); foreach ($suite->tests() as $test) { - if (!$test instanceof \PHPUnit_Framework_TestCase + if (!$test instanceof TestCase || isset($this->wasSkipped[$suiteName]['*']) || isset($this->wasSkipped[$suiteName][$test->getName()])) { $skipped[] = $test; @@ -131,10 +150,10 @@ public function startTestSuite(\PHPUnit_Framework_TestSuite $suite) } } - public function addSkippedTest(\PHPUnit_Framework_Test $test, \Exception $e, $time) + public function addSkippedTest(Test $test, \Exception $e, $time) { if (0 < $this->state) { - if ($test instanceof \PHPUnit_Framework_TestCase) { + if ($test instanceof TestCase) { $class = get_class($test); $method = $test->getName(); } else { @@ -146,10 +165,10 @@ public function addSkippedTest(\PHPUnit_Framework_Test $test, \Exception $e, $ti } } - public function startTest(\PHPUnit_Framework_Test $test) + public function startTest(Test $test) { - if (-2 < $this->state && $test instanceof \PHPUnit_Framework_TestCase) { - $groups = \PHPUnit_Util_Test::getGroups(get_class($test), $test->getName(false)); + if (-2 < $this->state && $test instanceof TestCase) { + $groups = TestUtil::getGroups(get_class($test), $test->getName(false)); if (in_array('time-sensitive', $groups, true)) { ClockMock::register(get_class($test)); @@ -159,14 +178,14 @@ public function startTest(\PHPUnit_Framework_Test $test) DnsMock::register(get_class($test)); } - $annotations = \PHPUnit_Util_Test::parseTestMethodAnnotations(get_class($test), $test->getName(false)); + $annotations = TestUtil::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); + $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)) { - $test->getTestResultObject()->addError($test, new \PHPUnit_Framework_AssertionFailedError('Only tests with the `@group legacy` annotation can have `@expectedDeprecation`.'), 0); + $test->getTestResultObject()->addError($test, new 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')); @@ -174,27 +193,27 @@ public function startTest(\PHPUnit_Framework_Test $test) } } - public function addWarning(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_Warning $e, $time) + public function addWarning(Test $test, Warning $e, $time) { - if ($test instanceof \PHPUnit_Framework_TestCase) { + if ($test instanceof TestCase) { $this->testsWithWarnings[$test->getName()] = true; } } - public function endTest(\PHPUnit_Framework_Test $test, $time) + public function endTest(Test $test, $time) { $className = get_class($test); - $classGroups = \PHPUnit_Util_Test::getGroups($className); - $groups = \PHPUnit_Util_Test::getGroups($className, $test->getName(false)); + $classGroups = TestUtil::getGroups($className); + $groups = TestUtil::getGroups($className, $test->getName(false)); if ($this->expectedDeprecations) { restore_error_handler(); - if (!in_array($test->getStatus(), array(\PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED, \PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE, \PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE, \PHPUnit_Runner_BaseTestRunner::STATUS_ERROR), true)) { + if (!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 (\PHPUnit_Framework_AssertionFailedError $e) { + } catch (AssertionFailedError $e) { $test->getTestResultObject()->addFailure($test, $e, $time); } } @@ -202,7 +221,7 @@ public function endTest(\PHPUnit_Framework_Test $test, $time) $this->expectedDeprecations = $this->gatheredDeprecations = array(); $this->previousErrorHandler = null; } - if (-2 < $this->state && $test instanceof \PHPUnit_Framework_TestCase) { + if (-2 < $this->state && $test instanceof TestCase) { if (in_array('time-sensitive', $groups, true)) { ClockMock::withClockMock(false); } @@ -211,19 +230,19 @@ public function endTest(\PHPUnit_Framework_Test $test, $time) } } - if ($test instanceof \PHPUnit_Framework_TestCase && 0 === strpos($test->getName(), 'testLegacy') && !isset($this->testsWithWarnings[$test->getName()]) && !in_array('legacy', $groups, true)) { + if ($test instanceof TestCase && 0 === strpos($test->getName(), 'testLegacy') && !isset($this->testsWithWarnings[$test->getName()]) && !in_array('legacy', $groups, true)) { $result = $test->getTestResultObject(); if (method_exists($result, 'addWarning')) { - $result->addWarning($test, new \PHPUnit_Framework_Warning('Using the "testLegacy" prefix to mark tests as legacy is deprecated since version 3.3 and will be removed in 4.0. Use the "@group legacy" notation instead to add the test to the legacy group.'), $time); + $result->addWarning($test, new Warning('Using the "testLegacy" prefix to mark tests as legacy is deprecated since version 3.3 and will be removed in 4.0. Use the "@group legacy" notation instead to add the test to the legacy group.'), $time); } } - if ($test instanceof \PHPUnit_Framework_TestCase && strpos($className, '\Legacy') && !isset($this->testsWithWarnings[$test->getName()]) && !in_array('legacy', $classGroups, true)) { + if ($test instanceof TestCase && strpos($className, '\Legacy') && !isset($this->testsWithWarnings[$test->getName()]) && !in_array('legacy', $classGroups, true)) { $result = $test->getTestResultObject(); if (method_exists($result, 'addWarning')) { - $result->addWarning($test, new \PHPUnit_Framework_Warning('Using the "Legacy" prefix to mark all tests of a class as legacy is deprecated since version 3.3 and will be removed in 4.0. Use the "@group legacy" notation instead to add the test to the legacy group.'), $time); + $result->addWarning($test, new Warning('Using the "Legacy" prefix to mark all tests of a class as legacy is deprecated since version 3.3 and will be removed in 4.0. Use the "@group legacy" notation instead to add the test to the legacy group.'), $time); } } } diff --git a/src/Symfony/Bridge/PhpUnit/Tests/DnsMockTest.php b/src/Symfony/Bridge/PhpUnit/Tests/DnsMockTest.php index 034d07a30f67a..276257f3a9225 100644 --- a/src/Symfony/Bridge/PhpUnit/Tests/DnsMockTest.php +++ b/src/Symfony/Bridge/PhpUnit/Tests/DnsMockTest.php @@ -11,9 +11,14 @@ namespace Symfony\Bridge\PhpUnit\Tests; +if (class_exists('PHPUnit\Framework\Test')) { + use PHPUnit\Framework\TestCase; +} else { + use \PHPUnit_Framework_TestCase as TestCase; +} use Symfony\Bridge\PhpUnit\DnsMock; -class DnsMockTest extends \PHPUnit_Framework_TestCase +class DnsMockTest extends TestCase { protected function tearDown() { diff --git a/src/Symfony/Bridge/PhpUnit/TextUI/Command.php b/src/Symfony/Bridge/PhpUnit/TextUI/Command.php index 203fd16414820..f7cc2976a4714 100644 --- a/src/Symfony/Bridge/PhpUnit/TextUI/Command.php +++ b/src/Symfony/Bridge/PhpUnit/TextUI/Command.php @@ -11,10 +11,16 @@ namespace Symfony\Bridge\PhpUnit\TextUI; +if (class_exists('PHPUnit\Framework\Test')) { + use PHPUnit\TextUI\Command as PHPUnitCommand; +} else { + use \PHPUnit_TextUI_Command as PHPUnitCommand; +} + /** * {@inheritdoc} */ -class Command extends \PHPUnit_TextUI_Command +class Command extends PHPUnitCommand { /** * {@inheritdoc} diff --git a/src/Symfony/Bridge/PhpUnit/TextUI/TestRunner.php b/src/Symfony/Bridge/PhpUnit/TextUI/TestRunner.php index 94602bb3d63ca..3bf1eb44e86bb 100644 --- a/src/Symfony/Bridge/PhpUnit/TextUI/TestRunner.php +++ b/src/Symfony/Bridge/PhpUnit/TextUI/TestRunner.php @@ -11,12 +11,17 @@ namespace Symfony\Bridge\PhpUnit\TextUI; +if (class_exists('PHPUnit\Framework\Test')) { + use PHPUnit\TextUI\TestRunner as PHPUnitTestRunner; +} else { + use \PHPUnit_TextUI_TestRunner as PHPUnitTestRunner; +} use Symfony\Bridge\PhpUnit\SymfonyTestsListener; /** * {@inheritdoc} */ -class TestRunner extends \PHPUnit_TextUI_TestRunner +class TestRunner extends PHPUnitTestRunner { /** * {@inheritdoc} diff --git a/src/Symfony/Bridge/PhpUnit/bootstrap.php b/src/Symfony/Bridge/PhpUnit/bootstrap.php index 5e2ed0ca85f82..f2ceffb1b51ae 100644 --- a/src/Symfony/Bridge/PhpUnit/bootstrap.php +++ b/src/Symfony/Bridge/PhpUnit/bootstrap.php @@ -13,7 +13,7 @@ use Symfony\Bridge\PhpUnit\DeprecationErrorHandler; // Detect if we're loaded by an actual run of phpunit -if (!defined('PHPUNIT_COMPOSER_INSTALL') && !class_exists('PHPUnit_TextUI_Command', false)) { +if (!defined('PHPUNIT_COMPOSER_INSTALL') && !class_exists('PHPUnit_TextUI_Command', false) && !class_exists('PHPUnit\TextUI\Command', false)) { return; }