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

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

Merged
Merged
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
40 changes: 31 additions & 9 deletions src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php
Expand Up @@ -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.
*
Expand All @@ -23,6 +26,7 @@ class DeprecationErrorHandler
const MODE_DISABLED = 'disabled';

private static $isRegistered = false;
private static $isAtLeastPhpUnit83;

/**
* Registers and configures the deprecation handler.
Expand All @@ -44,6 +48,7 @@ public static function register($mode = 0)
}

$UtilPrefix = class_exists('PHPUnit_Util_ErrorHandler') ? 'PHPUnit_Util_' : 'PHPUnit\Util\\';
self::$isAtLeastPhpUnit83 = method_exists('PHPUnit\Util\ErrorHandler', '__invoke');
Copy link
Member

Choose a reason for hiding this comment

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

ErrorHandler::class

Copy link
Member

Choose a reason for hiding this comment

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

nvm. Didn't notice that version were 3.4


$getMode = function () use ($mode) {
static $memoizedMode = false;
Expand Down Expand Up @@ -106,9 +111,7 @@ public static function register($mode = 0)
);
$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();
Expand Down Expand Up @@ -183,7 +186,7 @@ public static function register($mode = 0)

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);
}
Expand Down Expand Up @@ -285,12 +288,8 @@ public static function collectDeprecations($outputFile)
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);
});
Expand All @@ -300,6 +299,29 @@ public static function collectDeprecations($outputFile)
});
}

/**
* @internal
*/
public static function getPhpUnitErrorHandler()
{
if (!self::$isAtLeastPhpUnit83) {
return (class_exists('PHPUnit_Util_ErrorHandler', false) ? 'PHPUnit_Util_' : 'PHPUnit\Util\\').'ErrorHandler::handleError';
Copy link
Member

Choose a reason for hiding this comment

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

return [ErrorHandler::class, 'handleError'] since #32940

Copy link
Member

Choose a reason for hiding this comment

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

nvm. Didn't notice that version were 3.4

}

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.
*
Expand Down