Skip to content

Commit

Permalink
test: cover nested transactions
Browse files Browse the repository at this point in the history
_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.
  • Loading branch information
simPod committed Oct 19, 2024
1 parent 220aec4 commit 51d29d5
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions tests/Functional/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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();

Expand All @@ -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));
}
}

0 comments on commit 51d29d5

Please sign in to comment.