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 |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var ConnectionInterface |
11
|
|
|
*/ |
12
|
|
|
private $connection = null; |
13
|
|
|
|
14
|
|
|
protected function setUp(): void |
15
|
|
|
{ |
16
|
|
|
$this->connection = TestUtil::getConnectionDriver(); |
17
|
|
|
$this->connection->setParams(array('host' => '127.0.0.1', 'port' => 9307)); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
protected function tearDown(): void |
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() |
|
|
|
|
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
// create a new connection and get info |
38
|
|
|
$this->connection->setParams(array('host' => '127.0.0.2')); |
|
|
|
|
39
|
|
|
$this->connection->setParam('port', 9308); |
|
|
|
|
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(); |
|
|
|
|
80
|
|
|
$this->assertNotNull($this->connection->getConnection()); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testGetConnectionThrowsException() |
84
|
|
|
{ |
85
|
|
|
$this->expectException(Foolz\SphinxQL\Exception\ConnectionException::class); |
86
|
|
|
$this->connection->getConnection(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testConnect() |
90
|
|
|
{ |
91
|
|
|
$this->connection->connect(); |
92
|
|
|
|
93
|
|
|
$this->connection->setParam('options', array(MYSQLI_OPT_CONNECT_TIMEOUT => 1)); |
94
|
|
|
$this->connection->connect(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testConnectThrowsException() |
98
|
|
|
{ |
99
|
|
|
$this->expectException(Foolz\SphinxQL\Exception\ConnectionException::class); |
100
|
|
|
$this->connection->setParam('port', 9308); |
101
|
|
|
$this->connection->connect(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testPing() |
105
|
|
|
{ |
106
|
|
|
$this->connection->connect(); |
107
|
|
|
$this->assertTrue($this->connection->ping()); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testClose() |
111
|
|
|
{ |
112
|
|
|
$this->expectException(Foolz\SphinxQL\Exception\ConnectionException::class); |
113
|
|
|
$encoding = mb_internal_encoding(); |
114
|
|
|
$this->connection->connect(); |
115
|
|
|
|
116
|
|
|
if (method_exists($this->connection, 'getInternalEncoding')) { |
117
|
|
|
$this->assertEquals($encoding, $this->connection->getInternalEncoding()); |
118
|
|
|
$this->assertEquals('UTF-8', mb_internal_encoding()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$this->connection->close(); |
|
|
|
|
122
|
|
|
$this->assertEquals($encoding, mb_internal_encoding()); |
123
|
|
|
$this->connection->getConnection(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testQuery() |
127
|
|
|
{ |
128
|
|
|
$this->connection->connect(); |
129
|
|
|
$this->assertSame(array( |
130
|
|
|
array('Variable_name' => 'total', 'Value' => '0'), |
131
|
|
|
array('Variable_name' => 'total_found', 'Value' => '0'), |
132
|
|
|
array('Variable_name' => 'time', 'Value' => '0.000'), |
133
|
|
|
), $this->connection->query('SHOW META')->getStored()); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testMultiQuery() |
137
|
|
|
{ |
138
|
|
|
$this->connection->connect(); |
139
|
|
|
$query = $this->connection->multiQuery(array('SHOW META')); |
140
|
|
|
$this->assertSame(array( |
141
|
|
|
array('Variable_name' => 'total', 'Value' => '0'), |
142
|
|
|
array('Variable_name' => 'total_found', 'Value' => '0'), |
143
|
|
|
array('Variable_name' => 'time', 'Value' => '0.000'), |
144
|
|
|
), $query->getNext()->fetchAllAssoc()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testEmptyMultiQuery() |
148
|
|
|
{ |
149
|
|
|
$this->expectException(Foolz\SphinxQL\Exception\SphinxQLException::class); |
150
|
|
|
$this->expectExceptionMessage('The Queue is empty.'); |
151
|
|
|
$this->connection->connect(); |
152
|
|
|
$this->connection->multiQuery(array()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testMultiQueryThrowsException() |
156
|
|
|
{ |
157
|
|
|
$this->expectException(Foolz\SphinxQL\Exception\DatabaseException::class); |
158
|
|
|
$this->connection->multiQuery(array('SHOW METAL')); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testQueryThrowsException() |
162
|
|
|
{ |
163
|
|
|
$this->expectException(Foolz\SphinxQL\Exception\DatabaseException::class); |
164
|
|
|
$this->connection->query('SHOW METAL'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testEscape() |
168
|
|
|
{ |
169
|
|
|
$result = $this->connection->escape('\' "" \'\' '); |
170
|
|
|
$this->assertEquals('\'\\\' \\"\\" \\\'\\\' \'', $result); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testEscapeThrowsException() |
174
|
|
|
{ |
175
|
|
|
$this->expectException(Foolz\SphinxQL\Exception\ConnectionException::class); |
176
|
|
|
// or we get the wrong error popping up |
177
|
|
|
$this->connection->setParam('port', 9308); |
178
|
|
|
$this->connection->connect(); |
179
|
|
|
$this->connection->escape('\' "" \'\' '); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function testQuote() |
183
|
|
|
{ |
184
|
|
|
$this->connection->connect(); |
185
|
|
|
$this->assertEquals('null', $this->connection->quote(null)); |
186
|
|
|
$this->assertEquals(1, $this->connection->quote(true)); |
187
|
|
|
$this->assertEquals(0, $this->connection->quote(false)); |
188
|
|
|
$this->assertEquals("fo'o'bar", $this->connection->quote(new Expression("fo'o'bar"))); |
189
|
|
|
$this->assertEquals(123, $this->connection->quote(123)); |
190
|
|
|
$this->assertEquals("12.300000", $this->connection->quote(12.3)); |
191
|
|
|
$this->assertEquals("'12.3'", $this->connection->quote('12.3')); |
192
|
|
|
$this->assertEquals("'12'", $this->connection->quote('12')); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function testQuoteArr() |
196
|
|
|
{ |
197
|
|
|
$this->connection->connect(); |
198
|
|
|
$this->assertEquals( |
199
|
|
|
array('null', 1, 0, "fo'o'bar", 123, "12.300000", "'12.3'", "'12'"), |
200
|
|
|
$this->connection->quoteArr(array(null, true, false, new Expression("fo'o'bar"), 123, 12.3, '12.3', '12')) |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
} |
205
|
|
|
|