Skip to content

Instantly share code, notes, and snippets.

@bcremer
Created April 7, 2020 13:21
Show Gist options
  • Save bcremer/ff454cb4e0a0ef53f3dd0440c8d616f9 to your computer and use it in GitHub Desktop.
Save bcremer/ff454cb4e0a0ef53f3dd0440c8d616f9 to your computer and use it in GitHub Desktop.
Reset PHPUnit testcase properties to save memory after each test.
<?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