Skip to content

Commit 6974ebb

Browse files
YaFounicolas-grekas
authored andcommitted
[PhpUnitBridge] Fix deprecation handler with PHPUnit 10
1 parent 279ffbf commit 6974ebb

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

DeprecationErrorHandler.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\PhpUnit;
1313

1414
use PHPUnit\Framework\TestResult;
15+
use PHPUnit\Util\Error\Handler;
1516
use PHPUnit\Util\ErrorHandler;
1617
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Configuration;
1718
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Deprecation;
@@ -51,7 +52,7 @@ class DeprecationErrorHandler
5152
];
5253

5354
private static $isRegistered = false;
54-
private static $isAtLeastPhpUnit83;
55+
private static $errorHandler;
5556

5657
/**
5758
* Registers and configures the deprecation handler.
@@ -335,16 +336,23 @@ private function displayDeprecations($groups, $configuration)
335336

336337
private static function getPhpUnitErrorHandler()
337338
{
338-
if (!isset(self::$isAtLeastPhpUnit83)) {
339-
self::$isAtLeastPhpUnit83 = class_exists(ErrorHandler::class) && method_exists(ErrorHandler::class, '__invoke');
339+
if (!$eh = self::$errorHandler) {
340+
if (class_exists(Handler::class)) {
341+
$eh = self::$errorHandler = Handler::class;
342+
} elseif (method_exists(ErrorHandler::class, '__invoke')) {
343+
$eh = self::$errorHandler = ErrorHandler::class;
344+
} else {
345+
return self::$errorHandler = 'PHPUnit\Util\ErrorHandler::handleError';
346+
}
340347
}
341-
if (!self::$isAtLeastPhpUnit83) {
342-
return 'PHPUnit\Util\ErrorHandler::handleError';
348+
349+
if ('PHPUnit\Util\ErrorHandler::handleError' === $eh) {
350+
return $eh;
343351
}
344352

345353
foreach (debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) {
346354
if (isset($frame['object']) && $frame['object'] instanceof TestResult) {
347-
return new ErrorHandler(
355+
return new $eh(
348356
$frame['object']->getConvertDeprecationsToExceptions(),
349357
$frame['object']->getConvertErrorsToExceptions(),
350358
$frame['object']->getConvertNoticesToExceptions(),

DeprecationErrorHandler/Deprecation.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use PHPUnit\Framework\TestSuite;
16+
use PHPUnit\Metadata\Api\Groups;
17+
use PHPUnit\Util\Error\Handler;
1618
use PHPUnit\Util\Test;
1719
use Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerFor;
1820
use Symfony\Component\Debug\DebugClassLoader as LegacyDebugClassLoader;
@@ -192,12 +194,13 @@ public function isLegacy()
192194
}
193195

194196
$method = $this->originatingMethod();
197+
$groups = class_exists(Groups::class) ? [new Groups(), 'groups'] : [Test::class, 'getGroups'];
195198

196199
return 0 === strpos($method, 'testLegacy')
197200
|| 0 === strpos($method, 'provideLegacy')
198201
|| 0 === strpos($method, 'getLegacy')
199202
|| strpos($this->originClass, '\Legacy')
200-
|| \in_array('legacy', Test::getGroups($this->originClass, $method), true);
203+
|| \in_array('legacy', $groups($this->originClass, $method), true);
201204
}
202205

203206
/**

0 commit comments

Comments
 (0)