Skip to content

Commit 43c6126

Browse files
Also support filtering using --filter, --exclude-filter, --group, and --exclude-group when listing groups using --list-groups
1 parent 6c16485 commit 43c6126

File tree

7 files changed

+114
-44
lines changed

7 files changed

+114
-44
lines changed

ChangeLog-11.1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ All notable changes of the PHPUnit 11.1 release series are documented in this fi
88

99
* [#5175](https://github.com/sebastianbergmann/phpunit/issues/5175): `#[CoversMethod]` and `#[UsesMethod]` attributes for more fine-grained code coverage targeting
1010
* [#5696](https://github.com/sebastianbergmann/phpunit/pull/5696): `#[DisableReturnValueGenerationForTestDoubles]` attribute for disabling return value generation for test doubles created using `createMock()`, `createMockForIntersectionOfInterfaces()`, `createPartialMock()`, `createStub()`, and `createStubForIntersectionOfInterfaces()`
11-
* [#5720](https://github.com/sebastianbergmann/phpunit/issues/5720): Support filtering using `--filter`, `--exclude-filter`, `--group`, and `--exclude-group` when listing tests using `--list-tests` and `--list-tests-xml`
11+
* [#5720](https://github.com/sebastianbergmann/phpunit/issues/5720): Support filtering using `--filter`, `--exclude-filter`, `--group`, and `--exclude-group` when listing tests using `--list-tests` and `--list-tests-xml` as well as listing groups with `--list-groups`
1212
* `--only-summary-for-coverage-text` CLI option to reduce the code coverage report in text format to a summary
1313
* `--show-uncovered-for-coverage-text` CLI option to expand the code coverage report in text format to include a list of uncovered files
1414

src/TextUI/Application.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,14 @@ private function executeCommandsThatDoNotRequireTheTestSuite(Configuration $conf
419419
private function executeCommandsThatRequireTheTestSuite(Configuration $configuration, CliConfiguration $cliConfiguration, TestSuite $testSuite): void
420420
{
421421
if ($cliConfiguration->listGroups()) {
422-
$this->execute(new ListGroupsCommand($testSuite));
422+
$this->execute(
423+
new ListGroupsCommand(
424+
$this->filteredTests(
425+
$configuration,
426+
$testSuite,
427+
),
428+
),
429+
);
423430
}
424431

425432
if ($cliConfiguration->listTests()) {

src/TextUI/Command/Commands/ListGroupsCommand.php

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,52 @@
99
*/
1010
namespace PHPUnit\TextUI\Command;
1111

12+
use function array_merge;
13+
use function array_unique;
1214
use function sort;
1315
use function sprintf;
1416
use function str_starts_with;
15-
use PHPUnit\Framework\TestSuite;
16-
use PHPUnit\TextUI\Configuration\Registry;
17+
use PHPUnit\Framework\TestCase;
18+
use PHPUnit\Runner\PhptTestCase;
1719

1820
/**
1921
* @internal This class is not covered by the backward compatibility promise for PHPUnit
2022
*/
2123
final readonly class ListGroupsCommand implements Command
2224
{
23-
private TestSuite $suite;
24-
25-
public function __construct(TestSuite $suite)
25+
/**
26+
* @psalm-var list<TestCase|PhptTestCase>
27+
*/
28+
private array $tests;
29+
30+
/**
31+
* @psalm-param list<TestCase|PhptTestCase> $tests
32+
*/
33+
public function __construct(array $tests)
2634
{
27-
$this->suite = $suite;
35+
$this->tests = $tests;
2836
}
2937

3038
public function execute(): Result
3139
{
32-
$buffer = $this->warnAboutConflictingOptions();
33-
$buffer .= 'Available test group(s):' . PHP_EOL;
40+
$groups = [];
41+
42+
foreach ($this->tests as $test) {
43+
if ($test instanceof PhptTestCase) {
44+
$groups[] = 'default';
45+
46+
continue;
47+
}
48+
49+
$groups = array_merge($groups, $test->groups());
50+
}
51+
52+
$groups = array_unique($groups);
3453

35-
$groups = $this->suite->groups();
3654
sort($groups);
3755

56+
$buffer = 'Available test group(s):' . PHP_EOL;
57+
3858
foreach ($groups as $group) {
3959
if (str_starts_with($group, '__phpunit_')) {
4060
continue;
@@ -48,37 +68,4 @@ public function execute(): Result
4868

4969
return Result::from($buffer);
5070
}
51-
52-
private function warnAboutConflictingOptions(): string
53-
{
54-
$buffer = '';
55-
56-
$configuration = Registry::get();
57-
58-
if ($configuration->hasFilter()) {
59-
$buffer .= 'The --filter and --list-groups options cannot be combined, --filter is ignored' . PHP_EOL;
60-
}
61-
62-
if ($configuration->hasExcludeFilter()) {
63-
$buffer .= 'The --exclude-filter and --list-groups options cannot be combined, --exclude-filter is ignored' . PHP_EOL;
64-
}
65-
66-
if ($configuration->hasGroups()) {
67-
$buffer .= 'The --group and --list-groups options cannot be combined, --group is ignored' . PHP_EOL;
68-
}
69-
70-
if ($configuration->hasExcludeGroups()) {
71-
$buffer .= 'The --exclude-group and --list-groups options cannot be combined, --exclude-group is ignored' . PHP_EOL;
72-
}
73-
74-
if ($configuration->includeTestSuite() !== '') {
75-
$buffer .= 'The --testsuite and --list-groups options cannot be combined, --testsuite is ignored' . PHP_EOL;
76-
}
77-
78-
if (!empty($buffer)) {
79-
$buffer .= PHP_EOL;
80-
}
81-
82-
return $buffer;
83-
}
8471
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
phpunit --exclude-filter testOne --list-groups ../../_files/listing-tests-and-groups
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = '--exclude-filter';
8+
$_SERVER['argv'][] = 'testOne';
9+
$_SERVER['argv'][] = '--list-groups';
10+
$_SERVER['argv'][] = __DIR__ . '/../../_files/listing-tests-and-groups';
11+
12+
require_once __DIR__ . '/../../../bootstrap.php';
13+
14+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
15+
--EXPECTF--
16+
PHPUnit %s by Sebastian Bergmann and contributors.
17+
18+
Available test group(s):
19+
- two
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
phpunit --exclude-group one --list-groups ../../_files/listing-tests-and-groups
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = '--exclude-group';
8+
$_SERVER['argv'][] = 'one';
9+
$_SERVER['argv'][] = '--list-groups';
10+
$_SERVER['argv'][] = __DIR__ . '/../../_files/listing-tests-and-groups';
11+
12+
require_once __DIR__ . '/../../../bootstrap.php';
13+
14+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
15+
--EXPECTF--
16+
PHPUnit %s by Sebastian Bergmann and contributors.
17+
18+
Available test group(s):
19+
- two
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
phpunit --exclude-filter testOne --list-groups ../../_files/listing-tests-and-groups
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = '--exclude-filter';
8+
$_SERVER['argv'][] = 'testOne';
9+
$_SERVER['argv'][] = '--list-groups';
10+
$_SERVER['argv'][] = __DIR__ . '/../../_files/listing-tests-and-groups';
11+
12+
require_once __DIR__ . '/../../../bootstrap.php';
13+
14+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
15+
--EXPECTF--
16+
PHPUnit %s by Sebastian Bergmann and contributors.
17+
18+
Available test group(s):
19+
- two
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
phpunit --group one --list-groups ../../_files/listing-tests-and-groups
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = '--group';
8+
$_SERVER['argv'][] = 'one';
9+
$_SERVER['argv'][] = '--list-groups';
10+
$_SERVER['argv'][] = __DIR__ . '/../../_files/listing-tests-and-groups';
11+
12+
require_once __DIR__ . '/../../../bootstrap.php';
13+
14+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
15+
--EXPECTF--
16+
PHPUnit %s by Sebastian Bergmann and contributors.
17+
18+
Available test group(s):
19+
- one

0 commit comments

Comments
 (0)