11
11
12
12
namespace Symfony \Bridge \PhpUnit ;
13
13
14
+ use PHPUnit \Framework \TestResult ;
15
+ use PHPUnit \Util \ErrorHandler ;
14
16
use Symfony \Bridge \PhpUnit \DeprecationErrorHandler \Configuration ;
15
17
use Symfony \Bridge \PhpUnit \DeprecationErrorHandler \Deprecation ;
16
18
@@ -49,6 +51,7 @@ class DeprecationErrorHandler
49
51
50
52
private static $ isRegistered = false ;
51
53
private static $ utilPrefix ;
54
+ private static $ isAtLeastPhpUnit83 ;
52
55
53
56
/**
54
57
* Registers and configures the deprecation handler.
@@ -73,14 +76,15 @@ public static function register($mode = 0)
73
76
}
74
77
75
78
self ::$ utilPrefix = class_exists ('PHPUnit_Util_ErrorHandler ' ) ? 'PHPUnit_Util_ ' : 'PHPUnit\Util \\' ;
79
+ self ::$ isAtLeastPhpUnit83 = method_exists ('PHPUnit\Util\ErrorHandler ' , '__invoke ' );
76
80
77
81
$ handler = new self ();
78
82
$ oldErrorHandler = set_error_handler ([$ handler , 'handleError ' ]);
79
83
80
84
if (null !== $ oldErrorHandler ) {
81
85
restore_error_handler ();
82
86
83
- if ([self ::$ utilPrefix .'ErrorHandler ' , 'handleError ' ] === $ oldErrorHandler ) {
87
+ if ($ oldErrorHandler instanceof ErrorHandler || [self ::$ utilPrefix .'ErrorHandler ' , 'handleError ' ] === $ oldErrorHandler ) {
84
88
restore_error_handler ();
85
89
self ::register ($ mode );
86
90
}
@@ -100,12 +104,7 @@ public static function collectDeprecations($outputFile)
100
104
return $ previousErrorHandler ($ type , $ msg , $ file , $ line , $ context );
101
105
}
102
106
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 \call_user_func (self ::getPhpUnitErrorHandler (), $ type , $ msg , $ file , $ line , $ context );
109
108
}
110
109
111
110
$ deprecations [] = [error_reporting (), $ msg , $ file ];
@@ -122,9 +121,7 @@ public static function collectDeprecations($outputFile)
122
121
public function handleError ($ type , $ msg , $ file , $ line , $ context = [])
123
122
{
124
123
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 );
124
+ return \call_user_func (self ::getPhpUnitErrorHandler (), $ type , $ msg , $ file , $ line , $ context );
128
125
}
129
126
130
127
$ deprecation = new Deprecation ($ msg , debug_backtrace (), $ file );
@@ -319,6 +316,26 @@ private function displayDeprecations($groups, $configuration)
319
316
}
320
317
}
321
318
319
+ private static function getPhpUnitErrorHandler ()
320
+ {
321
+ if (!self ::$ isAtLeastPhpUnit83 ) {
322
+ return (class_exists ('PHPUnit_Util_ErrorHandler ' , false ) ? 'PHPUnit_Util_ ' : 'PHPUnit\Util \\' ).'ErrorHandler::handleError ' ;
323
+ }
324
+
325
+ foreach (debug_backtrace (DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS ) as $ frame ) {
326
+ if (isset ($ frame ['object ' ]) && $ frame ['object ' ] instanceof TestResult) {
327
+ return new ErrorHandler (
328
+ $ frame ['object ' ]->getConvertDeprecationsToExceptions (),
329
+ $ frame ['object ' ]->getConvertErrorsToExceptions (),
330
+ $ frame ['object ' ]->getConvertNoticesToExceptions (),
331
+ $ frame ['object ' ]->getConvertWarningsToExceptions ()
332
+ );
333
+ }
334
+ }
335
+
336
+ return function () { return false ; };
337
+ }
338
+
322
339
/**
323
340
* Returns true if STDOUT is defined and supports colorization.
324
341
*
0 commit comments