Created
August 25, 2020 08:08
-
-
Save TheRatG/9acb8b499dbd52e9ecfbf6eba58accd0 to your computer and use it in GitHub Desktop.
Faster PHPUnit
This file contains 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 | |
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