Repository navigation
Expand file tree
/
Copy pathSQLTransactionTest.php
More file actions
81 lines (66 loc) · 2.72 KB
/
Copy pathSQLTransactionTest.php
File metadata and controls
81 lines (66 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace Tests\Unit;
use PDOException;
use PHPUnit\Framework\TestCase;
use Utopia\Database\Adapter\MySQL;
use Utopia\Database\Adapter\Postgres;
use Utopia\Database\Exception\Transaction as TransactionException;
class SQLTransactionTest extends TestCase
{
/**
* A pooled connection (e.g. Swoole PDOProxy) can keep its own transaction
* counter that survives a reconnect, so it reports an open transaction the
* underlying connection no longer holds. The cleanup rollBack() then throws
* "There is no active transaction". startTransaction() must swallow that and
* still begin a fresh transaction instead of surfacing it as a failure.
*/
public function testStartTransactionRecoversFromDesyncedRollback(): void
{
$pdo = $this->getMockBuilder(\PDO::class)
->disableOriginalConstructor()
->getMock();
$pdo->method('inTransaction')->willReturn(true);
$pdo->method('rollBack')->willThrowException(
new PDOException('There is no active transaction')
);
$pdo->expects($this->once())
->method('beginTransaction')
->willReturn(true);
$adapter = new MySQL($pdo);
$this->assertTrue($adapter->startTransaction());
$this->assertTrue($adapter->inTransaction());
}
public function testStartTransactionDoesNotMaskBeginFailureAfterDesyncedRollback(): void
{
$pdo = $this->getMockBuilder(\PDO::class)
->disableOriginalConstructor()
->getMock();
$pdo->method('inTransaction')->willReturn(true);
$pdo->method('rollBack')->willThrowException(
new PDOException('There is no active transaction')
);
$pdo->expects($this->once())
->method('beginTransaction')
->willThrowException(new PDOException('Connection lost'));
$adapter = new MySQL($pdo);
$this->expectException(TransactionException::class);
$this->expectExceptionMessage('Failed to start transaction: Connection lost');
$adapter->startTransaction();
}
public function testPostgresStartTransactionRecoversFromDesyncedRollback(): void
{
$pdo = $this->getMockBuilder(\PDO::class)
->disableOriginalConstructor()
->getMock();
$pdo->method('inTransaction')->willReturn(true);
$pdo->method('rollBack')->willThrowException(
new PDOException('There is no active transaction')
);
$pdo->expects($this->once())
->method('beginTransaction')
->willReturn(true);
$adapter = new Postgres($pdo);
$this->assertTrue($adapter->startTransaction());
$this->assertTrue($adapter->inTransaction());
}
}