1111
1212namespace Symfony \Bridge \PhpUnit ;
1313
14+ use PHPUnit \Framework \TestResult ;
1415use PHPUnit \Util \ErrorHandler ;
1516use Symfony \Bridge \PhpUnit \DeprecationErrorHandler \Configuration ;
1617use Symfony \Bridge \PhpUnit \DeprecationErrorHandler \Deprecation ;
@@ -49,6 +50,7 @@ class DeprecationErrorHandler
4950 ];
5051
5152 private static $ isRegistered = false ;
53+ private static $ isAtLeastPhpUnit83 ;
5254
5355 /**
5456 * Registers and configures the deprecation handler.
@@ -72,13 +74,15 @@ public static function register($mode = 0)
7274 return ;
7375 }
7476
77+ self ::$ isAtLeastPhpUnit83 = !class_exists ('PHPUnit_Util_ErrorHandler ' ) && method_exists (ErrorHandler::class, '__invoke ' );
78+
7579 $ handler = new self ();
7680 $ oldErrorHandler = set_error_handler ([$ handler , 'handleError ' ]);
7781
7882 if (null !== $ oldErrorHandler ) {
7983 restore_error_handler ();
8084
81- if ([ErrorHandler::class, 'handleError ' ] === $ oldErrorHandler ) {
85+ if ($ oldErrorHandler instanceof ErrorHandler || [ErrorHandler::class, 'handleError ' ] === $ oldErrorHandler ) {
8286 restore_error_handler ();
8387 self ::register ($ mode );
8488 }
@@ -98,7 +102,7 @@ public static function collectDeprecations($outputFile)
98102 return $ previousErrorHandler ($ type , $ msg , $ file , $ line , $ context );
99103 }
100104
101- return ErrorHandler:: handleError ( $ type , $ msg , $ file , $ line , $ context );
105+ return \call_user_func ( self :: getPhpUnitErrorHandler (), $ type , $ msg , $ file , $ line , $ context );
102106 }
103107
104108 $ deprecations [] = [error_reporting (), $ msg , $ file ];
@@ -115,7 +119,7 @@ public static function collectDeprecations($outputFile)
115119 public function handleError ($ type , $ msg , $ file , $ line , $ context = [])
116120 {
117121 if ((E_USER_DEPRECATED !== $ type && E_DEPRECATED !== $ type ) || !$ this ->getConfiguration ()->isEnabled ()) {
118- return ErrorHandler:: handleError ( $ type , $ msg , $ file , $ line , $ context );
122+ return \call_user_func ( self :: getPhpUnitErrorHandler (), $ type , $ msg , $ file , $ line , $ context );
119123 }
120124
121125 $ deprecation = new Deprecation ($ msg , debug_backtrace (), $ file );
@@ -310,6 +314,26 @@ private function displayDeprecations($groups, $configuration)
310314 }
311315 }
312316
317+ private static function getPhpUnitErrorHandler ()
318+ {
319+ if (!self ::$ isAtLeastPhpUnit83 ) {
320+ return 'PHPUnit\Util\ErrorHandler::handleError ' ;
321+ }
322+
323+ foreach (debug_backtrace (DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS ) as $ frame ) {
324+ if (isset ($ frame ['object ' ]) && $ frame ['object ' ] instanceof TestResult) {
325+ return new ErrorHandler (
326+ $ frame ['object ' ]->getConvertDeprecationsToExceptions (),
327+ $ frame ['object ' ]->getConvertErrorsToExceptions (),
328+ $ frame ['object ' ]->getConvertNoticesToExceptions (),
329+ $ frame ['object ' ]->getConvertWarningsToExceptions ()
330+ );
331+ }
332+ }
333+
334+ return function () { return false ; };
335+ }
336+
313337 /**
314338 * Returns true if STDOUT is defined and supports colorization.
315339 *
0 commit comments