From 976f3f374e25806542d789dba2f813db31274d40 Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Thu, 31 Oct 2024 17:07:53 +0100 Subject: [PATCH] Cover how `transactional()` behaves in different auto commit modes (#6563) | Q | A |------------- | ----------- | Type | improvement | Fixed issues | #### Summary Covers the concern expressed by @greg0ire https://github.com/doctrine/dbal/pull/6545#issuecomment-2423739575 This ensures the transaction is (not)active based on `autocommmit` mode when interacting with `transactional()`. --- tests/ConnectionTest.php | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 16d9c8a51d..eb34a9bcaa 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -397,6 +397,46 @@ public function testConnectStartsTransactionInNoAutoCommitMode(): void self::assertTrue($conn->isTransactionActive()); } + public function testTransactionIsNotActiveAfterTransactionalInAutoCommitMode(): void + { + $driverMock = $this->createStub(Driver::class); + $driverMock + ->method('connect') + ->willReturn( + $this->createStub(DriverConnection::class), + ); + + $conn = new Connection([], $driverMock); + + $conn->setAutoCommit(true); + + self::assertFalse($conn->isTransactionActive()); + + $conn->transactional(static function (Connection $connection): void { + }); + + self::assertFalse($conn->isTransactionActive()); + } + + public function testTransactionIsActiveAfterTransactionalInNoAutoCommitMode(): void + { + $driverMock = $this->createMock(Driver::class); + $driverMock + ->method('connect') + ->willReturn( + $this->createStub(DriverConnection::class), + ); + + $conn = new Connection([], $driverMock); + + $conn->setAutoCommit(false); + + $conn->transactional(static function (Connection $connection): void { + }); + + self::assertTrue($conn->isTransactionActive()); + } + public function testCommitStartsTransactionInNoAutoCommitMode(): void { $driver = $this->createStub(Driver::class);