Skip to content

Commit

Permalink
Use test id instead of test object for group-based filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jun 13, 2024
1 parent db349b0 commit 52d2488
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 43 deletions.
45 changes: 20 additions & 25 deletions src/Framework/TestSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class TestSuite implements IteratorAggregate, Reorderable, SelfDescribing, Test
private string $name;

/**
* @psalm-var array<string,list<Test>>
* @psalm-var array<non-empty-string, list<non-empty-string>>
*/
private array $groups = [];

Expand Down Expand Up @@ -175,17 +175,25 @@ public function addTest(Test $test, array $groups = []): void
$groups[] = 'default';
}

foreach ($groups as $group) {
if (!isset($this->groups[$group])) {
$this->groups[$group] = [$test];
} else {
$this->groups[$group][] = $test;
}
}

if ($test instanceof TestCase) {
$id = $test->valueObjectForEvents()->id();

$test->setGroups($groups);
}

if ($test instanceof PhptTestCase) {
$id = $test->valueObjectForEvents()->id();
}

if (isset($id)) {
foreach ($groups as $group) {
if (!isset($this->groups[$group])) {
$this->groups[$group] = [$id];
} else {
$this->groups[$group][] = $id;
}
}
}
}
}

Expand Down Expand Up @@ -310,6 +318,9 @@ public function groups(): array
);
}

/**
* @psalm-return array<non-empty-string, list<non-empty-string>>
*/
public function groupDetails(): array
{
return $this->groups;
Expand Down Expand Up @@ -384,22 +395,6 @@ public function run(): void
break;
}
}

if ($test instanceof TestCase || $test instanceof self) {
foreach ($test->groups() as $group) {
if (!isset($this->groups[$group])) {
continue;
}

foreach (array_keys($this->groups[$group]) as $key) {
if ($test === $this->groups[$group][$key]) {
unset($this->groups[$group][$key]);

break;
}
}
}
}
}

$this->invokeMethodsAfterLastTest($emitter);
Expand Down
5 changes: 3 additions & 2 deletions src/Runner/Filter/ExcludeGroupFilterIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
final class ExcludeGroupFilterIterator extends GroupFilterIterator
{
/**
* @psalm-param list<int> $groupTests
* @psalm-param non-empty-string $id
* @psalm-param list<non-empty-string> $groupTests
*/
protected function doAccept(int $id, array $groupTests): bool
protected function doAccept(string $id, array $groupTests): bool
{
return !in_array($id, $groupTests, true);
}
Expand Down
30 changes: 16 additions & 14 deletions src/Runner/Filter/GroupFilterIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
*/
namespace PHPUnit\Runner\Filter;

use function array_map;
use function array_merge;
use function array_push;
use function array_values;
use function in_array;
use function spl_object_id;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestSuite;
use PHPUnit\Runner\PhptTestCase;
use RecursiveFilterIterator;
use RecursiveIterator;

Expand All @@ -25,7 +25,7 @@
abstract class GroupFilterIterator extends RecursiveFilterIterator
{
/**
* @psalm-var list<int>
* @psalm-var list<non-empty-string>
*/
private readonly array $groupTests;

Expand All @@ -40,17 +40,14 @@ public function __construct(RecursiveIterator $iterator, array $groups, TestSuit
$groupTests = [];

foreach ($suite->groupDetails() as $group => $tests) {
if (in_array((string) $group, $groups, true)) {
$testHashes = array_map(
'spl_object_id',
$tests,
);
if (in_array($group, $groups, true)) {
$groupTests = array_merge($groupTests, $tests);

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

$this->groupTests = array_values($groupTests);
$this->groupTests = $groupTests;
}

public function accept(): bool
Expand All @@ -61,11 +58,16 @@ public function accept(): bool
return true;
}

return $this->doAccept(spl_object_id($test), $this->groupTests);
if ($test instanceof TestCase || $test instanceof PhptTestCase) {
return $this->doAccept($test->valueObjectForEvents()->id(), $this->groupTests);
}

return true;
}

/**
* @psalm-param list<int> $groupTests
* @psalm-param non-empty-string $id
* @psalm-param list<non-empty-string> $groupTests
*/
abstract protected function doAccept(int $id, array $groupTests): bool;
abstract protected function doAccept(string $id, array $groupTests): bool;
}
5 changes: 3 additions & 2 deletions src/Runner/Filter/IncludeGroupFilterIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
final class IncludeGroupFilterIterator extends GroupFilterIterator
{
/**
* @psalm-param list<int> $groupTests
* @psalm-param non-empty-string $id
* @psalm-param list<non-empty-string> $groupTests
*/
protected function doAccept(int $id, array $groupTests): bool
protected function doAccept(string $id, array $groupTests): bool
{
return in_array($id, $groupTests, true);
}
Expand Down

0 comments on commit 52d2488

Please sign in to comment.