Skip to content

Commit c4b4ca7

Browse files
committed
Updated Readme, added examples for cluster, added mock for global functions
1 parent 4e00d93 commit c4b4ca7

File tree

12 files changed

+208
-33
lines changed

12 files changed

+208
-33
lines changed

examples/clusters.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,42 @@
9595
],
9696
]
9797
]);
98-
$RedisClient->set('foo', $time = time());
98+
$RedisClient->set('foo', 'foo-42');
9999
echo ClusterMap::getSlotByKey('foo') . PHP_EOL; // 12182
100-
echo $RedisClient->get('foo') . PHP_EOL; // 1483317104
100+
101+
$RedisClient->set('bar', 'bar-42');
102+
echo ClusterMap::getSlotByKey('bar') . PHP_EOL; // 5061
103+
104+
echo $RedisClient->get('foo') . PHP_EOL; // foo-42
105+
echo $RedisClient->get('bar') . PHP_EOL; // bar-42
106+
107+
// But, be careful with multi operation for pseudo Clusters,
108+
print_r($RedisClient->mget(['foo', 'bar'])); // ['foo-42', null]
109+
print_r($RedisClient->mget(['bar', 'foo'])); // ['bar-42', null]
110+
111+
112+
// Example 6. Each connection to Redis Server uses the same config.
113+
// For example, use can use password for all servers
114+
$RedisClient = ClientFactory::create([
115+
'server' => '127.0.0.1:6382', // Default server for connection
116+
'timeout' => 2,
117+
'password' => 'test-password-123',
118+
'cluster' => [
119+
'enabled' => true,
120+
'clusters' => [
121+
5460 => '127.0.0.1:6382', // slots from 0 to 5460
122+
10922 => '127.0.0.1:6384', // slots from 5461 to 10922
123+
16383 => '127.0.0.1:6386', // slots from 10923 to 16383
124+
],
125+
]
126+
]);
127+
$RedisClient->set('foo', 'foo-43');
128+
echo ClusterMap::getSlotByKey('foo') . PHP_EOL; // 12182
129+
130+
$RedisClient->set('bar', 'bar-43');
131+
echo ClusterMap::getSlotByKey('bar') . PHP_EOL; // 5061
132+
133+
echo $RedisClient->get('foo') . PHP_EOL; // foo-42
134+
echo $RedisClient->get('bar') . PHP_EOL; // bar-42
135+
101136

phpunit.xml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,16 @@
1919
beStrictAboutTestSize = "true"
2020
>
2121
<testsuites>
22-
<testsuite name="All tests">
23-
<directory suffix="Test.php" >./tests/</directory>
22+
<testsuite name="Build Tests">
23+
<directory suffix="Test.php" >./tests/Build/</directory>
24+
</testsuite>
25+
<testsuite name="Unit tests">
26+
<directory suffix="Test.php" >./tests/Unit/</directory>
27+
</testsuite>
28+
<testsuite name="Integration tests">
29+
<exclude>./tests/Integration/BaseVersionTest.php</exclude>
30+
<exclude>./tests/Integration/ClusterVersionTest.php</exclude>
31+
<directory suffix="Test.php" >./tests/Integration/</directory>
2432
</testsuite>
2533
</testsuites>
2634

src/RedisClient/Command/Parameter/Parameter.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static function address($param) {
5050
* @throws InvalidArgumentException
5151
*/
5252
public static function aggregate($param) {
53-
$param = strtoupper((string) $param);
53+
$param = strtoupper((string)$param);
5454
if (in_array($param, static::$aggregateParams)) {
5555
return $param;
5656
}
@@ -94,7 +94,7 @@ public static function assocArrayFlip(array $array) {
9494
* @return InvalidArgumentException
9595
*/
9696
public static function bitOperation($operation) {
97-
$operation = strtoupper((string) $operation);
97+
$operation = strtoupper((string)$operation);
9898
if (in_array($operation, static::$bitOperationParams)) {
9999
return $operation;
100100
}
@@ -106,7 +106,7 @@ public static function bitOperation($operation) {
106106
* @return int
107107
*/
108108
public static function bit($bit) {
109-
return (int) (bool) $bit;
109+
return (int)(bool)$bit;
110110
}
111111

112112
/**
@@ -120,13 +120,13 @@ public static function command($command) {
120120
/**
121121
* @param int|float|string $param
122122
* @param int[]|string[] $enum
123-
* @return string
123+
* @return string|bool
124124
*/
125125
public static function enum($param, array $enum) {
126126
if (!in_array($param, $enum)) {
127-
//throw new InvalidArgumentException('Incorrect param "'. $param .'" for enum('. implode(', ', $enum) .') ');
127+
return false;
128128
}
129-
return (string) $param;
129+
return (string)$param;
130130
}
131131

132132
/**
@@ -161,15 +161,15 @@ public static function geoUnit($unit) {
161161
* @return int
162162
*/
163163
public static function integer($int) {
164-
return (int) $int;
164+
return (int)$int;
165165
}
166166

167167
/**
168168
* @param mixed
169169
* @return int[]
170170
*/
171171
public static function integers($integers) {
172-
$integers = (array) $integers;
172+
$integers = (array)$integers;
173173
return array_map('static::integer', $integers);
174174
}
175175

@@ -178,15 +178,15 @@ public static function integers($integers) {
178178
* @return mixed
179179
*/
180180
public static function key($key) {
181-
return (string) $key;
181+
return (string)$key;
182182
}
183183

184184
/**
185185
* @param string|string[] $keys
186186
* @return array
187187
*/
188188
public static function keys($keys) {
189-
$keys = (array) $keys;
189+
$keys = (array)$keys;
190190
return array_map('static::key', $keys);
191191
}
192192

@@ -196,23 +196,23 @@ public static function keys($keys) {
196196
*/
197197
public static function limit($limit) {
198198
if (is_numeric($limit)) {
199-
return [0, (int) $limit];
199+
return [0, (int)$limit];
200200
}
201201
if (is_array($limit) && isset($limit['count'])) {
202202
return [
203203
empty($limit['offset']) ? 0: (int) $limit['offset'],
204-
(int) $limit['count'],
204+
(int)$limit['count'],
205205
];
206206
}
207207
if ($limit && is_string($limit) && preg_match('/^-?\d+\s+-?\d+$/', $limit)) {
208208
$limit = preg_split('/\s+/', trim($limit), 2);
209209
}
210210
if (is_array($limit)) {
211211
if (isset($limit[0]) && isset($limit[1])) {
212-
return [(int) $limit[0], (int) $limit[1]];
212+
return [(int)$limit[0], (int)$limit[1]];
213213
}
214214
if (isset($limit[0]) && !isset($limit[1])) {
215-
return [0, (int) $limit[0]];
215+
return [0, (int)$limit[0]];
216216
}
217217
}
218218
throw new InvalidArgumentException('Invalid limit '. $limit);
@@ -255,7 +255,7 @@ public static function nxXx($param) {
255255
* @return int
256256
*/
257257
public static function port($int) {
258-
$int = (int) $int;
258+
$int = (int)$int;
259259
if ($int > 0 && $int <= 65535) {
260260
return $int;
261261
}
@@ -289,7 +289,7 @@ public static function string($string) {
289289
* @return array
290290
*/
291291
public static function strings($strings) {
292-
$strings = (array) $strings;
292+
$strings = (array)$strings;
293293
return array_map('static::string', $strings);
294294
}
295295

src/RedisClient/Command/Response/ResponseParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public static function parseInfo($response) {
133133
* @return int
134134
*/
135135
public static function parseInteger($response) {
136-
return (int) $response;
136+
return (int)$response;
137137
}
138138

139139
/**

src/RedisClient/Connection/StreamConnection.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ public function onConnect($callback) {
9090
*/
9191
protected function getResource() {
9292
if (!$this->resource) {
93-
if (!$this->resource = stream_socket_client($this->server)) {
94-
throw new ConnectionException('Unable to connect to '. $this->server);
93+
$errno = null;
94+
$errstr = null;
95+
if (!$this->resource = stream_socket_client($this->server, $errno, $errstr)) {
96+
throw new ConnectionException('Unable to connect to '. $this->server . ' ('. $errstr .')');
9597
}
9698
if (isset($this->timeout)) {
9799
stream_set_timeout($this->resource, 0, $this->timeout);

tests/Integration/BaseVersionTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,6 @@ protected function setUp() {
125125
}
126126

127127
public function testSetup() {
128-
if (!static::$Redis) {
129-
$this->markTestSkipped();
130-
}
131128
$this->assertTrue(static::$Redis instanceof AbstractRedisClient, 'Can not create RedisClient for test');
132129
}
133130

tests/Integration/ClusterVersionTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,4 @@ protected function createRedisClient(array $config = []) {
6161
return new $class($config);
6262
}
6363

64-
public function test_empty() {
65-
$this->assertTrue(true);
66-
}
67-
6864
}

tests/Unit/AbstractRedisClientTest.php renamed to tests/Unit/Client/AbstractRedisClientTest.php

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11-
namespace Test\Unit;
11+
namespace Test\Unit\Client;
1212

1313
use RedisClient\Client\AbstractRedisClient;
14+
use RedisClient\Exception\EmptyResponseException;
15+
use RedisClient\Exception\MovedResponseException;
16+
use RedisClient\RedisClient;
17+
use Test\Unit\GlobalFunctionMock;
1418

1519
/**
1620
* @see AbstractRedisClient
21+
* @runTestsInSeparateProcesses
1722
*/
1823
class AbstractRedisClientTest extends \PHPUnit_Framework_TestCase {
1924

@@ -95,4 +100,65 @@ public function test_getStructure($expect, $command, $params) {
95100
$this->assertSame($expect, $Method->invoke($Client, $command, $params));
96101
}
97102

103+
protected function mockStream() {
104+
include_once(__DIR__ . '/../GlobalFunctionMock.php');
105+
GlobalFunctionMock::mockFunction('RedisClient\Connection', 'stream_socket_client', function() {return true;});
106+
GlobalFunctionMock::mockFunction('RedisClient\Connection', 'stream_set_timeout', function() {return true;});
107+
GlobalFunctionMock::mockFunction('RedisClient\Connection', 'fwrite', function($h, $m, $c) {return $c;});
108+
GlobalFunctionMock::mockFunction('RedisClient\Connection', 'fgets', function() {return '';});
109+
GlobalFunctionMock::mockFunction('RedisClient\Connection', 'fread', function() {return '';});
110+
GlobalFunctionMock::mockFunction('RedisClient\Connection', 'fclose', function() {return true;});
111+
}
112+
113+
public function test_mockStream() {
114+
$this->mockStream();
115+
GlobalFunctionMock::mockFunction('RedisClient\Connection', 'fgets', function() {
116+
return "+TEST\r\n";
117+
});
118+
119+
$Redis = new RedisClient();
120+
$this->assertSame('TEST', $Redis->ping());
121+
unset($Redis);
122+
$this->assertSame(1, GlobalFunctionMock::getCountCalls('stream_socket_client'));
123+
$this->assertSame(1, GlobalFunctionMock::getCountCalls('stream_set_timeout'));
124+
$this->assertSame(1, GlobalFunctionMock::getCountCalls('fwrite'));
125+
$this->assertSame(1, GlobalFunctionMock::getCountCalls('fgets'));
126+
$this->assertSame(0, GlobalFunctionMock::getCountCalls('fread'));
127+
$this->assertSame(0, GlobalFunctionMock::getCountCalls('fclose'));
128+
}
129+
130+
public function test_stream() {
131+
$Redis = new RedisClient();
132+
$this->assertSame('PONG', $Redis->ping());
133+
}
134+
135+
public function test_MovedErrorResponse() {
136+
$this->mockStream();
137+
GlobalFunctionMock::mockFunction('RedisClient\Connection', 'fwrite', function($h, $m, $c) {
138+
$this->assertSame(true, $h);
139+
$this->assertSame("*2\r\n$3\r\nGET\r\n$3\r\nkey\r\n", $m);
140+
$this->assertSame(22, $c);
141+
return $c;
142+
});
143+
GlobalFunctionMock::mockFunction('RedisClient\Connection', 'fgets', function() {
144+
return "-MOVED 42 server\r\n";
145+
});
146+
147+
$Redis = new RedisClient();
148+
try {
149+
$Redis->get('key');
150+
$this->assertTrue(false, 'Expect MovedResponseException');
151+
} catch (\Exception $Ex) {
152+
/** @var MovedResponseException $Ex*/
153+
$this->assertSame(true, $Ex instanceof MovedResponseException);
154+
$this->assertSame(42, $Ex->getSlot());
155+
$this->assertSame('server', $Ex->getServer());
156+
}
157+
158+
$this->assertSame(1, GlobalFunctionMock::getCountCalls('stream_socket_client'));
159+
$this->assertSame(1, GlobalFunctionMock::getCountCalls('stream_set_timeout'));
160+
$this->assertSame(1, GlobalFunctionMock::getCountCalls('fwrite'));
161+
$this->assertSame(1, GlobalFunctionMock::getCountCalls('fgets'));
162+
$this->assertSame(0, GlobalFunctionMock::getCountCalls('fread'));
163+
}
98164
}

tests/Unit/ClientFactoryTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111
namespace Test\Unit;
12+
1213
use RedisClient\Client\Version\RedisClient2x6;
1314
use RedisClient\Client\Version\RedisClient2x8;
1415
use RedisClient\Client\Version\RedisClient3x0;

tests/Unit/ResponseParserTest.php renamed to tests/Unit/Command/Response/ResponseParserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
11-
namespace Test\Unit;
11+
namespace Test\Unit\Command\Response;
1212

1313
use RedisClient\Client\AbstractRedisClient;
1414
use RedisClient\Command\Response\ResponseParser;

0 commit comments

Comments
 (0)