Passed
Push — ci-build-matrix ( 1c3307...9a015f )
by Hung
02:21
created

ConnectionTest   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Importance

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

21 Methods

Rating   Name   Duplication   Size   Complexity  
A testQuote() 0 11 1
A test() 0 3 1
A testPing() 0 4 1
A testGetConnectionThrowsException() 0 3 1
A testEscapeThrowsException() 0 6 1
B testGetParams() 0 34 1
A testQueryThrowsException() 0 3 1
A tearDown() 0 3 1
A testQuoteArr() 0 6 1
A setUp() 0 5 1
A testQuery() 0 8 1
A testMultiQueryThrowsException() 0 3 1
A testEmptyMultiQuery() 0 4 1
A testGetConnection() 0 4 1
A testEscape() 0 4 1
A testConnect() 0 6 1
A testClose() 0 11 2
A testGetConnectionParams() 0 8 1
A testConnectThrowsException() 0 5 1
A testConnectThrowsPHPException() 0 4 1
A testMultiQuery() 0 9 1
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
        $this->connection->silenceConnectionWarning(false);
19
    }
20
21
    public function tearDown()
22
    {
23
        $this->connection = null;
24
    }
25
26
    public function test()
27
    {
28
        TestUtil::getConnectionDriver();
29
    }
30
31
    public function testGetParams()
32
    {
33
        $this->assertSame(
34
            array('host' => '127.0.0.1', 'port' => 9307, 'socket' => null),
35
            $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

35
            $this->connection->/** @scrutinizer ignore-call */ 
36
                               getParams()
Loading history...
36
        );
37
38
        // create a new connection and get info
39
        $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

39
        $this->connection->/** @scrutinizer ignore-call */ 
40
                           setParams(array('host' => '127.0.0.2'));
Loading history...
40
        $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

40
        $this->connection->/** @scrutinizer ignore-call */ 
41
                           setParam('port', 9308);
Loading history...
41
        $this->assertSame(
42
            array('host' => '127.0.0.2', 'port' => 9308, 'socket' => null),
43
            $this->connection->getParams()
44
        );
45
46
        $this->connection->setParam('host', 'localhost');
47
        $this->assertSame(
48
            array('host' => '127.0.0.1', 'port' => 9308, 'socket' => null),
49
            $this->connection->getParams()
50
        );
51
52
        // create a unix socket connection with host param
53
        $this->connection->setParam('host', 'unix:/var/run/sphinx.sock');
54
        $this->assertSame(
55
            array('host' => null, 'port' => 9308, 'socket' => '/var/run/sphinx.sock'),
56
            $this->connection->getParams()
57
        );
58
59
        // create unix socket connection with socket param
60
        $this->connection->setParam('host', '127.0.0.1');
61
        $this->connection->setParam('socket', '/var/run/sphinx.sock');
62
        $this->assertSame(
63
            array('host' => null, 'port' => 9308, 'socket' => '/var/run/sphinx.sock'),
64
            $this->connection->getParams()
65
        );
66
    }
67
68
    public function testGetConnectionParams()
69
    {
70
        // verify that (deprecated) getConnectionParams continues to work
71
        $this->assertSame(array('host' => '127.0.0.1', 'port' => 9307, 'socket' => null), $this->connection->getParams());
72
73
        // create a new connection and get info
74
        $this->connection->setParams(array('host' => '127.0.0.1', 'port' => 9308));
75
        $this->assertSame(array('host' => '127.0.0.1', 'port' => 9308, 'socket' => null), $this->connection->getParams());
76
    }
77
78
    public function testGetConnection()
79
    {
80
        $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

80
        $this->connection->/** @scrutinizer ignore-call */ 
81
                           connect();
Loading history...
81
        $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

81
        $this->assertNotNull($this->connection->/** @scrutinizer ignore-call */ getConnection());
Loading history...
82
    }
83
84
    /**
85
     * @expectedException Foolz\SphinxQL\Exception\ConnectionException
86
     */
87
    public function testGetConnectionThrowsException()
88
    {
89
        $this->connection->getConnection();
90
    }
91
92
    public function testConnect()
93
    {
94
        $this->connection->connect();
95
96
        $this->connection->setParam('options', array(MYSQLI_OPT_CONNECT_TIMEOUT => 1));
97
        $this->connection->connect();
98
    }
99
100
    /**
101
     * @expectedException PHPUnit\Framework\Error\Warning
102
     */
103
    public function testConnectThrowsPHPException()
104
    {
105
        $this->connection->setParam('port', 9308);
106
        $this->connection->connect();
107
    }
108
109
    /**
110
     * @expectedException Foolz\SphinxQL\Exception\ConnectionException
111
     */
112
    public function testConnectThrowsException()
113
    {
114
        $this->connection->setParam('port', 9308);
115
        $this->connection->silenceConnectionWarning(true);
0 ignored issues
show
Bug introduced by
The method silenceConnectionWarning() 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

115
        $this->connection->/** @scrutinizer ignore-call */ 
116
                           silenceConnectionWarning(true);
Loading history...
116
        $this->connection->connect();
117
    }
118
119
    public function testPing()
120
    {
121
        $this->connection->connect();
122
        $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

122
        $this->assertTrue($this->connection->/** @scrutinizer ignore-call */ ping());
Loading history...
123
    }
124
125
    /**
126
     * @expectedException Foolz\SphinxQL\Exception\ConnectionException
127
     */
128
    public function testClose()
129
    {
130
        $encoding = mb_internal_encoding();
131
        $this->connection->connect();
132
        if (method_exists($this->connection, 'getInternalEncoding')) {
133
		    $this->assertEquals($encoding, $this->connection->getInternalEncoding());
134
		    $this->assertEquals('UTF-8', mb_internal_encoding());
135
        }
136
        $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

136
        $this->connection->/** @scrutinizer ignore-call */ 
137
                           close();
Loading history...
137
        $this->assertEquals($encoding, mb_internal_encoding());
138
        $this->connection->getConnection();
139
    }
140
141
    public function testQuery()
142
    {
143
        $this->connection->connect();
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
        ), $this->connection->query('SHOW META')->getStored());
0 ignored issues
show
Bug introduced by
The method getStored() does not exist on Foolz\SphinxQL\Drivers\ResultSetInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Foolz\SphinxQL\Drivers\ResultSetInterface. ( Ignorable by Annotation )

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

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