|
4 | 4 |
|
5 | 5 | use PDOException; |
6 | 6 | use PHPUnit\Framework\TestCase; |
| 7 | +use ReflectionMethod; |
7 | 8 | use Utopia\Database\Adapter\MySQL; |
8 | 9 | use Utopia\Database\Adapter\Postgres; |
| 10 | +use Utopia\Database\Adapter\SQL; |
9 | 11 | use Utopia\Database\Exception\Transaction as TransactionException; |
10 | 12 |
|
11 | | -class SQLTransactionTest extends TestCase |
| 13 | +final class SQLTransactionTest extends TestCase |
12 | 14 | { |
13 | 15 | /** |
14 | 16 | * A pooled connection (e.g. Swoole PDOProxy) can keep its own transaction |
@@ -59,8 +61,36 @@ public function testStartTransactionDoesNotMaskBeginFailureAfterDesyncedRollback |
59 | 61 | $adapter->startTransaction(); |
60 | 62 | } |
61 | 63 |
|
| 64 | + public function testStartTransactionDoesNotMaskFalseBeginResult(): void |
| 65 | + { |
| 66 | + $pdo = $this->getMockBuilder(\PDO::class) |
| 67 | + ->disableOriginalConstructor() |
| 68 | + ->getMock(); |
| 69 | + |
| 70 | + $pdo->method('inTransaction')->willReturn(false); |
| 71 | + $pdo->expects($this->once()) |
| 72 | + ->method('beginTransaction') |
| 73 | + ->willReturn(false); |
| 74 | + |
| 75 | + $cleanup = $this->getMockBuilder(\PDOStatement::class) |
| 76 | + ->disableOriginalConstructor() |
| 77 | + ->getMock(); |
| 78 | + $cleanup->method('execute')->willReturn(true); |
| 79 | + $pdo->method('prepare')->with('ROLLBACK')->willReturn($cleanup); |
| 80 | + |
| 81 | + $adapter = new MySQL($pdo); |
| 82 | + |
| 83 | + $this->expectException(TransactionException::class); |
| 84 | + $this->expectExceptionMessage('Failed to start transaction'); |
| 85 | + |
| 86 | + $adapter->startTransaction(); |
| 87 | + } |
| 88 | + |
62 | 89 | public function testPostgresStartTransactionRecoversFromDesyncedRollback(): void |
63 | 90 | { |
| 91 | + $method = new ReflectionMethod(Postgres::class, 'startTransaction'); |
| 92 | + $this->assertSame(SQL::class, $method->getDeclaringClass()->getName()); |
| 93 | + |
64 | 94 | $pdo = $this->getMockBuilder(\PDO::class) |
65 | 95 | ->disableOriginalConstructor() |
66 | 96 | ->getMock(); |
|
0 commit comments