Skip to content
16 changes: 12 additions & 4 deletions src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down
71 changes: 45 additions & 26 deletions src/Symfony/Bridge/PhpUnit/SymfonyTestsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <p@tchwork.com>
*/
class SymfonyTestsListener extends \PHPUnit_Framework_BaseTestListener
class SymfonyTestsListener extends BaseTestListener
{
private static $globallyEnabled = false;
private $state = -1;
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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());
}
Expand All @@ -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;
Expand All @@ -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 {
Expand All @@ -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));
Expand All @@ -159,50 +178,50 @@ 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'));
}
}
}

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);
}
}

$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);
}
Expand All @@ -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);
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Bridge/PhpUnit/Tests/DnsMockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
8 changes: 7 additions & 1 deletion src/Symfony/Bridge/PhpUnit/TextUI/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Bridge/PhpUnit/TextUI/TestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/PhpUnit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down