Passed
Pull Request — master (#153)
by Adrian
01:57
created

PercolateQueriesTest::testInsert()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 18
nc 16
nop 6
dl 0
loc 29
rs 8.439
c 1
b 0
f 1
1
<?php
2
3
use Foolz\SphinxQL\Exception\SphinxQLException;
4
use Foolz\SphinxQL\Percolate;
5
use Foolz\SphinxQL\Tests\TestUtil;
6
use Foolz\SphinxQL\SphinxQL;
7
/**
8
 * @group Manticore
9
 * @package Foolz\SphinxQL
10
 * @author Vicent Valls
11
 */
12
class PercolateQueriesTest extends \PHPUnit\Framework\TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
13
{
14
    public static $conn = null;
15
16
17
    public static function setUpBeforeClass()
18
    {
19
	    $conn = TestUtil::getConnectionDriver();
20
	    $conn->setParam('port', 9307);
21
	    self::$conn = $conn;
22
23
        $sphinxQL = new SphinxQL(self::$conn);
24
        $sphinxQL->query('TRUNCATE RTINDEX pq')->execute();
25
    }
26
27
28
    /**
29
     * @dataProvider insertProvider
30
     * @throws \Foolz\SphinxQL\Exception\SphinxQLException
31
     */
32
    public function testInsert($testNumber, $query, $index, $tags, $filter, $compiledQuery)
33
    {
34
35
        if ($testNumber == 2) {
36
            $this->expectException(SphinxQLException::class);
37
            $this->expectExceptionMessage('Index can\'t be empty');
38
        }
39
40
        if ($testNumber == 3) {
41
            $this->expectException(SphinxQLException::class);
42
            $this->expectExceptionMessage('Query can\'t be empty');
43
        }
44
45
        if ($testNumber == 10) {
46
            $this->expectException(SphinxQLException::class);
47
            $this->expectExceptionMessage('Allow only one filter. If there is a comma in the text, it must be shielded');
48
        }
49
50
51
        $percolate = new Percolate(self::$conn);
52
        $percolate
53
            ->insert($query)
54
            ->into($index)
55
            ->tags($tags)
56
            ->filter($filter)
57
            ->execute();
58
59
        if (in_array($testNumber, [1, 4, 5, 6, 7, 8, 9, 11])) {
60
            $this->assertEquals($compiledQuery, $percolate->getLastQuery());
61
        }
62
63
64
        //$this->markTestIncomplete(true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
65
    }
66
67
    public function insertProvider()
68
    {
69
70
        /**
71
         * 1) Just insert
72
         * 2) Insert empty index
73
         * 3) Insert empty query
74
         * 4) Insert with special symbols
75
         * 5) Insert with tags as string without filter
76
         * 6) Insert with tags as array of string without filter
77
         * 7) Insert tags with special symbols
78
         * 8) Insert with filter, withowt tags
79
         * 9) Insert filter with special symbols
80
         * 10) Insert two filters
81
         * 11) Insert filter + tags
82
         */
83
84
85
        return [
86
            [
87
                1,
88
                'full text query terms',
89
                'pq',
90
                null,
91
                null,
92
                "INSERT INTO pq (query) VALUES ('full text query terms')"
93
            ],
94
95
            [
96
                2,
97
                'full text query terms',
98
                null,
99
                null,
100
                null,
101
                null
102
            ],
103
104
            [
105
                3,
106
                null,
107
                'pq',
108
                null,
109
                null,
110
                null
111
            ],
112
113
            [
114
                4,
115
                '@doc (text) \' ^ $ " | ! ~ / = >< & - \query terms',
116
                'pq',
117
                null,
118
                null,
119
                'INSERT INTO pq (query) VALUES (\'@doc (text) \\\\\\\' ^ $ \\\\\\" | \\\\! \\\\~ \\\\/ = >\\\\< & \\\\- \\\\\\\\query terms\')'
120
            ],
121
122
            [
123
                5,
124
                '@subject match by field',
125
                'pq',
126
                'tag2,tag3',
127
                'price>3',
128
                "INSERT INTO pq (query, tags, filters) VALUES ('@subject match by field', 'tag2,tag3', 'price>3')"
129
            ],
130
131
            [
132
                6,
133
                '@subject orange',
134
                'pq',
135
                ['tag2', 'tag3'],
136
                null,
137
                "INSERT INTO pq (query, tags) VALUES ('@subject orange', 'tag2,tag3')"
138
            ],
139
140
            [
141
                7,
142
                '@subject orange',
143
                'pq',
144
                '@doc (text) \' ^ $ " | ! ~ / = >< & - \query terms',
145
                null,
146
                'INSERT INTO pq (query, tags) VALUES (\'@subject orange\', \'@doc (text) \\\\\\\' ^ $ \\\\\" | \\\\! \\\\~ \\\\/ = >\\\\< & \\\\- \\\\\\\\query terms\')'
147
            ],
148
149
            [
150
                8,
151
                'catch me',
152
                'pq',
153
                null,
154
                'price>3',
155
                'INSERT INTO pq (query, filters) VALUES (\'catch me\', \'price>3\')'
156
            ],
157
158
            [
159
                9,
160
                'catch me if can',
161
                'pq',
162
                null,
163
                'p\@r\'ice>3',
164
                'INSERT INTO pq (query, filters) VALUES (\'catch me if can\', \'price>3\')'
165
            ],
166
167
            [
168
                10,
169
                'catch me if can',
170
                'pq',
171
                null,
172
                'price>3, secondFilter>0',
173
                null
174
            ],
175
            [
176
                11,
177
                'orange|apple|cherry',
178
                'pq',
179
                ['tag2', 'tag3'],
180
                'price>3',
181
                "INSERT INTO pq (query, tags, filters) VALUES ('orange|apple|cherry', 'tag2,tag3', 'price>3')"
182
            ],
183
        ];
184
    }
185
186
    /**
187
     * @dataProvider callPqProvider
188
     * @throws \Foolz\SphinxQL\Exception\SphinxQLException
189
     */
190
191
    public function testPercolate($testNumber, $index, $documents, $options, $result)
192
    {
193
        if ($testNumber == 2) {
194
            $this->expectException(SphinxQLException::class);
195
            $this->expectExceptionMessage('Document can\'t be empty');
196
197
        } elseif ($testNumber == 3) {
198
            $this->expectException(SphinxQLException::class);
199
            $this->expectExceptionMessage('Index can\'t be empty');
200
201
        } elseif ($testNumber == 12) {
202
            $this->expectException(SphinxQLException::class);
203
            $this->expectExceptionMessage('Documents must be in json format');
204
205
        } elseif ($testNumber == 13) {
206
            $this->expectException(SphinxQLException::class);
207
            $this->expectExceptionMessage('Documents array must be associate');
208
209
        }
210
211
        $query = (new Percolate(self::$conn))
212
            ->callPQ()
213
            ->from($index)
214
            ->documents($documents)
215
            ->options($options)
216
            ->execute();
217
218
219
        if (in_array($testNumber, [1, 4, 5, 6, 7, 8, 9, 11])) {
220
            $query = $query->fetchAllAssoc();
221
            $this->assertEquals($result[0], $query[0]['Query']);
222
            $this->assertEquals($result[1], count($query));
223
        }
224
225
        if ($testNumber == 10) {
226
            $query = $query->fetchAllAssoc();
227
            $this->assertEquals($result[0], $query[0]['UID']);
228
            $this->assertEquals($result[1], count($query));
229
        }
230
231
    }
232
233
    public function callPqProvider()
234
    {
235
        /**
236
         * 1) Call PQ
237
         * 2) Document empty
238
         * 3) Index empty
239
         * 4) Documents array of string
240
         * 5) Documents associate array
241
         * 6) Documents array of associate array
242
         * 7) Documents jsonObject
243
         * 8) Documents jsonArray of jsonObject
244
         * 9) Documents phpArray of jsonObject
245
         * 10) Option OPTION_QUERY
246
         * 11) Option OPTION_DOCS
247
         * Throws OPTION_DOCS_JSON
248
         * 12) Not json string
249
         * 13) Not array with non json string
250
         */
251
252
253
        return [
254
            [1, 'pq', 'full text query terms', [Percolate::OPTION_QUERY => 1], ['full text query terms', 2]],
255
            [2, 'pq', '', [], null],
256
            [3, '', 'full', [], null],
257
            [
258
                4,
259
                'pq',
260
                ['query terms', 'full text query terms'],
261
                [Percolate::OPTION_QUERY => 1],
262
                ['full text query terms', 2]
263
            ],
264
            [5, 'pq', ['subject' => 'document about orange'], [Percolate::OPTION_QUERY => 1], ['@subject orange', 2]],
265
            [
266
                6,
267
                'pq',
268
                [['subject' => 'document about orange'], ['subject' => 'match by field', 'price' => 1]],
269
                [Percolate::OPTION_QUERY => 1],
270
                ['@subject orange', 2]
271
            ],
272
            [7, 'pq', '{"subject":"document about orange"}', [Percolate::OPTION_QUERY => 1], ['@subject orange', 2]],
273
            [
274
                8,
275
                'pq',
276
                '[{"subject":"document about orange"}, {"subject":"match by field","price":10}]',
277
                [Percolate::OPTION_QUERY => 1],
278
                ['@subject match by field', 3]
279
            ],
280
            [
281
                9,
282
                'pq',
283
                ['{"subject":"document about orange"}', '{"subject":"match by field","price":10}'],
284
                [Percolate::OPTION_QUERY => 1],
285
                ['@subject match by field', 3]
286
            ],
287
            [10, 'pq', 'full text query terms', [Percolate::OPTION_QUERY => 0], [1, 2]],
288
            [
289
                11,
290
                'pq',
291
                ['{"subject":"document about orange"}', '{"subject":"match by field","price":10}'],
292
                [Percolate::OPTION_QUERY => 1, Percolate::OPTION_DOCS => 1],
293
                ['@subject match by field', 3]
294
            ],
295
            [12, 'pq', 'full text query terms', [Percolate::OPTION_DOCS_JSON => 1], null],
296
            [13, 'pq', ['full text query terms','full text'], [Percolate::OPTION_DOCS_JSON => 1], null],
297
        ];
298
    }
299
300
}
301