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

ConnectionTest::testEscapeThrowsException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
use Foolz\SphinxQL\Drivers\ConnectionInterface;
4
use Foolz\SphinxQL\Expression;
5
use Foolz\SphinxQL\Tests\TestUtil;
6
7
class ConnectionTest 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...
8
{
9
    /**
10
     * @var ConnectionInterface
11
     */
12
    private $connection = null;
13
14
    public function setUp()
15
    {
16
        $this->connection = TestUtil::getConnectionDriver();
17
        $this->connection->setParams(array('host' => '127.0.0.1', 'port' => 9307));
18
    }
19
20
    public function tearDown()
21
    {
22
        $this->connection = null;
23
    }
24
25
    public function test()
26
    {
27
        TestUtil::getConnectionDriver();
28
    }
29
30
    public function testGetParams()
31
    {
32
        $this->assertSame(
33
            array('host' => '127.0.0.1', 'port' => 9307, 'socket' => null),
34
            $this->connection->getParams()
0 ignored issues
show
Bug introduced by
The method getParams() does not exist on Foolz\SphinxQL\Drivers\ConnectionInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Foolz\SphinxQL\Drivers\ConnectionInterface. ( Ignorable by Annotation )

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

34
            $this->connection->/** @scrutinizer ignore-call */ 
35
                               getParams()
Loading history...
35
        );
36
37
        // create a new connection and get info
38
        $this->connection->setParams(array('host' => '127.0.0.2'));
0 ignored issues
show
Bug introduced by
The method setParams() does not exist on Foolz\SphinxQL\Drivers\ConnectionInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Foolz\SphinxQL\Drivers\ConnectionInterface. ( Ignorable by Annotation )

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

38
        $this->connection->/** @scrutinizer ignore-call */ 
39
                           setParams(array('host' => '127.0.0.2'));
Loading history...
39
        $this->connection->setParam('port', 9308);
0 ignored issues
show
Bug introduced by
The method setParam() does not exist on Foolz\SphinxQL\Drivers\ConnectionInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Foolz\SphinxQL\Drivers\ConnectionInterface. ( Ignorable by Annotation )

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

39
        $this->connection->/** @scrutinizer ignore-call */ 
40
                           setParam('port', 9308);
Loading history...
40
        $this->assertSame(
41
            array('host' => '127.0.0.2', 'port' => 9308, 'socket' => null),
42
            $this->connection->getParams()
43
        );
44
45
        $this->connection->setParam('host', 'localhost');
46
        $this->assertSame(
47
            array('host' => '127.0.0.1', 'port' => 9308, 'socket' => null),
48
            $this->connection->getParams()
49
        );
50
51
        // create a unix socket connection with host param
52
        $this->connection->setParam('host', 'unix:/var/run/sphinx.sock');
53
        $this->assertSame(
54
            array('host' => null, 'port' => 9308, 'socket' => '/var/run/sphinx.sock'),
55
            $this->connection->getParams()
56
        );
57
58
        // create unix socket connection with socket param
59
        $this->connection->setParam('host', '127.0.0.1');
60
        $this->connection->setParam('socket', '/var/run/sphinx.sock');
61
        $this->assertSame(
62
            array('host' => null, 'port' => 9308, 'socket' => '/var/run/sphinx.sock'),
63
            $this->connection->getParams()
64
        );
65
    }
66
67
    public function testGetConnectionParams()
68
    {
69
        // verify that (deprecated) getConnectionParams continues to work
70
        $this->assertSame(array('host' => '127.0.0.1', 'port' => 9307, 'socket' => null), $this->connection->getParams());
71
72
        // create a new connection and get info
73
        $this->connection->setParams(array('host' => '127.0.0.1', 'port' => 9308));
74
        $this->assertSame(array('host' => '127.0.0.1', 'port' => 9308, 'socket' => null), $this->connection->getParams());
75
    }
76
77
    public function testGetConnection()
78
    {
79
        $this->connection->connect();
0 ignored issues
show
Bug introduced by
The method connect() does not exist on Foolz\SphinxQL\Drivers\ConnectionInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Foolz\SphinxQL\Drivers\ConnectionInterface. ( Ignorable by Annotation )

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

79
        $this->connection->/** @scrutinizer ignore-call */ 
80
                           connect();
Loading history...
80
        $this->assertNotNull($this->connection->getConnection());
0 ignored issues
show
Bug introduced by
The method getConnection() does not exist on Foolz\SphinxQL\Drivers\ConnectionInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Foolz\SphinxQL\Drivers\ConnectionInterface. ( Ignorable by Annotation )

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

80
        $this->assertNotNull($this->connection->/** @scrutinizer ignore-call */ getConnection());
Loading history...
81
    }
82
83
    /**
84
     * @expectedException Foolz\SphinxQL\Exception\ConnectionException
85
     */
86
    public function testGetConnectionThrowsException()
87
    {
88
        $this->connection->getConnection();
89
    }
90
91
    public function testConnect()
92
    {
93
        $this->connection->connect();
94
95
        $this->connection->setParam('options', array(MYSQLI_OPT_CONNECT_TIMEOUT => 1));
96
        $this->connection->connect();
97
    }
98
99
    /**
100
     * @expectedException Foolz\SphinxQL\Exception\ConnectionException
101
     */
102
    public function testConnectThrowsException()
103
    {
104
        $this->connection->setParam('port', 9308);
105
        $this->connection->connect();
106
    }
107
108
    public function testPing()
109
    {
110
        $this->connection->connect();
111
        $this->assertTrue($this->connection->ping());
0 ignored issues
show
Bug introduced by
The method ping() does not exist on Foolz\SphinxQL\Drivers\ConnectionInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Foolz\SphinxQL\Drivers\ConnectionBase. Are you sure you never get one of those? ( Ignorable by Annotation )

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

111
        $this->assertTrue($this->connection->/** @scrutinizer ignore-call */ ping());
Loading history...
112
    }
113
114
    /**
115
     * @expectedException Foolz\SphinxQL\Exception\ConnectionException
116
     */
117
    public function testClose()
118
    {
119
        $encoding = mb_internal_encoding();
120
        $this->connection->connect();
121
        if (method_exists($this->connection, 'getInternalEncoding')) {
122
		    $this->assertEquals($encoding, $this->connection->getInternalEncoding());
123
		    $this->assertEquals('UTF-8', mb_internal_encoding());
124
        }
125
        $this->connection->close();
0 ignored issues
show
Bug introduced by
The method close() does not exist on Foolz\SphinxQL\Drivers\ConnectionInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Foolz\SphinxQL\Drivers\ConnectionInterface. ( Ignorable by Annotation )

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

125
        $this->connection->/** @scrutinizer ignore-call */ 
126
                           close();
Loading history...
126
        $this->assertEquals($encoding, mb_internal_encoding());
127
        $this->connection->getConnection();
128
    }
129
130
    public function testQuery()
131
    {
132
        $this->connection->connect();
133
        $this->assertSame(array(
134
            array('Variable_name' => 'total', 'Value' => '0'),
135
            array('Variable_name' => 'total_found', 'Value' => '0'),
136
            array('Variable_name' => 'time', 'Value' => '0.000'),
137
        ), $this->connection->query('SHOW META')->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

137
        ), /** @scrutinizer ignore-deprecated */ $this->connection->query('SHOW META')->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...
138
    }
139
140
    public function testMultiQuery()
141
    {
142
        $this->connection->connect();
143
        $query = $this->connection->multiQuery(array('SHOW META'));
144
        $this->assertSame(array(
145
            array('Variable_name' => 'total', 'Value' => '0'),
146
            array('Variable_name' => 'total_found', 'Value' => '0'),
147
            array('Variable_name' => 'time', 'Value' => '0.000'),
148
        ), $query->getNext()->fetchAllAssoc());
149
    }
150
151
    /**
152
     * @expectedException        Foolz\SphinxQL\Exception\SphinxQLException
153
     * @expectedExceptionMessage The Queue is empty.
154
     */
155
    public function testEmptyMultiQuery()
156
    {
157
        $this->connection->connect();
158
        $this->connection->multiQuery(array());
159
    }
160
161
    /**
162
     * @expectedException Foolz\SphinxQL\Exception\DatabaseException
163
     */
164
    public function testMultiQueryThrowsException()
165
    {
166
        $this->connection->multiQuery(array('SHOW METAL'));
167
    }
168
169
    /**
170
     * @expectedException Foolz\SphinxQL\Exception\DatabaseException
171
     */
172
    public function testQueryThrowsException()
173
    {
174
        $this->connection->query('SHOW METAL');
175
    }
176
177
    public function testEscape()
178
    {
179
        $result = $this->connection->escape('\' "" \'\' ');
180
        $this->assertEquals('\'\\\' \\"\\" \\\'\\\' \'', $result);
181
    }
182
183
    /**
184
     * @expectedException Foolz\SphinxQL\Exception\ConnectionException
185
     */
186
    public function testEscapeThrowsException()
187
    {
188
        // or we get the wrong error popping up
189
        $this->connection->setParam('port', 9308);
190
        $this->connection->connect();
191
        $this->connection->escape('\' "" \'\' ');
192
    }
193
194
    public function testQuote()
195
    {
196
        $this->connection->connect();
197
        $this->assertEquals('null', $this->connection->quote(null));
198
        $this->assertEquals(1, $this->connection->quote(true));
199
        $this->assertEquals(0, $this->connection->quote(false));
200
        $this->assertEquals("fo'o'bar", $this->connection->quote(new Expression("fo'o'bar")));
201
        $this->assertEquals(123, $this->connection->quote(123));
202
        $this->assertEquals("12.3", $this->connection->quote(12.3));
203
        $this->assertEquals("'12.3'", $this->connection->quote('12.3'));
204
        $this->assertEquals("'12'", $this->connection->quote('12'));
205
    }
206
207
    public function testQuoteArr()
208
    {
209
        $this->connection->connect();
210
        $this->assertEquals(
211
            array('null', 1, 0, "fo'o'bar", 123, "12.3", "'12.3'", "'12'"),
212
            $this->connection->quoteArr(array(null, true, false, new Expression("fo'o'bar"), 123, 12.3, '12.3', '12'))
213
        );
214
    }
215
216
}
217