22namespace ScriptFUSION \PHPUnitImmediateExceptionPrinter ;
33
44use PHPUnit \Framework \AssertionFailedError ;
5+ use PHPUnit \Framework \ExceptionWrapper ;
56use PHPUnit \Framework \Test ;
67
78trait Printer
@@ -93,7 +94,7 @@ protected function onEndTest($test, $time)
9394 $ this ->writePerformance ($ time );
9495
9596 if ($ this ->exception ) {
96- $ this ->writeExceptionTrace ($ this ->exception );
97+ $ this ->writeException ($ this ->exception );
9798 }
9899 }
99100
@@ -129,18 +130,42 @@ protected function writePerformance($time)
129130 *
130131 * @param \Exception $exception Exception.
131132 */
132- protected function writeExceptionTrace (\Exception $ exception )
133+ protected function writeException (\Exception $ exception )
134+ {
135+ if ($ exception instanceof \PHPUnit_Framework_AssertionFailedError
136+ || $ exception instanceof AssertionFailedError) {
137+ $ this ->writeAssertionFailure ($ exception );
138+ } elseif ($ exception instanceof \PHPUnit_Framework_ExceptionWrapper
139+ || $ exception instanceof ExceptionWrapper) {
140+ $ this ->writeExceptionTrace ($ exception );
141+ }
142+ }
143+
144+ protected function writeAssertionFailure ($ exception )
133145 {
134146 $ this ->writeNewLine ();
135147
136- // Parse nested exception trace line by line.
137148 foreach (explode ("\n" , $ exception ) as $ line ) {
149+ $ this ->writeWithColor ('fg-red ' , $ line );
150+ }
151+ }
152+
153+ /**
154+ * @param ExceptionWrapper $exception
155+ */
156+ protected function writeExceptionTrace ($ exception )
157+ {
158+ $ this ->writeNewLine ();
159+
160+ do {
161+ $ exceptionStack [] = $ exception ;
162+ } while ($ exception = $ exception ->getPreviousWrapped ());
163+
164+ // Parse nested exception trace line by line.
165+ foreach (explode ("\n" , $ exception = array_shift ($ exceptionStack )) as $ line ) {
138166 // Print exception name and message.
139- if (!$ exception instanceof \PHPUnit_Framework_AssertionFailedError
140- && !$ exception instanceof AssertionFailedError
141- && false !== $ pos = strpos ($ line , ': ' )
142- ) {
143- $ whitespace = str_repeat (' ' , $ pos + 2 );
167+ if ($ exception && false !== $ pos = strpos ($ line , $ exception ->getClassName () . ': ' )) {
168+ $ whitespace = str_repeat (' ' , ($ pos += strlen ($ exception ->getClassName ())) + 2 );
144169 $ this ->writeWithColor ('bg-red,fg-white ' , $ whitespace );
145170
146171 // Exception name.
@@ -150,6 +175,8 @@ protected function writeExceptionTrace(\Exception $exception)
150175
151176 $ this ->writeWithColor ('bg-red,fg-white ' , $ whitespace );
152177
178+ $ exception = array_shift ($ exceptionStack );
179+
153180 continue ;
154181 }
155182
0 commit comments