From e6840ba8b1618aa76b0e5ccbdc71654fbb376e2a Mon Sep 17 00:00:00 2001 From: David Maicher Date: Thu, 8 Jun 2023 16:35:27 +0200 Subject: [PATCH] make it mandatory to enable savepoints for nested transactions --- .../DoctrineTestCompilerPass.php | 15 +++++++++++++++ .../DoctrineTestCompilerPassTest.php | 6 ++++++ 2 files changed, 21 insertions(+) diff --git a/src/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPass.php b/src/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPass.php index f781855..5b5c261 100644 --- a/src/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPass.php +++ b/src/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPass.php @@ -62,6 +62,10 @@ private function modifyConnectionService(ContainerBuilder $container, string $na { $connectionDefinition = $container->getDefinition(sprintf('doctrine.dbal.%s_connection', $name)); + if (!$this->hasSavepointsEnabled($connectionDefinition)) { + throw new \LogicException(sprintf('This bundle relies on savepoints for nested database transactions. You need to enable "use_savepoints" on the Doctrine DBAL config for connection "%s".', $name)); + } + $connectionDefinition->replaceArgument( 0, $this->getModifiedConnectionOptions($connectionDefinition->getArgument(0), $name), @@ -174,4 +178,15 @@ private function validateConnectionNames(array $configNames, array $existingName throw new \InvalidArgumentException(sprintf('Unknown doctrine dbal connection name(s): %s.', implode(', ', $unknown))); } } + + private function hasSavepointsEnabled(Definition $connectionDefinition): bool + { + foreach ($connectionDefinition->getMethodCalls() as $call) { + if ($call[0] === 'setNestTransactionsWithSavepoints' && $call[1]) { + return true; + } + } + + return false; + } } diff --git a/tests/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPassTest.php b/tests/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPassTest.php index bfcd7a9..943fb26 100644 --- a/tests/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPassTest.php +++ b/tests/DAMA/DoctrineTestBundle/DependencyInjection/DoctrineTestCompilerPassTest.php @@ -51,6 +51,12 @@ public function testProcess(array $config, callable $assertCallback, callable $e ], ]])); + foreach (['a', 'b', 'c'] as $name) { + $containerBuilder->getDefinition(sprintf('doctrine.dbal.%s_connection', $name)) + ->addMethodCall('setNestTransactionsWithSavepoints', [true]) + ; + } + $containerBuilder->setDefinition( 'doctrine.dbal.a_connection.configuration', (new Definition(Configuration::class))