Skip to content

Commit 55f2689

Browse files
authored
fix: edge case with Doctrine Middleware & early kernel boot (#993)
1 parent 2bffed8 commit 55f2689

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

src/ZenstruckFoundryBundle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ final class ZenstruckFoundryBundle extends AbstractBundle implements CompilerPas
3737
{
3838
public function boot(): void
3939
{
40-
if ($this->container && !Configuration::isBooted()) {
40+
if ($this->container) {
4141
Configuration::boot($this->container->get('.zenstruck_foundry.configuration')); // @phpstan-ignore argument.type
4242
}
4343
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the zenstruck/foundry package.
7+
*
8+
* (c) Kevin Bond <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Zenstruck\Foundry\Tests\Fixture\ResetDatabase;
15+
16+
use Doctrine\DBAL\Driver;
17+
use Doctrine\DBAL\Driver\Middleware;
18+
use Symfony\Component\HttpKernel\KernelInterface;
19+
20+
final class DoctrineMiddleware implements Middleware
21+
{
22+
public function __construct(
23+
private KernelInterface $kernel, // @phpstan-ignore property.onlyWritten
24+
) {
25+
}
26+
27+
public function wrap(Driver $driver): Driver
28+
{
29+
return $driver;
30+
}
31+
}

tests/Fixture/ResetDatabase/ResetDatabaseTestKernel.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
6464

6565
$c->register(GlobalInvokableService::class);
6666

67+
$c->register(DoctrineMiddleware::class)
68+
->setAutowired(true)
69+
->setAutoconfigured(true);
70+
6771
if (self::hasORM()) {
6872
$c->register(OrmResetterDecorator::class)->setAutowired(true)->setAutoconfigured(true);
6973
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the zenstruck/foundry package.
7+
*
8+
* (c) Kevin Bond <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Zenstruck\Foundry\Tests\Integration\ResetDatabase;
15+
16+
use Doctrine\DBAL\Connection;
17+
use Doctrine\DBAL\Driver\Middleware;
18+
use PHPUnit\Framework\Attributes\BeforeClass;
19+
use PHPUnit\Framework\Attributes\RequiresPhpunit;
20+
use PHPUnit\Framework\Attributes\Test;
21+
use Zenstruck\Foundry\Test\ResetDatabase;
22+
use Zenstruck\Foundry\Tests\Fixture\ResetDatabase\DoctrineMiddleware;
23+
24+
/**
25+
* @requires PHPUnit >=11.3
26+
*/
27+
#[RequiresPhpunit('>=11.3')]
28+
final class EarlyBootedKernelTest extends ResetDatabaseTestCase
29+
{
30+
/**
31+
* Needs to happen before {@see ResetDatabase::_resetDatabaseBeforeFirstTest()}.
32+
*/
33+
#[BeforeClass(10)]
34+
public static function before(): void
35+
{
36+
self::bootKernel();
37+
}
38+
39+
#[Test]
40+
public function connection_uses_doctrine_middleware(): void
41+
{
42+
/** @var Connection $connection */
43+
$connection = self::getContainer()->get(Connection::class);
44+
45+
self::assertContains(
46+
DoctrineMiddleware::class,
47+
\array_map(static fn(Middleware $middleware) => $middleware::class, $connection->getConfiguration()->getMiddlewares())
48+
);
49+
}
50+
}

0 commit comments

Comments
 (0)