Skip to content

Commit d331ea1

Browse files
committed
Merge pull request #27 from cheprasov/dev-with-versions
Added unit test, methods with names echo and eval are avaliable now
2 parents 3f9935d + 548f3d5 commit d331ea1

21 files changed

+495
-52
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# php-redis-client
22
Project in development
33

4-
Docker for tests
4+
Docker with Redis for tests
55
https://hub.docker.com/r/cheprasov/redis-for-tests/

phpunit.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
verbose = "true"
1717
beStrictAboutTestsThatDoNotTestAnything = "true"
1818
checkForUnintentionallyCoveredCode = "false"
19-
beStrictAboutOutputDuringTests = "true"
19+
beStrictAboutOutputDuringTests = "false"
2020
beStrictAboutTestSize = "true"
2121
>
2222
<testsuites>

src/RedisClient/Client/AbstractRedisClient.php

+73-26
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
use RedisClient\Protocol\ProtocolInterface;
1919
use RedisClient\Protocol\RedisProtocol;
2020

21-
/**
22-
* Class RedisClient
23-
* @package RedisClient
24-
*/
21+
2522
abstract class AbstractRedisClient {
2623

2724
const VERSION = '1.0.0';
@@ -106,28 +103,6 @@ protected function executeCommand(array $command, array $params = null, $parserI
106103
return $response;
107104
}
108105

109-
/**
110-
* @param string[] $structure
111-
* @return mixed
112-
* @throws ErrorResponseException
113-
*/
114-
public function executeRaw($structure) {
115-
$response = $this->getProtocol()->send($structure);
116-
if ($response instanceof ErrorResponseException) {
117-
throw $response;
118-
}
119-
return $response;
120-
}
121-
122-
/**
123-
* @param string $stringCommand
124-
* @return mixed
125-
* @throws ErrorResponseException
126-
*/
127-
public function executeRawString($stringCommand) {
128-
return $this->executeRaw(explode(' ', $stringCommand));
129-
}
130-
131106
/**
132107
* @inheritdoc
133108
*/
@@ -196,4 +171,76 @@ protected function executePipeline(PipelineInterface $Pipeline) {
196171
return $Pipeline->parseResponse($responses);
197172
}
198173

174+
/**
175+
* @param string[] $structure
176+
* @return mixed
177+
* @throws ErrorResponseException
178+
*/
179+
public function executeRaw($structure) {
180+
$response = $this->getProtocol()->send($structure);
181+
if ($response instanceof ErrorResponseException) {
182+
throw $response;
183+
}
184+
return $response;
185+
}
186+
187+
/**
188+
* @param string $command
189+
* @return mixed
190+
*/
191+
public function executeRawString($command) {
192+
return $this->executeRaw($this->parseRawString($command));
193+
}
194+
195+
/**
196+
* @param string $command
197+
* @return string[]
198+
*/
199+
public function parseRawString($command) {
200+
$structure = [];
201+
$line = ''; $quotes = false;
202+
for ($i = 0, $length = strlen($command); $i <= $length; ++$i) {
203+
if ($i === $length) {
204+
if (isset($line[0])) {
205+
$structure[] = $line;
206+
$line = '';
207+
}
208+
break;
209+
}
210+
if ($command[$i] === '"' && $i && $command[$i - 1] !== '\\') {
211+
$quotes = !$quotes;
212+
if (!$quotes && !isset($line[0]) && $i + 1 === $length) {
213+
$structure[] = $line;
214+
$line = '';
215+
}
216+
} else if ($command[$i] === ' ' && !$quotes) {
217+
if (isset($command[$i + 1]) && trim($command[$i + 1])) {
218+
if (count($structure) || isset($line[0])) {
219+
$structure[] = $line;
220+
$line = '';
221+
}
222+
}
223+
} else {
224+
$line .= $command[$i];
225+
}
226+
}
227+
array_walk($structure, function(&$line) {
228+
$line = str_replace('\\"', '"', $line);
229+
});
230+
return $structure;
231+
}
232+
233+
/**
234+
* @param string $name
235+
* @param array $arguments
236+
* @return mixed
237+
* @throws \Exception
238+
*/
239+
public function __call($name , array $arguments) {
240+
if ($method = $this->getMethodNameForReservedWord($name)) {
241+
return call_user_func_array([$this, $method], $arguments);
242+
}
243+
throw new \Exception('Call to undefined method '. static::class. '::'. $name);
244+
}
245+
199246
}

src/RedisClient/Command/Response/ResponseParser.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,17 @@ public static function parseAssocArray($response) {
5959
}
6060

6161
/**
62-
* @param string[] $response
62+
* @param string $response
6363
* @return array
6464
*/
6565
public static function parseClientList($response) {
6666
if (!is_string($response)) {
6767
return $response;
6868
}
6969
$array = [];
70-
foreach (explode("\n", $response) as $client) {
70+
foreach (explode("\n", trim($response)) as $client) {
7171
$c = [];
72-
foreach (explode(' ', $client) as $param) {
72+
foreach (explode(' ', trim($client)) as $param) {
7373
$args = explode('=', $param, 2);
7474
if (isset($args[0], $args[1]) && ($key = trim($args[0]))) {
7575
$c[$key] = trim($args[1]);
@@ -137,10 +137,10 @@ public static function parseInteger($response) {
137137
}
138138

139139
/**
140-
* @param array $response
141-
* @return string string
140+
* @param string[] $response
141+
* @return string
142142
*/
143-
public static function parseTime($response) {
143+
public static function parseTime(array $response) {
144144
if (is_array($response) && count($response) === 2) {
145145
if (($len = strlen($response[1])) < 6) {
146146
$response[1] = str_repeat('0', 6 - $len) . $response[1];

src/RedisClient/Command/Traits/AbstractCommandsTrait.php

+23
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
*/
1111
namespace RedisClient\Command\Traits;
1212

13+
/**
14+
* @method mixed eval($script, $keys = null, $args = null)
15+
* @method string echo($message)
16+
*/
1317
trait AbstractCommandsTrait {
1418

1519
/**
@@ -32,4 +36,23 @@ abstract protected function subscribeCommand(array $subCommand, array $unsubComm
3236
* @return string
3337
*/
3438
abstract public function getVersion();
39+
40+
/**
41+
* @var array
42+
*/
43+
protected static $methodForReservedWord = [
44+
'eval' => 'evalScript',
45+
'echo' => 'echoMessage',
46+
];
47+
48+
/**
49+
* @param string $name
50+
* @return string|null
51+
*/
52+
protected function getMethodNameForReservedWord($name) {
53+
if (isset(static::$methodForReservedWord[$name])) {
54+
return static::$methodForReservedWord[$name];
55+
}
56+
return null;
57+
}
3558
}

src/RedisClient/Command/Traits/Version2x6/ConnectionCommandsTrait.php

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public function auth($password) {
3333
* Available since 1.0.0.
3434
* @link http://redis.io/commands/echo
3535
*
36+
* method for reversed word <echo> in PHP
37+
*
3638
* @param string $message
3739
* @return string Returns message
3840
*/

src/RedisClient/Command/Traits/Version2x6/ScriptingCommandsTrait.php

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ trait ScriptingCommandsTrait {
2222
* Time complexity: Depends on the script that is executed.
2323
* @link http://redis.io/commands/eval
2424
*
25+
* method for reversed word <eval> in PHP
26+
*
2527
* @param string $script
2628
* @param array|null $keys
2729
* @param array|null $args

src/RedisClient/Pipeline/AbstractPipeline.php

+13
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,17 @@ public function parseResponse($responses) {
8383
return $responses;
8484
}
8585

86+
/**
87+
* @param string $name
88+
* @param array $arguments
89+
* @return mixed
90+
* @throws \Exception
91+
*/
92+
public function __call($name , array $arguments) {
93+
if ($method = $this->getMethodNameForReservedWord($name)) {
94+
return call_user_func_array([$this, $method], $arguments);
95+
}
96+
throw new \Exception('Call to undefined method '. static::class. '::'. $name);
97+
}
98+
8699
}

src/RedisClient/Pipeline/Version/Pipeline2x6.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
*
1919
* Connection
2020
* @method Pipeline2x6 auth($password)
21-
* @method Pipeline2x6 echoMessage($message)
21+
* @method Pipeline2x6 echo($message)
22+
* @method Pipeline2x6 echoMessage($message) - alias method for reversed word <echo>
2223
* @method Pipeline2x6 ping($message = null)
2324
* @method Pipeline2x6 quit()
2425
* @method Pipeline2x6 select($db)
@@ -85,7 +86,8 @@
8586
* @method Pipeline2x6 unsubscribe($channels)
8687
*
8788
* Scripting
88-
* @method Pipeline2x6 evalScript($script, $keys = null, $args = null)
89+
* @method Pipeline2x6 eval($script, $keys = null, $args = null)
90+
* @method Pipeline2x6 evalScript($script, $keys = null, $args = null) - alias method for reversed word <eval>
8991
* @method Pipeline2x6 evalsha($sha, $keys = null, $args = null)
9092
* @method Pipeline2x6 scriptExists($scriptsSha)
9193
* @method Pipeline2x6 scriptFlush()

src/RedisClient/Pipeline/Version/Pipeline2x8.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
*
1919
* Connection
2020
* @method Pipeline2x8 auth($password)
21-
* @method Pipeline2x8 echoMessage($message)
21+
* @method Pipeline2x8 echo($message)
22+
* @method Pipeline2x8 echoMessage($message) - alias method for reversed word <echo>
2223
* @method Pipeline2x8 ping($message = null)
2324
* @method Pipeline2x8 quit()
2425
* @method Pipeline2x8 select($db)
@@ -85,7 +86,8 @@
8586
* @method Pipeline2x8 unsubscribe($channels)
8687
*
8788
* Scripting
88-
* @method Pipeline2x8 evalScript($script, $keys = null, $args = null)
89+
* @method Pipeline2x8 eval($script, $keys = null, $args = null)
90+
* @method Pipeline2x8 evalScript($script, $keys = null, $args = null) - alias method for reversed word <eval>
8991
* @method Pipeline2x8 evalsha($sha, $keys = null, $args = null)
9092
* @method Pipeline2x8 scriptExists($scriptsSha)
9193
* @method Pipeline2x8 scriptFlush()

src/RedisClient/Pipeline/Version/Pipeline3x0.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
*
1919
* Connection
2020
* @method Pipeline3x0 auth($password)
21-
* @method Pipeline3x0 echoMessage($message)
21+
* @method Pipeline3x0 echo($message)
22+
* @method Pipeline3x0 echoMessage($message) - alias method for reversed word <echo>
2223
* @method Pipeline3x0 ping($message = null)
2324
* @method Pipeline3x0 quit()
2425
* @method Pipeline3x0 select($db)
@@ -85,7 +86,8 @@
8586
* @method Pipeline3x0 unsubscribe($channels)
8687
*
8788
* Scripting
88-
* @method Pipeline3x0 evalScript($script, $keys = null, $args = null)
89+
* @method Pipeline3x0 eval($script, $keys = null, $args = null)
90+
* @method Pipeline3x0 evalScript($script, $keys = null, $args = null) - alias method for reversed word <eval>
8991
* @method Pipeline3x0 evalsha($sha, $keys = null, $args = null)
9092
* @method Pipeline3x0 scriptExists($scriptsSha)
9193
* @method Pipeline3x0 scriptFlush()

src/RedisClient/Pipeline/Version/Pipeline3x2.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
*
1919
* Connection
2020
* @method Pipeline3x2 auth($password)
21-
* @method Pipeline3x2 echoMessage($message)
21+
* @method Pipeline3x2 echo($message)
22+
* @method Pipeline3x2 echoMessage($message) - alias method for reversed word <echo>
2223
* @method Pipeline3x2 ping($message = null)
2324
* @method Pipeline3x2 quit()
2425
* @method Pipeline3x2 select($db)
@@ -85,7 +86,8 @@
8586
* @method Pipeline3x2 unsubscribe($channels)
8687
*
8788
* Scripting
88-
* @method Pipeline3x2 evalScript($script, $keys = null, $args = null)
89+
* @method Pipeline3x2 eval($script, $keys = null, $args = null)
90+
* @method Pipeline3x2 evalScript($script, $keys = null, $args = null) - alias method for reversed word <eval>
8991
* @method Pipeline3x2 evalsha($sha, $keys = null, $args = null)
9092
* @method Pipeline3x2 scriptExists($scriptsSha)
9193
* @method Pipeline3x2 scriptFlush()
@@ -275,6 +277,7 @@
275277
* @method Pipeline3x2 geopos($key, $members)
276278
* @method Pipeline3x2 georadius($key, $longitude, $latitude, $radius, $unit, $withcoord = false, $withdist = false, $withhash = false, $count = null, $asc = null)
277279
* @method Pipeline3x2 georadiusbymember($key, $member, $radius, $unit, $withcoord = false, $withdist = false, $withhash = false, $count = null, $asc = null)
280+
* @method Pipeline3x2 geodel($key, $members)
278281
*
279282
* Hashes
280283
* @method Pipeline3x2 hstrlen($key, $field)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* This file is part of RedisClient.
4+
* git: https://github.com/cheprasov/php-redis-client
5+
*
6+
* (C) Alexander Cheprasov <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
namespace Test\Build;
12+
13+
class PipelineAnnotationsBuildTest extends \PHPUnit_Framework_TestCase {
14+
15+
/**
16+
* Update annotations for pipeline on tests
17+
*/
18+
public function test_generate_annotations_for_pipeline() {
19+
chdir(__DIR__.'/../../');
20+
$result = `php ./tools/generate_annotations_for_pipeline.php update`;
21+
22+
$lines = explode("\n", trim($result));
23+
$this->assertSame(4, count($lines));
24+
25+
foreach ($lines as $line) {
26+
$this->assertSame(true, strpos($line, 'updated') > 0 || strpos($line, 'has not changes') > 0);
27+
}
28+
}
29+
30+
}

tests/Integration/Version2x6/ConnectionCommandsTest.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,22 @@ public function test_auth() {
6060
}
6161
}
6262

63-
public function test_echo() {
63+
public function test_echoMessage() {
6464
$Redis = static::$Redis;
6565

6666
$this->assertSame('message', $Redis->echoMessage('message'));
6767
$this->assertSame('', $Redis->echoMessage(''));
6868
$this->assertSame('foo bar', $Redis->echoMessage('foo bar'));
6969
}
7070

71+
public function test_echo() {
72+
$Redis = static::$Redis;
73+
74+
$this->assertSame('message', $Redis->echo('message'));
75+
$this->assertSame('', $Redis->echo(''));
76+
$this->assertSame('foo bar', $Redis->echo('foo bar'));
77+
}
78+
7179
public function test_ping() {
7280
$Redis = static::$Redis;
7381
$this->assertSame('PONG', $Redis->ping());

0 commit comments

Comments
 (0)