Skip to content

Commit

Permalink
[TesterBundle] PHPUnit KernelShutdown extension
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Jan 18, 2024
1 parent 576de6a commit a226506
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Draw\Bundle\TesterBundle\PHPUnit\Extension;

use PHPUnit\Event\Code\TestMethod;
use PHPUnit\Event\Test\Finished as TestFinished;
use PHPUnit\Event\Test\FinishedSubscriber as TestFinishedSubscriber;
use PHPUnit\Event\TestSuite\Finished as TestSuiteFinished;
use PHPUnit\Event\TestSuite\FinishedSubscriber as TestSuiteFinishedSubscriber;
use PHPUnit\Runner\Extension\Extension;
use PHPUnit\Runner\Extension\Facade;
use PHPUnit\Runner\Extension\ParameterCollection;
use PHPUnit\TextUI\Configuration\Configuration;

class KernelShutdownExtension implements Extension
{
public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
{
$facade->registerSubscribers(
new class() implements TestFinishedSubscriber {
public function notify(TestFinished $event): void
{
$test = $event->test();

\assert($test instanceof TestMethod);

KernelShutdownExtension::ensureKernelShutdown($test->className());
}
},
new class() implements TestSuiteFinishedSubscriber {
public function notify(TestSuiteFinished $event): void
{
$class = $event->testSuite()->name();

KernelShutdownExtension::ensureKernelShutdown($class);
}
}
);
}

public static function ensureKernelShutdown(string $class): void
{
if (!class_exists($class)) {
return;
}

$reflection = new \ReflectionClass($class);

if ($reflection->hasMethod('ensureKernelShutdown')) {
$method = $reflection->getMethod('ensureKernelShutdown');
$method->invoke(null);

$reflection->getProperty('kernel')->setValue(null);
}
}
}
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@
</source>
<extensions>
<bootstrap class="Draw\Component\Tester\PHPUnit\Extension\CarbonResetExtension"/>
<bootstrap class="Draw\Bundle\TesterBundle\PHPUnit\Extension\KernelShutdownExtension"/>
</extensions>
</phpunit>

0 comments on commit a226506

Please sign in to comment.