1
|
|
|
<?php |
2
|
|
|
namespace Foolz\SphinxQL\Tests; |
3
|
|
|
|
4
|
|
|
use Foolz\SphinxQL\Drivers\ConnectionInterface; |
5
|
|
|
use Foolz\SphinxQL\Exception\ConnectionException; |
6
|
|
|
use Foolz\SphinxQL\Exception\DatabaseException; |
7
|
|
|
use Foolz\SphinxQL\Exception\SphinxQLException; |
8
|
|
|
use Foolz\SphinxQL\Helper; |
9
|
|
|
use Foolz\SphinxQL\SphinxQL; |
10
|
|
|
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
|
13
|
|
|
class HelperTest extends TestCase{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var ConnectionInterface |
17
|
|
|
*/ |
18
|
|
|
public static $connection; |
19
|
|
|
|
20
|
|
|
public static function setUpBeforeClass(): void{ |
21
|
|
|
self::$connection = TestUtil::getConnectionDriver(); |
22
|
|
|
self::$connection->setParam('port',9307); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @throws ConnectionException |
27
|
|
|
* @throws DatabaseException |
28
|
|
|
* @throws SphinxQLException |
29
|
|
|
*/ |
30
|
|
|
protected function setUp(): void{ |
31
|
|
|
$this->createSphinxQL()->query('TRUNCATE RTINDEX rt')->execute(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return SphinxQL |
36
|
|
|
*/ |
37
|
|
|
protected function createSphinxQL(): SphinxQL |
38
|
|
|
{ |
39
|
|
|
return new SphinxQL(self::$connection); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return Helper |
44
|
|
|
*/ |
45
|
|
|
protected function createHelper(): Helper |
46
|
|
|
{ |
47
|
|
|
return new Helper(self::$connection); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @throws ConnectionException |
52
|
|
|
* @throws DatabaseException |
53
|
|
|
* @throws SphinxQLException |
54
|
|
|
*/ |
55
|
|
|
public function testShowTables(): void |
56
|
|
|
{ |
57
|
|
|
$this->assertEquals([ |
58
|
|
|
[ |
59
|
|
|
'Index' => 'rt', |
60
|
|
|
'Type' => 'rt', |
61
|
|
|
] |
62
|
|
|
],$this->createHelper()->showTables('rt')->execute()->fetchAllAssoc()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @throws ConnectionException |
67
|
|
|
* @throws DatabaseException |
68
|
|
|
* @throws SphinxQLException |
69
|
|
|
*/ |
70
|
|
|
public function testDescribe(): void |
71
|
|
|
{ |
72
|
|
|
$describe = $this->createHelper()->describe('rt')->execute()->fetchAllAssoc(); |
73
|
|
|
array_shift($describe); |
74
|
|
|
|
75
|
|
|
$this->assertSame([ |
76
|
|
|
[ |
77
|
|
|
'Field' => 'title', |
78
|
|
|
'Type' => 'field', |
79
|
|
|
// 'Properties' => 'indexed', |
80
|
|
|
// 'Key' => '', |
81
|
|
|
], |
82
|
|
|
[ |
83
|
|
|
'Field' => 'content', |
84
|
|
|
'Type' => 'field', |
85
|
|
|
// 'Properties' => 'indexed', |
86
|
|
|
// 'Key' => '', |
87
|
|
|
], |
88
|
|
|
[ |
89
|
|
|
'Field' => 'gid', |
90
|
|
|
'Type' => 'uint', |
91
|
|
|
// 'Properties' => '', |
92
|
|
|
// 'Key' => '', |
93
|
|
|
], |
94
|
|
|
],$describe); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @throws ConnectionException |
99
|
|
|
* @throws DatabaseException |
100
|
|
|
* @throws SphinxQLException |
101
|
|
|
*/ |
102
|
|
|
public function testSetVariable(): void |
103
|
|
|
{ |
104
|
|
|
$this->createHelper()->setVariable('AUTOCOMMIT', 0)->execute(); |
105
|
|
|
$vars = Helper::pairsToAssoc($this->createHelper()->showVariables()->execute()->fetchAllAssoc()); |
106
|
|
|
$this->assertEquals(0, $vars['autocommit']); |
107
|
|
|
|
108
|
|
|
$this->createHelper()->setVariable('AUTOCOMMIT', 1)->execute(); |
109
|
|
|
$vars = Helper::pairsToAssoc($this->createHelper()->showVariables()->execute()->fetchAllAssoc()); |
110
|
|
|
$this->assertEquals(1, $vars['autocommit']); |
111
|
|
|
|
112
|
|
|
$this->createHelper()->setVariable('@foo', 1, true); |
113
|
|
|
$this->createHelper()->setVariable('@foo', [0], true); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @throws ConnectionException |
118
|
|
|
* @throws DatabaseException |
119
|
|
|
* @throws SphinxQLException |
120
|
|
|
*/ |
121
|
|
|
public function testCallSnippets(): void |
122
|
|
|
{ |
123
|
|
|
$snippets = $this->createHelper()->callSnippets( |
124
|
|
|
'this is my document text', |
125
|
|
|
'rt', |
126
|
|
|
'is' |
127
|
|
|
)->execute()->fetchAllAssoc(); |
128
|
|
|
$this->assertEquals([ |
129
|
|
|
[ |
130
|
|
|
'snippet' => 'this <b>is</b> my document text', |
131
|
|
|
] |
132
|
|
|
],$snippets); |
133
|
|
|
|
134
|
|
|
$snippets = $this->createHelper()->callSnippets( |
135
|
|
|
'this is my document text', |
136
|
|
|
'rt', |
137
|
|
|
'is', |
138
|
|
|
[ |
139
|
|
|
// 'query_mode' => 1, |
140
|
|
|
'before_match' => '<em>', |
141
|
|
|
'after_match' => '</em>', |
142
|
|
|
] |
143
|
|
|
)->execute()->fetchAllAssoc(); |
144
|
|
|
$this->assertEquals([ |
145
|
|
|
[ |
146
|
|
|
'snippet' => 'this <em>is</em> my document text', |
147
|
|
|
] |
148
|
|
|
],$snippets); |
149
|
|
|
|
150
|
|
|
$snippets = $this->createHelper()->callSnippets([ |
151
|
|
|
'this is my document text', |
152
|
|
|
'another document', |
153
|
|
|
],'rt','is',[ |
154
|
|
|
'allow_empty' => 1, |
155
|
|
|
])->execute()->fetchAllAssoc(); |
156
|
|
|
$this->assertEquals([ |
157
|
|
|
[ |
158
|
|
|
'snippet' => 'this <b>is</b> my document text', |
159
|
|
|
], |
160
|
|
|
[ |
161
|
|
|
'snippet' => '', |
162
|
|
|
], |
163
|
|
|
],$snippets); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @throws ConnectionException |
168
|
|
|
* @throws DatabaseException |
169
|
|
|
* @throws SphinxQLException |
170
|
|
|
*/ |
171
|
|
|
public function testCallKeywords(): void |
172
|
|
|
{ |
173
|
|
|
$keywords = $this->createHelper()->callKeywords( |
174
|
|
|
'test case', |
175
|
|
|
'rt' |
176
|
|
|
)->execute()->fetchAllAssoc(); |
177
|
|
|
$this->assertEquals([ |
178
|
|
|
[ |
179
|
|
|
'qpos' => '1', |
180
|
|
|
'tokenized' => 'test', |
181
|
|
|
'normalized' => 'test', |
182
|
|
|
], |
183
|
|
|
[ |
184
|
|
|
'qpos' => '2', |
185
|
|
|
'tokenized' => 'case', |
186
|
|
|
'normalized' => 'case', |
187
|
|
|
], |
188
|
|
|
],$keywords); |
189
|
|
|
|
190
|
|
|
$keywords = $this->createHelper()->callKeywords( |
191
|
|
|
'test case', |
192
|
|
|
'rt', |
193
|
|
|
1 |
194
|
|
|
)->execute()->fetchAllAssoc(); |
195
|
|
|
$this->assertEquals([ |
196
|
|
|
[ |
197
|
|
|
'qpos' => '1', |
198
|
|
|
'tokenized' => 'test', |
199
|
|
|
'normalized' => 'test', |
200
|
|
|
'docs' => '0', |
201
|
|
|
'hits' => '0', |
202
|
|
|
], |
203
|
|
|
[ |
204
|
|
|
'qpos' => '2', |
205
|
|
|
'tokenized' => 'case', |
206
|
|
|
'normalized' => 'case', |
207
|
|
|
'docs' => '0', |
208
|
|
|
'hits' => '0', |
209
|
|
|
], |
210
|
|
|
],$keywords); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @throws ConnectionException |
215
|
|
|
* @throws DatabaseException |
216
|
|
|
*/ |
217
|
|
|
public function testUdfNotInstalled(): void |
218
|
|
|
{ |
219
|
|
|
$this->expectException(DatabaseException::class); |
220
|
|
|
$this->expectExceptionMessage('Sphinx expr: syntax error'); |
221
|
|
|
|
222
|
|
|
self::$connection->query('SELECT MY_UDF()'); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
// /** |
226
|
|
|
// * @throws ConnectionException |
227
|
|
|
// * @throws DatabaseException |
228
|
|
|
// * @throws SphinxQLException |
229
|
|
|
// */ |
230
|
|
|
// public function testCreateFunction(): void |
231
|
|
|
// { |
232
|
|
|
// $this->createHelper()->createFunction('my_udf', 'INT', 'test_udf.so')->execute(); |
233
|
|
|
// |
234
|
|
|
// $this->assertSame([ |
235
|
|
|
// [ |
236
|
|
|
// 'MY_UDF()' => '42', |
237
|
|
|
// ], |
238
|
|
|
// ],self::$connection->query('SELECT MY_UDF()')->fetchAllAssoc()); |
239
|
|
|
// |
240
|
|
|
// $this->createHelper()->dropFunction('my_udf')->execute(); |
241
|
|
|
// } |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @throws ConnectionException |
245
|
|
|
* @throws DatabaseException |
246
|
|
|
* @throws SphinxQLException |
247
|
|
|
*/ |
248
|
|
|
public function testTruncateRtIndex(): void |
249
|
|
|
{ |
250
|
|
|
$this->createSphinxQL() |
251
|
|
|
->insert() |
252
|
|
|
->into('rt') |
253
|
|
|
->set([ |
254
|
|
|
'id' => 1, |
255
|
|
|
'title' => 'this is a title', |
256
|
|
|
'content' => 'this is the content', |
257
|
|
|
'gid' => 100, |
258
|
|
|
]) |
259
|
|
|
->execute(); |
260
|
|
|
|
261
|
|
|
$result = $this->createSphinxQL() |
262
|
|
|
->select() |
263
|
|
|
->from('rt') |
264
|
|
|
->execute() |
265
|
|
|
->fetchAllAssoc(); |
266
|
|
|
|
267
|
|
|
$this->assertCount(1, $result); |
268
|
|
|
|
269
|
|
|
$this->createHelper()->truncateRtIndex('rt')->execute(); |
270
|
|
|
|
271
|
|
|
$result = $this->createSphinxQL() |
272
|
|
|
->select() |
273
|
|
|
->from('rt') |
274
|
|
|
->execute() |
275
|
|
|
->fetchAllAssoc(); |
276
|
|
|
|
277
|
|
|
$this->assertCount(0, $result); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Actually executing these queries may not be useful nor easy to test |
282
|
|
|
* @throws ConnectionException |
283
|
|
|
* @throws DatabaseException |
284
|
|
|
* @throws SphinxQLException |
285
|
|
|
*/ |
286
|
|
|
public function testMiscellaneous(): void |
287
|
|
|
{ |
288
|
|
|
$query = $this->createHelper()->showMeta(); |
289
|
|
|
$this->assertEquals('SHOW META', $query->compile()->getCompiled()); |
290
|
|
|
|
291
|
|
|
$query = $this->createHelper()->showWarnings(); |
292
|
|
|
$this->assertEquals('SHOW WARNINGS', $query->compile()->getCompiled()); |
293
|
|
|
|
294
|
|
|
$query = $this->createHelper()->showStatus(); |
295
|
|
|
$this->assertEquals('SHOW STATUS', $query->compile()->getCompiled()); |
296
|
|
|
|
297
|
|
|
$query = $this->createHelper()->attachIndex('disk', 'rt'); |
298
|
|
|
$this->assertEquals('ATTACH INDEX disk TO RTINDEX rt', $query->compile()->getCompiled()); |
299
|
|
|
|
300
|
|
|
$query = $this->createHelper()->flushRtIndex('rt'); |
301
|
|
|
$this->assertEquals('FLUSH RTINDEX rt', $query->compile()->getCompiled()); |
302
|
|
|
|
303
|
|
|
$query = $this->createHelper()->optimizeIndex('rt'); |
304
|
|
|
$this->assertEquals('OPTIMIZE INDEX rt', $query->compile()->getCompiled()); |
305
|
|
|
|
306
|
|
|
$query = $this->createHelper()->showIndexStatus('rt'); |
307
|
|
|
$this->assertEquals('SHOW INDEX rt STATUS', $query->compile()->getCompiled()); |
308
|
|
|
|
309
|
|
|
$query = $this->createHelper()->flushRamchunk('rt'); |
310
|
|
|
$this->assertEquals('FLUSH RAMCHUNK rt', $query->compile()->getCompiled()); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
} |