diff --git a/lib/Predis.php b/lib/Predis.php index 4dd771e9..25fcac43 100644 --- a/lib/Predis.php +++ b/lib/Predis.php @@ -1823,6 +1823,7 @@ class RedisServer_vNext extends RedisServer_v2_0 { /* commands operating on string values */ 'strlen' => '\Predis\Commands\Strlen', + 'setrange' => '\Predis\Commands\SetRange', 'getrange' => '\Predis\Commands\Substr', /* commands operating on the key space */ @@ -2358,6 +2359,10 @@ class Append extends \Predis\MultiBulkCommand { public function getCommandId() { return 'APPEND'; } } +class SetRange extends \Predis\MultiBulkCommand { + public function getCommandId() { return 'SETRANGE'; } +} + class Substr extends \Predis\MultiBulkCommand { public function getCommandId() { return 'SUBSTR'; } } diff --git a/test/PredisShared.php b/test/PredisShared.php index ceb5eff2..b56b771d 100644 --- a/test/PredisShared.php +++ b/test/PredisShared.php @@ -20,6 +20,7 @@ class RC { const EXCEPTION_WRONG_TYPE = 'Operation against a key holding the wrong kind of value'; const EXCEPTION_NO_SUCH_KEY = 'no such key'; const EXCEPTION_OUT_OF_RANGE = 'index out of range'; + const EXCEPTION_OFFSET_RANGE = 'offset is out of range'; const EXCEPTION_INVALID_DB_IDX = 'invalid DB index'; const EXCEPTION_VALUE_NOT_INT = 'value is not an integer'; const EXCEPTION_EXEC_NO_MULTI = 'EXEC without MULTI'; diff --git a/test/RedisCommandsTest.php b/test/RedisCommandsTest.php index 7b838d1a..2dd3a41f 100644 --- a/test/RedisCommandsTest.php +++ b/test/RedisCommandsTest.php @@ -223,6 +223,28 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { }); } + function testSetRange() { + $this->assertEquals(6, $this->redis->setrange('var', 0, 'foobar')); + $this->assertEquals('foobar', $this->redis->get('var')); + $this->assertEquals(6, $this->redis->setrange('var', 3, 'foo')); + $this->assertEquals('foofoo', $this->redis->get('var')); + $this->assertEquals(16, $this->redis->setrange('var', 10, 'barbar')); + $this->assertEquals("foofoo\x00\x00\x00\x00barbar", $this->redis->get('var')); + + $this->assertEquals(4, $this->redis->setrange('binary', 0, pack('l', -2147483648))); + list($unpacked) = array_values(unpack('l', $this->redis->get('binary'))); + $this->assertEquals(-2147483648, $unpacked); + + RC::testForServerException($this, RC::EXCEPTION_OFFSET_RANGE, function($test) { + $test->redis->setrange('var', -1, 'bogus'); + }); + + RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) { + $test->redis->rpush('metavars', 'foo'); + $test->redis->setrange('metavars', 0, 'hoge'); + }); + } + function testSubstr() { $this->redis->set('var', 'foobar'); $this->assertEquals('foo', $this->redis->substr('var', 0, 2));