Skip to content

Commit efc519f

Browse files
Closes #5976
1 parent 3946ac3 commit efc519f

File tree

5 files changed

+124
-2
lines changed

5 files changed

+124
-2
lines changed

ChangeLog-11.5.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes of the PHPUnit 11.5 release series are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.
44

5+
## [11.5.12] - 2025-MM-DD
6+
7+
### Fixed
8+
9+
* [#5976](https://github.com/sebastianbergmann/phpunit/issues/5976): TestDox result printer does not display details about errors triggered in before-first-test and after-last-test methods
10+
511
## [11.5.11] - 2025-03-05
612

713
### Fixed
@@ -123,6 +129,7 @@ All notable changes of the PHPUnit 11.5 release series are documented in this fi
123129
* [#6055](https://github.com/sebastianbergmann/phpunit/issues/6055): `assertNotContainsOnly()` (use `assertContainsNotOnlyArray()`, `assertContainsNotOnlyBool()`, `assertContainsNotOnlyCallable()`, `assertContainsNotOnlyFloat()`, `assertContainsNotOnlyInt()`, `assertContainsNotOnlyIterable()`, `assertContainsNotOnlyNumeric()`, `assertContainsNotOnlyObject()`, `assertContainsNotOnlyResource()`, `assertContainsNotOnlyClosedResource()`, `assertContainsNotOnlyScalar()`, or `assertContainsNotOnlyString()` instead)
124130
* [#6059](https://github.com/sebastianbergmann/phpunit/issues/6059): `containsOnly()` (use `containsOnlyArray()`, `containsOnlyBool()`, `containsOnlyCallable()`, `containsOnlyFloat()`, `containsOnlyInt()`, `containsOnlyIterable()`, `containsOnlyNumeric()`, `containsOnlyObject()`, `containsOnlyResource()`, `containsOnlyClosedResource()`, `containsOnlyScalar()`, or `containsOnlyString()` instead)
125131

132+
[11.5.12]: https://github.com/sebastianbergmann/phpunit/compare/11.5.10...11.5
126133
[11.5.11]: https://github.com/sebastianbergmann/phpunit/compare/11.5.10...11.5.11
127134
[11.5.10]: https://github.com/sebastianbergmann/phpunit/compare/11.5.9...11.5.10
128135
[11.5.9]: https://github.com/sebastianbergmann/phpunit/compare/11.5.8...11.5.9

src/TextUI/Output/Facade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static function printResult(TestResult $result, ?array $testDoxResult, Du
9292
}
9393

9494
if (self::$testDoxResultPrinter !== null && $testDoxResult !== null) {
95-
self::$testDoxResultPrinter->print($testDoxResult);
95+
self::$testDoxResultPrinter->print($result, $testDoxResult);
9696
}
9797

9898
if (self::$defaultResultPrinter !== null) {

src/TextUI/Output/TestDox/ResultPrinter.php

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
use function preg_match;
1818
use function preg_split;
1919
use function rtrim;
20+
use function sprintf;
2021
use function str_starts_with;
2122
use function trim;
2223
use PHPUnit\Event\Code\Throwable;
24+
use PHPUnit\Event\Test\AfterLastTestMethodErrored;
25+
use PHPUnit\Event\Test\BeforeFirstTestMethodErrored;
2326
use PHPUnit\Framework\TestStatus\TestStatus;
2427
use PHPUnit\Logging\TestDox\TestResult as TestDoxTestResult;
2528
use PHPUnit\Logging\TestDox\TestResultCollection;
29+
use PHPUnit\TestRunner\TestResult\TestResult;
2630
use PHPUnit\TextUI\Output\Printer;
2731
use PHPUnit\Util\Color;
2832

@@ -49,7 +53,7 @@ public function __construct(Printer $printer, bool $colors, int $columns, bool $
4953
/**
5054
* @param array<string, TestResultCollection> $tests
5155
*/
52-
public function print(array $tests): void
56+
public function print(TestResult $result, array $tests): void
5357
{
5458
$this->doPrint($tests, false);
5559

@@ -58,6 +62,29 @@ public function print(array $tests): void
5862

5963
$this->doPrint($tests, true);
6064
}
65+
66+
$beforeFirstTestMethodErrored = [];
67+
$afterLastTestMethodErrored = [];
68+
69+
foreach ($result->testErroredEvents() as $error) {
70+
if ($error instanceof BeforeFirstTestMethodErrored) {
71+
$beforeFirstTestMethodErrored[$error->calledMethod()->className() . '::' . $error->calledMethod()->methodName()] = $error;
72+
}
73+
74+
if ($error instanceof AfterLastTestMethodErrored) {
75+
$afterLastTestMethodErrored[$error->calledMethod()->className() . '::' . $error->calledMethod()->methodName()] = $error;
76+
}
77+
}
78+
79+
$this->printBeforeClassOrAfterClassErrors(
80+
'These before-first-test methods errored:',
81+
$beforeFirstTestMethodErrored,
82+
);
83+
84+
$this->printBeforeClassOrAfterClassErrors(
85+
'These after-last-test methods errored:',
86+
$afterLastTestMethodErrored,
87+
);
6188
}
6289

6390
/**
@@ -407,4 +434,34 @@ private function symbolFor(TestStatus $status): string
407434

408435
return '?';
409436
}
437+
438+
/**
439+
* @param non-empty-string $header
440+
* @param array<non-empty-string, AfterLastTestMethodErrored|BeforeFirstTestMethodErrored> $errors
441+
*/
442+
private function printBeforeClassOrAfterClassErrors(string $header, array $errors): void
443+
{
444+
if (empty($errors)) {
445+
return;
446+
}
447+
448+
$index = 0;
449+
450+
$this->printer->print($header . PHP_EOL . PHP_EOL);
451+
452+
foreach ($errors as $method => $error) {
453+
$this->printer->print(
454+
sprintf(
455+
'%d) %s' . PHP_EOL,
456+
++$index,
457+
$method,
458+
),
459+
);
460+
461+
$this->printer->print(trim($error->throwable()->description()) . PHP_EOL . PHP_EOL);
462+
$this->printer->print($this->formatStackTrace($error->throwable()->stackTrace()) . PHP_EOL);
463+
}
464+
465+
$this->printer->print(PHP_EOL);
466+
}
410467
}

tests/end-to-end/regression/5976.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
https://github.com/sebastianbergmann/phpunit/issues/5976
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = '--testdox';
8+
$_SERVER['argv'][] = __DIR__ . '/5976/Issue5976Test.php';
9+
10+
require_once __DIR__ . '/../../bootstrap.php';
11+
12+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
13+
--EXPECTF--
14+
PHPUnit %s by Sebastian Bergmann and contributors.
15+
16+
Runtime: %s
17+
18+
E 1 / 1 (100%)
19+
20+
Time: %s, Memory: %s
21+
22+
These before-first-test methods errored:
23+
24+
1) PHPUnit\TestFixture\Issue5967\Issue5976Test::setUpBeforeClass
25+
Exception: message
26+
27+
%sIssue5976Test.php:%d
28+
29+
ERRORS!
30+
Tests: 1, Assertions: 0, Errors: 1.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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\Issue5967;
11+
12+
use Exception;
13+
use PHPUnit\Framework\TestCase;
14+
15+
final class Issue5976Test extends TestCase
16+
{
17+
/**
18+
* @throws Exception
19+
*/
20+
public static function setUpBeforeClass(): void
21+
{
22+
throw new Exception('message');
23+
}
24+
25+
public function testOne(): void
26+
{
27+
}
28+
}

0 commit comments

Comments
 (0)