1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TBolier\RethinkConnect\Test\Connection; |
5
|
|
|
|
6
|
|
|
use ArrayObject; |
7
|
|
|
use TBolier\RethinkQL\Response\Cursor; |
8
|
|
|
use TBolier\RethinkQL\Response\ResponseInterface; |
9
|
|
|
use TBolier\RethinkQL\IntegrationTest\BaseTestCase; |
10
|
|
|
|
11
|
|
|
class TableTest extends BaseTestCase |
12
|
|
|
{ |
13
|
|
|
public function setUp() |
14
|
|
|
{ |
15
|
|
|
parent::setUp(); |
16
|
|
|
|
17
|
|
|
if (!\in_array('tabletest', $this->r()->db()->tableList()->run()->getData(), true)) { |
|
|
|
|
18
|
|
|
$this->r()->db()->tableCreate('tabletest')->run(); |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function tearDown() |
23
|
|
|
{ |
24
|
|
|
if (\in_array('tabletest', $this->r()->db()->tableList()->run()->getData(), true)) { |
|
|
|
|
25
|
|
|
$this->r()->db()->tableDrop('tabletest')->run(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
parent::tearDown(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @throws \Exception |
33
|
|
|
*/ |
34
|
|
|
public function testEmptyTable() |
35
|
|
|
{ |
36
|
|
|
/** @var ResponseInterface $count */ |
37
|
|
|
$count = $this->r() |
38
|
|
|
->table('tabletest') |
39
|
|
|
->filter([ |
40
|
|
|
[ |
41
|
|
|
'title' => 'Test document', |
42
|
|
|
], |
43
|
|
|
]) |
44
|
|
|
->count() |
45
|
|
|
->run(); |
46
|
|
|
|
47
|
|
|
$this->assertInternalType('int', $count->getData()); |
48
|
|
|
|
49
|
|
|
$res = $this->r() |
50
|
|
|
->table('tabletest') |
51
|
|
|
->delete() |
52
|
|
|
->run(); |
53
|
|
|
|
54
|
|
|
$this->assertObStatus(['deleted' => $count->getData()], $res->getData()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @throws \Exception |
59
|
|
|
*/ |
60
|
|
|
public function testInsert() |
61
|
|
|
{ |
62
|
|
|
$res = $this->insertDocument(1); |
63
|
|
|
|
64
|
|
|
$this->assertObStatus(['inserted' => 1], $res->getData()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @throws \Exception |
69
|
|
|
*/ |
70
|
|
|
public function testMultipleInserts() |
71
|
|
|
{ |
72
|
|
|
$res = $this->r() |
73
|
|
|
->table('tabletest') |
74
|
|
|
->insert([ |
75
|
|
|
[ |
76
|
|
|
'documentId' => 1, |
77
|
|
|
'title' => 'Test document', |
78
|
|
|
'description' => 'My first document.', |
79
|
|
|
], |
80
|
|
|
[ |
81
|
|
|
'documentId' => 2, |
82
|
|
|
'title' => 'Test document', |
83
|
|
|
'description' => 'My first document.', |
84
|
|
|
], |
85
|
|
|
[ |
86
|
|
|
'documentId' => 3, |
87
|
|
|
'title' => 'Test document', |
88
|
|
|
'description' => 'My first document.', |
89
|
|
|
] |
90
|
|
|
]) |
91
|
|
|
->run(); |
92
|
|
|
|
93
|
|
|
$this->assertObStatus(['inserted' => 3], $res->getData()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @throws \Exception |
98
|
|
|
*/ |
99
|
|
|
public function testCount() |
100
|
|
|
{ |
101
|
|
|
$this->insertDocument(1); |
102
|
|
|
|
103
|
|
|
$res = $this->r() |
104
|
|
|
->table('tabletest') |
105
|
|
|
->count() |
106
|
|
|
->run(); |
107
|
|
|
|
108
|
|
|
$this->assertInternalType('int', $res->getData()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @throws \Exception |
113
|
|
|
*/ |
114
|
|
|
public function testFilter() |
115
|
|
|
{ |
116
|
|
|
$this->insertDocument(1); |
117
|
|
|
|
118
|
|
|
/** @var Cursor $cursor */ |
119
|
|
|
$cursor = $this->r() |
120
|
|
|
->table('tabletest') |
121
|
|
|
->filter([ |
122
|
|
|
[ |
123
|
|
|
'title' => 'Test document', |
124
|
|
|
], |
125
|
|
|
]) |
126
|
|
|
->run(); |
127
|
|
|
|
128
|
|
|
$this->assertInstanceOf(\Iterator::class, $cursor); |
129
|
|
|
$this->assertInternalType('array', $cursor->current()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @throws \Exception |
134
|
|
|
*/ |
135
|
|
|
public function testFilterCount() |
136
|
|
|
{ |
137
|
|
|
$this->insertDocument(1); |
138
|
|
|
$this->insertDocument(2); |
139
|
|
|
$this->insertDocument(3); |
140
|
|
|
$this->insertDocument(4); |
141
|
|
|
$this->insertDocument(5); |
142
|
|
|
|
143
|
|
|
/** @var ResponseInterface $res */ |
144
|
|
|
$res = $this->r() |
145
|
|
|
->table('tabletest') |
146
|
|
|
->filter([ |
147
|
|
|
[ |
148
|
|
|
'title' => 'Test document', |
149
|
|
|
], |
150
|
|
|
]) |
151
|
|
|
->count() |
152
|
|
|
->run(); |
153
|
|
|
|
154
|
|
|
$this->assertEquals(5, $res->getData()); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @throws \Exception |
159
|
|
|
*/ |
160
|
|
|
public function testUpdate() |
161
|
|
|
{ |
162
|
|
|
$this->insertDocument(1); |
163
|
|
|
|
164
|
|
|
$this->r() |
165
|
|
|
->table('tabletest') |
166
|
|
|
->insert([ |
167
|
|
|
[ |
168
|
|
|
'title' => 'Update document', |
169
|
|
|
], |
170
|
|
|
]) |
171
|
|
|
->run(); |
172
|
|
|
|
173
|
|
|
/** @var ResponseInterface $count */ |
174
|
|
|
$count = $this->r() |
175
|
|
|
->table('tabletest') |
176
|
|
|
->filter([ |
177
|
|
|
[ |
178
|
|
|
'title' => 'Update document', |
179
|
|
|
], |
180
|
|
|
]) |
181
|
|
|
->count() |
182
|
|
|
->run(); |
183
|
|
|
/** @var ResponseInterface $res */ |
184
|
|
|
$res = $this->r() |
185
|
|
|
->table('tabletest') |
186
|
|
|
->filter([ |
187
|
|
|
[ |
188
|
|
|
'title' => 'Update document', |
189
|
|
|
], |
190
|
|
|
]) |
191
|
|
|
->update([ |
192
|
|
|
[ |
193
|
|
|
'title' => 'Updated document', |
194
|
|
|
], |
195
|
|
|
]) |
196
|
|
|
->run(); |
197
|
|
|
|
198
|
|
|
$this->assertObStatus(['replaced' => $count->getData()], $res->getData()); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @throws \Exception |
203
|
|
|
*/ |
204
|
|
|
public function testDeleteDocument() |
205
|
|
|
{ |
206
|
|
|
$this->r() |
207
|
|
|
->table('tabletest') |
208
|
|
|
->insert([ |
209
|
|
|
[ |
210
|
|
|
'title' => 'Delete document', |
211
|
|
|
], |
212
|
|
|
]) |
213
|
|
|
->run(); |
214
|
|
|
|
215
|
|
|
/** @var ResponseInterface $count */ |
216
|
|
|
$count = $this->r() |
217
|
|
|
->table('tabletest') |
218
|
|
|
->filter([ |
219
|
|
|
[ |
220
|
|
|
'title' => 'Delete document', |
221
|
|
|
], |
222
|
|
|
]) |
223
|
|
|
->count() |
224
|
|
|
->run(); |
225
|
|
|
|
226
|
|
|
$this->assertInternalType('int', $count->getData()); |
227
|
|
|
|
228
|
|
|
/** @var ResponseInterface $res */ |
229
|
|
|
$res = $this->r() |
230
|
|
|
->table('tabletest') |
231
|
|
|
->filter([ |
232
|
|
|
[ |
233
|
|
|
'title' => 'Delete document', |
234
|
|
|
], |
235
|
|
|
]) |
236
|
|
|
->delete() |
237
|
|
|
->run(); |
238
|
|
|
|
239
|
|
|
$this->assertObStatus(['deleted' => $count->getData()], $res->getData()); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return void |
244
|
|
|
* @throws \Exception |
245
|
|
|
*/ |
246
|
|
|
public function testGet(): void |
247
|
|
|
{ |
248
|
|
|
$this->r() |
249
|
|
|
->table('tabletest') |
250
|
|
|
->insert([ |
251
|
|
|
[ |
252
|
|
|
'id' => 'foo', |
253
|
|
|
], |
254
|
|
|
]) |
255
|
|
|
->run(); |
256
|
|
|
|
257
|
|
|
/** @var ResponseInterface $res */ |
258
|
|
|
$res = $this->r() |
259
|
|
|
->table('tabletest') |
260
|
|
|
->get('foo') |
261
|
|
|
->run(); |
262
|
|
|
|
263
|
|
|
$this->assertEquals(['id' => 'foo'], $res->getData()); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @return void |
268
|
|
|
* @throws \Exception |
269
|
|
|
*/ |
270
|
|
|
public function testGetNonExistingDocument(): void |
271
|
|
|
{ |
272
|
|
|
/** @var ResponseInterface $res */ |
273
|
|
|
$res = $this->r() |
274
|
|
|
->table('tabletest') |
275
|
|
|
->get('bar') |
276
|
|
|
->run(); |
277
|
|
|
|
278
|
|
|
$this->assertEquals(null, $res->getData()); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @param $status |
283
|
|
|
* @param $data |
284
|
|
|
* @throws \Exception |
285
|
|
|
*/ |
286
|
|
|
protected function assertObStatus($status, $data) |
287
|
|
|
{ |
288
|
|
|
$res = []; |
289
|
|
|
$statuses = [ |
290
|
|
|
'unchanged', |
291
|
|
|
'skipped', |
292
|
|
|
'replaced', |
293
|
|
|
'inserted', |
294
|
|
|
'errors', |
295
|
|
|
'deleted', |
296
|
|
|
]; |
297
|
|
|
$data = new ArrayObject($data); |
298
|
|
|
|
299
|
|
|
foreach ($statuses as $s) { |
300
|
|
|
$status[$s] = $status[$s] ?? 0; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
$data->setFlags($data::ARRAY_AS_PROPS); |
304
|
|
|
|
305
|
|
|
foreach ($statuses as $s) { |
306
|
|
|
$res[$s] = $data[$s] ?? 0; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
$this->assertEquals($status, $res); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @param int $documentId |
314
|
|
|
* @return ResponseInterface |
315
|
|
|
*/ |
316
|
|
|
private function insertDocument(int $documentId): ResponseInterface |
317
|
|
|
{ |
318
|
|
|
$res = $this->r() |
319
|
|
|
->table('tabletest') |
320
|
|
|
->insert([ |
321
|
|
|
[ |
322
|
|
|
'documentId' => $documentId, |
323
|
|
|
'title' => 'Test document', |
324
|
|
|
'description' => 'My first document.', |
325
|
|
|
], |
326
|
|
|
]) |
327
|
|
|
->run(); |
328
|
|
|
|
329
|
|
|
return $res; |
|
|
|
|
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|