Skip to content

Commit d9ed545

Browse files
committed
test: cover transaction begin failure
1 parent 41536db commit d9ed545

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

tests/unit/SQLTransactionTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PDOException;
66
use PHPUnit\Framework\TestCase;
77
use Utopia\Database\Adapter\MySQL;
8+
use Utopia\Database\Exception\Transaction as TransactionException;
89

910
class SQLTransactionTest extends TestCase
1011
{
@@ -34,4 +35,26 @@ public function testStartTransactionRecoversFromDesyncedRollback(): void
3435
$this->assertTrue($adapter->startTransaction());
3536
$this->assertTrue($adapter->inTransaction());
3637
}
38+
39+
public function testStartTransactionDoesNotMaskBeginFailureAfterDesyncedRollback(): void
40+
{
41+
$pdo = $this->getMockBuilder(\PDO::class)
42+
->disableOriginalConstructor()
43+
->getMock();
44+
45+
$pdo->method('inTransaction')->willReturn(true);
46+
$pdo->method('rollBack')->willThrowException(
47+
new PDOException('There is no active transaction')
48+
);
49+
$pdo->expects($this->once())
50+
->method('beginTransaction')
51+
->willThrowException(new PDOException('Connection lost'));
52+
53+
$adapter = new MySQL($pdo);
54+
55+
$this->expectException(TransactionException::class);
56+
$this->expectExceptionMessage('Failed to start transaction: Connection lost');
57+
58+
$adapter->startTransaction();
59+
}
3760
}

0 commit comments

Comments
 (0)