From cfe874607f76507dd614a984928320d79d3f39c3 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Thu, 13 Nov 2014 16:43:36 +0100 Subject: [PATCH] fixed return type for redis simple strings fixes #4745 --- extensions/redis/CHANGELOG.md | 3 +-- extensions/redis/Connection.php | 15 ++++++++--- .../extensions/redis/RedisConnectionTest.php | 26 +++++++++++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/extensions/redis/CHANGELOG.md b/extensions/redis/CHANGELOG.md index 8a16cd35b2..ef0d5d3e08 100644 --- a/extensions/redis/CHANGELOG.md +++ b/extensions/redis/CHANGELOG.md @@ -4,8 +4,7 @@ Yii Framework 2 redis extension Change Log 2.0.1 under development ----------------------- -- no changes in this release. - +- Bug #4745: value of simple string returns was ignored by redis client and `true` is returned instead, now only `OK` will result in a `true` while all other values are returned as is (cebe) 2.0.0 October 12, 2014 ---------------------- diff --git a/extensions/redis/Connection.php b/extensions/redis/Connection.php index e2111443b3..801c66a144 100644 --- a/extensions/redis/Connection.php +++ b/extensions/redis/Connection.php @@ -337,7 +337,8 @@ class Connection extends Component * @return array|bool|null|string Dependent on the executed command this method * will return different data types: * - * - `true` for commands that return "status reply". + * - `true` for commands that return "status reply" with the message `'OK'` or `'PONG'`. + * - `string` for commands that return "status reply" that does not have the message `OK` (since version 2.0.1). * - `string` for commands that return "integer reply" * as the value is in the range of a signed 64 bit integer. * - `string` or `null` for commands that return "bulk reply". @@ -363,6 +364,11 @@ class Connection extends Component return $this->parseResponse(implode(' ', $params)); } + /** + * @param string $command + * @return mixed + * @throws Exception on error + */ private function parseResponse($command) { if (($line = fgets($this->_socket)) === false) { @@ -372,8 +378,11 @@ class Connection extends Component $line = mb_substr($line, 1, -2, '8bit'); switch ($type) { case '+': // Status reply - - return true; + if ($line === 'OK' || $line === 'PONG') { + return true; + } else { + return $line; + } case '-': // Error reply throw new Exception("Redis error: " . $line . "\nRedis command was: " . $command); case ':': // Integer reply diff --git a/tests/unit/extensions/redis/RedisConnectionTest.php b/tests/unit/extensions/redis/RedisConnectionTest.php index 5a6e20cb43..b25e28dcda 100644 --- a/tests/unit/extensions/redis/RedisConnectionTest.php +++ b/tests/unit/extensions/redis/RedisConnectionTest.php @@ -53,4 +53,30 @@ class RedisConnectionTest extends RedisTestCase $db->set('hi', $data); $this->assertEquals($data, $db->get('hi')); } + + /** + * https://github.com/yiisoft/yii2/issues/4745 + */ + public function testReturnType() + { + $redis = $this->getConnection(); + $redis->executeCommand('SET',['key1','val1']); + $redis->executeCommand('HMSET',['hash1','hk3','hv3','hk4','hv4']); + $redis->executeCommand('RPUSH',['newlist2','tgtgt','tgtt','44',11]); + $redis->executeCommand('SADD',['newset2','segtggttval','sv1','sv2','sv3']); + $redis->executeCommand('ZADD',['newz2',2,'ss',3,'pfpf']); + $allKeys = $redis->executeCommand('KEYS',['*']); + sort($allKeys); + $this->assertEquals(['hash1', 'key1', 'newlist2', 'newset2', 'newz2'], $allKeys); + $expected = [ + 'hash1' => 'hash', + 'key1' => 'string', + 'newlist2' => 'list', + 'newset2' => 'set', + 'newz2' => 'zset', + ]; + foreach($allKeys as $key) { + $this->assertEquals($expected[$key], $redis->executeCommand('TYPE',[$key])); + } + } }