bug #32933 [PhpUnitBridge] fixed PHPUnit 8.3 compatibility: method handleError was renamed to __invoke (karser)

This PR was merged into the 3.4 branch.

Discussion
----------

[PhpUnitBridge] fixed PHPUnit 8.3 compatibility: method handleError was renamed to __invoke

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #32879   <!-- #-prefixed issue number(s), if any -->
| License       | MIT

The PHPUnit method  [handleError](https://github.com/sebastianbergmann/phpunit/blob/8.2.5/src/Util/ErrorHandler.php#L38) was renamed to [__invoke](https://github.com/sebastianbergmann/phpunit/blob/8.3/src/Util/ErrorHandler.php#L71) in v8.3.

So we should check in Symfony [DeprecationErrorHandler](https://github.com/symfony/symfony/blob/v4.3.3/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php) if method `handleError` exists, otherwise call `__invoke`

It works with phpunit v8.2.5 and 8.3.2.
The PHPUnit handler is called when I trigger some error, e.g `iconv('fdsfs', 'fsdfds', '');`

Commits
-------

0c9539fdb4 [PhpUnitBridge] fixed PHPUnit 8.3 compatibility: method handleError was renamed to __invoke
This commit is contained in:
Nicolas Grekas 2019-08-05 15:16:20 +02:00
commit b5e99f3b65
1 changed files with 31 additions and 9 deletions

View File

@ -11,6 +11,9 @@
namespace Symfony\Bridge\PhpUnit;
use PHPUnit\Framework\TestResult;
use PHPUnit\Util\ErrorHandler;
/**
* Catch deprecation notices and print a summary report at the end of the test suite.
*
@ -23,6 +26,7 @@ class DeprecationErrorHandler
const MODE_DISABLED = 'disabled';
private static $isRegistered = false;
private static $isAtLeastPhpUnit83;
/**
* Registers and configures the deprecation handler.
@ -44,6 +48,7 @@ class DeprecationErrorHandler
}
$UtilPrefix = class_exists('PHPUnit_Util_ErrorHandler') ? 'PHPUnit_Util_' : 'PHPUnit\Util\\';
self::$isAtLeastPhpUnit83 = method_exists('PHPUnit\Util\ErrorHandler', '__invoke');
$getMode = function () use ($mode) {
static $memoizedMode = false;
@ -106,9 +111,7 @@ class DeprecationErrorHandler
);
$deprecationHandler = function ($type, $msg, $file, $line, $context = array()) use (&$deprecations, $getMode, $UtilPrefix, $inVendors) {
if ((E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) || DeprecationErrorHandler::MODE_DISABLED === $mode = $getMode()) {
$ErrorHandler = $UtilPrefix.'ErrorHandler';
return $ErrorHandler::handleError($type, $msg, $file, $line, $context);
return \call_user_func(DeprecationErrorHandler::getPhpUnitErrorHandler(), $type, $msg, $file, $line, $context);
}
$trace = debug_backtrace();
@ -183,7 +186,7 @@ class DeprecationErrorHandler
if (null !== $oldErrorHandler) {
restore_error_handler();
if (array($UtilPrefix.'ErrorHandler', 'handleError') === $oldErrorHandler) {
if ($oldErrorHandler instanceof ErrorHandler || array($UtilPrefix.'ErrorHandler', 'handleError') === $oldErrorHandler) {
restore_error_handler();
self::register($mode);
}
@ -285,12 +288,8 @@ class DeprecationErrorHandler
if ($previousErrorHandler) {
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 \call_user_func(DeprecationErrorHandler::getPhpUnitErrorHandler(), $type, $msg, $file, $line, $context);
}
$deprecations[] = array(error_reporting(), $msg, $file);
});
@ -300,6 +299,29 @@ class DeprecationErrorHandler
});
}
/**
* @internal
*/
public static function getPhpUnitErrorHandler()
{
if (!self::$isAtLeastPhpUnit83) {
return (class_exists('PHPUnit_Util_ErrorHandler', false) ? 'PHPUnit_Util_' : 'PHPUnit\Util\\').'ErrorHandler::handleError';
}
foreach (debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) {
if (isset($frame['object']) && $frame['object'] instanceof TestResult) {
return new ErrorHandler(
$frame['object']->getConvertDeprecationsToExceptions(),
$frame['object']->getConvertErrorsToExceptions(),
$frame['object']->getConvertNoticesToExceptions(),
$frame['object']->getConvertWarningsToExceptions()
);
}
}
return function () { return false; };
}
/**
* Returns true if STDOUT is defined and supports colorization.
*