Skip to content

Commit 541fd37

Browse files
Maarten Scholdersebastianbergmann
authored andcommitted
Refactor unsetting tests during TestSuite::run, for even faster destruction of instances.
- Continuing on the destruction fix: The iterator also holds the array of tests. For tests with dataProviders, this results in not immediately destructing each Test, but only when the deeper TestSuite is done. - Refactored cleanup tricks: Just do a first loop so we no longer need properties nor the iterator, and then array_shift as we go. - Added test coverage.
1 parent 8afb89b commit 541fd37

File tree

2 files changed

+58
-24
lines changed

2 files changed

+58
-24
lines changed

src/Framework/TestSuite.php

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use const PHP_EOL;
1313
use function array_keys;
1414
use function array_map;
15+
use function array_shift;
1516
use function assert;
1617
use function call_user_func;
1718
use function class_exists;
@@ -348,38 +349,24 @@ public function run(): void
348349
return;
349350
}
350351

352+
/** @var Test[] $tests Local array, so we clear references to instances asap during the real loop. */
353+
$tests = [];
354+
351355
foreach ($this as $test) {
356+
$tests[] = $test;
357+
}
358+
359+
$this->tests = [];
360+
$this->groups = [];
361+
362+
while ($test = array_shift($tests)) {
352363
if (TestResultFacade::shouldStop()) {
353364
$emitter->testRunnerExecutionAborted();
354365

355366
break;
356367
}
357368

358369
$test->run();
359-
360-
foreach (array_keys($this->tests) as $key) {
361-
if ($test === $this->tests[$key]) {
362-
unset($this->tests[$key]);
363-
364-
break;
365-
}
366-
}
367-
368-
if ($test instanceof TestCase || $test instanceof self) {
369-
foreach ($test->groups() as $group) {
370-
if (!isset($this->groups[$group])) {
371-
continue;
372-
}
373-
374-
foreach (array_keys($this->groups[$group]) as $key) {
375-
if ($test === $this->groups[$group][$key]) {
376-
unset($this->groups[$group][$key]);
377-
378-
break;
379-
}
380-
}
381-
}
382-
}
383370
}
384371

385372
$this->invokeMethodsAfterLastTest($emitter);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Framework;
11+
12+
use PHPUnit\Framework\Attributes\CoversClass;
13+
use PHPUnit\Framework\Attributes\Depends;
14+
use PHPUnit\Framework\Attributes\Small;
15+
use PHPUnit\Framework\Attributes\TestWith;
16+
17+
#[CoversClass(TestSuite::class)]
18+
#[Small]
19+
final class TestSuiteRunDestructsTestCaseTest extends TestCase
20+
{
21+
private static int $destructsDone = 0;
22+
23+
public function __destruct()
24+
{
25+
self::$destructsDone++;
26+
}
27+
28+
public function testFirstTest(): void
29+
{
30+
$this->assertSame(0, self::$destructsDone);
31+
}
32+
33+
#[Depends('testFirstTest')]
34+
public function testSecondTest(): void
35+
{
36+
$this->assertSame(1, self::$destructsDone);
37+
}
38+
39+
#[Depends('testSecondTest')]
40+
#[TestWith([2])]
41+
#[TestWith([3])]
42+
#[TestWith([4])]
43+
public function testThirdTestWhichUsesDataProvider($numberOfTestsBeforeThisOne): void
44+
{
45+
$this->assertSame($numberOfTestsBeforeThisOne, self::$destructsDone);
46+
}
47+
}

0 commit comments

Comments
 (0)