Passed
Pull Request — master (#197)
by
unknown
01:27
created

ConnectionTest::testConnect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
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
     * @var Connection $connection
17
     */
18
    private $connection;
19
20
    protected function setUp(): void
21
    {
22
        $this->connection = TestUtil::getConnectionDriver();
23
        $this->connection->setParam('port',9307);
24
    }
25
26
    protected function tearDown(): void
27
    {
28
        $this->connection = null;
29
    }
30
31
    public function test(): void
32
    {
33
        self::assertNotNull(TestUtil::getConnectionDriver());
34
    }
35
36
    public function testGetParams(): void
37
    {
38
        $this->assertSame([
39
			'host'		=> '127.0.0.1',
40
			'port'		=> 9307,
41
			'socket'	=> null,
42
		],$this->connection->getParams());
43
44
        // create a new connection and get info
45
        $this->connection->setParams([
46
			'host'		=> '127.0.0.2',
47
		]);
48
        $this->connection->setParam('port',9308);
49
        $this->assertSame([
50
			'host'		=> '127.0.0.2',
51
			'port'		=> 9308,
52
			'socket'	=> null,
53
		],$this->connection->getParams());
54
55
        $this->connection->setParam('host','localhost');
56
        $this->assertSame([
57
			'host'		=> '127.0.0.1',
58
			'port'		=> 9308,
59
			'socket'	=> null,
60
		],$this->connection->getParams());
61
62
        // create a unix socket connection with host param
63
        $this->connection->setParam('host','unix:/var/run/sphinx.sock');
64
        $this->assertSame([
65
			'host'		=> null,
66
			'port'		=> 9308,
67
			'socket'	=> '/var/run/sphinx.sock',
68
		],$this->connection->getParams());
69
70
        // create unix socket connection with socket param
71
        $this->connection->setParam('host', '127.0.0.1');
72
        $this->connection->setParam('socket', '/var/run/sphinx.sock');
73
        $this->assertSame([
74
			'host'		=> null,
75
			'port'		=> 9308,
76
			'socket'	=> '/var/run/sphinx.sock',
77
		],$this->connection->getParams());
78
    }
79
80
    public function testGetConnectionParams(): void
81
    {
82
        // verify that (deprecated) getConnectionParams continues to work
83
        $this->assertSame([
84
			'host'		=> '127.0.0.1',
85
			'port'		=> 9307,
86
			'socket'	=> null,
87
		],$this->connection->getParams());
88
89
        // create a new connection and get info
90
        $this->connection->setParams([
91
			'host'		=> '127.0.0.1',
92
			'port'		=> 9308,
93
		]);
94
        $this->assertSame([
95
			'host'		=> '127.0.0.1',
96
			'port'		=> 9308,
97
			'socket'	=> null,
98
		],$this->connection->getParams());
99
    }
100
101
    /**
102
     * @throws ConnectionException
103
     */
104
    public function testGetConnection(): void
105
    {
106
        $this->connection->connect();
107
        $this->assertNotNull($this->connection->getConnection());
108
    }
109
110
    /**
111
     * @throws ConnectionException
112
     */
113
    public function testGetConnectionThrowsException(): void
114
    {
115
        $this->expectException(ConnectionException::class);
116
117
        $this->connection->getConnection();
118
    }
119
120
    /**
121
     * @throws ConnectionException
122
     */
123
    public function testConnect(): void
124
    {
125
        $this->connection->connect();
126
127
        $this->connection->setParam('options',[
128
			MYSQLI_OPT_CONNECT_TIMEOUT => 1,
129
		]);
130
        self::assertIsBool($this->connection->connect());
131
    }
132
133
    /**
134
     * @throws ConnectionException
135
     */
136
    public function testConnectThrowsException(): void
137
    {
138
        $this->expectException(ConnectionException::class);
139
140
        $this->connection->setParam('port', 9308);
141
        $this->connection->connect();
142
    }
143
144
    /**
145
     * @throws ConnectionException
146
     */
147
    public function testPing(): void
148
    {
149
        $this->connection->connect();
150
        $this->assertTrue($this->connection->ping());
151
    }
152
153
    /**
154
     * @throws ConnectionException
155
     */
156
    public function testClose(): void
157
    {
158
        $this->expectException(ConnectionException::class);
159
160
        $encoding = mb_internal_encoding();
161
        $this->connection->connect();
162
163
        if (method_exists($this->connection, 'getInternalEncoding')) {
164
            $this->assertEquals($encoding, $this->connection->getInternalEncoding());
165
            $this->assertEquals('UTF-8', mb_internal_encoding());
166
        }
167
168
        $this->connection->close();
169
        $this->assertEquals($encoding, mb_internal_encoding());
170
        $this->connection->getConnection();
171
    }
172
173
    /**
174
     * @throws ConnectionException
175
     * @throws DatabaseException
176
     */
177
    public function testQuery(): void
178
    {
179
        $this->connection->connect();
180
181
        $this->assertSame([
182
			[
183
				'Variable_name'		=> 'total',
184
				'Value'				=> '0',
185
			],
186
			[
187
				'Variable_name'		=> 'total_found',
188
				'Value'				=> '0',
189
			],
190
			[
191
				'Variable_name'		=> 'time',
192
				'Value'				=> '0.000',
193
			],
194
		],$this->connection->query('SHOW META')->store()->fetchAllAssoc());
195
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(['null', 1, 0, "fo'o'bar", 123, '12.300000', "'12.3'", "'12'"],
318
            $this->connection->quoteArr([null, true, false, new Expression("fo'o'bar"), 123, 12.3, '12.3', '12']));
319
    }
320
321
}