-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* develop: add WebTestCase for phpunit emulation
- Loading branch information
Showing
4 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
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,7 @@ | ||
# Changelog | ||
|
||
## 1.1.0 - 2023-09-02 | ||
|
||
### Added | ||
|
||
- `Innmind\BlackBox\Symfony\Bundle\FrameworkBundle\Test\WebTestCase` |
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
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
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,80 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Innmind\BlackBox\Symfony\Bundle\FrameworkBundle\Test; | ||
|
||
use Innmind\BlackBox\PHPUnit\Framework\TestCase; | ||
use Symfony\Bundle\FrameworkBundle\KernelBrowser; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
use Symfony\Contracts\Service\ResetInterface; | ||
use App\Kernel; | ||
|
||
abstract class WebTestCase extends TestCase | ||
{ | ||
protected static ?KernelInterface $kernel = null; | ||
protected static bool $booted = false; | ||
protected static ?KernelBrowser $_client = null; | ||
|
||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
self::ensureKernelShutdown(); | ||
} | ||
|
||
protected static function bootKernel(): KernelInterface | ||
{ | ||
self::ensureKernelShutdown(); | ||
|
||
$kernel = self::createKernel(); | ||
$kernel->boot(); | ||
self::$kernel = $kernel; | ||
self::$booted = true; | ||
|
||
return self::$kernel; | ||
} | ||
|
||
protected static function getContainer(): ContainerInterface | ||
{ | ||
$kernel = self::$kernel ?? self::bootKernel(); | ||
|
||
if (!self::$booted) { | ||
$kernel = self::bootKernel(); | ||
} | ||
|
||
/** @var ContainerInterface */ | ||
return $kernel->getContainer()->get('test.service_container'); | ||
} | ||
|
||
protected static function createKernel(): KernelInterface | ||
{ | ||
return new Kernel('test', true); | ||
} | ||
|
||
protected static function ensureKernelShutdown(): void | ||
{ | ||
if (null !== self::$kernel) { | ||
self::$kernel->boot(); | ||
$container = self::$kernel->getContainer(); | ||
self::$kernel->shutdown(); | ||
self::$booted = false; | ||
|
||
if ($container instanceof ResetInterface) { | ||
$container->reset(); | ||
} | ||
} | ||
} | ||
|
||
protected static function createClient(): KernelBrowser | ||
{ | ||
if (self::$_client) { | ||
return self::$_client; | ||
} | ||
|
||
$kernel = self::bootKernel(); | ||
/** @var KernelBrowser */ | ||
self::$_client = $kernel->getContainer()->get('test.client'); | ||
|
||
return self::$_client; | ||
} | ||
} |