瀏覽代碼

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

Daniele Alessandri 14 年之前
父節點
當前提交
e3ee595768
共有 3 個文件被更改,包括 39 次插入0 次删除
  1. 5 0
      lib/Predis.php
  2. 2 0
      test/PredisShared.php
  3. 32 0
      test/RedisCommandsTest.php

+ 5 - 0
lib/Predis.php

@@ -1825,6 +1825,7 @@ class RedisServer_vNext extends RedisServer_v2_0 {
             'strlen'                    => '\Predis\Commands\Strlen',
             'setrange'                  => '\Predis\Commands\SetRange',
             'getrange'                  => '\Predis\Commands\Substr',
+            'setbit'                    => '\Predis\Commands\SetBit',
 
             /* commands operating on the key space */
             'persist'                   => '\Predis\Commands\Persist',
@@ -2367,6 +2368,10 @@ class Substr extends \Predis\MultiBulkCommand {
     public function getCommandId() { return 'SUBSTR'; }
 }
 
+class SetBit extends \Predis\MultiBulkCommand {
+    public function getCommandId() { return 'SETBIT'; }
+}
+
 class Strlen extends \Predis\MultiBulkCommand {
     public function getCommandId() { return 'STRLEN'; }
 }

+ 2 - 0
test/PredisShared.php

@@ -26,6 +26,8 @@ class RC {
     const EXCEPTION_EXEC_NO_MULTI  = 'EXEC without MULTI';
     const EXCEPTION_SETEX_TTL      = 'invalid expire time in SETEX';
     const EXCEPTION_HASH_VALNOTINT = 'hash value is not an integer';
+    const EXCEPTION_BIT_VALUE      = 'bit is not an integer or out of range';
+    const EXCEPTION_BIT_OFFSET     = 'bit offset is not an integer or out of range';
 
     private static $_connection;
 

+ 32 - 0
test/RedisCommandsTest.php

@@ -262,6 +262,38 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
         });
     }
 
+    function testSetBit() {
+        $this->assertEquals(0, $this->redis->setbit('binary', 31, 1));
+        $this->assertEquals(0, $this->redis->setbit('binary', 0, 1));
+        $this->assertEquals(4, $this->redis->strlen('binary'));
+        $this->assertEquals("\x80\x00\00\x01", $this->redis->get('binary'));
+
+        $this->assertEquals(1, $this->redis->setbit('binary', 0, 0));
+        $this->assertEquals(0, $this->redis->setbit('binary', 0, 0));
+        $this->assertEquals("\x00\x00\00\x01", $this->redis->get('binary'));
+
+        RC::testForServerException($this, RC::EXCEPTION_BIT_OFFSET, function($test) {
+            $test->redis->setbit('binary', -1, 1);
+        });
+
+        RC::testForServerException($this, RC::EXCEPTION_BIT_OFFSET, function($test) {
+            $test->redis->setbit('binary', 'invalid', 1);
+        });
+
+        RC::testForServerException($this, RC::EXCEPTION_BIT_VALUE, function($test) {
+            $test->redis->setbit('binary', 15, 255);
+        });
+
+        RC::testForServerException($this, RC::EXCEPTION_BIT_VALUE, function($test) {
+            $test->redis->setbit('binary', 15, 'invalid');
+        });
+
+        RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
+            $test->redis->rpush('metavars', 'foo');
+            $test->redis->setbit('metavars', 0, 1);
+        });
+    }
+
     function testStrlen() {
         $this->redis->set('var', 'foobar');
         $this->assertEquals(6, $this->redis->strlen('var'));