Skip to content

Commit 387b451

Browse files
Use test id instead of test object for group-based filtering
1 parent e1a851b commit 387b451

File tree

4 files changed

+42
-43
lines changed

4 files changed

+42
-43
lines changed

src/Framework/TestSuite.php

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class TestSuite implements IteratorAggregate, Reorderable, SelfDescribing, Test
6262
private string $name;
6363

6464
/**
65-
* @psalm-var array<string,list<Test>>
65+
* @psalm-var array<non-empty-string, list<non-empty-string>>
6666
*/
6767
private array $groups = [];
6868

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

178-
foreach ($groups as $group) {
179-
if (!isset($this->groups[$group])) {
180-
$this->groups[$group] = [$test];
181-
} else {
182-
$this->groups[$group][] = $test;
183-
}
184-
}
185-
186178
if ($test instanceof TestCase) {
179+
$id = $test->valueObjectForEvents()->id();
180+
187181
$test->setGroups($groups);
188182
}
183+
184+
if ($test instanceof PhptTestCase) {
185+
$id = $test->valueObjectForEvents()->id();
186+
}
187+
188+
if (isset($id)) {
189+
foreach ($groups as $group) {
190+
if (!isset($this->groups[$group])) {
191+
$this->groups[$group] = [$id];
192+
} else {
193+
$this->groups[$group][] = $id;
194+
}
195+
}
196+
}
189197
}
190198
}
191199

@@ -310,6 +318,9 @@ public function groups(): array
310318
);
311319
}
312320

321+
/**
322+
* @psalm-return array<non-empty-string, list<non-empty-string>>
323+
*/
313324
public function groupDetails(): array
314325
{
315326
return $this->groups;
@@ -384,22 +395,6 @@ public function run(): void
384395
break;
385396
}
386397
}
387-
388-
if ($test instanceof TestCase || $test instanceof self) {
389-
foreach ($test->groups() as $group) {
390-
if (!isset($this->groups[$group])) {
391-
continue;
392-
}
393-
394-
foreach (array_keys($this->groups[$group]) as $key) {
395-
if ($test === $this->groups[$group][$key]) {
396-
unset($this->groups[$group][$key]);
397-
398-
break;
399-
}
400-
}
401-
}
402-
}
403398
}
404399

405400
$this->invokeMethodsAfterLastTest($emitter);

src/Runner/Filter/ExcludeGroupFilterIterator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
final class ExcludeGroupFilterIterator extends GroupFilterIterator
1818
{
1919
/**
20-
* @psalm-param list<int> $groupTests
20+
* @psalm-param non-empty-string $id
21+
* @psalm-param list<non-empty-string> $groupTests
2122
*/
22-
protected function doAccept(int $id, array $groupTests): bool
23+
protected function doAccept(string $id, array $groupTests): bool
2324
{
2425
return !in_array($id, $groupTests, true);
2526
}

src/Runner/Filter/GroupFilterIterator.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
*/
1010
namespace PHPUnit\Runner\Filter;
1111

12-
use function array_map;
12+
use function array_merge;
1313
use function array_push;
14-
use function array_values;
1514
use function in_array;
16-
use function spl_object_id;
1715
use PHPUnit\Framework\Test;
16+
use PHPUnit\Framework\TestCase;
1817
use PHPUnit\Framework\TestSuite;
18+
use PHPUnit\Runner\PhptTestCase;
1919
use RecursiveFilterIterator;
2020
use RecursiveIterator;
2121

@@ -25,7 +25,7 @@
2525
abstract class GroupFilterIterator extends RecursiveFilterIterator
2626
{
2727
/**
28-
* @psalm-var list<int>
28+
* @psalm-var list<non-empty-string>
2929
*/
3030
private readonly array $groupTests;
3131

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

4242
foreach ($suite->groupDetails() as $group => $tests) {
43-
if (in_array((string) $group, $groups, true)) {
44-
$testHashes = array_map(
45-
'spl_object_id',
46-
$tests,
47-
);
43+
if (in_array($group, $groups, true)) {
44+
$groupTests = array_merge($groupTests, $tests);
4845

49-
array_push($groupTests, ...$testHashes);
46+
array_push($groupTests, ...$groupTests);
5047
}
5148
}
5249

53-
$this->groupTests = array_values($groupTests);
50+
$this->groupTests = $groupTests;
5451
}
5552

5653
public function accept(): bool
@@ -61,11 +58,16 @@ public function accept(): bool
6158
return true;
6259
}
6360

64-
return $this->doAccept(spl_object_id($test), $this->groupTests);
61+
if ($test instanceof TestCase || $test instanceof PhptTestCase) {
62+
return $this->doAccept($test->valueObjectForEvents()->id(), $this->groupTests);
63+
}
64+
65+
return true;
6566
}
6667

6768
/**
68-
* @psalm-param list<int> $groupTests
69+
* @psalm-param non-empty-string $id
70+
* @psalm-param list<non-empty-string> $groupTests
6971
*/
70-
abstract protected function doAccept(int $id, array $groupTests): bool;
72+
abstract protected function doAccept(string $id, array $groupTests): bool;
7173
}

src/Runner/Filter/IncludeGroupFilterIterator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
final class IncludeGroupFilterIterator extends GroupFilterIterator
1818
{
1919
/**
20-
* @psalm-param list<int> $groupTests
20+
* @psalm-param non-empty-string $id
21+
* @psalm-param list<non-empty-string> $groupTests
2122
*/
22-
protected function doAccept(int $id, array $groupTests): bool
23+
protected function doAccept(string $id, array $groupTests): bool
2324
{
2425
return in_array($id, $groupTests, true);
2526
}

0 commit comments

Comments
 (0)