-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TesterBundle] PHPUnit KernelShutdown extension
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
packages/tester-bundle/PHPUnit/Extension/KernelShutdownExtension.php
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
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); | ||
} | ||
} | ||
} |
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