Skip to content

Commit c50071d

Browse files
Merge branch '11.2'
2 parents 778070d + afd1715 commit c50071d

File tree

4 files changed

+66
-35
lines changed

4 files changed

+66
-35
lines changed

src/Framework/TestSuite.php

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use const PHP_EOL;
1313
use function array_keys;
14-
use function array_map;
1514
use function array_merge;
1615
use function assert;
1716
use function call_user_func;
@@ -62,7 +61,7 @@ class TestSuite implements IteratorAggregate, Reorderable, SelfDescribing, Test
6261
private string $name;
6362

6463
/**
65-
* @psalm-var array<string,list<Test>>
64+
* @psalm-var array<non-empty-string, list<non-empty-string>>
6665
*/
6766
private array $groups = [];
6867

@@ -81,6 +80,7 @@ class TestSuite implements IteratorAggregate, Reorderable, SelfDescribing, Test
8180
*/
8281
private ?array $providedTests = null;
8382
private ?Factory $iteratorFilter = null;
83+
private bool $wasRun = false;
8484

8585
/**
8686
* @psalm-param non-empty-string $name
@@ -160,31 +160,42 @@ public function toString(): string
160160
*/
161161
public function addTest(Test $test, array $groups = []): void
162162
{
163+
if ($test instanceof self) {
164+
$this->tests[] = $test;
165+
166+
$this->clearCaches();
167+
168+
return;
169+
}
170+
171+
assert($test instanceof TestCase || $test instanceof PhptTestCase);
172+
163173
$class = new ReflectionClass($test);
164174

165175
if (!$class->isAbstract()) {
166176
$this->tests[] = $test;
167-
$this->clearCaches();
168177

169-
if ($test instanceof self && empty($groups)) {
170-
$groups = $test->groups();
171-
}
178+
$this->clearCaches();
172179

173180
if ($this->containsOnlyVirtualGroups($groups)) {
174181
$groups[] = 'default';
175182
}
176183

184+
if ($test instanceof TestCase) {
185+
$id = $test->valueObjectForEvents()->id();
186+
187+
$test->setGroups($groups);
188+
} else {
189+
$id = $test->valueObjectForEvents()->id();
190+
}
191+
177192
foreach ($groups as $group) {
178193
if (!isset($this->groups[$group])) {
179-
$this->groups[$group] = [$test];
194+
$this->groups[$group] = [$id];
180195
} else {
181-
$this->groups[$group][] = $test;
196+
$this->groups[$group][] = $id;
182197
}
183198
}
184-
185-
if ($test instanceof TestCase) {
186-
$test->setGroups($groups);
187-
}
188199
}
189200
}
190201

@@ -299,16 +310,16 @@ public function name(): string
299310
/**
300311
* Returns the test groups of the suite.
301312
*
302-
* @psalm-return list<string>
313+
* @psalm-return list<non-empty-string>
303314
*/
304315
public function groups(): array
305316
{
306-
return array_map(
307-
'strval',
308-
array_keys($this->groups),
309-
);
317+
return array_keys($this->groups);
310318
}
311319

320+
/**
321+
* @psalm-return array<non-empty-string, list<non-empty-string>>
322+
*/
312323
public function groupDetails(): array
313324
{
314325
return $this->groups;
@@ -346,6 +357,14 @@ public function collect(): array
346357
*/
347358
public function run(): void
348359
{
360+
if ($this->wasRun) {
361+
// @codeCoverageIgnoreStart
362+
throw new Exception('The tests aggregated by this TestSuite were already run');
363+
// @codeCoverageIgnoreEnd
364+
}
365+
366+
$this->wasRun = true;
367+
349368
if (count($this) === 0) {
350369
return;
351370
}
@@ -367,6 +386,14 @@ public function run(): void
367386
}
368387

369388
$test->run();
389+
390+
foreach (array_keys($this->tests) as $key) {
391+
if ($test === $this->tests[$key]) {
392+
unset($this->tests[$key]);
393+
394+
break;
395+
}
396+
}
370397
}
371398

372399
$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)