Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destroy TestCase object after its test was run #5861

Closed
Closed
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
61 changes: 44 additions & 17 deletions src/Framework/TestSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use const PHP_EOL;
use function array_keys;
use function array_map;
use function array_merge;
use function assert;
use function call_user_func;
Expand Down Expand Up @@ -62,7 +61,7 @@
private string $name;

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

Expand All @@ -81,6 +80,7 @@
*/
private ?array $providedTests = null;
private ?Factory $iteratorFilter = null;
private bool $wasRun = false;

/**
* @psalm-param non-empty-string $name
Expand Down Expand Up @@ -160,31 +160,42 @@
*/
public function addTest(Test $test, array $groups = []): void
{
if ($test instanceof self) {

Check warning on line 163 in src/Framework/TestSuite.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ */ public function addTest(Test $test, array $groups = []): void { - if ($test instanceof self) { + if (true) { $this->tests[] = $test; $this->clearCaches(); return;
$this->tests[] = $test;

$this->clearCaches();

Check warning on line 166 in src/Framework/TestSuite.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ { if ($test instanceof self) { $this->tests[] = $test; - $this->clearCaches(); + return; } assert($test instanceof TestCase || $test instanceof PhptTestCase);

return;
}

assert($test instanceof TestCase || $test instanceof PhptTestCase);

Check warning on line 171 in src/Framework/TestSuite.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ $this->clearCaches(); return; } - assert($test instanceof TestCase || $test instanceof PhptTestCase); + assert(true || $test instanceof PhptTestCase); $class = new ReflectionClass($test); if (!$class->isAbstract()) { $this->tests[] = $test;

Check warning on line 171 in src/Framework/TestSuite.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ $this->clearCaches(); return; } - assert($test instanceof TestCase || $test instanceof PhptTestCase); + assert($test instanceof TestCase || true); $class = new ReflectionClass($test); if (!$class->isAbstract()) { $this->tests[] = $test;

Check warning on line 171 in src/Framework/TestSuite.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ $this->clearCaches(); return; } - assert($test instanceof TestCase || $test instanceof PhptTestCase); + assert($test instanceof TestCase || false); $class = new ReflectionClass($test); if (!$class->isAbstract()) { $this->tests[] = $test;

Check warning on line 171 in src/Framework/TestSuite.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "LogicalOrAllSubExprNegation": --- Original +++ New @@ @@ $this->clearCaches(); return; } - assert($test instanceof TestCase || $test instanceof PhptTestCase); + assert(!$test instanceof TestCase || !$test instanceof PhptTestCase); $class = new ReflectionClass($test); if (!$class->isAbstract()) { $this->tests[] = $test;

$class = new ReflectionClass($test);

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

if ($test instanceof self && empty($groups)) {
$groups = $test->groups();
}
$this->clearCaches();

Check warning on line 178 in src/Framework/TestSuite.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ $class = new ReflectionClass($test); if (!$class->isAbstract()) { $this->tests[] = $test; - $this->clearCaches(); + if ($this->containsOnlyVirtualGroups($groups)) { $groups[] = 'default'; }

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

if ($test instanceof TestCase) {

Check warning on line 184 in src/Framework/TestSuite.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ if ($this->containsOnlyVirtualGroups($groups)) { $groups[] = 'default'; } - if ($test instanceof TestCase) { + if (true) { $id = $test->valueObjectForEvents()->id(); $test->setGroups($groups); } else {

Check warning on line 184 in src/Framework/TestSuite.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ if ($this->containsOnlyVirtualGroups($groups)) { $groups[] = 'default'; } - if ($test instanceof TestCase) { + if (false) { $id = $test->valueObjectForEvents()->id(); $test->setGroups($groups); } else {
$id = $test->valueObjectForEvents()->id();

$test->setGroups($groups);

Check warning on line 187 in src/Framework/TestSuite.php

View workflow job for this annotation

GitHub Actions / Mutation Testing

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ } if ($test instanceof TestCase) { $id = $test->valueObjectForEvents()->id(); - $test->setGroups($groups); + } else { $id = $test->valueObjectForEvents()->id(); }
} else {
$id = $test->valueObjectForEvents()->id();
}

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

if ($test instanceof TestCase) {
$test->setGroups($groups);
}
}
}

Expand Down Expand Up @@ -299,16 +310,16 @@
/**
* Returns the test groups of the suite.
*
* @psalm-return list<string>
* @psalm-return list<non-empty-string>
*/
public function groups(): array
{
return array_map(
'strval',
array_keys($this->groups),
);
return array_keys($this->groups);

Check warning on line 317 in src/Framework/TestSuite.php

View check run for this annotation

Codecov / codecov/patch

src/Framework/TestSuite.php#L317

Added line #L317 was not covered by tests
}

/**
* @psalm-return array<non-empty-string, list<non-empty-string>>
*/
public function groupDetails(): array
{
return $this->groups;
Expand Down Expand Up @@ -346,6 +357,14 @@
*/
public function run(): void
{
if ($this->wasRun) {
// @codeCoverageIgnoreStart
throw new Exception('The tests aggregated by this TestSuite were already run');
// @codeCoverageIgnoreEnd
}

$this->wasRun = true;

if (count($this) === 0) {
return;
}
Expand All @@ -367,6 +386,14 @@
}

$test->run();

foreach (array_keys($this->tests) as $key) {
if ($test === $this->tests[$key]) {
unset($this->tests[$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 @@
$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 @@
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;

Check warning on line 65 in src/Runner/Filter/GroupFilterIterator.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/Filter/GroupFilterIterator.php#L65

Added line #L65 was not covered by tests
}

/**
* @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