Commit cfe87460 by Carsten Brandt

fixed return type for redis simple strings

fixes #4745
parent 2de384e3
......@@ -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
----------------------
......
......@@ -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
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
......
......@@ -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]));
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment