Skip to content

Commit

Permalink
Also support filtering using --filter, --exclude-filter, --group, and…
Browse files Browse the repository at this point in the history
… --exclude-group when listing groups using --list-groups
  • Loading branch information
sebastianbergmann committed Mar 8, 2024
1 parent 6c16485 commit 43c6126
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 44 deletions.
2 changes: 1 addition & 1 deletion ChangeLog-11.1.md
Expand Up @@ -8,7 +8,7 @@ All notable changes of the PHPUnit 11.1 release series are documented in this fi

* [#5175](https://github.com/sebastianbergmann/phpunit/issues/5175): `#[CoversMethod]` and `#[UsesMethod]` attributes for more fine-grained code coverage targeting
* [#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()`
* [#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`
* [#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`
* `--only-summary-for-coverage-text` CLI option to reduce the code coverage report in text format to a summary
* `--show-uncovered-for-coverage-text` CLI option to expand the code coverage report in text format to include a list of uncovered files

Expand Down
9 changes: 8 additions & 1 deletion src/TextUI/Application.php
Expand Up @@ -419,7 +419,14 @@ private function executeCommandsThatDoNotRequireTheTestSuite(Configuration $conf
private function executeCommandsThatRequireTheTestSuite(Configuration $configuration, CliConfiguration $cliConfiguration, TestSuite $testSuite): void
{
if ($cliConfiguration->listGroups()) {
$this->execute(new ListGroupsCommand($testSuite));
$this->execute(
new ListGroupsCommand(
$this->filteredTests(
$configuration,
$testSuite,
),
),
);
}

if ($cliConfiguration->listTests()) {
Expand Down
71 changes: 29 additions & 42 deletions src/TextUI/Command/Commands/ListGroupsCommand.php
Expand Up @@ -9,32 +9,52 @@
*/
namespace PHPUnit\TextUI\Command;

use function array_merge;
use function array_unique;
use function sort;
use function sprintf;
use function str_starts_with;
use PHPUnit\Framework\TestSuite;
use PHPUnit\TextUI\Configuration\Registry;
use PHPUnit\Framework\TestCase;
use PHPUnit\Runner\PhptTestCase;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final readonly class ListGroupsCommand implements Command
{
private TestSuite $suite;

public function __construct(TestSuite $suite)
/**
* @psalm-var list<TestCase|PhptTestCase>
*/
private array $tests;

/**
* @psalm-param list<TestCase|PhptTestCase> $tests
*/
public function __construct(array $tests)
{
$this->suite = $suite;
$this->tests = $tests;
}

public function execute(): Result
{
$buffer = $this->warnAboutConflictingOptions();
$buffer .= 'Available test group(s):' . PHP_EOL;
$groups = [];

foreach ($this->tests as $test) {
if ($test instanceof PhptTestCase) {
$groups[] = 'default';

continue;
}

$groups = array_merge($groups, $test->groups());
}

$groups = array_unique($groups);

$groups = $this->suite->groups();
sort($groups);

$buffer = 'Available test group(s):' . PHP_EOL;

foreach ($groups as $group) {
if (str_starts_with($group, '__phpunit_')) {
continue;
Expand All @@ -48,37 +68,4 @@ public function execute(): Result

return Result::from($buffer);
}

private function warnAboutConflictingOptions(): string
{
$buffer = '';

$configuration = Registry::get();

if ($configuration->hasFilter()) {
$buffer .= 'The --filter and --list-groups options cannot be combined, --filter is ignored' . PHP_EOL;
}

if ($configuration->hasExcludeFilter()) {
$buffer .= 'The --exclude-filter and --list-groups options cannot be combined, --exclude-filter is ignored' . PHP_EOL;
}

if ($configuration->hasGroups()) {
$buffer .= 'The --group and --list-groups options cannot be combined, --group is ignored' . PHP_EOL;
}

if ($configuration->hasExcludeGroups()) {
$buffer .= 'The --exclude-group and --list-groups options cannot be combined, --exclude-group is ignored' . PHP_EOL;
}

if ($configuration->includeTestSuite() !== '') {
$buffer .= 'The --testsuite and --list-groups options cannot be combined, --testsuite is ignored' . PHP_EOL;
}

if (!empty($buffer)) {
$buffer .= PHP_EOL;
}

return $buffer;
}
}
@@ -0,0 +1,19 @@
--TEST--
phpunit --exclude-filter testOne --list-groups ../../_files/listing-tests-and-groups
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = '--exclude-filter';
$_SERVER['argv'][] = 'testOne';
$_SERVER['argv'][] = '--list-groups';
$_SERVER['argv'][] = __DIR__ . '/../../_files/listing-tests-and-groups';

require_once __DIR__ . '/../../../bootstrap.php';

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Available test group(s):
- two
@@ -0,0 +1,19 @@
--TEST--
phpunit --exclude-group one --list-groups ../../_files/listing-tests-and-groups
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = '--exclude-group';
$_SERVER['argv'][] = 'one';
$_SERVER['argv'][] = '--list-groups';
$_SERVER['argv'][] = __DIR__ . '/../../_files/listing-tests-and-groups';

require_once __DIR__ . '/../../../bootstrap.php';

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Available test group(s):
- two
@@ -0,0 +1,19 @@
--TEST--
phpunit --exclude-filter testOne --list-groups ../../_files/listing-tests-and-groups
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = '--exclude-filter';
$_SERVER['argv'][] = 'testOne';
$_SERVER['argv'][] = '--list-groups';
$_SERVER['argv'][] = __DIR__ . '/../../_files/listing-tests-and-groups';

require_once __DIR__ . '/../../../bootstrap.php';

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Available test group(s):
- two
@@ -0,0 +1,19 @@
--TEST--
phpunit --group one --list-groups ../../_files/listing-tests-and-groups
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = '--group';
$_SERVER['argv'][] = 'one';
$_SERVER['argv'][] = '--list-groups';
$_SERVER['argv'][] = __DIR__ . '/../../_files/listing-tests-and-groups';

require_once __DIR__ . '/../../../bootstrap.php';

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Available test group(s):
- one

0 comments on commit 43c6126

Please sign in to comment.