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