Passed
Pull Request — master (#197)
by
unknown
07:20
created

HelperTest::testCallKeywords()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 28
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 40
rs 9.472
1
<?php
2
namespace Foolz\SphinxQL\Tests;
3
4
use Foolz\SphinxQL\Drivers\ConnectionInterface;
5
use Foolz\SphinxQL\Exception\ConnectionException;
6
use Foolz\SphinxQL\Exception\DatabaseException;
7
use Foolz\SphinxQL\Exception\SphinxQLException;
8
use Foolz\SphinxQL\Helper;
9
use Foolz\SphinxQL\SphinxQL;
10
11
use PHPUnit\Framework\TestCase;
12
13
class HelperTest extends TestCase
14
{
15
16
    /**
17
     * @var ConnectionInterface
18
     */
19
    public static $connection;
20
21
    public static function setUpBeforeClass(): void
22
    {
23
        self::$connection = TestUtil::getConnectionDriver();
24
        self::$connection->setParam('port', 9307);
25
    }
26
27
    /**
28
     * @throws ConnectionException
29
     * @throws DatabaseException
30
     * @throws SphinxQLException
31
     */
32
    protected function setUp(): void
33
    {
34
        $this->createSphinxQL()->query('TRUNCATE RTINDEX rt')->execute();
35
    }
36
37
    /**
38
     * @return SphinxQL
39
     */
40
    protected function createSphinxQL(): SphinxQL
41
    {
42
        return new SphinxQL(self::$connection);
43
    }
44
45
    /**
46
     * @return Helper
47
     */
48
    protected function createHelper(): Helper
49
    {
50
        return new Helper(self::$connection);
51
    }
52
53
    /**
54
     * @throws ConnectionException
55
     * @throws DatabaseException
56
     * @throws SphinxQLException
57
     */
58
    public function testShowTables(): void
59
    {
60
        $this->assertEquals([
61
            [
62
                'Index'	=> 'rt',
63
                'Type'	=> 'rt',
64
            ]
65
        ], $this->createHelper()->showTables('rt')->execute()->fetchAllAssoc());
66
    }
67
68
    /**
69
     * @throws ConnectionException
70
     * @throws DatabaseException
71
     * @throws SphinxQLException
72
     */
73
    public function testDescribe(): void
74
    {
75
        $describe = $this->createHelper()->describe('rt')->execute()->fetchAllAssoc();
76
        array_shift($describe);
77
78
        $expect = (TestUtil::getSearchBuild()==='SPHINX3')?[
79
            [
80
                'Field'			=> 'title',
81
                'Type'			=> 'field',
82
                'Properties'	=> 'indexed',
83
                'Key'			=> '',
84
            ],
85
            [
86
                'Field'			=> 'content',
87
                'Type'			=> 'field',
88
                'Properties'	=> 'indexed',
89
                'Key'			=> '',
90
            ],
91
            [
92
                'Field'			=> 'gid',
93
                'Type'			=> 'uint',
94
                'Properties'	=> '',
95
                'Key'			=> '',
96
            ],
97
        ]:[
98
            [
99
                'Field'			=> 'title',
100
                'Type'			=> 'field',
101
            ],
102
            [
103
                'Field'			=> 'content',
104
                'Type'			=> 'field',
105
            ],
106
            [
107
                'Field'			=> 'gid',
108
                'Type'			=> 'uint',
109
            ],
110
        ];
111
        $this->assertSame($expect, $describe);
112
    }
113
114
    /**
115
     * @throws ConnectionException
116
     * @throws DatabaseException
117
     * @throws SphinxQLException
118
     */
119
    public function testSetVariable(): void
120
    {
121
        $this->createHelper()->setVariable('AUTOCOMMIT', 0)->execute();
122
        $vars = Helper::pairsToAssoc($this->createHelper()->showVariables()->execute()->fetchAllAssoc());
123
        $this->assertEquals(0, $vars['autocommit']);
124
125
        $this->createHelper()->setVariable('AUTOCOMMIT', 1)->execute();
126
        $vars = Helper::pairsToAssoc($this->createHelper()->showVariables()->execute()->fetchAllAssoc());
127
        $this->assertEquals(1, $vars['autocommit']);
128
129
        $this->createHelper()->setVariable('@foo', 1, true);
130
        $this->createHelper()->setVariable('@foo', [0], true);
131
    }
132
133
    /**
134
     * @throws ConnectionException
135
     * @throws DatabaseException
136
     * @throws SphinxQLException
137
     */
138
    public function testCallSnippets(): void
139
    {
140
        $snippets = $this->createHelper()->callSnippets(
141
            'this is my document text',
142
            'rt',
143
            'is'
144
        )->execute()->fetchAllAssoc();
145
        $this->assertEquals([
146
            [
147
                'snippet' => 'this <b>is</b> my document text',
148
            ]
149
        ], $snippets);
150
151
        $snippets = $this->createHelper()->callSnippets(
152
            'this is my document text',
153
            'rt',
154
            'is',
155
            [
156
//				'query_mode'	=> 1,
157
                'before_match'	=> '<em>',
158
                'after_match'	=> '</em>',
159
            ]
160
        )->execute()->fetchAllAssoc();
161
        $this->assertEquals([
162
            [
163
                'snippet' => 'this <em>is</em> my document text',
164
            ]
165
        ], $snippets);
166
167
        $snippets = $this->createHelper()->callSnippets([
168
            'this is my document text',
169
            'another document',
170
        ], 'rt', 'is', [
171
            'allow_empty' => 1,
172
        ])->execute()->fetchAllAssoc();
173
        $this->assertEquals([
174
            [
175
                'snippet' => 'this <b>is</b> my document text',
176
            ],
177
            [
178
                'snippet' => '',
179
            ],
180
        ], $snippets);
181
    }
182
183
    /**
184
     * @throws ConnectionException
185
     * @throws DatabaseException
186
     * @throws SphinxQLException
187
     */
188
    public function testCallKeywords(): void
189
    {
190
        $keywords = $this->createHelper()->callKeywords(
191
            'test case',
192
            'rt'
193
        )->execute()->fetchAllAssoc();
194
        $this->assertEquals([
195
            [
196
                'qpos'       => '1',
197
                'tokenized'  => 'test',
198
                'normalized' => 'test',
199
            ],
200
            [
201
                'qpos'       => '2',
202
                'tokenized'  => 'case',
203
                'normalized' => 'case',
204
            ],
205
        ], $keywords);
206
207
        $keywords = $this->createHelper()->callKeywords(
208
            'test case',
209
            'rt',
210
            1
211
        )->execute()->fetchAllAssoc();
212
        $this->assertEquals([
213
            [
214
                'qpos'       => '1',
215
                'tokenized'  => 'test',
216
                'normalized' => 'test',
217
                'docs'       => '0',
218
                'hits'       => '0',
219
            ],
220
            [
221
                'qpos'       => '2',
222
                'tokenized'  => 'case',
223
                'normalized' => 'case',
224
                'docs'       => '0',
225
                'hits'       => '0',
226
            ],
227
        ], $keywords);
228
    }
229
230
    /**
231
     * @throws ConnectionException
232
     * @throws DatabaseException
233
     */
234
    public function testUdfNotInstalled(): void
235
    {
236
        $this->expectException(DatabaseException::class);
237
        $this->expectExceptionMessage('Sphinx expr: syntax error');
238
239
        self::$connection->query('SELECT MY_UDF()');
240
    }
241
242
    //	/**
243
    //	 * @throws ConnectionException
244
    //	 * @throws DatabaseException
245
    //	 * @throws SphinxQLException
246
    //	 */
247
    //	public function testCreateFunction(): void
248
    //	{
249
    //		$this->createHelper()->createFunction('my_udf', 'INT', 'test_udf.so')->execute();
250
//
251
    //		$this->assertSame([
252
    //			[
253
    //				'MY_UDF()' => '42',
254
    //			],
255
    //		],self::$connection->query('SELECT MY_UDF()')->fetchAllAssoc());
256
//
257
    //		$this->createHelper()->dropFunction('my_udf')->execute();
258
    //	}
259
260
    /**
261
     * @throws ConnectionException
262
     * @throws DatabaseException
263
     * @throws SphinxQLException
264
     */
265
    public function testTruncateRtIndex(): void
266
    {
267
        $this->createSphinxQL()
268
            ->insert()
269
            ->into('rt')
270
            ->set([
271
                'id'		=> 1,
272
                'title'		=> 'this is a title',
273
                'content'	=> 'this is the content',
274
                'gid'		=> 100,
275
            ])
276
            ->execute();
277
278
        $result = $this->createSphinxQL()
279
            ->select()
280
            ->from('rt')
281
            ->execute()
282
            ->fetchAllAssoc();
283
284
        $this->assertCount(1, $result);
285
286
        $this->createHelper()->truncateRtIndex('rt')->execute();
287
288
        $result = $this->createSphinxQL()
289
            ->select()
290
            ->from('rt')
291
            ->execute()
292
            ->fetchAllAssoc();
293
294
        $this->assertCount(0, $result);
295
    }
296
297
    /**
298
     * Actually executing these queries may not be useful nor easy to test
299
     * @throws ConnectionException
300
     * @throws DatabaseException
301
     * @throws SphinxQLException
302
     */
303
    public function testMiscellaneous(): void
304
    {
305
        $query = $this->createHelper()->showMeta();
306
        $this->assertEquals('SHOW META', $query->compile()->getCompiled());
307
308
        $query = $this->createHelper()->showWarnings();
309
        $this->assertEquals('SHOW WARNINGS', $query->compile()->getCompiled());
310
311
        $query = $this->createHelper()->showStatus();
312
        $this->assertEquals('SHOW STATUS', $query->compile()->getCompiled());
313
314
        $query = $this->createHelper()->attachIndex('disk', 'rt');
315
        $this->assertEquals('ATTACH INDEX disk TO RTINDEX rt', $query->compile()->getCompiled());
316
317
        $query = $this->createHelper()->flushRtIndex('rt');
318
        $this->assertEquals('FLUSH RTINDEX rt', $query->compile()->getCompiled());
319
320
        $query = $this->createHelper()->optimizeIndex('rt');
321
        $this->assertEquals('OPTIMIZE INDEX rt', $query->compile()->getCompiled());
322
323
        $query = $this->createHelper()->showIndexStatus('rt');
324
        $this->assertEquals('SHOW INDEX rt STATUS', $query->compile()->getCompiled());
325
326
        $query = $this->createHelper()->flushRamchunk('rt');
327
        $this->assertEquals('FLUSH RAMCHUNK rt', $query->compile()->getCompiled());
328
    }
329
}
330