Skip to content

Commit ef9b1d4

Browse files
committed
Refactored exception trace printing to be more robust.
Added stack trace exception test.
1 parent eecb203 commit ef9b1d4

File tree

4 files changed

+57
-8
lines changed

4 files changed

+57
-8
lines changed

src/Printer.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace ScriptFUSION\PHPUnitImmediateExceptionPrinter;
33

44
use PHPUnit\Framework\AssertionFailedError;
5+
use PHPUnit\Framework\ExceptionWrapper;
56
use PHPUnit\Framework\Test;
67

78
trait 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

test/CapabilitiesTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public function testException()
2020
throw new \LogicException('foo');
2121
}
2222

23+
public function testExceptionStackTrace()
24+
{
25+
throw new StackTraceException(new NestedStackTraceException);
26+
}
27+
2328
public function testNestedException()
2429
{
2530
new ExceptionThrower;

test/NestedStackTraceException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace ScriptFUSIONTest\PHPUnitImmediateExceptionPrinter;
3+
4+
class NestedStackTraceException extends StackTraceException
5+
{
6+
// Intentionally empty.
7+
}

test/StackTraceException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace ScriptFUSIONTest\PHPUnitImmediateExceptionPrinter;
3+
4+
class StackTraceException extends \RuntimeException
5+
{
6+
public function __construct(StackTraceException $previous = null)
7+
{
8+
parent::__construct("$this", 0, $previous);
9+
}
10+
}

0 commit comments

Comments
 (0)