Skip to content
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

Fixed PHPUnit 8.3 incompatibility: method handleError was renamed to __invoke #32890

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 19 additions & 10 deletions src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php
Expand Up @@ -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.
Expand Down Expand Up @@ -49,6 +50,7 @@ class DeprecationErrorHandler

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

/**
* Registers and configures the deprecation handler.
Expand All @@ -73,14 +75,16 @@ 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']);

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

if ([self::$utilPrefix.'ErrorHandler', 'handleError'] === $oldErrorHandler) {
$handlerMethod = self::$isHandlerInvokable ? '__invoke' : 'handleError';
if ([self::$utilPrefix.'ErrorHandler', $handlerMethod] === $oldErrorHandler) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the previous handler is the PhpUnit one, then $oldErrorHandler will contains an instance of PHPUnit\Util\ErrorHandler

restore_error_handler();
self::register($mode);
}
Expand All @@ -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];
Expand All @@ -116,15 +115,25 @@ 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);
}

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);
Expand Down
86 changes: 86 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Legacy/ErrorHandlerCallerV83.php
@@ -0,0 +1,86 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <dpoddubny@gmail.com>
*
* @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;
}
}