Skip to content

Commit

Permalink
add WebTestCase for phpunit emulation
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Sep 2, 2023
1 parent 8b37158 commit 49816ae
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
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`
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,9 @@ return static function() {
);
}
```

### PHPUnit emulation

BlackBox is able to run PHPUnit tests and this extension allows to run functional tests. For this to work you only need to prefix the `Symfony\Bundle\FrameworkBundle\Test\WebTestCase` by `Innmind\BlackBox\`.

**Warning**: custom assertions provided by Symfony are not supported.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"require": {
"php": "~8.2",
"innmind/black-box": "~5.0",
"innmind/black-box": "~5.3",
"symfony/framework-bundle": "~6.0",
"symfony/browser-kit": "~6.0",
"symfony/http-foundation": "~6.0",
Expand Down
80 changes: 80 additions & 0 deletions src/Bundle/FrameworkBundle/Test/WebTestCase.php
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;
}
}

0 comments on commit 49816ae

Please sign in to comment.