Skip to content

Commit 7730b3e

Browse files
Merge branch '12.3'
2 parents a196bd5 + d4cd32c commit 7730b3e

File tree

4 files changed

+136
-15
lines changed

4 files changed

+136
-15
lines changed

src/TextUI/Output/TestDox/ResultPrinter.php

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use const PHP_EOL;
1313
use function array_map;
14-
use function assert;
1514
use function explode;
1615
use function implode;
1716
use function preg_match;
@@ -177,7 +176,7 @@ private function printTestResultBody(TestDoxTestResult $test): void
177176
}
178177

179178
$this->printTestResultBodyStart($test);
180-
$this->printThrowable($test);
179+
$this->printThrowable($test->status(), $test->throwable());
181180
$this->printTestResultBodyEnd($test);
182181
}
183182

@@ -207,27 +206,23 @@ private function printTestResultBodyEnd(TestDoxTestResult $test): void
207206
$this->printer->print(PHP_EOL);
208207
}
209208

210-
private function printThrowable(TestDoxTestResult $test): void
209+
private function printThrowable(TestStatus $status, Throwable $throwable): void
211210
{
212-
$throwable = $test->throwable();
213-
214-
assert($throwable instanceof Throwable);
215-
216211
$message = trim($throwable->description());
217212
$stackTrace = $this->formatStackTrace($throwable->stackTrace());
218213
$diff = '';
219214

220215
if ($message !== '' && $this->colors) {
221216
['message' => $message, 'diff' => $diff] = $this->colorizeMessageAndDiff(
222217
$message,
223-
$this->messageColorFor($test->status()),
218+
$this->messageColorFor($status),
224219
);
225220
}
226221

227222
if ($message !== '') {
228223
$this->printer->print(
229224
$this->prefixLines(
230-
$this->prefixFor('message', $test->status()),
225+
$this->prefixFor('message', $status),
231226
$message,
232227
),
233228
);
@@ -238,24 +233,48 @@ private function printThrowable(TestDoxTestResult $test): void
238233
if ($diff !== '') {
239234
$this->printer->print(
240235
$this->prefixLines(
241-
$this->prefixFor('diff', $test->status()),
236+
$this->prefixFor('diff', $status),
242237
$diff,
243238
),
244239
);
245240

246241
$this->printer->print(PHP_EOL);
247242
}
248243

249-
if ($stackTrace !== '') {
250-
if ($message !== '' || $diff !== '') {
251-
$prefix = $this->prefixFor('default', $test->status());
244+
if (!empty($stackTrace)) {
245+
if (!empty($message) || !empty($diff)) {
246+
$tracePrefix = $this->prefixFor('default', $status);
252247
} else {
253-
$prefix = $this->prefixFor('trace', $test->status());
248+
$tracePrefix = $this->prefixFor('trace', $status);
254249
}
255250

256251
$this->printer->print(
257-
$this->prefixLines($prefix, PHP_EOL . $stackTrace),
252+
$this->prefixLines($tracePrefix, PHP_EOL . $stackTrace),
253+
);
254+
}
255+
256+
if ($throwable->hasPrevious()) {
257+
$this->printer->print(PHP_EOL);
258+
259+
$this->printer->print(
260+
$this->prefixLines(
261+
$this->prefixFor('default', $status),
262+
' ',
263+
),
264+
);
265+
266+
$this->printer->print(PHP_EOL);
267+
268+
$this->printer->print(
269+
$this->prefixLines(
270+
$this->prefixFor('default', $status),
271+
'Caused by:',
272+
),
258273
);
274+
275+
$this->printer->print(PHP_EOL);
276+
277+
$this->printThrowable($status, $throwable->previous());
259278
}
260279
}
261280

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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
https://github.com/sebastianbergmann/phpunit/issues/5863
3+
--SKIPIF--
4+
<?php declare(strict_types=1);
5+
if (stripos(\PHP_OS, 'WIN') === 0) {
6+
print 'skip: Colorized diff is different on Windows.';
7+
}
8+
--FILE--
9+
<?php declare(strict_types=1);
10+
$_SERVER['argv'][] = '--display-errors';
11+
$_SERVER['argv'][] = '--do-not-cache-result';
12+
$_SERVER['argv'][] = '--no-configuration';
13+
$_SERVER['argv'][] = '--no-progress';
14+
$_SERVER['argv'][] = '--testdox';
15+
$_SERVER['argv'][] = '--colors=always';
16+
$_SERVER['argv'][] = __DIR__ . '/../_files/ThrowsWithPreviousExceptionTest.php';
17+
18+
require_once __DIR__ . '/../../../bootstrap.php';
19+
20+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
21+
--EXPECTF--
22+
PHPUnit %s by Sebastian Bergmann and contributors.
23+
24+
Runtime: PHP %s
25+
26+
Time: %s, Memory: %s
27+
28+
Throws With Previous Exception (PHPUnit\TestFixture\ThrowsWithPreviousException)
29+
 ✘ Foo
30+
┐
31+
├ Exception: Outer
32+
│
33+
│ %sThrowsWithPreviousExceptionTest.php:%d%A
34+
│ Caused by:
35+
├ Exception: Inner
36+
│
37+
│ %sThrowsWithPreviousExceptionTest.php:%d%A
38+
┴
39+
40+
ERRORS!
41+
Tests: 1, Assertions: 0, Errors: 1.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
https://github.com/sebastianbergmann/phpunit/issues/5863
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)