Skip to content

Commit 8f432cf

Browse files
Closes #5908
1 parent 98cbd0f commit 8f432cf

File tree

5 files changed

+109
-6
lines changed

5 files changed

+109
-6
lines changed

ChangeLog-10.5.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes of the PHPUnit 10.5 release series are documented in this fi
77
### Fixed
88

99
* [#5887](https://github.com/sebastianbergmann/phpunit/pull/5887): Issue baseline generator does not correctly handle ignoring suppressed issues
10+
* [#5908](https://github.com/sebastianbergmann/phpunit/issues/5908): `--list-tests` and `--list-tests-xml` CLI options do not report error when data provider method throws exception
1011

1112
## [10.5.28] - 2024-07-18
1213

src/TextUI/Application.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,40 @@ public function run(array $argv): int
290290
// @codeCoverageIgnoreEnd
291291
}
292292

293-
private function execute(Command\Command $command): never
293+
private function execute(Command\Command $command, bool $requiresResultCollectedFromEvents = false): never
294294
{
295+
if ($requiresResultCollectedFromEvents) {
296+
try {
297+
TestResultFacade::init();
298+
EventFacade::instance()->seal();
299+
300+
$resultCollectedFromEvents = TestResultFacade::result();
301+
} catch (EventFacadeIsSealedException|UnknownSubscriberTypeException) {
302+
}
303+
}
304+
295305
print Version::getVersionString() . PHP_EOL . PHP_EOL;
296306

297307
$result = $command->execute();
298308

299309
print $result->output();
300310

301-
exit($result->shellExitCode());
311+
$shellExitCode = $result->shellExitCode();
312+
313+
if (isset($resultCollectedFromEvents) &&
314+
$resultCollectedFromEvents->hasTestTriggeredPhpunitErrorEvents()) {
315+
$shellExitCode = Result::EXCEPTION;
316+
317+
print PHP_EOL . PHP_EOL . 'There were errors:' . PHP_EOL;
318+
319+
foreach ($resultCollectedFromEvents->testTriggeredPhpunitErrorEvents() as $events) {
320+
foreach ($events as $event) {
321+
print PHP_EOL . trim($event->message()) . PHP_EOL;
322+
}
323+
}
324+
}
325+
326+
exit($shellExitCode);
302327
}
303328

304329
private function loadBootstrapScript(string $filename): void
@@ -438,11 +463,11 @@ private function executeCommandsThatOnlyRequireCliConfiguration(CliConfiguration
438463
private function executeCommandsThatRequireCliConfigurationAndTestSuite(CliConfiguration $cliConfiguration, TestSuite $testSuite): void
439464
{
440465
if ($cliConfiguration->listGroups()) {
441-
$this->execute(new ListGroupsCommand($testSuite));
466+
$this->execute(new ListGroupsCommand($testSuite), true);
442467
}
443468

444469
if ($cliConfiguration->listTests()) {
445-
$this->execute(new ListTestsAsTextCommand($testSuite));
470+
$this->execute(new ListTestsAsTextCommand($testSuite), true);
446471
}
447472

448473
if ($cliConfiguration->hasListTestsXml()) {
@@ -451,18 +476,19 @@ private function executeCommandsThatRequireCliConfigurationAndTestSuite(CliConfi
451476
$cliConfiguration->listTestsXml(),
452477
$testSuite,
453478
),
479+
true,
454480
);
455481
}
456482
}
457483

458484
private function executeCommandsThatRequireCompleteConfiguration(Configuration $configuration, CliConfiguration $cliConfiguration): void
459485
{
460486
if ($cliConfiguration->listSuites()) {
461-
$this->execute(new ListTestSuitesCommand($configuration->testSuite()));
487+
$this->execute(new ListTestSuitesCommand($configuration->testSuite()), true);
462488
}
463489

464490
if ($cliConfiguration->warmCoverageCache()) {
465-
$this->execute(new WarmCodeCoverageCacheCommand($configuration, CodeCoverageFilterRegistry::instance()));
491+
$this->execute(new WarmCodeCoverageCacheCommand($configuration, CodeCoverageFilterRegistry::instance()), true);
466492
}
467493
}
468494

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
https://github.com/sebastianbergmann/phpunit/issues/5908
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$file = tempnam(sys_get_temp_dir(), __FILE__);
6+
7+
$_SERVER['argv'][] = '--do-not-cache-result';
8+
$_SERVER['argv'][] = '--no-configuration';
9+
$_SERVER['argv'][] = '--list-tests-xml';
10+
$_SERVER['argv'][] = $file;
11+
$_SERVER['argv'][] = __DIR__ . '/5908/Issue5908Test.php';
12+
13+
require_once __DIR__ . '/../../bootstrap.php';
14+
15+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
16+
17+
unlink($file);
18+
--EXPECTF--
19+
PHPUnit %s by Sebastian Bergmann and contributors.
20+
21+
Wrote list of tests that would have been run to %s
22+
23+
24+
There were errors:
25+
26+
The data provider specified for PHPUnit\TestFixture\Issue5908\Issue5908Test::testOne is invalid
27+
message
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
https://github.com/sebastianbergmann/phpunit/issues/5908
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = '--list-tests';
8+
$_SERVER['argv'][] = __DIR__ . '/5908/Issue5908Test.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+
Available test(s):
17+
18+
19+
There were errors:
20+
21+
The data provider specified for PHPUnit\TestFixture\Issue5908\Issue5908Test::testOne is invalid
22+
message
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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\Issue5908;
11+
12+
use Exception;
13+
use PHPUnit\Framework\Attributes\DataProvider;
14+
use PHPUnit\Framework\TestCase;
15+
16+
final class Issue5908Test extends TestCase
17+
{
18+
public static function provider(): array
19+
{
20+
throw new Exception('message');
21+
}
22+
23+
#[DataProvider('provider')]
24+
public function testOne(int $value): void
25+
{
26+
}
27+
}

0 commit comments

Comments
 (0)