Skip to content

Commit 12717c9

Browse files
committed
Fixed PHPUnit 8.3 incompatibility: method handleError was renamed to __invoke
1 parent 8f1d9d2 commit 12717c9

File tree

2 files changed

+105
-10
lines changed

2 files changed

+105
-10
lines changed

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php

+19-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Configuration;
1515
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Deprecation;
16+
use Symfony\Bridge\PhpUnit\Legacy\ErrorHandlerCallerV83;
1617

1718
/**
1819
* Catch deprecation notices and print a summary report at the end of the test suite.
@@ -49,6 +50,7 @@ class DeprecationErrorHandler
4950

5051
private static $isRegistered = false;
5152
private static $utilPrefix;
53+
private static $isHandlerInvokable;
5254

5355
/**
5456
* Registers and configures the deprecation handler.
@@ -73,14 +75,16 @@ public static function register($mode = 0)
7375
}
7476

7577
self::$utilPrefix = class_exists('PHPUnit_Util_ErrorHandler') ? 'PHPUnit_Util_' : 'PHPUnit\Util\\';
78+
self::$isHandlerInvokable = method_exists(self::$utilPrefix.'ErrorHandler', '__invoke');
7679

7780
$handler = new self();
7881
$oldErrorHandler = set_error_handler([$handler, 'handleError']);
7982

8083
if (null !== $oldErrorHandler) {
8184
restore_error_handler();
8285

83-
if ([self::$utilPrefix.'ErrorHandler', 'handleError'] === $oldErrorHandler) {
86+
$handlerMethod = self::$isHandlerInvokable ? '__invoke' : 'handleError';
87+
if ([self::$utilPrefix.'ErrorHandler', $handlerMethod] === $oldErrorHandler) {
8488
restore_error_handler();
8589
self::register($mode);
8690
}
@@ -100,12 +104,7 @@ public static function collectDeprecations($outputFile)
100104
return $previousErrorHandler($type, $msg, $file, $line, $context);
101105
}
102106

103-
static $autoload = true;
104-
105-
$ErrorHandler = class_exists('PHPUnit_Util_ErrorHandler', $autoload) ? 'PHPUnit_Util_ErrorHandler' : 'PHPUnit\Util\ErrorHandler';
106-
$autoload = false;
107-
108-
return $ErrorHandler::handleError($type, $msg, $file, $line, $context);
107+
return self::callPhpUnitErrorHandler($type, $msg, $file, $line, $context);
109108
}
110109

111110
$deprecations[] = [error_reporting(), $msg, $file];
@@ -116,15 +115,25 @@ public static function collectDeprecations($outputFile)
116115
});
117116
}
118117

118+
private static function callPhpUnitErrorHandler($type, $msg, $file, $line, $context)
119+
{
120+
$ErrorHandler = self::$utilPrefix.'ErrorHandler';
121+
if (self::$isHandlerInvokable) {
122+
$handler = new ErrorHandlerCallerV83($_SERVER['argv']);
123+
124+
return $handler->handleError($type, $msg, $file, $line, $context);
125+
}
126+
127+
return $ErrorHandler::handleError($type, $msg, $file, $line, $context);
128+
}
129+
119130
/**
120131
* @internal
121132
*/
122133
public function handleError($type, $msg, $file, $line, $context = [])
123134
{
124135
if ((E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) || !$this->getConfiguration()->isEnabled()) {
125-
$ErrorHandler = self::$utilPrefix.'ErrorHandler';
126-
127-
return $ErrorHandler::handleError($type, $msg, $file, $line, $context);
136+
return static::callPhpUnitErrorHandler($type, $msg, $file, $line, $context);
128137
}
129138

130139
$deprecation = new Deprecation($msg, debug_backtrace(), $file);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\PhpUnit\Legacy;
13+
14+
use PHPUnit\TextUI\Command;
15+
use PHPUnit\Util\Configuration;
16+
use PHPUnit\Util\ErrorHandler;
17+
18+
/**
19+
* Since PHPUnit v8.3 ErrorHandler is invokable and requires variables in constructor.
20+
* This class reuses PHPUnit infrastructure to get these variables.
21+
*
22+
* @author Dmitrii Poddubnyi <dpoddubny@gmail.com>
23+
*
24+
* @internal
25+
*/
26+
class ErrorHandlerCallerV83 extends Command
27+
{
28+
public function __construct(array $argv)
29+
{
30+
$this->handleArguments($argv);
31+
}
32+
33+
public function handleError($type, $msg, $file, $line, $context)
34+
{
35+
$arguments = $this->arguments;
36+
$this->handleConfiguration($arguments);
37+
$object = new ErrorHandler(
38+
$arguments['convertDeprecationsToExceptions'],
39+
$arguments['convertErrorsToExceptions'],
40+
$arguments['convertNoticesToExceptions'],
41+
$arguments['convertWarningsToExceptions']
42+
);
43+
44+
return $object($type, $msg, $file, $line, $context);
45+
}
46+
47+
/**
48+
* This is simplified version of PHPUnit\TextUI\TestRunner::handleConfiguration.
49+
* https://github.com/sebastianbergmann/phpunit/blob/8.3.2/src/TextUI/TestRunner.php#L815-L1243.
50+
*/
51+
private function handleConfiguration(array &$arguments): void
52+
{
53+
if (isset($arguments['configuration']) &&
54+
!$arguments['configuration'] instanceof Configuration) {
55+
$arguments['configuration'] = Configuration::getInstance(
56+
$arguments['configuration']
57+
);
58+
}
59+
60+
if (isset($arguments['configuration'])) {
61+
$arguments['configuration']->handlePHPConfiguration();
62+
63+
$phpunitConfiguration = $arguments['configuration']->getPHPUnitConfiguration();
64+
65+
if (isset($phpunitConfiguration['convertDeprecationsToExceptions']) && !isset($arguments['convertDeprecationsToExceptions'])) {
66+
$arguments['convertDeprecationsToExceptions'] = $phpunitConfiguration['convertDeprecationsToExceptions'];
67+
}
68+
69+
if (isset($phpunitConfiguration['convertErrorsToExceptions']) && !isset($arguments['convertErrorsToExceptions'])) {
70+
$arguments['convertErrorsToExceptions'] = $phpunitConfiguration['convertErrorsToExceptions'];
71+
}
72+
73+
if (isset($phpunitConfiguration['convertNoticesToExceptions']) && !isset($arguments['convertNoticesToExceptions'])) {
74+
$arguments['convertNoticesToExceptions'] = $phpunitConfiguration['convertNoticesToExceptions'];
75+
}
76+
77+
if (isset($phpunitConfiguration['convertWarningsToExceptions']) && !isset($arguments['convertWarningsToExceptions'])) {
78+
$arguments['convertWarningsToExceptions'] = $phpunitConfiguration['convertWarningsToExceptions'];
79+
}
80+
}
81+
$arguments['convertDeprecationsToExceptions'] = $arguments['convertDeprecationsToExceptions'] ?? true;
82+
$arguments['convertErrorsToExceptions'] = $arguments['convertErrorsToExceptions'] ?? true;
83+
$arguments['convertNoticesToExceptions'] = $arguments['convertNoticesToExceptions'] ?? true;
84+
$arguments['convertWarningsToExceptions'] = $arguments['convertWarningsToExceptions'] ?? true;
85+
}
86+
}

0 commit comments

Comments
 (0)