Passed
Branch master (51e64f)
by Hung
02:02
created

HelperTest::testCallKeywords()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 34
nc 1
nop 0
dl 0
loc 45
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
use Foolz\SphinxQL\Drivers\ConnectionInterface;
4
use Foolz\SphinxQL\Helper;
5
use Foolz\SphinxQL\SphinxQL;
6
use Foolz\SphinxQL\Tests\TestUtil;
7
8
class HelperTest 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...
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()
0 ignored issues
show
Deprecated Code introduced by
The function Foolz\SphinxQL\Drivers\R...tInterface::getStored() has been deprecated: Commodity method for simple transition to version 1.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

44
            /** @scrutinizer ignore-deprecated */ $this->createHelper()->showTables()->execute()->getStored()

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
45
        );
46
    }
47
48
    public function testDescribe()
49
    {
50
        $describe = $this->createHelper()->describe('rt')->execute()->getStored();
0 ignored issues
show
Deprecated Code introduced by
The function Foolz\SphinxQL\Drivers\R...tInterface::getStored() has been deprecated: Commodity method for simple transition to version 1.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

50
        $describe = /** @scrutinizer ignore-deprecated */ $this->createHelper()->describe('rt')->execute()->getStored();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
51
        array_shift($describe);
0 ignored issues
show
Bug introduced by
It seems like $describe can also be of type integer; however, parameter $array of array_shift() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        array_shift(/** @scrutinizer ignore-type */ $describe);
Loading history...
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());
0 ignored issues
show
Bug introduced by
It seems like $this->createHelper()->s...>execute()->getStored() can also be of type integer; however, parameter $result of Foolz\SphinxQL\Helper::pairsToAssoc() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
        $vars = Helper::pairsToAssoc(/** @scrutinizer ignore-type */ $this->createHelper()->showVariables()->execute()->getStored());
Loading history...
Deprecated Code introduced by
The function Foolz\SphinxQL\Drivers\R...tInterface::getStored() has been deprecated: Commodity method for simple transition to version 1.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

65
        $vars = Helper::pairsToAssoc(/** @scrutinizer ignore-deprecated */ $this->createHelper()->showVariables()->execute()->getStored());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
66
        $this->assertEquals(0, $vars['autocommit']);
67
68
        $this->createHelper()->setVariable('AUTOCOMMIT', 1)->execute();
69
        $vars = Helper::pairsToAssoc($this->createHelper()->showVariables()->execute()->getStored());
0 ignored issues
show
Deprecated Code introduced by
The function Foolz\SphinxQL\Drivers\R...tInterface::getStored() has been deprecated: Commodity method for simple transition to version 1.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

69
        $vars = Helper::pairsToAssoc(/** @scrutinizer ignore-deprecated */ $this->createHelper()->showVariables()->execute()->getStored());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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(
0 ignored issues
show
Deprecated Code introduced by
The function Foolz\SphinxQL\Drivers\R...tInterface::getStored() has been deprecated: Commodity method for simple transition to version 1.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

78
        $snippets = /** @scrutinizer ignore-deprecated */ $this->createHelper()->callSnippets(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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(
0 ignored issues
show
Deprecated Code introduced by
The function Foolz\SphinxQL\Drivers\R...tInterface::getStored() has been deprecated: Commodity method for simple transition to version 1.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

88
        $snippets = /** @scrutinizer ignore-deprecated */ $this->createHelper()->callSnippets(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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(
0 ignored issues
show
Deprecated Code introduced by
The function Foolz\SphinxQL\Drivers\R...tInterface::getStored() has been deprecated: Commodity method for simple transition to version 1.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

103
        $snippets = /** @scrutinizer ignore-deprecated */ $this->createHelper()->callSnippets(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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(
0 ignored issues
show
Deprecated Code introduced by
The function Foolz\SphinxQL\Drivers\R...tInterface::getStored() has been deprecated: Commodity method for simple transition to version 1.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

120
        $keywords = /** @scrutinizer ignore-deprecated */ $this->createHelper()->callKeywords(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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(
0 ignored issues
show
Deprecated Code introduced by
The function Foolz\SphinxQL\Drivers\R...tInterface::getStored() has been deprecated: Commodity method for simple transition to version 1.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

140
        $keywords = /** @scrutinizer ignore-deprecated */ $this->createHelper()->callKeywords(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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()
0 ignored issues
show
Deprecated Code introduced by
The function Foolz\SphinxQL\Drivers\R...tInterface::getStored() has been deprecated: Commodity method for simple transition to version 1.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

180
            /** @scrutinizer ignore-deprecated */ $this->conn->query('SELECT MY_UDF()')->getStored()

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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()
0 ignored issues
show
Deprecated Code introduced by
The function Foolz\SphinxQL\Drivers\R...tInterface::getStored() has been deprecated: Commodity method for simple transition to version 1.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

201
        $result = /** @scrutinizer ignore-deprecated */ $this->createSphinxQL()

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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()
0 ignored issues
show
Deprecated Code introduced by
The function Foolz\SphinxQL\Drivers\R...tInterface::getStored() has been deprecated: Commodity method for simple transition to version 1.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

211
        $result = /** @scrutinizer ignore-deprecated */ $this->createSphinxQL()

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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