Skip to content

Commit

Permalink
Fixed PHPUnit 8.3 incompatibility: method handleError was renamed to …
Browse files Browse the repository at this point in the history
…__invoke
  • Loading branch information
karser committed Aug 3, 2019
1 parent 8f1d9d2 commit c32c4a4
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 10 deletions.
30 changes: 20 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) {
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,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);
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;
}
}

0 comments on commit c32c4a4

Please sign in to comment.