Passed
Push — ci-build-matrix ( 1c3307...9a015f )
by Hung
02:21
created

SphinxQLTest::testMatch()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 93
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 1
eloc 72
nc 1
nop 0
dl 0
loc 93
rs 8.4642
c 3
b 0
f 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use Foolz\SphinxQL\Expression;
4
use Foolz\SphinxQL\SphinxQL;
5
use Foolz\SphinxQL\Facet;
6
use Foolz\SphinxQL\Helper;
7
use Foolz\SphinxQL\Match;
8
use Foolz\SphinxQL\Tests\TestUtil;
9
10
class SphinxQLTest extends \PHPUnit\Framework\TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
11
{
12
    public static $conn = null;
13
14
    public static $data = array (
15
        0 => array('id' => '10', 'gid' => '9003',
16
            'title' => 'modifying the same line again', 'content' => 'because i am that lazy'),
17
        1 => array('id' => '11', 'gid' => '201',
18
            'title' => 'replacing value by value', 'content' => 'i have no idea who would use this directly'),
19
        2 => array('id' => '12', 'gid' => '200',
20
            'title' => 'simple logic', 'content' => 'inside the box there was the content'),
21
        3 => array('id' => '13', 'gid' => '304',
22
            'title' => 'i am getting bored', 'content' => 'with all this CONTENT'),
23
        4 => array('id' => '14', 'gid' => '304',
24
            'title' => 'i want a vacation', 'content' => 'the code is going to break sometime'),
25
        5 => array('id' => '15', 'gid' => '304',
26
            'title' => 'there\'s no hope in this class', 'content' => 'just give up'),
27
        6 => array('id' => '16', 'gid' => '500',
28
            'title' => 'we need to test', 'content' => 'selecting the best result in groups'),
29
        7 => array('id' => '17', 'gid' => '500',
30
            'title' => 'what is there to do', 'content' => 'we need to create dummy data for tests'),
31
    );
32
33
    public static function setUpBeforeClass()
34
    {
35
        $conn = TestUtil::getConnectionDriver();
36
        $conn->setParam('port', 9307);
37
        self::$conn = $conn;
38
39
        (new SphinxQL(self::$conn))->getConnection()->query('TRUNCATE RTINDEX rt');
40
    }
41
42
    /**
43
     * @return SphinxQL
44
     */
45
    protected function createSphinxQL()
46
    {
47
        return new SphinxQL(self::$conn);
48
    }
49
50
    public function refill()
51
    {
52
        $this->createSphinxQL()->getConnection()->query('TRUNCATE RTINDEX rt');
53
54
        $sq = $this->createSphinxQL()
55
            ->insert()
56
            ->into('rt')
57
            ->columns('id', 'gid', 'title', 'content');
58
59
        foreach (static::$data as $row) {
60
            $sq->values($row['id'], $row['gid'], $row['title'], $row['content']);
61
        }
62
63
        $sq->execute();
64
    }
65
66
    public function testExpr()
67
    {
68
        $result = SphinxQL::expr('');
69
70
        $this->assertInstanceOf(Expression::class, $result);
71
        $this->assertEquals('', (string) $result);
72
73
        $result = SphinxQL::expr('* \\ Ç"" \'');
74
75
        $this->assertInstanceOf(Expression::class, $result);
76
        $this->assertEquals('* \\ Ç"" \'', (string) $result);
77
    }
78
79
    /**
80
     * @covers \Foolz\SphinxQL\SphinxQL::transactionBegin
81
     * @covers \Foolz\SphinxQL\SphinxQL::transactionCommit
82
     * @covers \Foolz\SphinxQL\SphinxQL::transactionRollback
83
     */
84
    public function testTransactions()
85
    {
86
        $this->createSphinxQL()->transactionBegin();
87
        $this->createSphinxQL()->transactionRollback();
88
        $this->createSphinxQL()->transactionBegin();
89
        $this->createSphinxQL()->transactionCommit();
90
    }
91
92
    public function testQuery()
93
    {
94
        $describe = $this->createSphinxQL()
95
            ->query('DESCRIBE rt')
96
            ->execute()
97
            ->getStored();
0 ignored issues
show
Bug introduced by
The method getStored() does not exist on Foolz\SphinxQL\Drivers\ResultSetInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Foolz\SphinxQL\Drivers\ResultSetInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
            ->/** @scrutinizer ignore-call */ getStored();
Loading history...
98
99
        array_shift($describe);
100
        $this->assertSame(
101
            array(
102
                //	array('Field' => 'id', 'Type' => 'integer'), this can be bigint on id64 sphinx
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
103
                array('Field' => 'title', 'Type' => 'field'),
104
                array('Field' => 'content', 'Type' => 'field'),
105
                array('Field' => 'gid', 'Type' => 'uint'),
106
            ),
107
            $describe
108
        );
109
110
        $describe = $this->createSphinxQL()
111
            ->query('DESCRIBE rt');
112
        $describe->execute();
113
        $describe = $describe
114
            ->getResult()
115
            ->getStored();
116
117
        array_shift($describe);
118
        $this->assertSame(
119
            array(
120
                //	array('Field' => 'id', 'Type' => 'integer'), this can be bigint on id64 sphinx
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
121
                array('Field' => 'title', 'Type' => 'field'),
122
                array('Field' => 'content', 'Type' => 'field'),
123
                array('Field' => 'gid', 'Type' => 'uint'),
124
            ),
125
            $describe
126
        );
127
    }
128
129
    /**
130
     * @covers \Foolz\SphinxQL\SphinxQL::compile
131
     * @covers \Foolz\SphinxQL\SphinxQL::compileInsert
132
     * @covers \Foolz\SphinxQL\SphinxQL::compileSelect
133
     * @covers \Foolz\SphinxQL\SphinxQL::insert
134
     * @covers \Foolz\SphinxQL\SphinxQL::set
135
     * @covers \Foolz\SphinxQL\SphinxQL::value
136
     * @covers \Foolz\SphinxQL\SphinxQL::columns
137
     * @covers \Foolz\SphinxQL\SphinxQL::values
138
     * @covers \Foolz\SphinxQL\SphinxQL::into
139
     */
140
    public function testInsert()
141
    {
142
        $this->createSphinxQL()
143
            ->insert()
144
            ->into('rt')
145
            ->set(array(
146
                'id' => 10,
147
                'title' => 'the story of a long test unit',
148
                'content' => 'once upon a time there was a foo in the bar',
149
                'gid' => 9001
150
            ))
151
            ->execute();
152
153
        $result = $this->createSphinxQL()
154
            ->select()
155
            ->from('rt')
156
            ->execute()
157
            ->getStored();
158
159
        $this->assertCount(1, $result);
160
161
        $this->createSphinxQL()
162
            ->insert()
163
            ->into('rt')
164
            ->columns('id', 'title', 'content', 'gid')
165
            ->values(11, 'this is a title', 'this is the content', 100)
166
            ->execute();
167
168
        $result = $this->createSphinxQL()
169
            ->select()
170
            ->from('rt')
171
            ->execute()
172
            ->getStored();
173
174
        $this->assertCount(2, $result);
175
176
        $this->createSphinxQL()
177
            ->insert()
178
            ->into('rt')
179
            ->value('id', 12)
180
            ->value('title', 'simple logic')
181
            ->value('content', 'inside the box there was the content')
182
            ->value('gid', 200)
183
            ->execute();
184
185
        $result = $this->createSphinxQL()
186
            ->select()
187
            ->from('rt')
188
            ->execute()
189
            ->getStored();
190
191
        $this->assertCount(3, $result);
192
193
        $this->createSphinxQL()
194
            ->insert()
195
            ->into('rt')
196
            ->columns(array('id', 'title', 'content', 'gid'))
197
            ->values(array(13, 'i am getting bored', 'with all this CONTENT', 300))
198
            ->values(14, 'i want a vacation', 'the code is going to break sometime', 300)
199
            ->values(15, 'there\'s no hope in this class', 'just give up', 300)
200
            ->execute();
201
202
        $result = $this->createSphinxQL()
203
            ->select()
204
            ->from('rt')
205
            ->execute()
206
            ->getStored();
207
208
        $this->assertCount(6, $result);
209
210
        $this->createSphinxQL()
211
            ->insert()
212
            ->into('rt')
213
            ->columns('id', 'title', 'content', 'gid')
214
            ->values(16, 'we need to test', 'selecting the best result in groups', 500)
215
            ->values(17, 'what is there to do', 'we need to create dummy data for tests', 500)
216
            ->execute();
217
218
        $result = $this->createSphinxQL()
219
            ->select()
220
            ->from('rt')
221
            ->execute()
222
            ->getStored();
223
224
        $this->assertCount(8, $result);
225
226
        $this->createSphinxQL()
227
            ->insert()
228
            ->into('rt')
229
            ->set(array(
230
                'id' => 18,
231
                'title' => 'a multi set test',
232
                'content' => 'has text',
233
                'gid' => 9002
234
            ))
235
            ->set(array(
236
                'id' => 19,
237
                'title' => 'and a',
238
                'content' => 'second set call',
239
                'gid' => 9003
240
            ))
241
            ->execute();
242
243
        $result = $this->createSphinxQL()
244
            ->select()
245
            ->from('rt')
246
            ->execute()
247
            ->getStored();
248
249
        $this->assertCount(10, $result);
250
251
    }
252
253
    /**
254
     * @covers \Foolz\SphinxQL\SphinxQL::compile
255
     * @covers \Foolz\SphinxQL\SphinxQL::compileInsert
256
     * @covers \Foolz\SphinxQL\SphinxQL::compileSelect
257
     * @covers \Foolz\SphinxQL\SphinxQL::replace
258
     * @covers \Foolz\SphinxQL\SphinxQL::set
259
     * @covers \Foolz\SphinxQL\SphinxQL::value
260
     * @covers \Foolz\SphinxQL\SphinxQL::columns
261
     * @covers \Foolz\SphinxQL\SphinxQL::values
262
     * @covers \Foolz\SphinxQL\SphinxQL::into
263
     */
264
    public function testReplace()
265
    {
266
        $result = $this->createSphinxQL()
267
            ->replace()
268
            ->into('rt')
269
            ->set(array(
270
                'id' => 10,
271
                'title' => 'modified',
272
                'content' => 'this field was modified with replace',
273
                'gid' => 9002
274
            ))
275
            ->execute()
276
            ->getStored();
277
278
        $this->assertSame(1, $result);
279
280
        $result = $this->createSphinxQL()
281
            ->select()
282
            ->from('rt')
283
            ->where('id', '=', 10)
284
            ->execute()
285
            ->getStored();
286
287
        $this->assertEquals('9002', $result[0]['gid']);
288
289
        $result = $this->createSphinxQL()
290
            ->replace()
291
            ->into('rt')
292
            ->columns('id', 'title', 'content', 'gid')
293
            ->values(10, 'modifying the same line again', 'because i am that lazy', 9003)
294
            ->values(11, 'i am getting really creative with these strings', 'i\'ll need them to test MATCH!', 300)
295
            ->execute()
296
            ->getStored();
297
298
        $this->assertSame(2, $result);
299
300
        $result = $this->createSphinxQL()
301
            ->select()
302
            ->from('rt')
303
            ->where('id', 'IN', array(10, 11))
304
            ->execute()
305
            ->getStored();
306
307
        $this->assertEquals('9003', $result[0]['gid']);
308
        $this->assertEquals('300', $result[1]['gid']);
309
310
        $this->createSphinxQL()
311
            ->replace()
312
            ->into('rt')
313
            ->value('id', 11)
314
            ->value('title', 'replacing value by value')
315
            ->value('content', 'i have no idea who would use this directly')
316
            ->value('gid', 200)
317
            ->execute();
318
319
        $result = $this->createSphinxQL()
320
            ->select()
321
            ->from('rt')
322
            ->where('id', '=', 11)
323
            ->execute()
324
            ->getStored();
325
326
        $this->assertEquals('200', $result[0]['gid']);
327
    }
328
329
    /**
330
     * @covers \Foolz\SphinxQL\SphinxQL::compile
331
     * @covers \Foolz\SphinxQL\SphinxQL::compileUpdate
332
     * @covers \Foolz\SphinxQL\SphinxQL::compileSelect
333
     * @covers \Foolz\SphinxQL\SphinxQL::update
334
     * @covers \Foolz\SphinxQL\SphinxQL::value
335
     */
336
    public function testUpdate()
337
    {
338
        $result = $this->createSphinxQL()
339
            ->update('rt')
340
            ->where('id', '=', 11)
341
            ->value('gid', 201)
342
            ->execute()
343
            ->getStored();
344
345
        $this->assertSame(1, $result);
346
347
        $result = $this->createSphinxQL()
348
            ->update('rt')
349
            ->where('gid', '=', 300)
350
            ->value('gid', 305)
351
            ->execute()
352
            ->getStored();
353
354
        $this->assertSame(3, $result);
355
356
        $result = $this->createSphinxQL()
357
            ->select()
358
            ->from('rt')
359
            ->where('id', '=', 11)
360
            ->execute()
361
            ->getStored();
362
363
        $this->assertEquals('201', $result[0]['gid']);
364
365
        $result = $this->createSphinxQL()
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
366
            ->update('rt')
367
            ->where('gid', '=', 305)
368
            ->set(array('gid' => 304))
369
            ->execute()
370
            ->getStored();
371
372
        $result = $this->createSphinxQL()
373
            ->select()
374
            ->from('rt')
375
            ->where('gid', '=', 304)
376
            ->execute()
377
            ->getStored();
378
379
        $this->assertCount(3, $result);
380
381
        self::$conn->query('ALTER TABLE rt ADD COLUMN tags MULTI');
382
        $result = $this->createSphinxQL()
383
            ->select()
384
            ->from('rt')
385
            ->where('tags', 222)
386
            ->execute()
387
            ->getStored();
388
        $this->assertEmpty($result);
389
390
        $result = $this->createSphinxQL()
391
            ->update('rt')
392
            ->where('id', '=', 15)
393
            ->value('tags', array(111, 222))
0 ignored issues
show
Bug introduced by
array(111, 222) of type array<integer,integer> is incompatible with the type string expected by parameter $value of Foolz\SphinxQL\SphinxQL::value(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

393
            ->value('tags', /** @scrutinizer ignore-type */ array(111, 222))
Loading history...
394
            ->execute()
395
            ->getStored();
396
        $this->assertSame(1, $result);
397
398
        $result = $this->createSphinxQL()
399
            ->select()
400
            ->from('rt')
401
            ->where('tags', 222)
402
            ->execute()
403
            ->getStored();
404
        $this->assertEquals(
405
            array(
406
                array(
407
                    'id'   => '15',
408
                    'gid'  => '304',
409
                    'tags' => '111,222',
410
                ),
411
            ),
412
            $result
413
        );
414
        self::$conn->query('ALTER TABLE rt DROP COLUMN tags');
415
    }
416
417
    /**
418
     * @covers \Foolz\SphinxQL\SphinxQL::compileWhere
419
     * @covers \Foolz\SphinxQL\SphinxQL::from
420
     * @covers \Foolz\SphinxQL\SphinxQL::compileFilterCondition
421
     */
422
    public function testWhere()
423
    {
424
        $this->refill();
425
426
        $result = $this->createSphinxQL()
427
            ->select()
428
            ->from('rt')
429
            ->where('gid', 'BETWEEN', array(300, 400))
430
            ->execute()
431
            ->getStored();
432
433
        $this->assertCount(3, $result);
434
435
        $result = $this->createSphinxQL()
436
            ->select()
437
            ->from('rt')
438
            ->where('id', 'IN', array(11, 12, 13))
439
            ->execute()
440
            ->getStored();
441
442
        $this->assertCount(3, $result);
443
444
        $result = $this->createSphinxQL()
445
            ->select()
446
            ->from('rt')
447
            ->where('id', 'NOT IN', array(11, 12))
448
            ->execute()
449
            ->getStored();
450
451
        $this->assertCount(6, $result);
452
453
        $result = $this->createSphinxQL()
454
            ->select()
455
            ->from('rt')
456
            ->where('gid', '>', 300)
457
            ->execute()
458
            ->getStored();
459
460
        $this->assertCount(6, $result);
461
462
        $result = $this->createSphinxQL()
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
463
            ->select()
464
            ->from('rt')
465
            ->where('gid', 304)
466
            ->execute()
467
            ->getStored();
468
469
        $result = $this->createSphinxQL()
470
            ->select()
471
            ->from('rt')
472
            ->where('gid', '>', 300)
473
            ->execute()
474
            ->getStored();
475
476
        $this->assertCount(6, $result);
477
478
        $result = $this->createSphinxQL()
479
            ->select()
480
            ->from('rt')
481
            ->where('gid', '>', 300)
482
            ->where('id', '!=', 15)
483
            ->execute()
484
            ->getStored();
485
486
        $this->assertCount(5, $result);
487
488
        $result = $this->createSphinxQL()
489
            ->select()
490
            ->from('rt')
491
            ->match('content', 'content')
492
            ->where('gid', '>', 200)
493
            ->execute()
494
            ->getStored();
495
496
        $this->assertCount(1, $result);
497
    }
498
499
    /**
500
     * @covers \Foolz\SphinxQL\SphinxQL::match
501
     * @covers \Foolz\SphinxQL\SphinxQL::compileMatch
502
     * @covers \Foolz\SphinxQL\SphinxQL::halfEscapeMatch
503
     */
504
    public function testMatch()
505
    {
506
        $this->refill();
507
508
        $result = $this->createSphinxQL()
509
            ->select()
510
            ->from('rt')
511
            ->match('content', 'content')
512
            ->execute()
513
            ->getStored();
514
515
        $this->assertCount(2, $result);
516
517
        $result = $this->createSphinxQL()
518
            ->select()
519
            ->from('rt')
520
            ->match('title', 'value')
521
            ->execute()
522
            ->getStored();
523
524
        $this->assertCount(1, $result);
525
526
        $result = $this->createSphinxQL()
527
            ->select()
528
            ->from('rt')
529
            ->match('title', 'value')
530
            ->match('content', 'directly')
531
            ->execute()
532
            ->getStored();
533
534
        $this->assertCount(1, $result);
535
536
        $result = $this->createSphinxQL()
537
            ->select()
538
            ->from('rt')
539
            ->match('*', 'directly')
540
            ->execute()
541
            ->getStored();
542
543
        $this->assertCount(1, $result);
544
545
        $result = $this->createSphinxQL()
546
            ->select()
547
            ->from('rt')
548
            ->match(array('title', 'content'), 'to')
549
            ->execute()
550
            ->getStored();
551
552
        $this->assertCount(3, $result);
553
554
        $result = $this->createSphinxQL()
555
            ->select()
556
            ->from('rt')
557
            ->match('content', 'directly | lazy', true)
558
            ->execute()
559
            ->getStored();
560
561
        $this->assertCount(2, $result);
562
563
        $result = $this->createSphinxQL()
564
            ->select()
565
            ->from('rt')
566
            ->match(function ($m) {
567
                $m->field('content')
568
                    ->match('directly')
569
                    ->orMatch('lazy');
570
            })
571
            ->execute()
572
            ->getStored();
573
574
        $this->assertCount(2, $result);
575
576
        $match = (new Match($this->createSphinxQL()))
577
            ->field('content')
578
            ->match('directly')
579
            ->orMatch('lazy');
580
        $result = $this->createSphinxQL()
581
            ->select()
582
            ->from('rt')
583
            ->match($match)
584
            ->execute()
585
            ->getStored();
586
587
        $this->assertCount(2, $result);
588
589
        $result = $this->createSphinxQL()
590
            ->select()
591
            ->from('rt')
592
            ->match('')
593
            ->compile()
594
            ->getCompiled();
595
596
        $this->assertEquals('SELECT * FROM rt WHERE MATCH(\'\')', $result);
597
    }
598
599
    public function testEscapeMatch()
600
    {
601
        $match = 'this MAYBE that^32 and | hi';
602
        $this->assertSame('this maybe that\^32 and \| hi', $this->createSphinxQL()->escapeMatch($match));
603
        $this->assertSame($match, $this->createSphinxQL()->escapeMatch(SphinxQL::expr($match)));
604
        $this->assertSame('stärkergradig \| mb', $this->createSphinxQL()->escapeMatch('stärkergradig | mb'));
605
    }
606
607
    public function testHalfEscapeMatch()
608
    {
609
        $match = 'this MAYBE that^32 and | hi';
610
        $this->assertSame('this maybe that\^32 and | hi', $this->createSphinxQL()->halfEscapeMatch($match));
611
        $this->assertSame($match, $this->createSphinxQL()->halfEscapeMatch(SphinxQL::expr($match)));
612
        $this->assertSame('this \- not -that | hi \-', $this->createSphinxQL()->halfEscapeMatch('this -- not -that | | hi -'));
613
        $this->assertSame('stärkergradig | mb', $this->createSphinxQL()->halfEscapeMatch('stärkergradig | mb'));
614
        $this->assertSame('"unmatched quotes"', $this->createSphinxQL()->halfEscapeMatch('"unmatched quotes'));
615
    }
616
617
    /**
618
    * @covers \Foolz\SphinxQL\SphinxQL::setFullEscapeChars
619
    * @covers \Foolz\SphinxQL\SphinxQL::setHalfEscapeChars
620
    * @covers \Foolz\SphinxQL\SphinxQL::compileEscapeChars
621
    */
622
    public function testEscapeChars()
623
    {
624
        $this->assertEquals(array('%' => '\%'), $this->createSphinxQL()->compileEscapeChars(array('%')));
625
        $this->assertEquals(array('@' => '\@'), $this->createSphinxQL()->compileEscapeChars(array('@')));
626
627
        $match = 'this MAYBE that^32 and | hi';
628
        $sphinxql = $this->createSphinxQL()->setFullEscapeChars(array('^'));
629
        $this->assertSame('this maybe that\^32 and | hi', $sphinxql->escapeMatch($match));
630
631
        $sphinxql->setHalfEscapeChars(array('|'));
632
        $this->assertSame('this maybe that^32 and \| hi', $sphinxql->halfEscapeMatch($match));
633
    }
634
635
    public function testOption()
636
    {
637
        $this->refill();
638
639
        $result = $this->createSphinxQL()
640
            ->select()
641
            ->from('rt')
642
            ->match('content', 'content')
643
            ->option('max_matches', 1)
644
            ->execute()
645
            ->getStored();
646
647
        $this->assertCount(1, $result);
648
649
        $result = $this->createSphinxQL()
650
            ->select()
651
            ->from('rt')
652
            ->match('content', 'content')
653
            ->option('max_matches', SphinxQL::expr('1'))
654
            ->execute()
655
            ->getStored();
656
657
        $this->assertCount(1, $result);
658
659
        $result = $this->createSphinxQL()
660
            ->select()
661
            ->from('rt')
662
            ->option('comment', 'this should be quoted')
663
            ->compile()
664
            ->getCompiled();
665
666
        $this->assertEquals('SELECT * FROM rt OPTION comment = \'this should be quoted\'', $result);
667
668
        $result = $this->createSphinxQL()
669
            ->select()
670
            ->from('rt')
671
            ->option('field_weights', SphinxQL::expr('(content=50)'))
672
            ->compile()
673
            ->getCompiled();
674
675
        $this->assertEquals('SELECT * FROM rt OPTION field_weights = (content=50)', $result);
676
677
        $result = $this->createSphinxQL()
678
            ->select()
679
            ->from('rt')
680
            ->option('field_weights', array(
681
                'title'   => 80,
682
                'content' => 35,
683
                'tags'    => 92,
684
            ))
685
            ->compile()
686
            ->getCompiled();
687
688
        $this->assertEquals('SELECT * FROM rt OPTION field_weights = (title=80, content=35, tags=92)', $result);
689
    }
690
691
    public function testGroupBy()
692
    {
693
        $this->refill();
694
695
        $result = $this->createSphinxQL()
696
            ->select(SphinxQL::expr('count(*)'))
697
            ->from('rt')
698
            ->groupBy('gid')
699
            ->execute()
700
            ->getStored();
701
702
        $this->assertCount(5, $result);
703
        $this->assertEquals('3', $result[3]['count(*)']);
704
    }
705
706
    public function testHaving()
707
    {
708
        $this->refill();
709
710
        $result = $this->createSphinxQL()
711
            ->select(SphinxQL::expr('count(*) as cnt'))
712
            ->from('rt')
713
            ->groupBy('gid')
714
            ->having('cnt', '>', 1)
715
            ->execute();
716
717
        $this->assertCount(2, $result);
718
        $this->assertEquals('2', $result[1]['cnt']);
719
720
        $result = $this->createSphinxQL()
721
            ->select(SphinxQL::expr('count(*) as cnt'), SphinxQL::expr('GROUPBY() gd'))
722
            ->from('rt')
723
            ->groupBy('gid')
724
            ->having('gd', 304)
725
            ->execute();
726
727
        $this->assertCount(1, $result);
728
    }
729
730
    public function testOrderBy()
731
    {
732
        $this->refill();
733
734
        $result = $this->createSphinxQL()
735
            ->select()
736
            ->from('rt')
737
            ->orderBy('id', 'desc')
738
            ->execute()
739
            ->getStored();
740
741
        $this->assertEquals('17', $result[0]['id']);
742
743
        $result = $this->createSphinxQL()
744
            ->select()
745
            ->from('rt')
746
            ->orderBy('id', 'asc')
747
            ->execute()
748
            ->getStored();
749
750
        $this->assertEquals('10', $result[0]['id']);
751
    }
752
753
    public function testWithinGroupOrderBy()
754
    {
755
        $this->refill();
756
757
        $result = $this->createSphinxQL()
758
            ->select()
759
            ->from('rt')
760
            ->where('gid', 500)
761
            ->groupBy('gid')
762
            ->withinGroupOrderBy('id', 'desc')
763
            ->execute()
764
            ->getStored();
765
766
        $this->assertEquals('17', $result[0]['id']);
767
768
        $result = $this->createSphinxQL()
769
            ->select()
770
            ->from('rt')
771
            ->where('gid', 500)
772
            ->groupBy('gid')
773
            ->withinGroupOrderBy('id', 'asc')
774
            ->execute()
775
            ->getStored();
776
777
        $this->assertEquals('16', $result[0]['id']);
778
    }
779
780
    public function testGroupNBy()
781
    {
782
        $query = $this->createSphinxQL()
783
            ->select()
784
            ->from('rt')
785
            ->groupBy('gid');
786
        $this->assertEquals(
787
            'SELECT * FROM rt GROUP BY gid',
788
            $query->compile()->getCompiled()
789
        );
790
791
        $query->groupNBy(3);
792
        $this->assertEquals(
793
            'SELECT * FROM rt GROUP 3 BY gid',
794
            $query->compile()->getCompiled()
795
        );
796
797
        $query->resetGroupBy();
798
        $this->assertEquals(
799
            'SELECT * FROM rt',
800
            $query->compile()->getCompiled()
801
        );
802
803
        $query->groupBy('gid');
804
        $this->assertEquals(
805
            'SELECT * FROM rt GROUP BY gid',
806
            $query->compile()->getCompiled()
807
        );
808
809
        $query->resetGroupBy()
810
            ->groupNBy(3);
811
        $this->assertEquals(
812
            'SELECT * FROM rt',
813
            $query->compile()->getCompiled()
814
        );
815
    }
816
817
    public function testOffset()
818
    {
819
        $this->refill();
820
821
        $result = $this->createSphinxQL()
822
            ->select()
823
            ->from('rt')
824
            ->offset(4)
825
            ->execute()
826
            ->getStored();
827
828
        $this->assertCount(4, $result);
829
    }
830
831
    public function testLimit()
832
    {
833
        $this->refill();
834
835
        $result = $this->createSphinxQL()
836
            ->select()
837
            ->from('rt')
838
            ->limit(3)
839
            ->execute()
840
            ->getStored();
841
842
        $this->assertCount(3, $result);
843
844
        $result = $this->createSphinxQL()
845
            ->select()
846
            ->from('rt')
847
            ->limit(2, 3)
848
            ->execute()
849
            ->getStored();
850
851
        $this->assertCount(3, $result);
852
    }
853
854
    /**
855
     * @covers \Foolz\SphinxQL\SphinxQL::compile
856
     * @covers \Foolz\SphinxQL\SphinxQL::compileDelete
857
     * @covers \Foolz\SphinxQL\SphinxQL::delete
858
     */
859
    public function testDelete()
860
    {
861
        $this->refill();
862
863
        $result = $this->createSphinxQL()
864
            ->delete()
865
            ->from('rt')
866
            ->where('id', 'IN', array(10, 11, 12))
867
            ->execute()
868
            ->getStored();
869
870
        $this->assertSame(3, $result);
871
    }
872
873
    /**
874
     * @covers \Foolz\SphinxQL\SphinxQL::executeBatch
875
     * @covers \Foolz\SphinxQL\SphinxQL::enqueue
876
     * @covers \Foolz\SphinxQL\SphinxQL::getQueue
877
     * @covers \Foolz\SphinxQL\SphinxQL::getQueuePrev
878
     * @covers \Foolz\SphinxQL\SphinxQL::setQueuePrev
879
     */
880
    public function testQueue()
881
    {
882
        $this->refill();
883
884
        $result = $this->createSphinxQL()
885
            ->select()
886
            ->from('rt')
887
            ->where('gid', 9003)
888
            ->enqueue((new Helper(self::$conn))->showMeta())
889
            ->enqueue()
890
            ->select()
891
            ->from('rt')
892
            ->where('gid', 201)
893
            ->executeBatch()
894
            ->getStored();
895
896
        $this->assertEquals('10', $result[0][0]['id']);
897
        $this->assertEquals('1', $result[1][0]['Value']);
898
        $this->assertEquals('11', $result[2][0]['id']);
899
    }
900
901
    /**
902
     * @expectedException        Foolz\SphinxQL\Exception\SphinxQLException
903
     * @expectedExceptionMessage There is no Queue present to execute.
904
     */
905
    public function testEmptyQueue()
906
    {
907
        $this->createSphinxQL()
908
            ->executeBatch()
909
            ->getStored();
910
    }
911
912
    /**
913
     * @covers \Foolz\SphinxQL\SphinxQL::resetWhere
914
     * @covers \Foolz\SphinxQL\SphinxQL::resetMatch
915
     * @covers \Foolz\SphinxQL\SphinxQL::resetGroupBy
916
     * @covers \Foolz\SphinxQL\SphinxQL::resetWithinGroupOrderBy
917
     * @covers \Foolz\SphinxQL\SphinxQL::resetOptions
918
     * @covers \Foolz\SphinxQL\SphinxQL::resetHaving
919
     * @covers \Foolz\SphinxQL\SphinxQL::resetOrderBy
920
     */
921
    public function testResetMethods()
922
    {
923
        $result = $this->createSphinxQL()
924
            ->select()
925
            ->from('rt')
926
            ->where('id', 'IN', array(10, 11))
927
            ->resetWhere()
928
            ->match('title', 'value')
929
            ->resetMatch()
930
            ->groupBy('gid')
931
            ->resetGroupBy()
932
            ->having('gid', '=', '304')
933
            ->resetHaving()
934
            ->withinGroupOrderBy('id', 'desc')
935
            ->resetWithinGroupOrderBy()
936
            ->option('comment', 'this should be quoted')
937
            ->resetOptions()
938
            ->orderBy('id', 'desc')
939
            ->resetOrderBy()
940
            ->compile()
941
            ->getCompiled();
942
943
        $this->assertEquals('SELECT * FROM rt', $result);
944
    }
945
946
    /**
947
     * @covers \Foolz\SphinxQL\SphinxQL::select
948
     */
949
    public function testSelect()
950
    {
951
        $this->refill();
952
        $result = $this->createSphinxQL()
953
            ->select(array('id', 'gid'))
954
            ->from('rt')
955
            ->execute()
956
            ->getStored();
957
        $this->assertArrayHasKey('id', $result[0]);
958
        $this->assertArrayHasKey('gid', $result[0]);
959
        $this->assertEquals('10', $result[0]['id']);
960
        $this->assertEquals('9003', $result[0]['gid']);
961
962
        $result = $this->createSphinxQL()
963
            ->select('id', 'gid')
964
            ->from('rt')
965
            ->execute()
966
            ->getStored();
967
        $this->assertArrayHasKey('id', $result[0]);
968
        $this->assertArrayHasKey('gid', $result[0]);
969
        $this->assertEquals('10', $result[0]['id']);
970
        $this->assertEquals('9003', $result[0]['gid']);
971
972
        $result = $this->createSphinxQL()
973
            ->select(array('id'))
974
            ->from('rt')
975
            ->execute()
976
            ->getStored();
977
        $this->assertArrayHasKey('id', $result[0]);
978
        $this->assertArrayNotHasKey('gid', $result[0]);
979
        $this->assertEquals('10', $result[0]['id']);
980
981
        $result = $this->createSphinxQL()
982
            ->select('id')
983
            ->from('rt')
984
            ->execute()
985
            ->getStored();
986
        $this->assertArrayHasKey('id', $result[0]);
987
        $this->assertArrayNotHasKey('gid', $result[0]);
988
        $this->assertEquals('10', $result[0]['id']);
989
    }
990
991
    public function testSubselect()
992
    {
993
        $this->refill();
994
        $query = $this->createSphinxQL()
995
            ->select()
996
            ->from(function ($q) {
997
                $q->select('id')
998
                    ->from('rt')
999
                    ->orderBy('id', 'DESC');
1000
            })
1001
            ->orderBy('id', 'ASC');
1002
        $this->assertEquals(
1003
            'SELECT * FROM (SELECT id FROM rt ORDER BY id DESC) ORDER BY id ASC',
1004
            $query->compile()->getCompiled()
1005
        );
1006
        $result = $query
1007
            ->execute()
1008
            ->getStored();
1009
        $this->assertArrayHasKey('id', $result[0]);
1010
        $this->assertArrayNotHasKey('gid', $result[0]);
1011
        $this->assertEquals('10', $result[0]['id']);
1012
1013
        $subquery = $this->createSphinxQL()
1014
            ->select('id')
1015
            ->from('rt')
1016
            ->orderBy('id', 'DESC');
1017
        $query = $this->createSphinxQL()
1018
            ->select()
1019
            ->from($subquery)
1020
            ->orderBy('id', 'ASC');
1021
        $this->assertEquals(
1022
            'SELECT id FROM rt ORDER BY id DESC',
1023
            $subquery->compile()->getCompiled()
1024
        );
1025
        $this->assertEquals(
1026
            'SELECT * FROM (SELECT id FROM rt ORDER BY id DESC) ORDER BY id ASC',
1027
            $query->compile()->getCompiled()
1028
        );
1029
        $result = $subquery
1030
            ->execute()
1031
            ->getStored();
1032
        $this->assertArrayHasKey('id', $result[0]);
1033
        $this->assertArrayNotHasKey('gid', $result[0]);
1034
        $this->assertEquals('17', $result[0]['id']);
1035
        $result = $query
1036
            ->execute()
1037
            ->getStored();
1038
        $this->assertArrayHasKey('id', $result[0]);
1039
        $this->assertArrayNotHasKey('gid', $result[0]);
1040
        $this->assertEquals('10', $result[0]['id']);
1041
    }
1042
1043
    /**
1044
     * @covers \Foolz\SphinxQL\SphinxQL::setSelect
1045
     */
1046
    public function testSetSelect()
1047
    {
1048
        $this->refill();
1049
        $q1 = $this->createSphinxQL()
1050
            ->select(array('id', 'gid'))
1051
            ->from('rt');
1052
        $q2 = clone $q1;
1053
        $q2->setSelect(array('id'));
1054
        $result = $q1
1055
            ->execute()
1056
            ->getStored();
1057
        $this->assertArrayHasKey('id', $result[0]);
1058
        $this->assertArrayHasKey('gid', $result[0]);
1059
        $result = $q2
1060
            ->execute()
1061
            ->getStored();
1062
        $this->assertArrayHasKey('id', $result[0]);
1063
        $this->assertArrayNotHasKey('gid', $result[0]);
1064
1065
        $q1 = $this->createSphinxQL()
1066
            ->select('id', 'gid')
1067
            ->from('rt');
1068
        $q2 = clone $q1;
1069
        $q2->setSelect('id');
1070
        $result = $q1
1071
            ->execute()
1072
            ->getStored();
1073
        $this->assertArrayHasKey('id', $result[0]);
1074
        $this->assertArrayHasKey('gid', $result[0]);
1075
        $result = $q2
1076
            ->execute()
1077
            ->getStored();
1078
        $this->assertArrayHasKey('id', $result[0]);
1079
        $this->assertArrayNotHasKey('gid', $result[0]);
1080
    }
1081
1082
    /**
1083
     * @covers \Foolz\SphinxQL\SphinxQL::getSelect
1084
     */
1085
    public function testGetSelect()
1086
    {
1087
        $query = $this->createSphinxQL()
1088
            ->select('id', 'gid')
1089
            ->from('rt');
1090
        $this->assertEquals(array('id', 'gid'), $query->getSelect());
1091
    }
1092
1093
    /**
1094
     * @covers \Foolz\SphinxQL\SphinxQL::facet
1095
     * @covers \Foolz\SphinxQL\SphinxQL::compileSelect
1096
     */
1097
    public function testFacet()
1098
    {
1099
        $this->refill();
1100
1101
        // test both setting and not setting the connection
1102
        foreach (array(self::$conn, null) as $conn) {
1103
            $result = $this->createSphinxQL()
1104
                ->select()
1105
                ->from('rt')
1106
                ->facet((new Facet($conn))
1107
                    ->facetFunction('INTERVAL', array('gid', 300, 600))
1108
                    ->orderByFunction('FACET', '', 'ASC'))
0 ignored issues
show
Bug introduced by
'' of type string is incompatible with the type array expected by parameter $params of Foolz\SphinxQL\Facet::orderByFunction(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1108
                    ->orderByFunction('FACET', /** @scrutinizer ignore-type */ '', 'ASC'))
Loading history...
1109
                ->executeBatch()
1110
                ->getStored();
1111
1112
            $this->assertArrayHasKey('id', $result[0][0]);
1113
            $this->assertArrayHasKey('interval(gid,300,600)', $result[1][0]);
1114
            $this->assertArrayHasKey('count(*)', $result[1][0]);
1115
1116
            $this->assertEquals('2', $result[1][0]['count(*)']);
1117
            $this->assertEquals('5', $result[1][1]['count(*)']);
1118
            $this->assertEquals('1', $result[1][2]['count(*)']);
1119
1120
            $result = $this->createSphinxQL()
1121
                ->select()
1122
                ->from('rt')
1123
                ->facet((new Facet($conn))
1124
                    ->facet(array('gid'))
1125
                    ->orderBy('gid', 'ASC'))
1126
                ->executeBatch()
1127
                ->getStored();
1128
1129
            $this->assertArrayHasKey('id', $result[0][0]);
1130
            $this->assertArrayHasKey('gid', $result[1][0]);
1131
            $this->assertArrayHasKey('count(*)', $result[1][0]);
1132
1133
            $this->assertEquals('1', $result[1][0]['count(*)']);
1134
            $this->assertEquals('200', $result[1][0]['gid']);
1135
            $this->assertEquals('3', $result[1][2]['count(*)']);
1136
            $this->assertEquals('2', $result[1][3]['count(*)']);
1137
        }
1138
    }
1139
1140
    // issue #82
1141
    public function testClosureMisuse()
1142
    {
1143
        $query = $this->createSphinxQL()
1144
            ->select()
1145
            ->from('strlen')
1146
            ->orderBy('id', 'ASC');
1147
        $this->assertEquals(
1148
            'SELECT * FROM strlen ORDER BY id ASC',
1149
            $query->compile()->getCompiled()
1150
        );
1151
1152
        $query = $this->createSphinxQL()
1153
            ->select()
1154
            ->from('rt')
1155
            ->match('strlen', 'value');
1156
        $this->assertEquals(
1157
            "SELECT * FROM rt WHERE MATCH('(@strlen value)')",
1158
            $query->compile()->getCompiled()
1159
        );
1160
    }
1161
}
1162