From c32c4a4caaa908440accc5698628694c13d1227d Mon Sep 17 00:00:00 2001 From: karser Date: Fri, 2 Aug 2019 22:29:48 +0300 Subject: [PATCH] Fixed PHPUnit 8.3 incompatibility: method handleError was renamed to __invoke --- .../PhpUnit/DeprecationErrorHandler.php | 30 ++++--- .../PhpUnit/Legacy/ErrorHandlerCallerV83.php | 86 +++++++++++++++++++ 2 files changed, 106 insertions(+), 10 deletions(-) create mode 100644 src/Symfony/Bridge/PhpUnit/Legacy/ErrorHandlerCallerV83.php diff --git a/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php b/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php index e059fe2a9d995..dbd21e14f5f26 100644 --- a/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php +++ b/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php @@ -13,6 +13,7 @@ use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Configuration; use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Deprecation; +use Symfony\Bridge\PhpUnit\Legacy\ErrorHandlerCallerV83; /** * Catch deprecation notices and print a summary report at the end of the test suite. @@ -49,6 +50,7 @@ class DeprecationErrorHandler private static $isRegistered = false; private static $utilPrefix; + private static $isHandlerInvokable; /** * Registers and configures the deprecation handler. @@ -73,6 +75,7 @@ public static function register($mode = 0) } self::$utilPrefix = class_exists('PHPUnit_Util_ErrorHandler') ? 'PHPUnit_Util_' : 'PHPUnit\Util\\'; + self::$isHandlerInvokable = method_exists(self::$utilPrefix.'ErrorHandler', '__invoke'); $handler = new self(); $oldErrorHandler = set_error_handler([$handler, 'handleError']); @@ -80,7 +83,8 @@ public static function register($mode = 0) if (null !== $oldErrorHandler) { restore_error_handler(); - if ([self::$utilPrefix.'ErrorHandler', 'handleError'] === $oldErrorHandler) { + $handlerMethod = self::$isHandlerInvokable ? '__invoke' : 'handleError'; + if ([self::$utilPrefix.'ErrorHandler', $handlerMethod] === $oldErrorHandler) { restore_error_handler(); self::register($mode); } @@ -100,12 +104,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 self::callPhpUnitErrorHandler($type, $msg, $file, $line, $context); } $deprecations[] = [error_reporting(), $msg, $file]; @@ -116,15 +115,26 @@ public static function collectDeprecations($outputFile) }); } + private static function callPhpUnitErrorHandler($type, $msg, $file, $line, $context) + { + $ErrorHandler = self::$utilPrefix.'ErrorHandler'; + if (self::$isHandlerInvokable) { + $handler = new ErrorHandlerCallerV83($_SERVER['argv']); + return $handler->handleError($type, $msg, $file, $line, $context); +// $object = new $ErrorHandler($convertDeprecationsToExceptions = true, $convertErrorsToExceptions = true, $convertNoticesToExceptions = true, $convertWarningsToExceptions = true); +// return $object($type, $msg, $file, $line, $context); + } + + return $ErrorHandler::handleError($type, $msg, $file, $line, $context); + } + /** * @internal */ 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 static::callPhpUnitErrorHandler($type, $msg, $file, $line, $context); } $deprecation = new Deprecation($msg, debug_backtrace(), $file); diff --git a/src/Symfony/Bridge/PhpUnit/Legacy/ErrorHandlerCallerV83.php b/src/Symfony/Bridge/PhpUnit/Legacy/ErrorHandlerCallerV83.php new file mode 100644 index 0000000000000..8bd6f0f4fcaac --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Legacy/ErrorHandlerCallerV83.php @@ -0,0 +1,86 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\PhpUnit\Legacy; + +use PHPUnit\TextUI\Command; +use PHPUnit\Util\Configuration; +use PHPUnit\Util\ErrorHandler; + +/** + * Since PHPUnit v8.3 ErrorHandler is invokable and requires variables in constructor. + * This class reuses PHPUnit infrastructure to get these variables. + * + * @author Dmitrii Poddubnyi + * + * @internal + */ +class ErrorHandlerCallerV83 extends Command +{ + public function __construct(array $argv) + { + $this->handleArguments($argv); + } + + public function handleError($type, $msg, $file, $line, $context) + { + $arguments = $this->arguments; + $this->handleConfiguration($arguments); + $object = new ErrorHandler( + $arguments['convertDeprecationsToExceptions'], + $arguments['convertErrorsToExceptions'], + $arguments['convertNoticesToExceptions'], + $arguments['convertWarningsToExceptions'] + ); + + return $object($type, $msg, $file, $line, $context); + } + + /** + * This is simplified version of PHPUnit\TextUI\TestRunner::handleConfiguration. + * https://github.com/sebastianbergmann/phpunit/blob/8.3.2/src/TextUI/TestRunner.php#L815-L1243 + */ + private function handleConfiguration(array &$arguments): void + { + if (isset($arguments['configuration']) && + !$arguments['configuration'] instanceof Configuration) { + $arguments['configuration'] = Configuration::getInstance( + $arguments['configuration'] + ); + } + + if (isset($arguments['configuration'])) { + $arguments['configuration']->handlePHPConfiguration(); + + $phpunitConfiguration = $arguments['configuration']->getPHPUnitConfiguration(); + + if (isset($phpunitConfiguration['convertDeprecationsToExceptions']) && !isset($arguments['convertDeprecationsToExceptions'])) { + $arguments['convertDeprecationsToExceptions'] = $phpunitConfiguration['convertDeprecationsToExceptions']; + } + + if (isset($phpunitConfiguration['convertErrorsToExceptions']) && !isset($arguments['convertErrorsToExceptions'])) { + $arguments['convertErrorsToExceptions'] = $phpunitConfiguration['convertErrorsToExceptions']; + } + + if (isset($phpunitConfiguration['convertNoticesToExceptions']) && !isset($arguments['convertNoticesToExceptions'])) { + $arguments['convertNoticesToExceptions'] = $phpunitConfiguration['convertNoticesToExceptions']; + } + + if (isset($phpunitConfiguration['convertWarningsToExceptions']) && !isset($arguments['convertWarningsToExceptions'])) { + $arguments['convertWarningsToExceptions'] = $phpunitConfiguration['convertWarningsToExceptions']; + } + } + $arguments['convertDeprecationsToExceptions'] = $arguments['convertDeprecationsToExceptions'] ?? true; + $arguments['convertErrorsToExceptions'] = $arguments['convertErrorsToExceptions'] ?? true; + $arguments['convertNoticesToExceptions'] = $arguments['convertNoticesToExceptions'] ?? true; + $arguments['convertWarningsToExceptions'] = $arguments['convertWarningsToExceptions'] ?? true; + } +}