1
|
|
|
<?php |
2
|
|
|
namespace Foolz\SphinxQL\Tests\Drivers; |
3
|
|
|
|
4
|
|
|
use Foolz\SphinxQL\Drivers\ConnectionBase; |
5
|
|
|
use Foolz\Sphinxql\Drivers\ResultSetInterface; |
6
|
|
|
use Foolz\SphinxQL\Exception\ConnectionException; |
7
|
|
|
use Foolz\SphinxQL\Exception\DatabaseException; |
8
|
|
|
use Foolz\SphinxQL\Exception\ResultSetException; |
9
|
|
|
use Foolz\SphinxQL\Exception\SphinxQLException; |
10
|
|
|
use Foolz\SphinxQL\SphinxQL; |
11
|
|
|
use Foolz\SphinxQL\Tests\TestUtil; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
class ResultSetTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ConnectionBase $conn |
20
|
|
|
*/ |
21
|
|
|
public static $connection; |
22
|
|
|
|
23
|
|
|
public static $DATA = [ |
24
|
|
|
0 => [ |
25
|
|
|
'id' => '10', |
26
|
|
|
'gid' => '9003', |
27
|
|
|
'title' => 'modifying the same line again', |
28
|
|
|
'content' => 'because i am that lazy', |
29
|
|
|
], |
30
|
|
|
1 => [ |
31
|
|
|
'id' => '11', |
32
|
|
|
'gid' => '201', |
33
|
|
|
'title' => 'replacing value by value', |
34
|
|
|
'content' => 'i have no idea who would use this directly', |
35
|
|
|
], |
36
|
|
|
2 => [ |
37
|
|
|
'id' => '12', |
38
|
|
|
'gid' => '200', |
39
|
|
|
'title' => 'simple logic', |
40
|
|
|
'content' => 'inside the box there was the content', |
41
|
|
|
], |
42
|
|
|
3 => [ |
43
|
|
|
'id' => '13', |
44
|
|
|
'gid' => '304', |
45
|
|
|
'title' => 'i am getting bored', |
46
|
|
|
'content' => 'with all this CONTENT', |
47
|
|
|
], |
48
|
|
|
4 => [ |
49
|
|
|
'id' => '14', |
50
|
|
|
'gid' => '304', |
51
|
|
|
'title' => 'i want a vacation', |
52
|
|
|
'content' => 'the code is going to break sometime', |
53
|
|
|
], |
54
|
|
|
5 => [ |
55
|
|
|
'id' => '15', |
56
|
|
|
'gid' => '304', |
57
|
|
|
'title' => 'there\'s no hope in this class', |
58
|
|
|
'content' => 'just give up', |
59
|
|
|
], |
60
|
|
|
6 => [ |
61
|
|
|
'id' => '16', |
62
|
|
|
'gid' => '500', |
63
|
|
|
'title' => 'we need to test', |
64
|
|
|
'content' => 'selecting the best result in groups', |
65
|
|
|
], |
66
|
|
|
7 => [ |
67
|
|
|
'id' => '17', |
68
|
|
|
'gid' => '500', |
69
|
|
|
'title' => 'what is there to do', |
70
|
|
|
'content' => 'we need to create dummy data for tests', |
71
|
|
|
], |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @throws ConnectionException |
76
|
|
|
* @throws DatabaseException |
77
|
|
|
*/ |
78
|
|
|
public static function setUpBeforeClass(): void |
79
|
|
|
{ |
80
|
|
|
$conn = TestUtil::getConnectionDriver(); |
81
|
|
|
$conn->setParam('port', 9307); |
82
|
|
|
self::$connection = $conn; |
83
|
|
|
|
84
|
|
|
(new SphinxQL(self::$connection))->getConnection()->query('TRUNCATE RTINDEX rt'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return SphinxQL |
89
|
|
|
*/ |
90
|
|
|
protected function createSphinxQL(): SphinxQL |
91
|
|
|
{ |
92
|
|
|
return new SphinxQL(self::$connection); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @throws ConnectionException |
97
|
|
|
* @throws DatabaseException |
98
|
|
|
* @throws SphinxQLException |
99
|
|
|
*/ |
100
|
|
|
public function refill(): void |
101
|
|
|
{ |
102
|
|
|
$this->createSphinxQL()->getConnection()->query('TRUNCATE RTINDEX rt'); |
103
|
|
|
|
104
|
|
|
$sq = $this->createSphinxQL() |
105
|
|
|
->insert() |
106
|
|
|
->into('rt') |
107
|
|
|
->columns('id', 'gid', 'title', 'content'); |
108
|
|
|
|
109
|
|
|
foreach (static::$DATA as $row) { |
110
|
|
|
$sq->values($row['id'], $row['gid'], $row['title'], $row['content']); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$sq->execute(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @throws ConnectionException |
118
|
|
|
* @throws DatabaseException |
119
|
|
|
*/ |
120
|
|
|
public function testIsResultSet(): void |
121
|
|
|
{ |
122
|
|
|
$res = self::$connection->query('SELECT * FROM rt'); |
123
|
|
|
$this->assertInstanceOf(ResultSetInterface::class, $res); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @throws ConnectionException |
128
|
|
|
* @throws DatabaseException |
129
|
|
|
* @throws SphinxQLException |
130
|
|
|
*/ |
131
|
|
|
public function testStore(): void |
132
|
|
|
{ |
133
|
|
|
$this->refill(); |
134
|
|
|
$res = self::$connection->query('SELECT * FROM rt'); |
135
|
|
|
$res->store()->store(); |
136
|
|
|
$this->assertCount(8, $res->fetchAllNum()); |
137
|
|
|
|
138
|
|
|
$res = self::$connection->query('UPDATE rt SET gid = 202 WHERE gid < 202'); |
139
|
|
|
$this->assertEquals(2, $res->store()->getAffectedRows()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @throws ConnectionException |
144
|
|
|
* @throws DatabaseException |
145
|
|
|
* @throws SphinxQLException |
146
|
|
|
*/ |
147
|
|
|
public function testHasRow(): void |
148
|
|
|
{ |
149
|
|
|
$this->refill(); |
150
|
|
|
$res = self::$connection->query('SELECT * FROM rt'); |
151
|
|
|
$this->assertTrue($res->hasRow(2)); |
152
|
|
|
$this->assertTrue(isset($res[2])); |
153
|
|
|
$this->assertFalse($res->hasRow(1000)); |
154
|
|
|
$this->assertFalse(isset($res[1000])); |
155
|
|
|
$res->freeResult(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @throws ConnectionException |
160
|
|
|
* @throws DatabaseException |
161
|
|
|
* @throws ResultSetException |
162
|
|
|
* @throws SphinxQLException |
163
|
|
|
*/ |
164
|
|
|
public function testToRow(): void |
165
|
|
|
{ |
166
|
|
|
$this->refill(); |
167
|
|
|
$res = self::$connection->query('SELECT * FROM rt'); |
168
|
|
|
$res->toRow(2); |
169
|
|
|
$row = $res->fetchAssoc(); |
170
|
|
|
$this->assertEquals(12, $row['id']); |
171
|
|
|
$res->freeResult(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @throws ConnectionException |
176
|
|
|
* @throws DatabaseException |
177
|
|
|
* @throws ResultSetException |
178
|
|
|
* @throws SphinxQLException |
179
|
|
|
*/ |
180
|
|
|
public function testToRowThrows(): void |
181
|
|
|
{ |
182
|
|
|
$this->expectException(ResultSetException::class); |
183
|
|
|
$this->expectExceptionMessage('The row does not exist.'); |
184
|
|
|
|
185
|
|
|
$this->refill(); |
186
|
|
|
$res = self::$connection->query('SELECT * FROM rt'); |
187
|
|
|
$res->toRow(8); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @throws ConnectionException |
192
|
|
|
* @throws DatabaseException |
193
|
|
|
* @throws SphinxQLException |
194
|
|
|
*/ |
195
|
|
|
public function testHasNextRow(): void |
196
|
|
|
{ |
197
|
|
|
$this->refill(); |
198
|
|
|
$res = self::$connection->query('SELECT * FROM rt'); |
199
|
|
|
$this->assertTrue($res->hasNextRow()); |
200
|
|
|
$res->freeResult(); |
201
|
|
|
$res = self::$connection->query('SELECT * FROM rt WHERE id = 9000'); |
202
|
|
|
$this->assertFalse($res->hasNextRow()); |
203
|
|
|
$res->freeResult(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @throws ConnectionException |
208
|
|
|
* @throws DatabaseException |
209
|
|
|
* @throws ResultSetException |
210
|
|
|
* @throws SphinxQLException |
211
|
|
|
*/ |
212
|
|
|
public function testToNextRow(): void |
213
|
|
|
{ |
214
|
|
|
$this->refill(); |
215
|
|
|
$res = self::$connection->query('SELECT * FROM rt'); |
216
|
|
|
$res->toNextRow()->toNextRow()->toNextRow(); |
217
|
|
|
$row = $res->fetchAssoc(); |
218
|
|
|
$this->assertEquals(13, $row['id']); |
219
|
|
|
$res->freeResult(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @throws ConnectionException |
224
|
|
|
* @throws DatabaseException |
225
|
|
|
* @throws ResultSetException |
226
|
|
|
* @throws SphinxQLException |
227
|
|
|
*/ |
228
|
|
|
public function testToNextRowThrows(): void |
229
|
|
|
{ |
230
|
|
|
$this->expectException(ResultSetException::class); |
231
|
|
|
$this->expectExceptionMessage('The row does not exist.'); |
232
|
|
|
|
233
|
|
|
$this->refill(); |
234
|
|
|
$res = self::$connection->query('SELECT * FROM rt WHERE id = 10'); |
235
|
|
|
$res->toNextRow()->toNextRow(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @throws ConnectionException |
240
|
|
|
* @throws DatabaseException |
241
|
|
|
* @throws SphinxQLException |
242
|
|
|
*/ |
243
|
|
|
public function testCount(): void |
244
|
|
|
{ |
245
|
|
|
$this->refill(); |
246
|
|
|
$res = self::$connection->query('SELECT * FROM rt'); |
247
|
|
|
$this->assertEquals(8, $res->count()); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @throws ConnectionException |
252
|
|
|
* @throws DatabaseException |
253
|
|
|
* @throws SphinxQLException |
254
|
|
|
*/ |
255
|
|
|
public function testFetchAllAssoc(): void |
256
|
|
|
{ |
257
|
|
|
$expect = array( |
258
|
|
|
0 => array( |
259
|
|
|
'id' => '10', |
260
|
|
|
'gid' => '9003' |
261
|
|
|
), |
262
|
|
|
1 => array( |
263
|
|
|
'id' => '11', |
264
|
|
|
'gid' => '201' |
265
|
|
|
) |
266
|
|
|
); |
267
|
|
|
|
268
|
|
|
|
269
|
|
|
$this->refill(); |
270
|
|
|
$res = self::$connection->query('SELECT * FROM rt'); |
271
|
|
|
$array = $res->fetchAllAssoc(); |
272
|
|
|
$this->assertSame($expect[0], $array[0]); |
273
|
|
|
$this->assertSame($expect[1], $array[1]); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @throws ConnectionException |
278
|
|
|
* @throws DatabaseException |
279
|
|
|
* @throws SphinxQLException |
280
|
|
|
*/ |
281
|
|
|
public function testFetchAssoc(): void |
282
|
|
|
{ |
283
|
|
|
$expect = array( |
284
|
|
|
0 => array( |
285
|
|
|
'id' => '10', |
286
|
|
|
'gid' => '9003' |
287
|
|
|
), |
288
|
|
|
1 => array( |
289
|
|
|
'id' => '11', |
290
|
|
|
'gid' => '201' |
291
|
|
|
) |
292
|
|
|
); |
293
|
|
|
|
294
|
|
|
|
295
|
|
|
$this->refill(); |
296
|
|
|
$res = self::$connection->query('SELECT * FROM rt'); |
297
|
|
|
$this->assertSame($expect[0], $res->fetchAssoc()); |
298
|
|
|
$this->assertSame($expect[1], $res->fetchAssoc()); |
299
|
|
|
$res->fetchAssoc(); |
300
|
|
|
$res->fetchAssoc(); |
301
|
|
|
$res->fetchAssoc(); |
302
|
|
|
$res->fetchAssoc(); |
303
|
|
|
$res->fetchAssoc(); |
304
|
|
|
$res->fetchAssoc(); |
305
|
|
|
$this->assertNull($res->fetchAssoc()); |
306
|
|
|
|
307
|
|
|
$res = self::$connection->query('SELECT * FROM rt')->store(); |
308
|
|
|
$this->assertSame($expect[0], $res->fetchAssoc()); |
309
|
|
|
$this->assertSame($expect[1], $res->fetchAssoc()); |
310
|
|
|
$res->fetchAssoc(); |
311
|
|
|
$res->fetchAssoc(); |
312
|
|
|
$res->fetchAssoc(); |
313
|
|
|
$res->fetchAssoc(); |
314
|
|
|
$res->fetchAssoc(); |
315
|
|
|
$res->fetchAssoc(); |
316
|
|
|
$this->assertNull($res->fetchAssoc()); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @throws ConnectionException |
321
|
|
|
* @throws DatabaseException |
322
|
|
|
* @throws SphinxQLException |
323
|
|
|
*/ |
324
|
|
|
public function testFetchAllNum(): void |
325
|
|
|
{ |
326
|
|
|
$expect = array( |
327
|
|
|
0 => array( |
328
|
|
|
0 => '10', |
329
|
|
|
1 => '9003' |
330
|
|
|
), |
331
|
|
|
1 => array( |
332
|
|
|
0 => '11', |
333
|
|
|
1 => '201' |
334
|
|
|
) |
335
|
|
|
); |
336
|
|
|
|
337
|
|
|
$this->refill(); |
338
|
|
|
$res = self::$connection->query('SELECT * FROM rt LIMIT 2'); |
339
|
|
|
$array = $res->fetchAllNum(); |
340
|
|
|
$this->assertSame($expect, $array); |
341
|
|
|
|
342
|
|
|
$res = self::$connection->query('SELECT * FROM rt LIMIT 2'); |
343
|
|
|
$array = $res->store()->fetchAllNum(); |
344
|
|
|
$this->assertSame($expect, $array); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @throws ConnectionException |
349
|
|
|
* @throws DatabaseException |
350
|
|
|
* @throws SphinxQLException |
351
|
|
|
*/ |
352
|
|
|
public function testFetchNum(): void |
353
|
|
|
{ |
354
|
|
|
$expect = array( |
355
|
|
|
0 => array( |
356
|
|
|
0 => '10', |
357
|
|
|
1 => '9003' |
358
|
|
|
), |
359
|
|
|
1 => array( |
360
|
|
|
0 => '11', |
361
|
|
|
1 => '201' |
362
|
|
|
) |
363
|
|
|
); |
364
|
|
|
|
365
|
|
|
$this->refill(); |
366
|
|
|
$res = self::$connection->query('SELECT * FROM rt'); |
367
|
|
|
$this->assertSame($expect[0], $res->fetchNum()); |
368
|
|
|
$this->assertSame($expect[1], $res->fetchNum()); |
369
|
|
|
$res->fetchNum(); |
370
|
|
|
$res->fetchNum(); |
371
|
|
|
$res->fetchNum(); |
372
|
|
|
$res->fetchNum(); |
373
|
|
|
$res->fetchNum(); |
374
|
|
|
$res->fetchNum(); |
375
|
|
|
$this->assertNull($res->fetchNum()); |
376
|
|
|
|
377
|
|
|
$res = self::$connection->query('SELECT * FROM rt')->store(); |
378
|
|
|
$this->assertSame($expect[0], $res->fetchNum()); |
379
|
|
|
$this->assertSame($expect[1], $res->fetchNum()); |
380
|
|
|
$res->fetchNum(); |
381
|
|
|
$res->fetchNum(); |
382
|
|
|
$res->fetchNum(); |
383
|
|
|
$res->fetchNum(); |
384
|
|
|
$res->fetchNum(); |
385
|
|
|
$res->fetchNum(); |
386
|
|
|
$this->assertNull($res->fetchNum()); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* @throws ConnectionException |
391
|
|
|
* @throws DatabaseException |
392
|
|
|
* @throws SphinxQLException |
393
|
|
|
*/ |
394
|
|
|
public function testGetAffectedRows(): void |
395
|
|
|
{ |
396
|
|
|
$this->refill(); |
397
|
|
|
$res = self::$connection->query('UPDATE rt SET gid=0 WHERE id > 0'); |
398
|
|
|
$this->assertSame(8, $res->getAffectedRows()); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* @throws ConnectionException |
403
|
|
|
* @throws DatabaseException |
404
|
|
|
* @throws SphinxQLException |
405
|
|
|
*/ |
406
|
|
|
public function testArrayAccess(): void |
407
|
|
|
{ |
408
|
|
|
$expect = array( |
409
|
|
|
0 => array( |
410
|
|
|
'id' => '10', |
411
|
|
|
'gid' => '9003' |
412
|
|
|
), |
413
|
|
|
1 => array( |
414
|
|
|
'id' => '11', |
415
|
|
|
'gid' => '201' |
416
|
|
|
) |
417
|
|
|
); |
418
|
|
|
|
419
|
|
|
|
420
|
|
|
$this->refill(); |
421
|
|
|
$res = self::$connection->query('SELECT * FROM rt'); |
422
|
|
|
$this->assertSame($expect[0], $res[0]); |
423
|
|
|
$this->assertSame($expect[1], $res[1]); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* @throws ConnectionException |
428
|
|
|
* @throws DatabaseException |
429
|
|
|
* @throws SphinxQLException |
430
|
|
|
*/ |
431
|
|
|
public function testCountable(): void |
432
|
|
|
{ |
433
|
|
|
$this->refill(); |
434
|
|
|
$res = self::$connection->query('SELECT * FROM rt'); |
435
|
|
|
$this->assertCount($res->count(), $res); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* @throws ConnectionException |
440
|
|
|
* @throws DatabaseException |
441
|
|
|
* @throws SphinxQLException |
442
|
|
|
*/ |
443
|
|
|
public function testIterator(): void |
444
|
|
|
{ |
445
|
|
|
$expect = array( |
446
|
|
|
0 => array( |
447
|
|
|
'id' => '10', |
448
|
|
|
'gid' => '9003' |
449
|
|
|
), |
450
|
|
|
1 => array( |
451
|
|
|
'id' => '11', |
452
|
|
|
'gid' => '201' |
453
|
|
|
) |
454
|
|
|
); |
455
|
|
|
|
456
|
|
|
$this->refill(); |
457
|
|
|
$res = self::$connection->query('SELECT * FROM rt'); |
458
|
|
|
$array = array(); |
459
|
|
|
foreach ($res as $key => $value) { |
460
|
|
|
$array[$key] = $value; |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
$this->assertSame($expect[0], $array[0]); |
464
|
|
|
$this->assertSame($expect[1], $array[1]); |
465
|
|
|
|
466
|
|
|
$res = self::$connection->query('SELECT * FROM rt WHERE id = 404'); |
467
|
|
|
$array = array(); |
468
|
|
|
foreach ($res as $key => $value) { |
469
|
|
|
$array[$key] = $value; |
470
|
|
|
} |
471
|
|
|
$this->assertEmpty($array); |
472
|
|
|
} |
473
|
|
|
} |
474
|
|
|
|