11
11
12
12
namespace Symfony \Bridge \PhpUnit ;
13
13
14
+ use PHPUnit \Framework \TestResult ;
14
15
use PHPUnit \Util \ErrorHandler ;
15
16
use Symfony \Bridge \PhpUnit \DeprecationErrorHandler \Configuration ;
16
17
use Symfony \Bridge \PhpUnit \DeprecationErrorHandler \Deprecation ;
@@ -49,6 +50,7 @@ class DeprecationErrorHandler
49
50
];
50
51
51
52
private static $ isRegistered = false ;
53
+ private static $ isAtLeastPhpUnit83 ;
52
54
53
55
/**
54
56
* Registers and configures the deprecation handler.
@@ -72,13 +74,15 @@ public static function register($mode = 0)
72
74
return ;
73
75
}
74
76
77
+ self ::$ isAtLeastPhpUnit83 = !class_exists ('PHPUnit_Util_ErrorHandler ' ) && method_exists (ErrorHandler::class, '__invoke ' );
78
+
75
79
$ handler = new self ();
76
80
$ oldErrorHandler = set_error_handler ([$ handler , 'handleError ' ]);
77
81
78
82
if (null !== $ oldErrorHandler ) {
79
83
restore_error_handler ();
80
84
81
- if ([ErrorHandler::class, 'handleError ' ] === $ oldErrorHandler ) {
85
+ if ($ oldErrorHandler instanceof ErrorHandler || [ErrorHandler::class, 'handleError ' ] === $ oldErrorHandler ) {
82
86
restore_error_handler ();
83
87
self ::register ($ mode );
84
88
}
@@ -98,7 +102,7 @@ public static function collectDeprecations($outputFile)
98
102
return $ previousErrorHandler ($ type , $ msg , $ file , $ line , $ context );
99
103
}
100
104
101
- return ErrorHandler:: handleError ( $ type , $ msg , $ file , $ line , $ context );
105
+ return \call_user_func ( self :: getPhpUnitErrorHandler (), $ type , $ msg , $ file , $ line , $ context );
102
106
}
103
107
104
108
$ deprecations [] = [error_reporting (), $ msg , $ file ];
@@ -115,7 +119,7 @@ public static function collectDeprecations($outputFile)
115
119
public function handleError ($ type , $ msg , $ file , $ line , $ context = [])
116
120
{
117
121
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 );
119
123
}
120
124
121
125
$ deprecation = new Deprecation ($ msg , debug_backtrace (), $ file );
@@ -310,6 +314,26 @@ private function displayDeprecations($groups, $configuration)
310
314
}
311
315
}
312
316
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
+
313
337
/**
314
338
* Returns true if STDOUT is defined and supports colorization.
315
339
*
0 commit comments