Completed
Push — php8 ( de93f5...73a8b5 )
by Hung
44:50 queued 11s
created

ConnectionTest::testGetParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 32
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 42
rs 9.408
1
<?php
2
namespace Foolz\SphinxQL\Tests\Drivers;
3
4
use Foolz\SphinxQL\Drivers\Mysqli\Connection;
5
use Foolz\SphinxQL\Exception\ConnectionException;
6
use Foolz\SphinxQL\Exception\DatabaseException;
7
use Foolz\SphinxQL\Exception\SphinxQLException;
8
use Foolz\SphinxQL\Expression;
9
use Foolz\SphinxQL\Tests\TestUtil;
10
11
use PHPUnit\Framework\TestCase;
12
13
class ConnectionTest extends TestCase
14
{
15
16
    /**
17
     * @var Connection $connection
18
     */
19
    private $connection;
20
21
    protected function setUp(): void
22
    {
23
        $this->connection = TestUtil::getConnectionDriver();
24
        $this->connection->setParam('port', 9307);
25
    }
26
27
    protected function tearDown(): void
28
    {
29
        $this->connection = null;
30
    }
31
32
    public function test(): void
33
    {
34
        self::assertNotNull(TestUtil::getConnectionDriver());
35
    }
36
37
    public function testGetParams(): void
38
    {
39
        $this->assertSame([
40
            'host'		=> '127.0.0.1',
41
            'port'		=> 9307,
42
            'socket'	=> null,
43
        ], $this->connection->getParams());
44
45
        // create a new connection and get info
46
        $this->connection->setParams([
47
            'host'		=> '127.0.0.2',
48
        ]);
49
        $this->connection->setParam('port', 9308);
50
        $this->assertSame([
51
            'host'		=> '127.0.0.2',
52
            'port'		=> 9308,
53
            'socket'	=> null,
54
        ], $this->connection->getParams());
55
56
        $this->connection->setParam('host', 'localhost');
57
        $this->assertSame([
58
            'host'		=> '127.0.0.1',
59
            'port'		=> 9308,
60
            'socket'	=> null,
61
        ], $this->connection->getParams());
62
63
        // create a unix socket connection with host param
64
        $this->connection->setParam('host', 'unix:/var/run/sphinx.sock');
65
        $this->assertSame([
66
            'host'		=> null,
67
            'port'		=> 9308,
68
            'socket'	=> '/var/run/sphinx.sock',
69
        ], $this->connection->getParams());
70
71
        // create unix socket connection with socket param
72
        $this->connection->setParam('host', '127.0.0.1');
73
        $this->connection->setParam('socket', '/var/run/sphinx.sock');
74
        $this->assertSame([
75
            'host'		=> null,
76
            'port'		=> 9308,
77
            'socket'	=> '/var/run/sphinx.sock',
78
        ], $this->connection->getParams());
79
    }
80
81
    public function testGetConnectionParams(): void
82
    {
83
        // verify that (deprecated) getConnectionParams continues to work
84
        $this->assertSame([
85
            'host'		=> '127.0.0.1',
86
            'port'		=> 9307,
87
            'socket'	=> null,
88
        ], $this->connection->getParams());
89
90
        // create a new connection and get info
91
        $this->connection->setParams([
92
            'host'		=> '127.0.0.1',
93
            'port'		=> 9308,
94
        ]);
95
        $this->assertSame([
96
            'host'		=> '127.0.0.1',
97
            'port'		=> 9308,
98
            'socket'	=> null,
99
        ], $this->connection->getParams());
100
    }
101
102
    /**
103
     * @throws ConnectionException
104
     */
105
    public function testGetConnection(): void
106
    {
107
        $this->connection->connect();
108
        $this->assertNotNull($this->connection->getConnection());
109
    }
110
111
    /**
112
     * @throws ConnectionException
113
     */
114
    public function testGetConnectionThrowsException(): void
115
    {
116
        $this->expectException(ConnectionException::class);
117
118
        $this->connection->getConnection();
119
    }
120
121
    /**
122
     * @throws ConnectionException
123
     */
124
    public function testConnect(): void
125
    {
126
        $this->connection->connect();
127
128
        $this->connection->setParam('options', [
129
            MYSQLI_OPT_CONNECT_TIMEOUT => 1,
130
        ]);
131
        self::assertIsBool($this->connection->connect());
132
    }
133
134
    /**
135
     * @throws ConnectionException
136
     */
137
    public function testConnectThrowsException(): void
138
    {
139
        $this->expectException(ConnectionException::class);
140
141
        $this->connection->setParam('port', 9308);
142
        $this->connection->connect();
143
    }
144
145
    /**
146
     * @throws ConnectionException
147
     */
148
    public function testPing(): void
149
    {
150
        $this->connection->connect();
151
        $this->assertTrue($this->connection->ping());
152
    }
153
154
    /**
155
     * @throws ConnectionException
156
     */
157
    public function testClose(): void
158
    {
159
        $this->expectException(ConnectionException::class);
160
161
        $encoding = mb_internal_encoding();
162
        $this->connection->connect();
163
164
        if (method_exists($this->connection, 'getInternalEncoding')) {
165
            $this->assertEquals($encoding, $this->connection->getInternalEncoding());
166
            $this->assertEquals('UTF-8', mb_internal_encoding());
167
        }
168
169
        $this->connection->close();
170
        $this->assertEquals($encoding, mb_internal_encoding());
171
        $this->connection->getConnection();
172
    }
173
174
    /**
175
     * @throws ConnectionException
176
     * @throws DatabaseException
177
     */
178
    public function testQuery(): void
179
    {
180
        $this->connection->connect();
181
182
        $this->assertSame([
183
            [
184
                'Variable_name'		=> 'total',
185
                'Value'				=> '0',
186
            ],
187
            [
188
                'Variable_name'		=> 'total_found',
189
                'Value'				=> '0',
190
            ],
191
            [
192
                'Variable_name'		=> 'time',
193
                'Value'				=> '0.000',
194
            ],
195
        ], $this->connection->query('SHOW META')->store()->fetchAllAssoc());
196
    }
197
198
    //TODO
199
//    /**
200
//     * @throws ConnectionException
201
//     * @throws SphinxQLException
202
//     * @throws DatabaseException
203
//     */
204
//    public function testMultiQuery(): void
205
//    {
206
//        $this->connection->connect();
207
//        $query = $this->connection->multiQuery(['SHOW META']);
208
//
209
//        $result = $query->getNext();
210
//
211
//        $resultArr = [];
212
//        if ($result) {
213
//            $resultArr = $result->fetchAllAssoc();
214
//        }
215
//
216
//        $this->assertSame([
217
    //			[
218
    //				'Variable_name'		=> 'total',
219
    //				'Value'				=> '0',
220
    //			],
221
    //			[
222
    //				'Variable_name'		=> 'total_found',
223
    //				'Value'				=> '0',
224
    //			],
225
    //			[
226
    //				'Variable_name'		=> 'time',
227
    //				'Value'				=> '0.000',
228
    //			],
229
    //		], $resultArr);
230
//    }
231
232
    /**
233
     * @throws ConnectionException
234
     * @throws DatabaseException
235
     * @throws SphinxQLException
236
     */
237
    public function testEmptyMultiQuery(): void
238
    {
239
        $this->expectException(SphinxQLException::class);
240
        $this->expectExceptionMessage('The Queue is empty.');
241
        
242
        $this->connection->connect();
243
        $this->connection->multiQuery([]);
244
    }
245
246
    /**
247
     * @throws ConnectionException
248
     * @throws DatabaseException
249
     * @throws SphinxQLException
250
     */
251
    public function testMultiQueryThrowsException(): void
252
    {
253
        $this->expectException(DatabaseException::class);
254
255
        $this->connection->multiQuery(['SHOW METAL']);
256
    }
257
258
    /**
259
     * @throws ConnectionException
260
     * @throws DatabaseException
261
     */
262
    public function testQueryThrowsException(): void
263
    {
264
        $this->expectException(DatabaseException::class);
265
266
        $this->connection->query('SHOW METAL');
267
    }
268
269
    /**
270
     * @throws ConnectionException
271
     * @throws DatabaseException
272
     */
273
    public function testEscape(): void
274
    {
275
        $result = $this->connection->escape('\' "" \'\' ');
276
        $this->assertEquals('\'\\\' \\"\\" \\\'\\\' \'', $result);
277
    }
278
279
    /**
280
     * @throws ConnectionException
281
     * @throws DatabaseException
282
     */
283
    public function testEscapeThrowsException(): void
284
    {
285
        $this->expectException(ConnectionException::class);
286
287
        // or we get the wrong error popping up
288
        $this->connection->setParam('port', 9308);
289
        $this->connection->connect();
290
        $this->connection->escape('\' "" \'\' ');
291
    }
292
293
    /**
294
     * @throws ConnectionException
295
     * @throws DatabaseException
296
     */
297
    public function testQuote(): void
298
    {
299
        $this->connection->connect();
300
        $this->assertEquals('null', $this->connection->quote(null));
301
        $this->assertEquals(1, $this->connection->quote(true));
302
        $this->assertEquals(0, $this->connection->quote(false));
303
        $this->assertEquals("fo'o'bar", $this->connection->quote(new Expression("fo'o'bar")));
304
        $this->assertEquals(123, $this->connection->quote(123));
305
        $this->assertEquals('12.300000', $this->connection->quote(12.3));
306
        $this->assertEquals("'12.3'", $this->connection->quote('12.3'));
307
        $this->assertEquals("'12'", $this->connection->quote('12'));
308
    }
309
310
    /**
311
     * @throws ConnectionException
312
     * @throws DatabaseException
313
     */
314
    public function testQuoteArr(): void
315
    {
316
        $this->connection->connect();
317
        $this->assertEquals(
318
            ['null', 1, 0, "fo'o'bar", 123, '12.300000', "'12.3'", "'12'"],
319
            $this->connection->quoteArr([null, true, false, new Expression("fo'o'bar"), 123, 12.3, '12.3', '12'])
320
        );
321
    }
322
}
323