Skip to content

Commit

Permalink
long running process doctrine connection listener
Browse files Browse the repository at this point in the history
  • Loading branch information
alli83 committed Mar 12, 2024
1 parent 7150c1d commit cfda672
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
4 changes: 4 additions & 0 deletions DependencyInjection/DoctrineExtension.php
Expand Up @@ -537,6 +537,10 @@ protected function ormLoad(array $config, ContainerBuilder $container)
$container->removeDefinition('doctrine.mapping_import_command');
}

if (!$container->hasParameter('kernel.runtime_mode') || !$container->hasParameter('kernel.runtime_mode.worker')) {

Check failure on line 540 in DependencyInjection/DoctrineExtension.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Expected 1 space after NOT operator; 0 found

Check failure on line 540 in DependencyInjection/DoctrineExtension.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Expected 1 space after NOT operator; 0 found
$container->removeDefinition('doctrine.dbal.connection_middleware');
}

$entityManagers = [];
foreach (array_keys($config['entity_managers']) as $name) {
/** @psalm-suppress InvalidArrayOffset */
Expand Down
5 changes: 5 additions & 0 deletions Resources/config/middlewares.xml
Expand Up @@ -17,5 +17,10 @@
<argument type="service" id="doctrine.debug_data_holder" />
<argument type="service" id="debug.stopwatch" on-invalid="null" />
</service>
<service id="doctrine.dbal.connection_middleware" class="Symfony\Bridge\Doctrine\Middleware\DoctrineConnectionMiddleware">
<argument type="service" id="doctrine" />
<argument type="service" id="service_container" />
<tag name="doctrine.middleware" />
</service>
</services>
</container>
119 changes: 119 additions & 0 deletions Tests/DoctrineConnectionMiddlewareTest.php
@@ -0,0 +1,119 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\DoctrineBundle\Tests;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Exception as DBALException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Result;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Middleware\DoctrineConnectionDriver;
use Symfony\Component\DependencyInjection\Container;

/**
* Based on https://github.com/Baldinof/roadrunner-bundle/blob/3.x/src/Integration/Doctrine/DoctrineORMMiddleware.php
*/
class DoctrineConnectionMiddlewareTest extends TestCase
{
public const CONNECTION_NAME = 'doctrine.connection';
public const MANAGER_NAME = 'doctrine.manager';

private $managerRegistryMock;

Check failure on line 26 in Tests/DoctrineConnectionMiddlewareTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Property \Doctrine\Bundle\DoctrineBundle\Tests\DoctrineConnectionMiddlewareTest::$managerRegistryMock does not have native type hint nor @var annotation for its value.
private $connectionMock;

Check failure on line 27 in Tests/DoctrineConnectionMiddlewareTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Property \Doctrine\Bundle\DoctrineBundle\Tests\DoctrineConnectionMiddlewareTest::$connectionMock does not have native type hint nor @var annotation for its value.
private Container $container;
private $driver;

Check failure on line 29 in Tests/DoctrineConnectionMiddlewareTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Property \Doctrine\Bundle\DoctrineBundle\Tests\DoctrineConnectionMiddlewareTest::$driver does not have native type hint nor @var annotation for its value.
private DoctrineConnectionDriver $doctrineConnectionDriver;

Check failure on line 30 in Tests/DoctrineConnectionMiddlewareTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm

UndefinedClass

Tests/DoctrineConnectionMiddlewareTest.php:30:13: UndefinedClass: Class, interface or enum named Symfony\Bridge\Doctrine\Middleware\DoctrineConnectionDriver does not exist (see https://psalm.dev/019)

public function setUp(): void
{
$platform = $this->createMock(AbstractPlatform::class);
$platform->method('getDummySelectSQL')->willReturn('SELECT 1');

$this->managerRegistryMock = $this->createMock(ManagerRegistry::class);
$this->connectionMock = $this->createMock(Connection::class);

Check failure on line 38 in Tests/DoctrineConnectionMiddlewareTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space
$this->connectionMock->method('getDatabasePlatform')->willReturn($platform);
$connectionDriverMock = $this->createMock(Driver\Connection::class);

$this->container = new Container();
$this->container->set(self::CONNECTION_NAME, $this->connectionMock);

$this->managerRegistryMock->method('getConnectionNames')->willReturn([self::CONNECTION_NAME]);
$this->managerRegistryMock->method('getManagerNames')->willReturn([self::MANAGER_NAME]);

$this->driver = $this->createMock(Driver::class);
$this->driver->method('connect')->willReturn($connectionDriverMock);

$this->doctrineConnectionDriver = new DoctrineConnectionDriver($this->driver, $this->managerRegistryMock, $this->container);

Check failure on line 51 in Tests/DoctrineConnectionMiddlewareTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm

UndefinedClass

Tests/DoctrineConnectionMiddlewareTest.php:51:47: UndefinedClass: Class, interface or enum named Symfony\Bridge\Doctrine\Middleware\DoctrineConnectionDriver does not exist (see https://psalm.dev/019)
}

public function testSkipNotInitializedConnections()
{
$this->container->set(self::CONNECTION_NAME, null);

$this->connectionMock->expects($this->never())->method('isConnected');
$this->connectionMock->expects($this->never())->method('executeQuery');
$this->connectionMock->expects($this->never())->method('close');
$this->connectionMock->expects($this->never())->method('connect');

$this->doctrineConnectionDriver->connect([]);

Check failure on line 63 in Tests/DoctrineConnectionMiddlewareTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm

UndefinedClass

Tests/DoctrineConnectionMiddlewareTest.php:63:9: UndefinedClass: Class, interface or enum named Symfony\Bridge\Doctrine\Middleware\DoctrineConnectionDriver does not exist (see https://psalm.dev/019)
}

public function testSkipWhenNotConnected(): void
{
$this->connectionMock->method('isConnected')->willReturn(false);
$this->connectionMock->expects($this->never())->method('executeQuery');
$this->connectionMock->expects($this->never())->method('close');
$this->connectionMock->expects($this->never())->method('connect');

$this->doctrineConnectionDriver->connect([]);

Check failure on line 73 in Tests/DoctrineConnectionMiddlewareTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm

UndefinedClass

Tests/DoctrineConnectionMiddlewareTest.php:73:9: UndefinedClass: Class, interface or enum named Symfony\Bridge\Doctrine\Middleware\DoctrineConnectionDriver does not exist (see https://psalm.dev/019)
}

public function testItClosesNotPingableConnection(): void
{
$this->connectionMock->expects($this->exactly(2))->method('executeQuery')
->willReturnCallback(function () {
static $counter = 0;

if (1 === ++$counter) {
throw $this->createMock(DBALException::class);
}

return $this->createMock(Result::class);
});

$this->connectionMock->method('isConnected')->willReturn(true);
$this->connectionMock->expects($this->once())->method('close');
$this->connectionMock->expects($this->never())->method('connect');

$this->doctrineConnectionDriver->connect([]);

Check failure on line 93 in Tests/DoctrineConnectionMiddlewareTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm

UndefinedClass

Tests/DoctrineConnectionMiddlewareTest.php:93:9: UndefinedClass: Class, interface or enum named Symfony\Bridge\Doctrine\Middleware\DoctrineConnectionDriver does not exist (see https://psalm.dev/019)
}

public function testItDoesNotClosePingableConnection(): void
{
$this->connectionMock->expects($this->once())->method('executeQuery');
$this->connectionMock->method('isConnected')->willReturn(true);
$this->connectionMock->expects($this->never())->method('close');
$this->connectionMock->expects($this->never())->method('connect');

$this->doctrineConnectionDriver->connect([]);

Check failure on line 103 in Tests/DoctrineConnectionMiddlewareTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm

UndefinedClass

Tests/DoctrineConnectionMiddlewareTest.php:103:9: UndefinedClass: Class, interface or enum named Symfony\Bridge\Doctrine\Middleware\DoctrineConnectionDriver does not exist (see https://psalm.dev/019)
}

public function testItForcesRebootOnClosedManagerWhenMissingProxySupport()
{
$manager = $this->createMock(EntityManagerInterface::class);
$this->container->set(self::MANAGER_NAME, $manager);

$manager->expects($this->once())->method('isOpen')->willReturn(false);
$this->managerRegistryMock->expects($this->once())
->method('resetManager')
->with(self::MANAGER_NAME)
;

Check failure on line 115 in Tests/DoctrineConnectionMiddlewareTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Space found before semicolon; expected ");" but found ")\n ;"

$this->doctrineConnectionDriver->connect([]);

Check failure on line 117 in Tests/DoctrineConnectionMiddlewareTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm

UndefinedClass

Tests/DoctrineConnectionMiddlewareTest.php:117:9: UndefinedClass: Class, interface or enum named Symfony\Bridge\Doctrine\Middleware\DoctrineConnectionDriver does not exist (see https://psalm.dev/019)
}
}

0 comments on commit cfda672

Please sign in to comment.