Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions src/Runner/Filter/GroupFilterIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
*/
namespace PHPUnit\Runner\Filter;

use function array_intersect;
use function array_merge;
use function array_push;
use function array_values;
use function explode;
use function in_array;
use function str_contains;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestSuite;
Expand Down Expand Up @@ -39,16 +43,46 @@ public function __construct(RecursiveIterator $iterator, array $groups, TestSuit
{
parent::__construct($iterator);

$groupTests = [];
$groupTests = [];
$suiteGroups = $suite->groups();

foreach ($suite->groups() as $group => $tests) {
if (in_array($group, $groups, true)) {
$simpleGroups = [];
$compoundGroups = [];

foreach ($groups as $group) {
if (str_contains($group, '+')) {
$compoundGroups[] = explode('+', $group);
} else {
$simpleGroups[] = $group;
}
}

foreach ($suiteGroups as $group => $tests) {
if (in_array($group, $simpleGroups, true)) {
$groupTests = array_merge($groupTests, $tests);

array_push($groupTests, ...$groupTests);
}
}

foreach ($compoundGroups as $constituents) {
$testsInAllGroups = null;

foreach ($constituents as $part) {
$testsInGroup = $suiteGroups[$part] ?? [];

if ($testsInAllGroups === null) {
$testsInAllGroups = $testsInGroup;
} else {
$testsInAllGroups = array_values(array_intersect($testsInAllGroups, $testsInGroup));
}
}

if ($testsInAllGroups !== null && $testsInAllGroups !== []) {
$groupTests = array_merge($groupTests, $testsInAllGroups);
}
}

$this->groupTests = $groupTests;
}

Expand Down
4 changes: 2 additions & 2 deletions src/TextUI/Help.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ private function elements(): array
['arg' => '--testsuite <name>', 'desc' => 'Only run tests from the specified test suite(s)'],
['arg' => '--exclude-testsuite <name>', 'desc' => 'Exclude tests from the specified test suite(s)'],
['arg' => '--list-groups', 'desc' => 'List available test groups'],
['arg' => '--group <name>', 'desc' => 'Only run tests from the specified group(s)'],
['arg' => '--exclude-group <name>', 'desc' => 'Exclude tests from the specified group(s)'],
['arg' => '--group <name>', 'desc' => 'Only run tests from the specified group(s), use + to combine groups with AND logic (e.g. --group X+Y)'],
['arg' => '--exclude-group <name>', 'desc' => 'Exclude tests from the specified group(s), use + to combine groups with AND logic (e.g. --exclude-group X+Y)'],
['arg' => '--covers <name>', 'desc' => 'Only run tests that intend to cover <name>'],
['arg' => '--uses <name>', 'desc' => 'Only run tests that intend to use <name>'],
['arg' => '--requires-php-extension <name>', 'desc' => 'Only run tests that require PHP extension <name>'],
Expand Down
49 changes: 49 additions & 0 deletions tests/end-to-end/_files/groups/tests/MultiGroupTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture\Groups;

use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

final class MultiGroupTest extends TestCase
{
#[Group('X')]
public function testX(): void
{
$this->assertTrue(true);
}

#[Group('Y')]
public function testY(): void
{
$this->assertTrue(true);
}

#[Group('X')]
#[Group('Y')]
public function testXY(): void
{
$this->assertTrue(true);
}

#[Group('X')]
#[Group('Y')]
#[Group('Z')]
public function testXYZ(): void
{
$this->assertTrue(true);
}

#[Group('Z')]
public function testZ(): void
{
$this->assertTrue(true);
}
}
6 changes: 4 additions & 2 deletions tests/end-to-end/_files/output-cli-help-color.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
suite(s)
--list-groups  List available test groups
--group <name>  Only run tests from the specified
group(s)
group(s), use + to combine groups with
AND logic (e.g. --group X+Y)
--exclude-group <name>  Exclude tests from the specified
group(s)
group(s), use + to combine groups with
AND logic (e.g. --exclude-group X+Y)
--covers <name>  Only run tests that intend to cover
<name>
--uses <name>  Only run tests that intend to use <name>
Expand Down
4 changes: 2 additions & 2 deletions tests/end-to-end/_files/output-cli-usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ Selection:
--testsuite <name> Only run tests from the specified test suite(s)
--exclude-testsuite <name> Exclude tests from the specified test suite(s)
--list-groups List available test groups
--group <name> Only run tests from the specified group(s)
--exclude-group <name> Exclude tests from the specified group(s)
--group <name> Only run tests from the specified group(s), use + to combine groups with AND logic (e.g. --group X+Y)
--exclude-group <name> Exclude tests from the specified group(s), use + to combine groups with AND logic (e.g. --exclude-group X+Y)
--covers <name> Only run tests that intend to cover <name>
--uses <name> Only run tests that intend to use <name>
--requires-php-extension <name> Only run tests that require PHP extension <name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require_once __DIR__ . '/../../../bootstrap.php';
PHPUnit Started (PHPUnit %s using %s)
Test Runner Configured
Event Facade Sealed
Test Suite Loaded (3 tests)
Test Suite Loaded (8 tests)
Test Runner Started
Test Suite Sorted
Test Suite Filtered (3 tests)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require_once __DIR__ . '/../../../bootstrap.php';
PHPUnit Started (PHPUnit %s using %s)
Test Runner Configured
Event Facade Sealed
Test Suite Loaded (3 tests)
Test Suite Loaded (8 tests)
Test Runner Started
Test Suite Sorted
Test Suite Filtered (0 tests)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require_once __DIR__ . '/../../../bootstrap.php';
PHPUnit Started (PHPUnit %s using %s)
Test Runner Configured
Event Facade Sealed
Test Suite Loaded (3 tests)
Test Suite Loaded (8 tests)
Test Runner Started
Test Suite Sorted
Test Suite Filtered (1 test)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require_once __DIR__ . '/../../../bootstrap.php';
PHPUnit Started (PHPUnit %s using %s)
Test Runner Configured
Event Facade Sealed
Test Suite Loaded (3 tests)
Test Suite Loaded (8 tests)
Test Runner Started
Test Suite Sorted
Test Suite Filtered (0 tests)
Expand Down
40 changes: 40 additions & 0 deletions tests/end-to-end/cli/group/exclude-group-and-argument.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
phpunit --exclude-group X+Y tests/MultiGroupTest.php (AND exclude group filter)
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = '--debug';
$_SERVER['argv'][] = '--exclude-group';
$_SERVER['argv'][] = 'X+Y';
$_SERVER['argv'][] = __DIR__ . '/../../_files/groups/tests/MultiGroupTest.php';

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

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit Started (PHPUnit %s using %s)
Test Runner Configured
Event Facade Sealed
Test Suite Loaded (5 tests)
Test Runner Started
Test Suite Sorted
Test Suite Filtered (3 tests)
Test Runner Execution Started (3 tests)
Test Suite Started (PHPUnit\TestFixture\Groups\MultiGroupTest, 3 tests)
Test Preparation Started (PHPUnit\TestFixture\Groups\MultiGroupTest::testX)
Test Prepared (PHPUnit\TestFixture\Groups\MultiGroupTest::testX)
Test Passed (PHPUnit\TestFixture\Groups\MultiGroupTest::testX)
Test Finished (PHPUnit\TestFixture\Groups\MultiGroupTest::testX)
Test Preparation Started (PHPUnit\TestFixture\Groups\MultiGroupTest::testY)
Test Prepared (PHPUnit\TestFixture\Groups\MultiGroupTest::testY)
Test Passed (PHPUnit\TestFixture\Groups\MultiGroupTest::testY)
Test Finished (PHPUnit\TestFixture\Groups\MultiGroupTest::testY)
Test Preparation Started (PHPUnit\TestFixture\Groups\MultiGroupTest::testZ)
Test Prepared (PHPUnit\TestFixture\Groups\MultiGroupTest::testZ)
Test Passed (PHPUnit\TestFixture\Groups\MultiGroupTest::testZ)
Test Finished (PHPUnit\TestFixture\Groups\MultiGroupTest::testZ)
Test Suite Finished (PHPUnit\TestFixture\Groups\MultiGroupTest, 3 tests)
Test Runner Execution Finished
Test Runner Finished
PHPUnit Finished (Shell Exit Code: 0)
38 changes: 38 additions & 0 deletions tests/end-to-end/cli/group/exclude-group-and-or-argument.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
phpunit --exclude-group X+Y --exclude-group Z tests/MultiGroupTest.php (AND combined with OR exclude group filter)
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = '--debug';
$_SERVER['argv'][] = '--exclude-group';
$_SERVER['argv'][] = 'X+Y';
$_SERVER['argv'][] = '--exclude-group';
$_SERVER['argv'][] = 'Z';
$_SERVER['argv'][] = __DIR__ . '/../../_files/groups/tests/MultiGroupTest.php';

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

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit Started (PHPUnit %s using %s)
Test Runner Configured
Event Facade Sealed
Test Suite Loaded (5 tests)
Test Runner Started
Test Suite Sorted
Test Suite Filtered (2 tests)
Test Runner Execution Started (2 tests)
Test Suite Started (PHPUnit\TestFixture\Groups\MultiGroupTest, 2 tests)
Test Preparation Started (PHPUnit\TestFixture\Groups\MultiGroupTest::testX)
Test Prepared (PHPUnit\TestFixture\Groups\MultiGroupTest::testX)
Test Passed (PHPUnit\TestFixture\Groups\MultiGroupTest::testX)
Test Finished (PHPUnit\TestFixture\Groups\MultiGroupTest::testX)
Test Preparation Started (PHPUnit\TestFixture\Groups\MultiGroupTest::testY)
Test Prepared (PHPUnit\TestFixture\Groups\MultiGroupTest::testY)
Test Passed (PHPUnit\TestFixture\Groups\MultiGroupTest::testY)
Test Finished (PHPUnit\TestFixture\Groups\MultiGroupTest::testY)
Test Suite Finished (PHPUnit\TestFixture\Groups\MultiGroupTest, 2 tests)
Test Runner Execution Finished
Test Runner Finished
PHPUnit Finished (Shell Exit Code: 0)
44 changes: 44 additions & 0 deletions tests/end-to-end/cli/group/exclude-group-and-three-argument.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
phpunit --exclude-group X+Y+Z tests/MultiGroupTest.php (AND exclude group filter with three groups)
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = '--debug';
$_SERVER['argv'][] = '--exclude-group';
$_SERVER['argv'][] = 'X+Y+Z';
$_SERVER['argv'][] = __DIR__ . '/../../_files/groups/tests/MultiGroupTest.php';

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

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit Started (PHPUnit %s using %s)
Test Runner Configured
Event Facade Sealed
Test Suite Loaded (5 tests)
Test Runner Started
Test Suite Sorted
Test Suite Filtered (4 tests)
Test Runner Execution Started (4 tests)
Test Suite Started (PHPUnit\TestFixture\Groups\MultiGroupTest, 4 tests)
Test Preparation Started (PHPUnit\TestFixture\Groups\MultiGroupTest::testX)
Test Prepared (PHPUnit\TestFixture\Groups\MultiGroupTest::testX)
Test Passed (PHPUnit\TestFixture\Groups\MultiGroupTest::testX)
Test Finished (PHPUnit\TestFixture\Groups\MultiGroupTest::testX)
Test Preparation Started (PHPUnit\TestFixture\Groups\MultiGroupTest::testY)
Test Prepared (PHPUnit\TestFixture\Groups\MultiGroupTest::testY)
Test Passed (PHPUnit\TestFixture\Groups\MultiGroupTest::testY)
Test Finished (PHPUnit\TestFixture\Groups\MultiGroupTest::testY)
Test Preparation Started (PHPUnit\TestFixture\Groups\MultiGroupTest::testXY)
Test Prepared (PHPUnit\TestFixture\Groups\MultiGroupTest::testXY)
Test Passed (PHPUnit\TestFixture\Groups\MultiGroupTest::testXY)
Test Finished (PHPUnit\TestFixture\Groups\MultiGroupTest::testXY)
Test Preparation Started (PHPUnit\TestFixture\Groups\MultiGroupTest::testZ)
Test Prepared (PHPUnit\TestFixture\Groups\MultiGroupTest::testZ)
Test Passed (PHPUnit\TestFixture\Groups\MultiGroupTest::testZ)
Test Finished (PHPUnit\TestFixture\Groups\MultiGroupTest::testZ)
Test Suite Finished (PHPUnit\TestFixture\Groups\MultiGroupTest, 4 tests)
Test Runner Execution Finished
Test Runner Finished
PHPUnit Finished (Shell Exit Code: 0)
36 changes: 29 additions & 7 deletions tests/end-to-end/cli/group/exclude-group-configuration.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,43 @@ require_once __DIR__ . '/../../../bootstrap.php';
PHPUnit Started (PHPUnit %s using %s)
Test Runner Configured
Event Facade Sealed
Test Suite Loaded (3 tests)
Test Suite Loaded (8 tests)
Test Runner Started
Test Suite Sorted
Test Suite Filtered (1 test)
Test Runner Execution Started (1 test)
Test Suite Started (%s%etests%eend-to-end%e_files%egroups%ephpunit.xml, 1 test)
Test Suite Started (default, 1 test)
Test Suite Filtered (6 tests)
Test Runner Execution Started (6 tests)
Test Suite Started (%s%etests%eend-to-end%e_files%egroups%ephpunit.xml, 6 tests)
Test Suite Started (default, 6 tests)
Test Suite Started (PHPUnit\TestFixture\Groups\FooTest, 1 test)
Test Preparation Started (PHPUnit\TestFixture\Groups\FooTest::testThree)
Test Prepared (PHPUnit\TestFixture\Groups\FooTest::testThree)
Test Passed (PHPUnit\TestFixture\Groups\FooTest::testThree)
Test Finished (PHPUnit\TestFixture\Groups\FooTest::testThree)
Test Suite Finished (PHPUnit\TestFixture\Groups\FooTest, 1 test)
Test Suite Finished (default, 1 test)
Test Suite Finished (%s%etests%eend-to-end%e_files%egroups%ephpunit.xml, 1 test)
Test Suite Started (PHPUnit\TestFixture\Groups\MultiGroupTest, 5 tests)
Test Preparation Started (PHPUnit\TestFixture\Groups\MultiGroupTest::testX)
Test Prepared (PHPUnit\TestFixture\Groups\MultiGroupTest::testX)
Test Passed (PHPUnit\TestFixture\Groups\MultiGroupTest::testX)
Test Finished (PHPUnit\TestFixture\Groups\MultiGroupTest::testX)
Test Preparation Started (PHPUnit\TestFixture\Groups\MultiGroupTest::testY)
Test Prepared (PHPUnit\TestFixture\Groups\MultiGroupTest::testY)
Test Passed (PHPUnit\TestFixture\Groups\MultiGroupTest::testY)
Test Finished (PHPUnit\TestFixture\Groups\MultiGroupTest::testY)
Test Preparation Started (PHPUnit\TestFixture\Groups\MultiGroupTest::testXY)
Test Prepared (PHPUnit\TestFixture\Groups\MultiGroupTest::testXY)
Test Passed (PHPUnit\TestFixture\Groups\MultiGroupTest::testXY)
Test Finished (PHPUnit\TestFixture\Groups\MultiGroupTest::testXY)
Test Preparation Started (PHPUnit\TestFixture\Groups\MultiGroupTest::testXYZ)
Test Prepared (PHPUnit\TestFixture\Groups\MultiGroupTest::testXYZ)
Test Passed (PHPUnit\TestFixture\Groups\MultiGroupTest::testXYZ)
Test Finished (PHPUnit\TestFixture\Groups\MultiGroupTest::testXYZ)
Test Preparation Started (PHPUnit\TestFixture\Groups\MultiGroupTest::testZ)
Test Prepared (PHPUnit\TestFixture\Groups\MultiGroupTest::testZ)
Test Passed (PHPUnit\TestFixture\Groups\MultiGroupTest::testZ)
Test Finished (PHPUnit\TestFixture\Groups\MultiGroupTest::testZ)
Test Suite Finished (PHPUnit\TestFixture\Groups\MultiGroupTest, 5 tests)
Test Suite Finished (default, 6 tests)
Test Suite Finished (%s%etests%eend-to-end%e_files%egroups%ephpunit.xml, 6 tests)
Test Runner Execution Finished
Test Runner Finished
PHPUnit Finished (Shell Exit Code: 0)
36 changes: 36 additions & 0 deletions tests/end-to-end/cli/group/group-and-argument.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
phpunit --group X+Y tests/MultiGroupTest.php (AND group filter)
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = '--debug';
$_SERVER['argv'][] = '--group';
$_SERVER['argv'][] = 'X+Y';
$_SERVER['argv'][] = __DIR__ . '/../../_files/groups/tests/MultiGroupTest.php';

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

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit Started (PHPUnit %s using %s)
Test Runner Configured
Event Facade Sealed
Test Suite Loaded (5 tests)
Test Runner Started
Test Suite Sorted
Test Suite Filtered (2 tests)
Test Runner Execution Started (2 tests)
Test Suite Started (PHPUnit\TestFixture\Groups\MultiGroupTest, 2 tests)
Test Preparation Started (PHPUnit\TestFixture\Groups\MultiGroupTest::testXY)
Test Prepared (PHPUnit\TestFixture\Groups\MultiGroupTest::testXY)
Test Passed (PHPUnit\TestFixture\Groups\MultiGroupTest::testXY)
Test Finished (PHPUnit\TestFixture\Groups\MultiGroupTest::testXY)
Test Preparation Started (PHPUnit\TestFixture\Groups\MultiGroupTest::testXYZ)
Test Prepared (PHPUnit\TestFixture\Groups\MultiGroupTest::testXYZ)
Test Passed (PHPUnit\TestFixture\Groups\MultiGroupTest::testXYZ)
Test Finished (PHPUnit\TestFixture\Groups\MultiGroupTest::testXYZ)
Test Suite Finished (PHPUnit\TestFixture\Groups\MultiGroupTest, 2 tests)
Test Runner Execution Finished
Test Runner Finished
PHPUnit Finished (Shell Exit Code: 0)
Loading
Loading