Pārlūkot izejas kodu

New command: SETRANGE (Redis v2.2-dev).

Conflicts:

	lib/Predis.php
Daniele Alessandri 14 gadi atpakaļ
vecāks
revīzija
e1924551b1
3 mainītis faili ar 28 papildinājumiem un 0 dzēšanām
  1. 5 0
      lib/Predis.php
  2. 1 0
      test/PredisShared.php
  3. 22 0
      test/RedisCommandsTest.php

+ 5 - 0
lib/Predis.php

@@ -1795,6 +1795,7 @@ class RedisServer_vNext extends RedisServer_v2_0 {
 
             /* commands operating on string values */
             'strlen'                    => '\Predis\Commands\Strlen',
+            'setrange'                  => '\Predis\Commands\SetRange',
             'getrange'                  => '\Predis\Commands\GetRange',
 
             /* commands operating on the key space */
@@ -2347,6 +2348,10 @@ class Append extends Command {
     public function getCommandId() { return 'APPEND'; }
 }
 
+class SetRange extends Command {
+    public function getCommandId() { return 'SETRANGE'; }
+}
+
 class GetRange extends Command {
     public function getCommandId() { return 'GETRANGE'; }
 }

+ 1 - 0
test/PredisShared.php

@@ -30,6 +30,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';

+ 22 - 0
test/RedisCommandsTest.php

@@ -220,6 +220,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));