Passed
Pull Request — master (#197)
by
unknown
01:27
created

ResultSetTest::testCountable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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