Completed
Push — php8 ( de93f5...73a8b5 )
by Hung
44:50 queued 11s
created

MatchBuilderTest   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 315
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 191
c 1
b 0
f 0
dl 0
loc 315
rs 10
wmc 22

22 Methods

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