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

ISSUE-4798: Memory leaks in TestSuite class #4799

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/Framework/TestSuite.php
Expand Up @@ -10,7 +10,6 @@
namespace PHPUnit\Framework;

use const PHP_EOL;
use function array_diff;
use function array_keys;
use function array_merge;
use function basename;
Expand Down Expand Up @@ -121,9 +120,9 @@ class TestSuite implements IteratorAggregate, SelfDescribing, Test
private $iteratorFilter;

/**
* @var string[]
* @var int
*/
private $declaredClasses;
private $declaredClassesPointer;

/**
* Constructs a new TestSuite:.
Expand Down Expand Up @@ -155,7 +154,7 @@ public function __construct($theClass = '', string $name = '')
);
}

$this->declaredClasses = get_declared_classes();
$this->declaredClassesPointer = count(get_declared_classes());

if (!$theClass instanceof ReflectionClass) {
if (class_exists($theClass, true)) {
Expand Down Expand Up @@ -382,7 +381,7 @@ public function addTestFile(string $filename): void
// The given file may contain further stub classes in addition to the
// test class itself. Figure out the actual test class.
$filename = FileLoader::checkAndLoad($filename);
$newClasses = array_diff(get_declared_classes(), $this->declaredClasses);
$newClasses = array_slice(get_declared_classes(), $this->declaredClassesPointer);

// The diff is empty in case a parent class (with test methods) is added
// AFTER a child class that inherited from it. To account for that case,
Expand All @@ -392,8 +391,8 @@ public function addTestFile(string $filename): void
// On the assumption that test classes are defined first in files,
// process discovered classes in approximate LIFO order, so as to
// avoid unnecessary reflection.
$this->foundClasses = array_merge($newClasses, $this->foundClasses);
$this->declaredClasses = get_declared_classes();
$this->foundClasses = array_merge($newClasses, $this->foundClasses);
$this->declaredClassesPointer = count(get_declared_classes());
}

// The test class's name must match the filename, either in full, or as
Expand Down