From 589e3eef41f4190aecbb8d8af66cba0a97fcb192 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 | 40 ++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/tests/Functional/TransactionTest.php b/tests/Functional/TransactionTest.php index 1421bd10a8a..77e2fd09cc9 100644 --- a/tests/Functional/TransactionTest.php +++ b/tests/Functional/TransactionTest.php @@ -2,26 +2,24 @@ namespace Doctrine\DBAL\Tests\Functional; +use Doctrine\DBAL\Connection; use Doctrine\DBAL\Driver\Exception as DriverException; use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; +use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Tests\FunctionalTestCase; +use Doctrine\DBAL\Types\Types; use PDOException; use function sleep; class TransactionTest extends FunctionalTestCase { - protected function setUp(): void + public function testCommitFalse(): void { - if ($this->connection->getDatabasePlatform() instanceof AbstractMySQLPlatform) { - return; + if (! $this->connection->getDatabasePlatform() instanceof AbstractMySQLPlatform) { + $this->markTestSkipped('Restricted to MySQL.'); } - $this->markTestSkipped('Restricted to MySQL.'); - } - - public function testCommitFalse(): void - { $this->connection->executeStatement('SET SESSION wait_timeout=1'); self::assertTrue($this->connection->beginTransaction()); @@ -40,4 +38,30 @@ public function testCommitFalse(): void $this->connection->close(); } } + + 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)); + } }