Skip to content

Commit d019766

Browse files
staabmsebastianbergmann
authored andcommitted
Print previous exception in Testdox
1 parent 6c44178 commit d019766

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

src/TextUI/Output/TestDox/ResultPrinter.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,37 @@ private function printThrowable(TestStatus $status, Throwable $throwable): void
250250
} else {
251251
$tracePrefix = $this->prefixFor('trace', $status);
252252
}
253+
}
253254

255+
if ($stackTrace !== '') {
254256
$this->printer->print(
255257
$this->prefixLines($tracePrefix, PHP_EOL . $stackTrace),
256258
);
257259
}
260+
261+
if ($throwable->hasPrevious()) {
262+
$this->printer->print(PHP_EOL);
263+
264+
$this->printer->print(
265+
$this->prefixLines(
266+
$tracePrefix,
267+
' ',
268+
),
269+
);
270+
271+
$this->printer->print(PHP_EOL);
272+
273+
$this->printer->print(
274+
$this->prefixLines(
275+
$tracePrefix,
276+
'Caused by:',
277+
),
278+
);
279+
280+
$this->printer->print(PHP_EOL);
281+
282+
$this->printThrowable($status, $throwable->previous());
283+
}
258284
}
259285

260286
/**
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture;
11+
12+
use Exception;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class ThrowsWithPreviousExceptionTest extends TestCase
16+
{
17+
public function testFoo(): void
18+
{
19+
throw new Exception(
20+
'Outer',
21+
previous: new Exception('Inner'),
22+
);
23+
}
24+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
Testdox: print error message
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--display-errors';
6+
$_SERVER['argv'][] = '--do-not-cache-result';
7+
$_SERVER['argv'][] = '--no-configuration';
8+
$_SERVER['argv'][] = '--testdox';
9+
$_SERVER['argv'][] = __DIR__ . '/../_files/ThrowsWithPreviousExceptionTest.php';
10+
11+
require_once __DIR__ . '/../../../bootstrap.php';
12+
13+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
14+
--EXPECTF--
15+
PHPUnit %s by Sebastian Bergmann and contributors.
16+
17+
Runtime: PHP %s
18+
19+
E 1 / 1 (100%)
20+
21+
Time: %s, Memory: %s
22+
23+
Throws With Previous Exception (PHPUnit\TestFixture\ThrowsWithPreviousException)
24+
✘ Foo
25+
26+
│ Exception: Outer
27+
28+
│ %sThrowsWithPreviousExceptionTest.php:%d
29+
30+
│ Caused by:
31+
│ Exception: Inner
32+
33+
│ %sThrowsWithPreviousExceptionTest.php:%d
34+
35+
36+
ERRORS!
37+
Tests: 1, Assertions: 0, Errors: 1.

0 commit comments

Comments
 (0)