From 51d29d5b9b8b8a31445c24233be81ccaa3f3de84 Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Mon, 14 Oct 2024 10:09:36 +0200 Subject: [PATCH] test: cover nested transactions _I managed to break this behaviour in other PR so this should be covered._ It tests basic walkthrough where we nest transactions, propagate the result through the transaction stack and also that it is committed and not rolled back. --- tests/Functional/TransactionTest.php | 41 ++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/tests/Functional/TransactionTest.php b/tests/Functional/TransactionTest.php index 693271f83ee..ec92545c459 100644 --- a/tests/Functional/TransactionTest.php +++ b/tests/Functional/TransactionTest.php @@ -7,7 +7,9 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Exception\ConnectionLost; use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; +use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; +use Doctrine\DBAL\Types\Types; use function func_get_args; use function restore_error_handler; @@ -18,15 +20,6 @@ class TransactionTest extends FunctionalTestCase { - protected function setUp(): void - { - if ($this->connection->getDatabasePlatform() instanceof AbstractMySQLPlatform) { - return; - } - - self::markTestSkipped('Restricted to MySQL.'); - } - public function testCommitFailure(): void { $this->expectConnectionLoss(static function (Connection $connection): void { @@ -43,6 +36,10 @@ public function testRollbackFailure(): void private function expectConnectionLoss(callable $scenario): void { + if (! $this->connection->getDatabasePlatform() instanceof AbstractMySQLPlatform) { + self::markTestSkipped('Restricted to MySQL.'); + } + $this->connection->executeStatement('SET SESSION wait_timeout=1'); $this->connection->beginTransaction(); @@ -67,4 +64,30 @@ private function expectConnectionLoss(callable $scenario): void restore_error_handler(); } } + + public function testNestedTransactionWalkthrough(): void + { + $table = new Table('storage'); + $table->addColumn('test_int', Types::INTEGER); + $table->setPrimaryKey(['test_int']); + + $this->dropAndCreateTable($table); + + $query = 'SELECT count(test_int) FROM storage'; + + self::assertSame('0', (string) $this->connection->fetchOne($query)); + + $result = $this->connection->transactional( + static fn (Connection $connection) => $connection->transactional( + static function (Connection $connection) use ($query) { + $connection->insert('storage', ['test_int' => 1]); + + return $connection->fetchOne($query); + }, + ), + ); + + self::assertSame('1', (string) $result); + self::assertSame('1', (string) $this->connection->fetchOne($query)); + } }