1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Foolz\SphinxQL\Exception\SphinxQLException; |
4
|
|
|
use Foolz\SphinxQL\Percolate; |
5
|
|
|
use Foolz\SphinxQL\Tests\TestUtil; |
6
|
|
|
use Foolz\SphinxQL\SphinxQL; |
7
|
|
|
/** |
8
|
|
|
* @package Foolz\SphinxQL |
9
|
|
|
* @author Vicent Valls |
10
|
|
|
*/ |
11
|
|
|
class PercolateQueriesTest extends \PHPUnit\Framework\TestCase |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
public static $conn = null; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
public static function setUpBeforeClass() |
17
|
|
|
{ |
18
|
|
|
$conn = TestUtil::getConnectionDriver(); |
19
|
|
|
$conn->setParam('port', 9307); |
20
|
|
|
self::$conn = $conn; |
21
|
|
|
|
22
|
|
|
$sphinxQL = new SphinxQL(self::$conn); |
23
|
|
|
$sphinxQL->query('TRUNCATE RTINDEX pq')->execute(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @dataProvider insertProvider |
29
|
|
|
* @throws \Foolz\SphinxQL\Exception\SphinxQLException |
30
|
|
|
*/ |
31
|
|
|
public function testInsert($testNumber, $query, $index, $tags, $filter, $compiledQuery) |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
if ($testNumber == 2) { |
35
|
|
|
$this->expectException(SphinxQLException::class); |
36
|
|
|
$this->expectExceptionMessage('Index can\'t be empty'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if ($testNumber == 3) { |
40
|
|
|
$this->expectException(SphinxQLException::class); |
41
|
|
|
$this->expectExceptionMessage('Query can\'t be empty'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ($testNumber == 10) { |
45
|
|
|
$this->expectException(SphinxQLException::class); |
46
|
|
|
$this->expectExceptionMessage('Allow only one filter. If there is a comma in the text, it must be shielded'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
$percolate = new Percolate(self::$conn); |
51
|
|
|
$percolate |
52
|
|
|
->insert($query) |
53
|
|
|
->into($index) |
54
|
|
|
->tags($tags) |
55
|
|
|
->filter($filter) |
56
|
|
|
->execute(); |
57
|
|
|
|
58
|
|
|
if (in_array($testNumber, [1, 4, 5, 6, 7, 8, 9, 11])) { |
59
|
|
|
$this->assertEquals($compiledQuery, $percolate->getLastQuery()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
//$this->markTestIncomplete(true); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function insertProvider() |
67
|
|
|
{ |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* 1) Just insert |
71
|
|
|
* 2) Insert empty index |
72
|
|
|
* 3) Insert empty query |
73
|
|
|
* 4) Insert with special symbols |
74
|
|
|
* 5) Insert with tags as string without filter |
75
|
|
|
* 6) Insert with tags as array of string without filter |
76
|
|
|
* 7) Insert tags with special symbols |
77
|
|
|
* 8) Insert with filter, withowt tags |
78
|
|
|
* 9) Insert filter with special symbols |
79
|
|
|
* 10) Insert two filters |
80
|
|
|
* 11) Insert filter + tags |
81
|
|
|
*/ |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
return [ |
85
|
|
|
[ |
86
|
|
|
1, |
87
|
|
|
'full text query terms', |
88
|
|
|
'pq', |
89
|
|
|
null, |
90
|
|
|
null, |
91
|
|
|
"INSERT INTO pq (query) VALUES ('full text query terms')" |
92
|
|
|
], |
93
|
|
|
|
94
|
|
|
[ |
95
|
|
|
2, |
96
|
|
|
'full text query terms', |
97
|
|
|
null, |
98
|
|
|
null, |
99
|
|
|
null, |
100
|
|
|
null |
101
|
|
|
], |
102
|
|
|
|
103
|
|
|
[ |
104
|
|
|
3, |
105
|
|
|
null, |
106
|
|
|
'pq', |
107
|
|
|
null, |
108
|
|
|
null, |
109
|
|
|
null |
110
|
|
|
], |
111
|
|
|
|
112
|
|
|
[ |
113
|
|
|
4, |
114
|
|
|
'@doc (text) \' ^ $ " | ! ~ / = >< & - \query terms', |
115
|
|
|
'pq', |
116
|
|
|
null, |
117
|
|
|
null, |
118
|
|
|
'INSERT INTO pq (query) VALUES (\'@doc (text) \\\\\\\' ^ $ \\\\\\" | \\\\! \\\\~ \\\\/ = >\\\\< & \\\\- \\\\\\\\query terms\')' |
119
|
|
|
], |
120
|
|
|
|
121
|
|
|
[ |
122
|
|
|
5, |
123
|
|
|
'@subject match by field', |
124
|
|
|
'pq', |
125
|
|
|
'tag2,tag3', |
126
|
|
|
'price>3', |
127
|
|
|
"INSERT INTO pq (query, tags, filters) VALUES ('@subject match by field', 'tag2,tag3', 'price>3')" |
128
|
|
|
], |
129
|
|
|
|
130
|
|
|
[ |
131
|
|
|
6, |
132
|
|
|
'@subject orange', |
133
|
|
|
'pq', |
134
|
|
|
['tag2', 'tag3'], |
135
|
|
|
null, |
136
|
|
|
"INSERT INTO pq (query, tags) VALUES ('@subject orange', 'tag2,tag3')" |
137
|
|
|
], |
138
|
|
|
|
139
|
|
|
[ |
140
|
|
|
7, |
141
|
|
|
'@subject orange', |
142
|
|
|
'pq', |
143
|
|
|
'@doc (text) \' ^ $ " | ! ~ / = >< & - \query terms', |
144
|
|
|
null, |
145
|
|
|
'INSERT INTO pq (query, tags) VALUES (\'@subject orange\', \'@doc (text) \\\\\\\' ^ $ \\\\\" | \\\\! \\\\~ \\\\/ = >\\\\< & \\\\- \\\\\\\\query terms\')' |
146
|
|
|
], |
147
|
|
|
|
148
|
|
|
[ |
149
|
|
|
8, |
150
|
|
|
'catch me', |
151
|
|
|
'pq', |
152
|
|
|
null, |
153
|
|
|
'price>3', |
154
|
|
|
'INSERT INTO pq (query, filters) VALUES (\'catch me\', \'price>3\')' |
155
|
|
|
], |
156
|
|
|
|
157
|
|
|
[ |
158
|
|
|
9, |
159
|
|
|
'catch me if can', |
160
|
|
|
'pq', |
161
|
|
|
null, |
162
|
|
|
'p\@r\'ice>3', |
163
|
|
|
'INSERT INTO pq (query, filters) VALUES (\'catch me if can\', \'price>3\')' |
164
|
|
|
], |
165
|
|
|
|
166
|
|
|
[ |
167
|
|
|
10, |
168
|
|
|
'catch me if can', |
169
|
|
|
'pq', |
170
|
|
|
null, |
171
|
|
|
'price>3, secondFilter>0', |
172
|
|
|
null |
173
|
|
|
], |
174
|
|
|
[ |
175
|
|
|
11, |
176
|
|
|
'orange|apple|cherry', |
177
|
|
|
'pq', |
178
|
|
|
['tag2', 'tag3'], |
179
|
|
|
'price>3', |
180
|
|
|
"INSERT INTO pq (query, tags, filters) VALUES ('orange|apple|cherry', 'tag2,tag3', 'price>3')" |
181
|
|
|
], |
182
|
|
|
]; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @dataProvider callPqProvider |
187
|
|
|
* @throws \Foolz\SphinxQL\Exception\SphinxQLException |
188
|
|
|
*/ |
189
|
|
|
|
190
|
|
|
public function testPercolate($testNumber, $index, $documents, $options, $result) |
191
|
|
|
{ |
192
|
|
|
if ($testNumber == 2) { |
193
|
|
|
$this->expectException(SphinxQLException::class); |
194
|
|
|
$this->expectExceptionMessage('Document can\'t be empty'); |
195
|
|
|
|
196
|
|
|
} elseif ($testNumber == 3) { |
197
|
|
|
$this->expectException(SphinxQLException::class); |
198
|
|
|
$this->expectExceptionMessage('Index can\'t be empty'); |
199
|
|
|
|
200
|
|
|
} elseif ($testNumber == 12) { |
201
|
|
|
$this->expectException(SphinxQLException::class); |
202
|
|
|
$this->expectExceptionMessage('Documents must be in json format'); |
203
|
|
|
|
204
|
|
|
} elseif ($testNumber == 13) { |
205
|
|
|
$this->expectException(SphinxQLException::class); |
206
|
|
|
$this->expectExceptionMessage('Documents array must be associate'); |
207
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$query = (new Percolate(self::$conn)) |
211
|
|
|
->callPQ() |
212
|
|
|
->from($index) |
213
|
|
|
->documents($documents) |
214
|
|
|
->options($options) |
215
|
|
|
->execute(); |
216
|
|
|
|
217
|
|
|
|
218
|
|
|
if (in_array($testNumber, [1, 4, 5, 6, 7, 8, 9, 11])) { |
219
|
|
|
$query = $query->fetchAllAssoc(); |
220
|
|
|
$this->assertEquals($result[0], $query[0]['Query']); |
221
|
|
|
$this->assertEquals($result[1], count($query)); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
if ($testNumber == 10) { |
225
|
|
|
$query = $query->fetchAllAssoc(); |
226
|
|
|
$this->assertEquals($result[0], $query[0]['UID']); |
227
|
|
|
$this->assertEquals($result[1], count($query)); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function callPqProvider() |
233
|
|
|
{ |
234
|
|
|
/** |
235
|
|
|
* 1) Call PQ |
236
|
|
|
* 2) Document empty |
237
|
|
|
* 3) Index empty |
238
|
|
|
* 4) Documents array of string |
239
|
|
|
* 5) Documents associate array |
240
|
|
|
* 6) Documents array of associate array |
241
|
|
|
* 7) Documents jsonObject |
242
|
|
|
* 8) Documents jsonArray of jsonObject |
243
|
|
|
* 9) Documents phpArray of jsonObject |
244
|
|
|
* 10) Option OPTION_QUERY |
245
|
|
|
* 11) Option OPTION_DOCS |
246
|
|
|
* Throws OPTION_DOCS_JSON |
247
|
|
|
* 12) Not json string |
248
|
|
|
* 13) Not array with non json string |
249
|
|
|
*/ |
250
|
|
|
|
251
|
|
|
|
252
|
|
|
return [ |
253
|
|
|
[1, 'pq', 'full text query terms', [Percolate::OPTION_QUERY => 1], ['full text query terms', 2]], |
254
|
|
|
[2, 'pq', '', [], null], |
255
|
|
|
[3, '', 'full', [], null], |
256
|
|
|
[ |
257
|
|
|
4, |
258
|
|
|
'pq', |
259
|
|
|
['query terms', 'full text query terms'], |
260
|
|
|
[Percolate::OPTION_QUERY => 1], |
261
|
|
|
['full text query terms', 2] |
262
|
|
|
], |
263
|
|
|
[5, 'pq', ['subject' => 'document about orange'], [Percolate::OPTION_QUERY => 1], ['@subject orange', 2]], |
264
|
|
|
[ |
265
|
|
|
6, |
266
|
|
|
'pq', |
267
|
|
|
[['subject' => 'document about orange'], ['subject' => 'match by field', 'price' => 1]], |
268
|
|
|
[Percolate::OPTION_QUERY => 1], |
269
|
|
|
['@subject orange', 2] |
270
|
|
|
], |
271
|
|
|
[7, 'pq', '{"subject":"document about orange"}', [Percolate::OPTION_QUERY => 1], ['@subject orange', 2]], |
272
|
|
|
[ |
273
|
|
|
8, |
274
|
|
|
'pq', |
275
|
|
|
'[{"subject":"document about orange"}, {"subject":"match by field","price":10}]', |
276
|
|
|
[Percolate::OPTION_QUERY => 1], |
277
|
|
|
['@subject match by field', 3] |
278
|
|
|
], |
279
|
|
|
[ |
280
|
|
|
9, |
281
|
|
|
'pq', |
282
|
|
|
['{"subject":"document about orange"}', '{"subject":"match by field","price":10}'], |
283
|
|
|
[Percolate::OPTION_QUERY => 1], |
284
|
|
|
['@subject match by field', 3] |
285
|
|
|
], |
286
|
|
|
[10, 'pq', 'full text query terms', [Percolate::OPTION_QUERY => 0], [1, 2]], |
287
|
|
|
[ |
288
|
|
|
11, |
289
|
|
|
'pq', |
290
|
|
|
['{"subject":"document about orange"}', '{"subject":"match by field","price":10}'], |
291
|
|
|
[Percolate::OPTION_QUERY => 1, Percolate::OPTION_DOCS => 1], |
292
|
|
|
['@subject match by field', 3] |
293
|
|
|
], |
294
|
|
|
[12, 'pq', 'full text query terms', [Percolate::OPTION_DOCS_JSON => 1], null], |
295
|
|
|
[13, 'pq', ['full text query terms','full text'], [Percolate::OPTION_DOCS_JSON => 1], null], |
296
|
|
|
]; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
} |
300
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.