1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Foolz\SphinxQL\Drivers\ConnectionInterface; |
4
|
|
|
use Foolz\SphinxQL\SphinxQL; |
5
|
|
|
use Foolz\SphinxQL\Helper; |
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
|
|
|
public function setUp() |
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()->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
|
|
|
/** |
167
|
|
|
* @expectedException Foolz\SphinxQL\Exception\DatabaseException |
168
|
|
|
* @expectedExceptionMessage Sphinx expr: syntax error |
169
|
|
|
*/ |
170
|
|
|
public function testUdfNotInstalled() |
171
|
|
|
{ |
172
|
|
|
$this->conn->query('SELECT MY_UDF()'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function testCreateFunction() |
176
|
|
|
{ |
177
|
|
|
$this->createHelper()->createFunction('my_udf', 'INT', 'test_udf.so')->execute(); |
178
|
|
|
$this->assertSame( |
179
|
|
|
array(array('MY_UDF()' => '42')), |
180
|
|
|
$this->conn->query('SELECT MY_UDF()')->getStored() |
181
|
|
|
); |
182
|
|
|
$this->createHelper()->dropFunction('my_udf')->execute(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @covers \Foolz\SphinxQL\Helper::truncateRtIndex |
187
|
|
|
*/ |
188
|
|
|
public function testTruncateRtIndex() |
189
|
|
|
{ |
190
|
|
|
$this->createSphinxQL() |
191
|
|
|
->insert() |
192
|
|
|
->into('rt') |
193
|
|
|
->set(array( |
194
|
|
|
'id' => 1, |
195
|
|
|
'title' => 'this is a title', |
196
|
|
|
'content' => 'this is the content', |
197
|
|
|
'gid' => 100 |
198
|
|
|
)) |
199
|
|
|
->execute(); |
200
|
|
|
|
201
|
|
|
$result = $this->createSphinxQL() |
202
|
|
|
->select() |
203
|
|
|
->from('rt') |
204
|
|
|
->execute() |
205
|
|
|
->getStored(); |
206
|
|
|
|
207
|
|
|
$this->assertCount(1, $result); |
208
|
|
|
|
209
|
|
|
$this->createHelper()->truncateRtIndex('rt')->execute(); |
210
|
|
|
|
211
|
|
|
$result = $this->createSphinxQL() |
212
|
|
|
->select() |
213
|
|
|
->from('rt') |
214
|
|
|
->execute() |
215
|
|
|
->getStored(); |
216
|
|
|
|
217
|
|
|
$this->assertCount(0, $result); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
// actually executing these queries may not be useful nor easy to test |
221
|
|
|
public function testMiscellaneous() |
222
|
|
|
{ |
223
|
|
|
$query = $this->createHelper()->showMeta(); |
224
|
|
|
$this->assertEquals('SHOW META', $query->compile()->getCompiled()); |
225
|
|
|
|
226
|
|
|
$query = $this->createHelper()->showWarnings(); |
227
|
|
|
$this->assertEquals('SHOW WARNINGS', $query->compile()->getCompiled()); |
228
|
|
|
|
229
|
|
|
$query = $this->createHelper()->showStatus(); |
230
|
|
|
$this->assertEquals('SHOW STATUS', $query->compile()->getCompiled()); |
231
|
|
|
|
232
|
|
|
$query = $this->createHelper()->attachIndex('disk', 'rt'); |
233
|
|
|
$this->assertEquals('ATTACH INDEX disk TO RTINDEX rt', $query->compile()->getCompiled()); |
234
|
|
|
|
235
|
|
|
$query = $this->createHelper()->flushRtIndex('rt'); |
236
|
|
|
$this->assertEquals('FLUSH RTINDEX rt', $query->compile()->getCompiled()); |
237
|
|
|
|
238
|
|
|
$query = $this->createHelper()->optimizeIndex('rt'); |
239
|
|
|
$this->assertEquals('OPTIMIZE INDEX rt', $query->compile()->getCompiled()); |
240
|
|
|
|
241
|
|
|
$query = $this->createHelper()->showIndexStatus('rt'); |
242
|
|
|
$this->assertEquals('SHOW INDEX rt STATUS', $query->compile()->getCompiled()); |
243
|
|
|
|
244
|
|
|
$query = $this->createHelper()->flushRamchunk('rt'); |
245
|
|
|
$this->assertEquals('FLUSH RAMCHUNK rt', $query->compile()->getCompiled()); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
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.