diff --git a/src/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPass.php b/src/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPass.php index 52e8bd4..c1efbf1 100644 --- a/src/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPass.php +++ b/src/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPass.php @@ -48,11 +48,7 @@ public function process(ContainerBuilder $container): void if ($enableStaticConnectionsConfig === true || isset($enableStaticConnectionsConfig[$name]) && $enableStaticConnectionsConfig[$name] === true ) { - $connectionDefinition = $container->getDefinition(sprintf('doctrine.dbal.%s_connection', $name)); - $connectionOptions = $connectionDefinition->getArgument(0); - $connectionOptions['dama.connection_name'] = $name; - $connectionOptions['dama.keep_static'] = true; - $connectionDefinition->replaceArgument(0, $connectionOptions); + $this->addConnectionOptions($container, $name); } foreach ($cacheNames as $cacheName) { @@ -75,4 +71,12 @@ private function validateConnectionNames(array $configNames, array $existingName throw new \InvalidArgumentException(sprintf('Unknown doctrine dbal connection name(s): %s.', implode(', ', $unknown))); } } + + private function addConnectionOptions(ContainerBuilder $container, string $name): void + { + $connectionDefinition = $container->getDefinition(sprintf('doctrine.dbal.%s_connection', $name)); + $connectionOptions = $connectionDefinition->getArgument(0); + $connectionOptions['dama.keep_static'] = true; + $connectionDefinition->replaceArgument(0, $connectionOptions); + } } diff --git a/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriver.php b/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriver.php index 26ff383..c1efcd5 100644 --- a/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriver.php +++ b/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriver.php @@ -45,10 +45,7 @@ public function __construct(Driver $underlyingDriver, AbstractPlatform $platform public function connect(array $params, $username = null, $password = null, array $driverOptions = []): Connection { if (self::$keepStaticConnections) { - if (!isset($params['dama.connection_name'])) { - throw new \InvalidArgumentException('Did not find key "dama.connection_name" inside connection params.'); - } - $key = $params['dama.connection_name']; + $key = sha1(serialize($params).$username.$password); if (!isset(self::$connections[$key])) { self::$connections[$key] = $this->underlyingDriver->connect($params, $username, $password, $driverOptions); diff --git a/tests/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPassTest.php b/tests/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPassTest.php index 5378f1b..dced2de 100644 --- a/tests/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPassTest.php +++ b/tests/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPassTest.php @@ -72,7 +72,6 @@ function (ContainerBuilder $containerBuilder): void { } $this->assertSame([ - 'dama.connection_name' => 'a', 'dama.keep_static' => true, ], $containerBuilder->getDefinition('doctrine.dbal.a_connection')->getArgument(0)); }, @@ -103,14 +102,12 @@ function (ContainerBuilder $containerBuilder): void { $this->assertTrue($containerBuilder->hasDefinition('dama.doctrine.dbal.connection_factory')); $this->assertSame([ - 'dama.connection_name' => 'a', 'dama.keep_static' => true, ], $containerBuilder->getDefinition('doctrine.dbal.a_connection')->getArgument(0)); $this->assertSame([], $containerBuilder->getDefinition('doctrine.dbal.b_connection')->getArgument(0)); $this->assertSame([ - 'dama.connection_name' => 'c', 'dama.keep_static' => true, ], $containerBuilder->getDefinition('doctrine.dbal.c_connection')->getArgument(0)); },