Repository navigation
Expand file tree
/
Copy pathSQLGetDocumentTest.php
More file actions
147 lines (126 loc) · 4.61 KB
/
Copy pathSQLGetDocumentTest.php
File metadata and controls
147 lines (126 loc) · 4.61 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace Tests\Unit;
use Exception;
use PDOException;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
use Utopia\Database\Adapter\MySQL;
use Utopia\Database\Adapter\Postgres;
use Utopia\Database\Document;
use Utopia\Database\Exception\Timeout as TimeoutException;
final class SQLGetDocumentTest extends TestCase
{
public function testTranslatesExecuteTimeoutClosesCursorAndPreservesOriginalWhenCleanupFails(): void
{
$exception = $this->createTimeoutException();
$statement = $this->getMockBuilder(\PDOStatement::class)
->disableOriginalConstructor()
->getMock();
$statement->expects($this->once())
->method('bindValue')
->with(':_uid', 'document')
->willReturn(true);
$statement->expects($this->once())
->method('execute')
->willThrowException($exception);
$statement->expects($this->never())
->method('fetchAll');
$statement->expects($this->once())
->method('closeCursor')
->willThrowException(new PDOException('Failed to close cursor'));
$this->assertTimeout($this->createMySQL($statement), $exception);
}
public function testTranslatesFetchTimeoutAndClosesCursor(): void
{
$exception = $this->createTimeoutException();
$statement = $this->getMockBuilder(\PDOStatement::class)
->disableOriginalConstructor()
->getMock();
$statement->expects($this->once())
->method('bindValue')
->with(':_uid', 'document')
->willReturn(true);
$statement->expects($this->once())
->method('execute')
->willReturn(true);
$statement->expects($this->once())
->method('fetchAll')
->willThrowException($exception);
$statement->expects($this->once())
->method('closeCursor')
->willReturn(true);
$this->assertTimeout($this->createMySQL($statement), $exception);
}
public function testUsesPostgresExecuteHook(): void
{
$statement = $this->getMockBuilder(\PDOStatement::class)
->disableOriginalConstructor()
->getMock();
$statement->expects($this->once())
->method('bindValue')
->with(':_uid', 'document')
->willReturn(true);
$statement->expects($this->once())
->method('execute')
->willReturn(true);
$statement->expects($this->once())
->method('fetchAll')
->willReturn([]);
$statement->expects($this->once())
->method('closeCursor')
->willReturn(true);
$pdo = $this->getMockBuilder(\PDO::class)
->disableOriginalConstructor()
->getMock();
$pdo->expects($this->once())
->method('prepare')
->willReturn($statement);
$pdo->expects($this->once())
->method('exec')
->with("SET statement_timeout = '25ms'")
->willReturn(0);
$adapter = new Postgres($pdo);
$adapter->setDatabase('database');
$adapter->setNamespace('namespace');
$adapter->setTimeout(25);
$document = $adapter->getDocument(
new Document(['$id' => 'collection']),
'document'
);
$this->assertSame([], $document->getArrayCopy());
}
private function assertTimeout(MySQL $adapter, PDOException $exception): void
{
try {
$adapter->getDocument(
new Document(['$id' => 'collection']),
'document'
);
} catch (TimeoutException $timeout) {
$this->assertSame($exception, $timeout->getPrevious());
return;
}
$this->fail('Expected a timeout exception.');
}
private function createMySQL(\PDOStatement $statement): MySQL
{
$pdo = $this->getMockBuilder(\PDO::class)
->disableOriginalConstructor()
->getMock();
$pdo->expects($this->once())
->method('prepare')
->willReturn($statement);
$adapter = new MySQL($pdo);
$adapter->setDatabase('database');
$adapter->setNamespace('namespace');
return $adapter;
}
private function createTimeoutException(): PDOException
{
$exception = new PDOException('Query execution was interrupted');
$code = new ReflectionProperty(Exception::class, 'code');
$code->setValue($exception, 'HY000');
$exception->errorInfo = ['HY000', 3024, 'Query execution was interrupted'];
return $exception;
}
}