Created
April 7, 2020 13:21
-
-
Save bcremer/ff454cb4e0a0ef53f3dd0440c8d616f9 to your computer and use it in GitHub Desktop.
Reset PHPUnit testcase properties to save memory after each test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class BaseTestCase extends PHPUnit\Framework\TestCase | |
{ | |
/** | |
* Reset the properties to save memory after each test. | |
* @see https://kriswallsmith.net/post/18029585104/faster-phpunit | |
*/ | |
protected function tearDown(): void | |
{ | |
(function () { | |
foreach ((new \ReflectionObject($this))->getProperties() as $prop) { | |
if ($prop->isStatic() || strpos($prop->getDeclaringClass()->getName(), 'PHPUnit\\') === 0) { | |
continue; | |
} | |
unset($this->{$prop->getName()}); | |
} | |
})->call($this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment