1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Foolz\SphinxQL\Expression; |
4
|
|
|
use Foolz\SphinxQL\Facet; |
5
|
|
|
use Foolz\SphinxQL\Helper; |
6
|
|
|
use Foolz\SphinxQL\MatchBuilder; |
7
|
|
|
use Foolz\SphinxQL\SphinxQL; |
8
|
|
|
use Foolz\SphinxQL\Tests\TestUtil; |
9
|
|
|
|
10
|
|
|
class SphinxQLTest extends \PHPUnit\Framework\TestCase |
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(): void |
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(); |
98
|
|
|
|
99
|
|
|
array_shift($describe); |
|
|
|
|
100
|
|
|
$this->assertSame( |
101
|
|
|
array( |
102
|
|
|
// array('Field' => 'id', 'Type' => 'integer'), this can be bigint on id64 sphinx |
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 |
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() |
|
|
|
|
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)) |
|
|
|
|
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() |
|
|
|
|
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 MatchBuilder($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', [11, 12, 13]) |
867
|
|
|
->match('content', 'content') |
868
|
|
|
->execute() |
869
|
|
|
->getStored(); |
870
|
|
|
|
871
|
|
|
$this->assertSame(2, $result); |
872
|
|
|
} |
873
|
|
|
|
874
|
|
|
/** |
875
|
|
|
* @covers \Foolz\SphinxQL\SphinxQL::executeBatch |
876
|
|
|
* @covers \Foolz\SphinxQL\SphinxQL::enqueue |
877
|
|
|
* @covers \Foolz\SphinxQL\SphinxQL::getQueue |
878
|
|
|
* @covers \Foolz\SphinxQL\SphinxQL::getQueuePrev |
879
|
|
|
* @covers \Foolz\SphinxQL\SphinxQL::setQueuePrev |
880
|
|
|
*/ |
881
|
|
|
public function testQueue() |
882
|
|
|
{ |
883
|
|
|
$this->refill(); |
884
|
|
|
|
885
|
|
|
$result = $this->createSphinxQL() |
886
|
|
|
->select() |
887
|
|
|
->from('rt') |
888
|
|
|
->where('gid', 9003) |
889
|
|
|
->enqueue((new Helper(self::$conn))->showMeta()) |
890
|
|
|
->enqueue() |
891
|
|
|
->select() |
892
|
|
|
->from('rt') |
893
|
|
|
->where('gid', 201) |
894
|
|
|
->executeBatch() |
895
|
|
|
->getStored(); |
896
|
|
|
|
897
|
|
|
$this->assertEquals('10', $result[0][0]['id']); |
898
|
|
|
$this->assertEquals('1', $result[1][0]['Value']); |
899
|
|
|
$this->assertEquals('11', $result[2][0]['id']); |
900
|
|
|
} |
901
|
|
|
|
902
|
|
|
public function testEmptyQueue() |
903
|
|
|
{ |
904
|
|
|
$this->expectException(Foolz\SphinxQL\Exception\SphinxQLException::class); |
905
|
|
|
$this->expectExceptionMessage("There is no Queue present to execute."); |
906
|
|
|
$this->createSphinxQL() |
907
|
|
|
->executeBatch() |
908
|
|
|
->getStored(); |
909
|
|
|
} |
910
|
|
|
|
911
|
|
|
/** |
912
|
|
|
* @covers \Foolz\SphinxQL\SphinxQL::resetWhere |
913
|
|
|
* @covers \Foolz\SphinxQL\SphinxQL::resetMatch |
914
|
|
|
* @covers \Foolz\SphinxQL\SphinxQL::resetGroupBy |
915
|
|
|
* @covers \Foolz\SphinxQL\SphinxQL::resetWithinGroupOrderBy |
916
|
|
|
* @covers \Foolz\SphinxQL\SphinxQL::resetOptions |
917
|
|
|
* @covers \Foolz\SphinxQL\SphinxQL::resetFacets |
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
|
|
|
->facet( |
941
|
|
|
(new Facet(self::$conn))->facet(array('gid')) |
942
|
|
|
) |
943
|
|
|
->resetFacets() |
944
|
|
|
->compile() |
945
|
|
|
->getCompiled(); |
946
|
|
|
|
947
|
|
|
$this->assertEquals('SELECT * FROM rt', $result); |
948
|
|
|
} |
949
|
|
|
|
950
|
|
|
/** |
951
|
|
|
* @covers \Foolz\SphinxQL\SphinxQL::select |
952
|
|
|
*/ |
953
|
|
|
public function testSelect() |
954
|
|
|
{ |
955
|
|
|
$this->refill(); |
956
|
|
|
$result = $this->createSphinxQL() |
|
|
|
|
957
|
|
|
->select(array('id', 'gid')) |
958
|
|
|
->from('rt') |
959
|
|
|
->execute() |
960
|
|
|
->getStored(); |
961
|
|
|
$this->assertArrayHasKey('id', $result[0]); |
962
|
|
|
$this->assertArrayHasKey('gid', $result[0]); |
963
|
|
|
$this->assertEquals('10', $result[0]['id']); |
964
|
|
|
$this->assertEquals('9003', $result[0]['gid']); |
965
|
|
|
|
966
|
|
|
$result = $this->createSphinxQL() |
|
|
|
|
967
|
|
|
->select('id', 'gid') |
968
|
|
|
->from('rt') |
969
|
|
|
->execute() |
970
|
|
|
->getStored(); |
971
|
|
|
$this->assertArrayHasKey('id', $result[0]); |
972
|
|
|
$this->assertArrayHasKey('gid', $result[0]); |
973
|
|
|
$this->assertEquals('10', $result[0]['id']); |
974
|
|
|
$this->assertEquals('9003', $result[0]['gid']); |
975
|
|
|
|
976
|
|
|
$result = $this->createSphinxQL() |
|
|
|
|
977
|
|
|
->select(array('id')) |
978
|
|
|
->from('rt') |
979
|
|
|
->execute() |
980
|
|
|
->getStored(); |
981
|
|
|
$this->assertArrayHasKey('id', $result[0]); |
982
|
|
|
$this->assertArrayNotHasKey('gid', $result[0]); |
983
|
|
|
$this->assertEquals('10', $result[0]['id']); |
984
|
|
|
|
985
|
|
|
$result = $this->createSphinxQL() |
|
|
|
|
986
|
|
|
->select('id') |
987
|
|
|
->from('rt') |
988
|
|
|
->execute() |
989
|
|
|
->getStored(); |
990
|
|
|
$this->assertArrayHasKey('id', $result[0]); |
991
|
|
|
$this->assertArrayNotHasKey('gid', $result[0]); |
992
|
|
|
$this->assertEquals('10', $result[0]['id']); |
993
|
|
|
} |
994
|
|
|
|
995
|
|
|
public function testSubselect() |
996
|
|
|
{ |
997
|
|
|
$this->refill(); |
998
|
|
|
$query = $this->createSphinxQL() |
999
|
|
|
->select() |
1000
|
|
|
->from(function ($q) { |
1001
|
|
|
$q->select('id') |
1002
|
|
|
->from('rt') |
1003
|
|
|
->orderBy('id', 'DESC'); |
1004
|
|
|
}) |
1005
|
|
|
->orderBy('id', 'ASC'); |
1006
|
|
|
$this->assertEquals( |
1007
|
|
|
'SELECT * FROM (SELECT id FROM rt ORDER BY id DESC) ORDER BY id ASC', |
1008
|
|
|
$query->compile()->getCompiled() |
1009
|
|
|
); |
1010
|
|
|
$result = $query |
|
|
|
|
1011
|
|
|
->execute() |
1012
|
|
|
->getStored(); |
1013
|
|
|
$this->assertArrayHasKey('id', $result[0]); |
1014
|
|
|
$this->assertArrayNotHasKey('gid', $result[0]); |
1015
|
|
|
$this->assertEquals('10', $result[0]['id']); |
1016
|
|
|
|
1017
|
|
|
$subquery = $this->createSphinxQL() |
1018
|
|
|
->select('id') |
1019
|
|
|
->from('rt') |
1020
|
|
|
->orderBy('id', 'DESC'); |
1021
|
|
|
$query = $this->createSphinxQL() |
1022
|
|
|
->select() |
1023
|
|
|
->from($subquery) |
1024
|
|
|
->orderBy('id', 'ASC'); |
1025
|
|
|
$this->assertEquals( |
1026
|
|
|
'SELECT id FROM rt ORDER BY id DESC', |
1027
|
|
|
$subquery->compile()->getCompiled() |
1028
|
|
|
); |
1029
|
|
|
$this->assertEquals( |
1030
|
|
|
'SELECT * FROM (SELECT id FROM rt ORDER BY id DESC) ORDER BY id ASC', |
1031
|
|
|
$query->compile()->getCompiled() |
1032
|
|
|
); |
1033
|
|
|
$result = $subquery |
|
|
|
|
1034
|
|
|
->execute() |
1035
|
|
|
->getStored(); |
1036
|
|
|
$this->assertArrayHasKey('id', $result[0]); |
1037
|
|
|
$this->assertArrayNotHasKey('gid', $result[0]); |
1038
|
|
|
$this->assertEquals('17', $result[0]['id']); |
1039
|
|
|
$result = $query |
|
|
|
|
1040
|
|
|
->execute() |
1041
|
|
|
->getStored(); |
1042
|
|
|
$this->assertArrayHasKey('id', $result[0]); |
1043
|
|
|
$this->assertArrayNotHasKey('gid', $result[0]); |
1044
|
|
|
$this->assertEquals('10', $result[0]['id']); |
1045
|
|
|
} |
1046
|
|
|
|
1047
|
|
|
/** |
1048
|
|
|
* @covers \Foolz\SphinxQL\SphinxQL::setSelect |
1049
|
|
|
*/ |
1050
|
|
|
public function testSetSelect() |
1051
|
|
|
{ |
1052
|
|
|
$this->refill(); |
1053
|
|
|
$q1 = $this->createSphinxQL() |
1054
|
|
|
->select(array('id', 'gid')) |
1055
|
|
|
->from('rt'); |
1056
|
|
|
$q2 = clone $q1; |
1057
|
|
|
$q2->setSelect(array('id')); |
1058
|
|
|
$result = $q1 |
|
|
|
|
1059
|
|
|
->execute() |
1060
|
|
|
->getStored(); |
1061
|
|
|
$this->assertArrayHasKey('id', $result[0]); |
1062
|
|
|
$this->assertArrayHasKey('gid', $result[0]); |
1063
|
|
|
$result = $q2 |
|
|
|
|
1064
|
|
|
->execute() |
1065
|
|
|
->getStored(); |
1066
|
|
|
$this->assertArrayHasKey('id', $result[0]); |
1067
|
|
|
$this->assertArrayNotHasKey('gid', $result[0]); |
1068
|
|
|
|
1069
|
|
|
$q1 = $this->createSphinxQL() |
1070
|
|
|
->select('id', 'gid') |
1071
|
|
|
->from('rt'); |
1072
|
|
|
$q2 = clone $q1; |
1073
|
|
|
$q2->setSelect('id'); |
1074
|
|
|
$result = $q1 |
|
|
|
|
1075
|
|
|
->execute() |
1076
|
|
|
->getStored(); |
1077
|
|
|
$this->assertArrayHasKey('id', $result[0]); |
1078
|
|
|
$this->assertArrayHasKey('gid', $result[0]); |
1079
|
|
|
$result = $q2 |
|
|
|
|
1080
|
|
|
->execute() |
1081
|
|
|
->getStored(); |
1082
|
|
|
$this->assertArrayHasKey('id', $result[0]); |
1083
|
|
|
$this->assertArrayNotHasKey('gid', $result[0]); |
1084
|
|
|
} |
1085
|
|
|
|
1086
|
|
|
/** |
1087
|
|
|
* @covers \Foolz\SphinxQL\SphinxQL::getSelect |
1088
|
|
|
*/ |
1089
|
|
|
public function testGetSelect() |
1090
|
|
|
{ |
1091
|
|
|
$query = $this->createSphinxQL() |
1092
|
|
|
->select('id', 'gid') |
1093
|
|
|
->from('rt'); |
1094
|
|
|
$this->assertEquals(array('id', 'gid'), $query->getSelect()); |
1095
|
|
|
} |
1096
|
|
|
|
1097
|
|
|
/** |
1098
|
|
|
* @covers \Foolz\SphinxQL\SphinxQL::facet |
1099
|
|
|
* @covers \Foolz\SphinxQL\SphinxQL::compileSelect |
1100
|
|
|
*/ |
1101
|
|
|
public function testFacet() |
1102
|
|
|
{ |
1103
|
|
|
$this->refill(); |
1104
|
|
|
|
1105
|
|
|
// test both setting and not setting the connection |
1106
|
|
|
foreach (array(self::$conn, null) as $conn) { |
1107
|
|
|
$result = $this->createSphinxQL() |
1108
|
|
|
->select() |
1109
|
|
|
->from('rt') |
1110
|
|
|
->facet((new Facet($conn)) |
1111
|
|
|
->facetFunction('INTERVAL', array('gid', 300, 600)) |
1112
|
|
|
->orderByFunction('FACET', '', 'ASC')) |
|
|
|
|
1113
|
|
|
->executeBatch() |
1114
|
|
|
->getStored(); |
1115
|
|
|
|
1116
|
|
|
$this->assertArrayHasKey('id', $result[0][0]); |
1117
|
|
|
$this->assertArrayHasKey('interval(gid,300,600)', $result[1][0]); |
1118
|
|
|
$this->assertArrayHasKey('count(*)', $result[1][0]); |
1119
|
|
|
|
1120
|
|
|
$this->assertEquals('2', $result[1][0]['count(*)']); |
1121
|
|
|
$this->assertEquals('5', $result[1][1]['count(*)']); |
1122
|
|
|
$this->assertEquals('1', $result[1][2]['count(*)']); |
1123
|
|
|
|
1124
|
|
|
$result = $this->createSphinxQL() |
1125
|
|
|
->select() |
1126
|
|
|
->from('rt') |
1127
|
|
|
->facet((new Facet($conn)) |
1128
|
|
|
->facet(array('gid')) |
1129
|
|
|
->orderBy('gid', 'ASC')) |
1130
|
|
|
->executeBatch() |
1131
|
|
|
->getStored(); |
1132
|
|
|
|
1133
|
|
|
$this->assertArrayHasKey('id', $result[0][0]); |
1134
|
|
|
$this->assertArrayHasKey('gid', $result[1][0]); |
1135
|
|
|
$this->assertArrayHasKey('count(*)', $result[1][0]); |
1136
|
|
|
|
1137
|
|
|
$this->assertEquals('1', $result[1][0]['count(*)']); |
1138
|
|
|
$this->assertEquals('200', $result[1][0]['gid']); |
1139
|
|
|
$this->assertEquals('3', $result[1][2]['count(*)']); |
1140
|
|
|
$this->assertEquals('2', $result[1][3]['count(*)']); |
1141
|
|
|
} |
1142
|
|
|
} |
1143
|
|
|
|
1144
|
|
|
// issue #82 |
1145
|
|
|
public function testClosureMisuse() |
1146
|
|
|
{ |
1147
|
|
|
$query = $this->createSphinxQL() |
1148
|
|
|
->select() |
1149
|
|
|
->from('strlen') |
1150
|
|
|
->orderBy('id', 'ASC'); |
1151
|
|
|
$this->assertEquals( |
1152
|
|
|
'SELECT * FROM strlen ORDER BY id ASC', |
1153
|
|
|
$query->compile()->getCompiled() |
1154
|
|
|
); |
1155
|
|
|
|
1156
|
|
|
$query = $this->createSphinxQL() |
1157
|
|
|
->select() |
1158
|
|
|
->from('rt') |
1159
|
|
|
->match('strlen', 'value'); |
1160
|
|
|
$this->assertEquals( |
1161
|
|
|
"SELECT * FROM rt WHERE MATCH('(@strlen value)')", |
1162
|
|
|
$query->compile()->getCompiled() |
1163
|
|
|
); |
1164
|
|
|
} |
1165
|
|
|
} |
1166
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.