From 6046a57c9fa9ed3661d32cea60e380f46a080240 Mon Sep 17 00:00:00 2001 From: David Maicher Date: Tue, 19 Mar 2024 08:15:13 +0100 Subject: [PATCH] make "use_savepoints = true" no-op with DBAL 4 (#1773) --- src/DependencyInjection/DoctrineExtension.php | 11 +++++++++-- .../AbstractDoctrineExtensionTest.php | 4 ++++ tests/DependencyInjection/DoctrineExtensionTest.php | 10 +++++++--- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/DependencyInjection/DoctrineExtension.php b/src/DependencyInjection/DoctrineExtension.php index db7455e0..161626a7 100644 --- a/src/DependencyInjection/DoctrineExtension.php +++ b/src/DependencyInjection/DoctrineExtension.php @@ -300,8 +300,15 @@ protected function loadDbalConnection($name, array $connection, ContainerBuilder $def->setClass($options['wrapperClass']); } - if (! empty($connection['use_savepoints'])) { - $def->addMethodCall('setNestTransactionsWithSavepoints', [$connection['use_savepoints']]); + if (isset($connection['use_savepoints'])) { + // DBAL >= 4 always has savepoints enabled. So we only need to call "setNestTransactionsWithSavepoints" for DBAL < 4 + if (method_exists(Connection::class, 'getEventManager')) { + if ($connection['use_savepoints']) { + $def->addMethodCall('setNestTransactionsWithSavepoints', [$connection['use_savepoints']]); + } + } elseif (! $connection['use_savepoints']) { + throw new LogicException('The "use_savepoints" option can only be set to "true" and should ideally not be set when using DBAL >= 4'); + } } $container->setDefinition( diff --git a/tests/DependencyInjection/AbstractDoctrineExtensionTest.php b/tests/DependencyInjection/AbstractDoctrineExtensionTest.php index fc46b8ff..58c0fcf3 100644 --- a/tests/DependencyInjection/AbstractDoctrineExtensionTest.php +++ b/tests/DependencyInjection/AbstractDoctrineExtensionTest.php @@ -240,6 +240,10 @@ public function testDbalLoadSinglePrimaryReplicaConnection(): void public function testDbalLoadSavepointsForNestedTransactions(): void { + if (!method_exists(Connection::class, 'getEventManager')) { + self::markTestSkipped('This test requires DBAL < 4'); + } + $container = $this->loadContainer('dbal_savepoints'); $calls = $container->getDefinition('doctrine.dbal.savepoints_connection')->getMethodCalls(); diff --git a/tests/DependencyInjection/DoctrineExtensionTest.php b/tests/DependencyInjection/DoctrineExtensionTest.php index c788fec9..1468a65d 100644 --- a/tests/DependencyInjection/DoctrineExtensionTest.php +++ b/tests/DependencyInjection/DoctrineExtensionTest.php @@ -554,10 +554,14 @@ public function testUseSavePointsAddMethodCallToAddSavepointsToTheConnection(): ], ], $container); + $isUsingDBAL3 = method_exists(Connection::class, 'getEventManager'); + $calls = $container->getDefinition('doctrine.dbal.default_connection')->getMethodCalls(); - $this->assertCount(1, $calls); - $this->assertEquals('setNestTransactionsWithSavepoints', $calls[0][0]); - $this->assertTrue($calls[0][1][0]); + $this->assertCount((int) $isUsingDBAL3, $calls); + if ($isUsingDBAL3) { + $this->assertEquals('setNestTransactionsWithSavepoints', $calls[0][0]); + $this->assertTrue($calls[0][1][0]); + } } public function testAutoGenerateProxyClasses(): void