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
|
|
|
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() |
|
|
|
|
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
// create a new connection and get info |
39
|
|
|
$this->connection->setParams(array('host' => '127.0.0.2')); |
|
|
|
|
40
|
|
|
$this->connection->setParam('port', 9308); |
|
|
|
|
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(); |
|
|
|
|
81
|
|
|
$this->assertNotNull($this->connection->getConnection()); |
|
|
|
|
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); |
|
|
|
|
116
|
|
|
$this->connection->connect(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testPing() |
120
|
|
|
{ |
121
|
|
|
$this->connection->connect(); |
122
|
|
|
$this->assertTrue($this->connection->ping()); |
|
|
|
|
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(); |
|
|
|
|
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()); |
|
|
|
|
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
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.