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
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function tearDown() |
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
|
|
|
/** |
84
|
|
|
* @expectedException Foolz\SphinxQL\Exception\ConnectionException |
85
|
|
|
*/ |
86
|
|
|
public function testGetConnectionThrowsException() |
87
|
|
|
{ |
88
|
|
|
$this->connection->getConnection(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testConnect() |
92
|
|
|
{ |
93
|
|
|
$this->connection->connect(); |
94
|
|
|
|
95
|
|
|
$this->connection->setParam('options', array(MYSQLI_OPT_CONNECT_TIMEOUT => 1)); |
96
|
|
|
$this->connection->connect(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @expectedException Foolz\SphinxQL\Exception\ConnectionException |
101
|
|
|
*/ |
102
|
|
|
public function testConnectThrowsException() |
103
|
|
|
{ |
104
|
|
|
$this->connection->setParam('port', 9308); |
105
|
|
|
$this->connection->connect(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testPing() |
109
|
|
|
{ |
110
|
|
|
$this->connection->connect(); |
111
|
|
|
$this->assertTrue($this->connection->ping()); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @expectedException Foolz\SphinxQL\Exception\ConnectionException |
116
|
|
|
*/ |
117
|
|
|
public function testClose() |
118
|
|
|
{ |
119
|
|
|
$encoding = mb_internal_encoding(); |
120
|
|
|
$this->connection->connect(); |
121
|
|
|
if (method_exists($this->connection, 'getInternalEncoding')) { |
122
|
|
|
$this->assertEquals($encoding, $this->connection->getInternalEncoding()); |
123
|
|
|
$this->assertEquals('UTF-8', mb_internal_encoding()); |
124
|
|
|
} |
125
|
|
|
$this->connection->close(); |
|
|
|
|
126
|
|
|
$this->assertEquals($encoding, mb_internal_encoding()); |
127
|
|
|
$this->connection->getConnection(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testQuery() |
131
|
|
|
{ |
132
|
|
|
$this->connection->connect(); |
133
|
|
|
$this->assertSame(array( |
134
|
|
|
array('Variable_name' => 'total', 'Value' => '0'), |
135
|
|
|
array('Variable_name' => 'total_found', 'Value' => '0'), |
136
|
|
|
array('Variable_name' => 'time', 'Value' => '0.000'), |
137
|
|
|
), $this->connection->query('SHOW META')->getStored()); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testMultiQuery() |
141
|
|
|
{ |
142
|
|
|
$this->connection->connect(); |
143
|
|
|
$query = $this->connection->multiQuery(array('SHOW META')); |
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
|
|
|
), $query->getNext()->fetchAllAssoc()); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @expectedException Foolz\SphinxQL\Exception\SphinxQLException |
153
|
|
|
* @expectedExceptionMessage The Queue is empty. |
154
|
|
|
*/ |
155
|
|
|
public function testEmptyMultiQuery() |
156
|
|
|
{ |
157
|
|
|
$this->connection->connect(); |
158
|
|
|
$this->connection->multiQuery(array()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @expectedException Foolz\SphinxQL\Exception\DatabaseException |
163
|
|
|
*/ |
164
|
|
|
public function testMultiQueryThrowsException() |
165
|
|
|
{ |
166
|
|
|
$this->connection->multiQuery(array('SHOW METAL')); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @expectedException Foolz\SphinxQL\Exception\DatabaseException |
171
|
|
|
*/ |
172
|
|
|
public function testQueryThrowsException() |
173
|
|
|
{ |
174
|
|
|
$this->connection->query('SHOW METAL'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function testEscape() |
178
|
|
|
{ |
179
|
|
|
$result = $this->connection->escape('\' "" \'\' '); |
180
|
|
|
$this->assertEquals('\'\\\' \\"\\" \\\'\\\' \'', $result); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @expectedException Foolz\SphinxQL\Exception\ConnectionException |
185
|
|
|
*/ |
186
|
|
|
public function testEscapeThrowsException() |
187
|
|
|
{ |
188
|
|
|
// or we get the wrong error popping up |
189
|
|
|
$this->connection->setParam('port', 9308); |
190
|
|
|
$this->connection->connect(); |
191
|
|
|
$this->connection->escape('\' "" \'\' '); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function testQuote() |
195
|
|
|
{ |
196
|
|
|
$this->connection->connect(); |
197
|
|
|
$this->assertEquals('null', $this->connection->quote(null)); |
198
|
|
|
$this->assertEquals(1, $this->connection->quote(true)); |
199
|
|
|
$this->assertEquals(0, $this->connection->quote(false)); |
200
|
|
|
$this->assertEquals("fo'o'bar", $this->connection->quote(new Expression("fo'o'bar"))); |
201
|
|
|
$this->assertEquals(123, $this->connection->quote(123)); |
202
|
|
|
$this->assertEquals("12.3", $this->connection->quote(12.3)); |
203
|
|
|
$this->assertEquals("'12.3'", $this->connection->quote('12.3')); |
204
|
|
|
$this->assertEquals("'12'", $this->connection->quote('12')); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function testQuoteArr() |
208
|
|
|
{ |
209
|
|
|
$this->connection->connect(); |
210
|
|
|
$this->assertEquals( |
211
|
|
|
array('null', 1, 0, "fo'o'bar", 123, "12.3", "'12.3'", "'12'"), |
212
|
|
|
$this->connection->quoteArr(array(null, true, false, new Expression("fo'o'bar"), 123, 12.3, '12.3', '12')) |
213
|
|
|
); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
} |
217
|
|
|
|
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.