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