Skip to content

Instantly share code, notes, and snippets.

@TheRatG
Created August 25, 2020 08:08
Show Gist options
  • Save TheRatG/9acb8b499dbd52e9ecfbf6eba58accd0 to your computer and use it in GitHub Desktop.
Save TheRatG/9acb8b499dbd52e9ecfbf6eba58accd0 to your computer and use it in GitHub Desktop.
Faster PHPUnit
<?php
declare(strict_types=1);
namespace Tests\Core;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestListener;
use PHPUnit\Framework\TestListenerDefaultImplementation;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* Adapted from http://kriswallsmith.net/post/18029585104/faster-phpunit.
* and packaged into an event listener:
* https://tomnewby.net/posts/speed-up-phpunit-1-weird-trick/
*/
final class TestTidierListener implements TestListener
{
use TestListenerDefaultImplementation;
/**
* This goes through the test case and unsets any properties that have been set on the class.
*/
public function endTest(Test $test, $time): void
{
if (false === ($test instanceof WebTestCase)) {
// We only care about inspecting if this is a WebTestCase test
return;
}
self::stripProperties($test);
}
/**
* @param \object $target
*/
public static function stripProperties($target): void
{
$refl = new \ReflectionObject($target);
foreach ($refl->getProperties() as $prop) {
if (!$prop->isStatic() && 0 !== \strncmp($prop->getDeclaringClass()->getName(), 'PHPUnit_', 8)) {
$prop->setAccessible(true);
$prop->setValue($target, null);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment