1 | <?php |
||||
2 | |||||
3 | use Foolz\SphinxQL\MatchBuilder; |
||||
4 | use Foolz\SphinxQL\SphinxQL; |
||||
5 | use Foolz\SphinxQL\Tests\TestUtil; |
||||
6 | |||||
7 | class MatchBuilderTest extends \PHPUnit\Framework\TestCase |
||||
8 | { |
||||
9 | public static $sphinxql = null; |
||||
10 | |||||
11 | public static function setUpBeforeClass(): void |
||||
12 | { |
||||
13 | $conn = TestUtil::getConnectionDriver(); |
||||
14 | $conn->setParam('port', 9307); |
||||
15 | self::$sphinxql = new SphinxQL($conn); |
||||
16 | } |
||||
17 | |||||
18 | /** |
||||
19 | * @return MatchBuilder |
||||
20 | */ |
||||
21 | protected function createMatch() |
||||
22 | { |
||||
23 | return new MatchBuilder(self::$sphinxql); |
||||
24 | } |
||||
25 | |||||
26 | public function testMatch() |
||||
27 | { |
||||
28 | $match = $this->createMatch() |
||||
29 | ->match('test'); |
||||
30 | $this->assertEquals('test', $match->compile()->getCompiled()); |
||||
31 | |||||
32 | $match = $this->createMatch() |
||||
33 | ->match('test case'); |
||||
34 | $this->assertEquals('(test case)', $match->compile()->getCompiled()); |
||||
35 | |||||
36 | $match = $this->createMatch() |
||||
37 | ->match(function ($m) { |
||||
38 | $m->match('a')->orMatch('b'); |
||||
39 | }); |
||||
40 | $this->assertEquals('(a | b)', $match->compile()->getCompiled()); |
||||
41 | |||||
42 | $sub = new MatchBuilder(self::$sphinxql); |
||||
43 | $sub->match('a')->orMatch('b'); |
||||
44 | $match = $this->createMatch() |
||||
45 | ->match($sub); |
||||
46 | $this->assertEquals('(a | b)', $match->compile()->getCompiled()); |
||||
47 | |||||
48 | $match = $this->createMatch() |
||||
49 | ->match('test|case'); |
||||
50 | $this->assertEquals('test\|case', $match->compile()->getCompiled()); |
||||
51 | |||||
52 | $match = $this->createMatch() |
||||
53 | ->match(SphinxQL::expr('test|case')); |
||||
54 | $this->assertEquals('test|case', $match->compile()->getCompiled()); |
||||
55 | } |
||||
56 | |||||
57 | public function testOrMatch() |
||||
58 | { |
||||
59 | $match = $this->createMatch() |
||||
60 | ->match('test')->orMatch(); |
||||
61 | $this->assertEquals('test |', $match->compile()->getCompiled()); |
||||
62 | |||||
63 | $match = $this->createMatch() |
||||
64 | ->match('test')->orMatch('case'); |
||||
65 | $this->assertEquals('test | case', $match->compile()->getCompiled()); |
||||
66 | } |
||||
67 | |||||
68 | public function testMaybe() |
||||
69 | { |
||||
70 | $match = $this->createMatch() |
||||
71 | ->match('test') |
||||
72 | ->maybe(); |
||||
73 | $this->assertEquals('test MAYBE', $match->compile()->getCompiled()); |
||||
74 | |||||
75 | $match = $this->createMatch() |
||||
76 | ->match('test') |
||||
77 | ->maybe('case'); |
||||
78 | $this->assertEquals('test MAYBE case', $match->compile()->getCompiled()); |
||||
79 | } |
||||
80 | |||||
81 | public function testNot() |
||||
82 | { |
||||
83 | $match = $this->createMatch() |
||||
84 | ->not() |
||||
85 | ->match('test'); |
||||
86 | $this->assertEquals('-test', $match->compile()->getCompiled()); |
||||
87 | |||||
88 | $match = $this->createMatch() |
||||
89 | ->not('test'); |
||||
90 | $this->assertEquals('-test', $match->compile()->getCompiled()); |
||||
91 | } |
||||
92 | |||||
93 | public function testField() |
||||
94 | { |
||||
95 | $match = $this->createMatch() |
||||
96 | ->field('*') |
||||
97 | ->match('test'); |
||||
98 | $this->assertEquals('@* test', $match->compile()->getCompiled()); |
||||
99 | |||||
100 | $match = $this->createMatch() |
||||
101 | ->field('title') |
||||
102 | ->match('test'); |
||||
103 | $this->assertEquals('@title test', $match->compile()->getCompiled()); |
||||
104 | |||||
105 | $match = $this->createMatch() |
||||
106 | ->field('body', 50) |
||||
107 | ->match('test'); |
||||
108 | $this->assertEquals('@body[50] test', $match->compile()->getCompiled()); |
||||
109 | |||||
110 | $match = $this->createMatch() |
||||
111 | ->field('title', 'body') |
||||
112 | ->match('test'); |
||||
113 | $this->assertEquals('@(title,body) test', $match->compile()->getCompiled()); |
||||
114 | |||||
115 | $match = $this->createMatch() |
||||
116 | ->field(array('title', 'body')) |
||||
117 | ->match('test'); |
||||
118 | $this->assertEquals('@(title,body) test', $match->compile()->getCompiled()); |
||||
119 | |||||
120 | $match = $this->createMatch() |
||||
121 | ->field('@relaxed') |
||||
122 | ->field('nosuchfield') |
||||
123 | ->match('test'); |
||||
124 | $this->assertEquals('@@relaxed @nosuchfield test', $match->compile()->getCompiled()); |
||||
125 | } |
||||
126 | |||||
127 | public function testIgnoreField() |
||||
128 | { |
||||
129 | $match = $this->createMatch() |
||||
130 | ->ignoreField('title') |
||||
131 | ->match('test'); |
||||
132 | $this->assertEquals('@!title test', $match->compile()->getCompiled()); |
||||
133 | |||||
134 | $match = $this->createMatch() |
||||
135 | ->ignoreField('title', 'body') |
||||
136 | ->match('test'); |
||||
137 | $this->assertEquals('@!(title,body) test', $match->compile()->getCompiled()); |
||||
138 | |||||
139 | $match = $this->createMatch() |
||||
140 | ->ignoreField(array('title', 'body')) |
||||
141 | ->match('test'); |
||||
142 | $this->assertEquals('@!(title,body) test', $match->compile()->getCompiled()); |
||||
143 | } |
||||
144 | |||||
145 | public function testPhrase() |
||||
146 | { |
||||
147 | $match = $this->createMatch() |
||||
148 | ->phrase('test case'); |
||||
149 | $this->assertEquals('"test case"', $match->compile()->getCompiled()); |
||||
150 | } |
||||
151 | |||||
152 | public function testOrPhrase() |
||||
153 | { |
||||
154 | $match = $this->createMatch() |
||||
155 | ->phrase('test case')->orPhrase('another case'); |
||||
156 | $this->assertEquals('"test case" | "another case"', $match->compile()->getCompiled()); |
||||
157 | } |
||||
158 | |||||
159 | public function testProximity() |
||||
160 | { |
||||
161 | $match = $this->createMatch() |
||||
162 | ->proximity('test case', 5); |
||||
163 | $this->assertEquals('"test case"~5', $match->compile()->getCompiled()); |
||||
164 | } |
||||
165 | |||||
166 | public function testQuorum() |
||||
167 | { |
||||
168 | $match = $this->createMatch() |
||||
169 | ->quorum('this is a test case', 3); |
||||
170 | $this->assertEquals('"this is a test case"/3', $match->compile()->getCompiled()); |
||||
171 | |||||
172 | $match = $this->createMatch() |
||||
173 | ->quorum('this is a test case', 0.5); |
||||
174 | $this->assertEquals('"this is a test case"/0.5', $match->compile()->getCompiled()); |
||||
175 | } |
||||
176 | |||||
177 | public function testBefore() |
||||
178 | { |
||||
179 | $match = $this->createMatch() |
||||
180 | ->match('test') |
||||
181 | ->before(); |
||||
182 | $this->assertEquals('test <<', $match->compile()->getCompiled()); |
||||
183 | |||||
184 | $match = $this->createMatch() |
||||
185 | ->match('test') |
||||
186 | ->before('case'); |
||||
187 | $this->assertEquals('test << case', $match->compile()->getCompiled()); |
||||
188 | } |
||||
189 | |||||
190 | public function testExact() |
||||
191 | { |
||||
192 | $match = $this->createMatch() |
||||
193 | ->match('test') |
||||
194 | ->exact('cases'); |
||||
195 | $this->assertEquals('test =cases', $match->compile()->getCompiled()); |
||||
196 | |||||
197 | $match = $this->createMatch() |
||||
198 | ->match('test') |
||||
199 | ->exact() |
||||
200 | ->phrase('specific cases'); |
||||
201 | $this->assertEquals('test ="specific cases"', $match->compile()->getCompiled()); |
||||
202 | } |
||||
203 | |||||
204 | public function testBoost() |
||||
205 | { |
||||
206 | $match = $this->createMatch() |
||||
207 | ->match('test') |
||||
208 | ->boost(1.2); |
||||
209 | $this->assertEquals('test^1.2', $match->compile()->getCompiled()); |
||||
210 | |||||
211 | $match = $this->createMatch() |
||||
212 | ->match('test') |
||||
213 | ->boost('case', 1.2); |
||||
214 | $this->assertEquals('test case^1.2', $match->compile()->getCompiled()); |
||||
215 | } |
||||
216 | |||||
217 | public function testNear() |
||||
218 | { |
||||
219 | $match = $this->createMatch() |
||||
220 | ->match('test') |
||||
221 | ->near(3); |
||||
222 | $this->assertEquals('test NEAR/3', $match->compile()->getCompiled()); |
||||
223 | |||||
224 | $match = $this->createMatch() |
||||
225 | ->match('test') |
||||
226 | ->near('case', 3); |
||||
227 | $this->assertEquals('test NEAR/3 case', $match->compile()->getCompiled()); |
||||
228 | } |
||||
229 | |||||
230 | public function testSentence() |
||||
231 | { |
||||
232 | $match = $this->createMatch() |
||||
233 | ->match('test') |
||||
234 | ->sentence(); |
||||
235 | $this->assertEquals('test SENTENCE', $match->compile()->getCompiled()); |
||||
236 | |||||
237 | $match = $this->createMatch() |
||||
238 | ->match('test') |
||||
239 | ->sentence('case'); |
||||
240 | $this->assertEquals('test SENTENCE case', $match->compile()->getCompiled()); |
||||
241 | } |
||||
242 | |||||
243 | public function testParagraph() |
||||
244 | { |
||||
245 | $match = $this->createMatch() |
||||
246 | ->match('test') |
||||
247 | ->paragraph(); |
||||
248 | $this->assertEquals('test PARAGRAPH', $match->compile()->getCompiled()); |
||||
249 | |||||
250 | $match = $this->createMatch() |
||||
251 | ->match('test') |
||||
252 | ->paragraph('case'); |
||||
253 | $this->assertEquals('test PARAGRAPH case', $match->compile()->getCompiled()); |
||||
254 | } |
||||
255 | |||||
256 | public function testZone() |
||||
257 | { |
||||
258 | $match = $this->createMatch() |
||||
259 | ->zone('th'); |
||||
260 | $this->assertEquals('ZONE:(th)', $match->compile()->getCompiled()); |
||||
261 | |||||
262 | $match = $this->createMatch() |
||||
263 | ->zone(array('h3', 'h4')); |
||||
264 | $this->assertEquals('ZONE:(h3,h4)', $match->compile()->getCompiled()); |
||||
265 | |||||
266 | $match = $this->createMatch() |
||||
267 | ->zone('th', 'test'); |
||||
268 | $this->assertEquals('ZONE:(th) test', $match->compile()->getCompiled()); |
||||
269 | } |
||||
270 | |||||
271 | public function testZonespan() |
||||
272 | { |
||||
273 | $match = $this->createMatch() |
||||
274 | ->zonespan('th'); |
||||
275 | $this->assertEquals('ZONESPAN:(th)', $match->compile()->getCompiled()); |
||||
276 | |||||
277 | $match = $this->createMatch() |
||||
278 | ->zonespan('th', 'test'); |
||||
279 | $this->assertEquals('ZONESPAN:(th) test', $match->compile()->getCompiled()); |
||||
280 | } |
||||
281 | |||||
282 | public function testCompile() |
||||
283 | { |
||||
284 | $match = $this->createMatch() |
||||
285 | ->phrase('hello world') |
||||
286 | ->field('title') |
||||
287 | ->proximity('example program', 5) |
||||
288 | ->field('body') |
||||
289 | ->match('python') |
||||
290 | ->not(function ($m) { |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
291 | $m->match('php') |
||||
292 | ->orMatch('perl'); |
||||
293 | }) |
||||
294 | ->field('*') |
||||
295 | ->match('code'); |
||||
296 | $this->assertEquals('"hello world" @title "example program"~5 @body python -(php | perl) @* code', $match->compile()->getCompiled()); |
||||
297 | |||||
298 | $match = $this->createMatch() |
||||
299 | ->match('bag of words') |
||||
300 | ->before() |
||||
301 | ->phrase('exact phrase') |
||||
302 | ->before('red') |
||||
303 | ->orMatch('green') |
||||
304 | ->orMatch('blue'); |
||||
305 | $this->assertEquals('(bag of words) << "exact phrase" << red | green | blue', $match->compile()->getCompiled()); |
||||
306 | |||||
307 | $match = $this->createMatch() |
||||
308 | ->match('aaa') |
||||
309 | ->not(function ($m) { |
||||
0 ignored issues
–
show
function(...) { /* ... */ } of type callable is incompatible with the type string expected by parameter $keyword of Foolz\SphinxQL\MatchBuilder::not() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
310 | $m->match('bbb') |
||||
311 | ->not('ccc ddd'); |
||||
312 | }); |
||||
313 | $this->assertEquals('aaa -(bbb -(ccc ddd))', $match->compile()->getCompiled()); |
||||
314 | } |
||||
315 | |||||
316 | // issue #82 |
||||
317 | public function testClosureMisuse() |
||||
318 | { |
||||
319 | $match = $this->createMatch() |
||||
320 | ->match('strlen'); |
||||
321 | $this->assertEquals('strlen', $match->compile()->getCompiled()); |
||||
322 | } |
||||
323 | } |
||||
324 |