Skip to content

Commit 475907b

Browse files
sebastianbergmanntheseerlocalheinzsebastianheuerSchrank
committed
Closes #5720
Co-authored-by: Sebastian Bergmann <[email protected]> Co-authored-by: Arne Blankerts <[email protected]> Co-authored-by: Andreas Möller <[email protected]> Co-authored-by: Sebastian Heuer <[email protected]> Co-authored-by: Fabian Blechschmidt <[email protected]> Co-authored-by: Frank Sons <[email protected]>
1 parent 8a86514 commit 475907b

31 files changed

+332
-405
lines changed

.psalm/baseline.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,8 @@
714714
<code><![CDATA[logEventsVerboseText]]></code>
715715
<code><![CDATA[logfileJunit]]></code>
716716
<code><![CDATA[logfileTeamcity]]></code>
717+
<code><![CDATA[process]]></code>
718+
<code><![CDATA[process]]></code>
717719
</MissingThrowsDocblock>
718720
<UnresolvableInclude>
719721
<code><![CDATA[include_once $filename]]></code>

ChangeLog-11.1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +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`
1112
* `--only-summary-for-coverage-text` CLI option to reduce the code coverage report in text format to a summary
1213
* `--show-uncovered-for-coverage-text` CLI option to expand the code coverage report in text format to include a list of uncovered files
1314

src/Framework/TestSuite.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,28 @@ public function groupDetails(): array
305305
return $this->groups;
306306
}
307307

308+
/**
309+
* @psalm-return list<TestCase|PhptTestCase>
310+
*/
311+
public function collect(): array
312+
{
313+
$tests = [];
314+
315+
foreach ($this as $test) {
316+
if ($test instanceof self) {
317+
$tests = array_merge($tests, $test->collect());
318+
319+
continue;
320+
}
321+
322+
assert($test instanceof TestCase || $test instanceof PhptTestCase);
323+
324+
$tests[] = $test;
325+
}
326+
327+
return $tests;
328+
}
329+
308330
/**
309331
* @throws CodeCoverageException
310332
* @throws Event\RuntimeException

src/TextUI/Application.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PHPUnit\Event\EventFacadeIsSealedException;
2121
use PHPUnit\Event\Facade as EventFacade;
2222
use PHPUnit\Event\UnknownSubscriberTypeException;
23+
use PHPUnit\Framework\TestCase;
2324
use PHPUnit\Framework\TestSuite;
2425
use PHPUnit\Logging\EventLogger;
2526
use PHPUnit\Logging\JUnit\JunitXmlLogger;
@@ -38,6 +39,7 @@
3839
use PHPUnit\Runner\Extension\Facade as ExtensionFacade;
3940
use PHPUnit\Runner\Extension\PharLoader;
4041
use PHPUnit\Runner\GarbageCollection\GarbageCollectionHandler;
42+
use PHPUnit\Runner\PhptTestCase;
4143
use PHPUnit\Runner\ResultCache\DefaultResultCache;
4244
use PHPUnit\Runner\ResultCache\NullResultCache;
4345
use PHPUnit\Runner\ResultCache\ResultCache;
@@ -421,14 +423,24 @@ private function executeCommandsThatRequireTheTestSuite(Configuration $configura
421423
}
422424

423425
if ($cliConfiguration->listTests()) {
424-
$this->execute(new ListTestsAsTextCommand($testSuite));
426+
$this->execute(
427+
new ListTestsAsTextCommand(
428+
$this->filteredTests(
429+
$configuration,
430+
$testSuite,
431+
),
432+
),
433+
);
425434
}
426435

427436
if ($cliConfiguration->hasListTestsXml()) {
428437
$this->execute(
429438
new ListTestsAsXmlCommand(
439+
$this->filteredTests(
440+
$configuration,
441+
$testSuite,
442+
),
430443
$cliConfiguration->listTestsXml(),
431-
$testSuite,
432444
),
433445
);
434446
}
@@ -663,4 +675,14 @@ private function exitWithErrorMessage(string $message): never
663675

664676
exit(Result::EXCEPTION);
665677
}
678+
679+
/**
680+
* @psalm-return list<TestCase|PhptTestCase>
681+
*/
682+
private function filteredTests(Configuration $configuration, TestSuite $suite): array
683+
{
684+
(new TestSuiteFilterProcessor)->process($configuration, $suite);
685+
686+
return $suite->collect();
687+
}
666688
}

src/TextUI/Command/Commands/ListTestsAsTextCommand.php

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,25 @@
1212
use function sprintf;
1313
use function str_replace;
1414
use PHPUnit\Framework\TestCase;
15-
use PHPUnit\Framework\TestSuite;
1615
use PHPUnit\Runner\PhptTestCase;
17-
use PHPUnit\TextUI\Configuration\Registry;
18-
use RecursiveIteratorIterator;
1916

2017
/**
2118
* @internal This class is not covered by the backward compatibility promise for PHPUnit
2219
*/
2320
final readonly class ListTestsAsTextCommand implements Command
2421
{
25-
private TestSuite $suite;
22+
private array $tests;
2623

27-
public function __construct(TestSuite $suite)
24+
public function __construct(array $tests)
2825
{
29-
$this->suite = $suite;
26+
$this->tests = $tests;
3027
}
3128

3229
public function execute(): Result
3330
{
34-
$buffer = $this->warnAboutConflictingOptions();
31+
$buffer = 'Available test(s):' . PHP_EOL;
3532

36-
$buffer .= 'Available test(s):' . PHP_EOL;
37-
38-
foreach (new RecursiveIteratorIterator($this->suite) as $test) {
33+
foreach ($this->tests as $test) {
3934
if ($test instanceof TestCase) {
4035
$name = sprintf(
4136
'%s::%s',
@@ -56,29 +51,4 @@ public function execute(): Result
5651

5752
return Result::from($buffer);
5853
}
59-
60-
private function warnAboutConflictingOptions(): string
61-
{
62-
$buffer = '';
63-
64-
$configuration = Registry::get();
65-
66-
if ($configuration->hasFilter()) {
67-
$buffer .= 'The --filter and --list-tests options cannot be combined, --filter is ignored' . PHP_EOL;
68-
}
69-
70-
if ($configuration->hasGroups()) {
71-
$buffer .= 'The --group and --list-tests options cannot be combined, --group is ignored' . PHP_EOL;
72-
}
73-
74-
if ($configuration->hasExcludeGroups()) {
75-
$buffer .= 'The --exclude-group and --list-tests options cannot be combined, --exclude-group is ignored' . PHP_EOL;
76-
}
77-
78-
if (!empty($buffer)) {
79-
$buffer .= PHP_EOL;
80-
}
81-
82-
return $buffer;
83-
}
8454
}

src/TextUI/Command/Commands/ListTestsAsXmlCommand.php

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,25 @@
1313
use function ksort;
1414
use function sprintf;
1515
use PHPUnit\Framework\TestCase;
16-
use PHPUnit\Framework\TestSuite;
1716
use PHPUnit\Runner\PhptTestCase;
18-
use PHPUnit\TextUI\Configuration\Registry;
19-
use RecursiveIteratorIterator;
2017
use XMLWriter;
2118

2219
/**
2320
* @internal This class is not covered by the backward compatibility promise for PHPUnit
2421
*/
2522
final readonly class ListTestsAsXmlCommand implements Command
2623
{
24+
private array $tests;
2725
private string $filename;
28-
private TestSuite $suite;
2926

30-
public function __construct(string $filename, TestSuite $suite)
27+
public function __construct(array $tests, string $filename)
3128
{
29+
$this->tests = $tests;
3230
$this->filename = $filename;
33-
$this->suite = $suite;
3431
}
3532

3633
public function execute(): Result
3734
{
38-
$buffer = $this->warnAboutConflictingOptions();
3935
$writer = new XMLWriter;
4036

4137
$writer->openMemory();
@@ -50,7 +46,7 @@ public function execute(): Result
5046
$currentTestClass = null;
5147
$groups = [];
5248

53-
foreach (new RecursiveIteratorIterator($this->suite) as $test) {
49+
foreach ($this->tests as $test) {
5450
if ($test instanceof TestCase) {
5551
foreach ($test->groups() as $group) {
5652
if (!isset($groups[$group])) {
@@ -121,36 +117,11 @@ public function execute(): Result
121117

122118
file_put_contents($this->filename, $writer->outputMemory());
123119

124-
$buffer .= sprintf(
125-
'Wrote list of tests that would have been run to %s' . PHP_EOL,
126-
$this->filename,
120+
return Result::from(
121+
sprintf(
122+
'Wrote list of tests that would have been run to %s' . PHP_EOL,
123+
$this->filename,
124+
),
127125
);
128-
129-
return Result::from($buffer);
130-
}
131-
132-
private function warnAboutConflictingOptions(): string
133-
{
134-
$buffer = '';
135-
136-
$configuration = Registry::get();
137-
138-
if ($configuration->hasFilter()) {
139-
$buffer .= 'The --filter and --list-tests-xml options cannot be combined, --filter is ignored' . PHP_EOL;
140-
}
141-
142-
if ($configuration->hasGroups()) {
143-
$buffer .= 'The --group and --list-tests-xml options cannot be combined, --group is ignored' . PHP_EOL;
144-
}
145-
146-
if ($configuration->hasExcludeGroups()) {
147-
$buffer .= 'The --exclude-group and --list-tests-xml options cannot be combined, --exclude-group is ignored' . PHP_EOL;
148-
}
149-
150-
if (!empty($buffer)) {
151-
$buffer .= PHP_EOL;
152-
}
153-
154-
return $buffer;
155126
}
156127
}

tests/end-to-end/_files/list-tests/ExampleTest.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

tests/end-to-end/_files/list-tests/example.phpt

Whitespace-only changes.

tests/end-to-end/_files/list-tests/AnotherExampleTest.php renamed to tests/end-to-end/_files/listing-tests-and-groups/ExampleTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10-
namespace PHPUnit\TestFixture\ListTestsXml;
10+
namespace PHPUnit\TestFixture\ListingTestsAndGroups;
1111

12+
use PHPUnit\Framework\Attributes\Group;
1213
use PHPUnit\Framework\TestCase;
1314

14-
final class AnotherExampleTest extends TestCase
15+
final class ExampleTest extends TestCase
1516
{
17+
#[Group('one')]
1618
public function testOne(): void
1719
{
1820
$this->assertTrue(true);
1921
}
22+
23+
#[Group('two')]
24+
public function testTwo(): void
25+
{
26+
$this->assertTrue(true);
27+
}
2028
}

tests/end-to-end/cli/group/failure/list-groups-and-exclude-filter.phpt

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)