浏览代码

Add the WITHSCORES modifier only when option value is true.

Fixes issue #107.
Daniele Alessandri 12 年之前
父节点
当前提交
7434a9bd0e

+ 4 - 0
CHANGELOG.md

@@ -4,6 +4,10 @@ v0.8.4 (2013-xx-xx)
 - __FIX__: allow `HMSET` when using a cluster of Redis nodes with client-side
   sharding or redis-cluster (ISSUE #106).
 
+- __FIX__: set `WITHSCORES` modifer for `ZRANGE`, `ZREVRANGE`, `ZRANGEBYSCORE`
+  and `ZREVRANGEBYSCORE` only when the options array passed to these commands
+  has `WITHSCORES` set to `true` (ISSUE #107).
+
 
 v0.8.3 (2013-02-18)
 ===============================================================================

+ 2 - 2
lib/Predis/Command/ZSetRange.php

@@ -33,7 +33,7 @@ class ZSetRange extends PrefixableCommand
         if (count($arguments) === 4) {
             $lastType = gettype($arguments[3]);
 
-            if ($lastType === 'string' && strtolower($arguments[3]) === 'withscores') {
+            if ($lastType === 'string' && strtoupper($arguments[3]) === 'WITHSCORES') {
                 // Used for compatibility with older versions
                 $arguments[3] = array('WITHSCORES' => true);
                 $lastType = 'array';
@@ -59,7 +59,7 @@ class ZSetRange extends PrefixableCommand
         $opts = array_change_key_case($options, CASE_UPPER);
         $finalizedOpts = array();
 
-        if (isset($opts['WITHSCORES'])) {
+        if (!empty($opts['WITHSCORES'])) {
             $finalizedOpts[] = 'WITHSCORES';
         }
 

+ 18 - 0
tests/Predis/Command/ZSetRangeByScoreTest.php

@@ -127,6 +127,24 @@ class ZSetRangeByScoreTest extends CommandTestCase
         $this->assertSame($expected, $command->getArguments());
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testAddsWithscoresModifiersOnlyWhenOptionIsTrue()
+    {
+        $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => true));
+        $this->assertSame(array('zset', 0, 100, 'WITHSCORES'), $command->getArguments());
+
+        $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => 1));
+        $this->assertSame(array('zset', 0, 100, 'WITHSCORES'), $command->getArguments());
+
+        $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => false));
+        $this->assertSame(array('zset', 0, 100), $command->getArguments());
+
+        $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => 0));
+        $this->assertSame(array('zset', 0, 100), $command->getArguments());
+    }
+
     /**
      * @group connected
      */

+ 18 - 0
tests/Predis/Command/ZSetRangeTest.php

@@ -103,6 +103,24 @@ class ZSetRangeTest extends CommandTestCase
         $this->assertSame($expected, $command->getArguments());
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testAddsWithscoresModifiersOnlyWhenOptionIsTrue()
+    {
+        $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => true));
+        $this->assertSame(array('zset', 0, 100, 'WITHSCORES'), $command->getArguments());
+
+        $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => 1));
+        $this->assertSame(array('zset', 0, 100, 'WITHSCORES'), $command->getArguments());
+
+        $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => false));
+        $this->assertSame(array('zset', 0, 100), $command->getArguments());
+
+        $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => 0));
+        $this->assertSame(array('zset', 0, 100), $command->getArguments());
+    }
+
     /**
      * @group connected
      */

+ 18 - 0
tests/Predis/Command/ZSetReverseRangeByScoreTest.php

@@ -127,6 +127,24 @@ class ZSetReverseRangeByScoreTest extends CommandTestCase
         $this->assertSame($expected, $command->getArguments());
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testAddsWithscoresModifiersOnlyWhenOptionIsTrue()
+    {
+        $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => true));
+        $this->assertSame(array('zset', 0, 100, 'WITHSCORES'), $command->getArguments());
+
+        $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => 1));
+        $this->assertSame(array('zset', 0, 100, 'WITHSCORES'), $command->getArguments());
+
+        $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => false));
+        $this->assertSame(array('zset', 0, 100), $command->getArguments());
+
+        $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => 0));
+        $this->assertSame(array('zset', 0, 100), $command->getArguments());
+    }
+
     /**
      * @group connected
      */

+ 18 - 0
tests/Predis/Command/ZSetReverseRangeTest.php

@@ -103,6 +103,24 @@ class ZSetReverseRangeTest extends CommandTestCase
         $this->assertSame($expected, $command->getArguments());
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testAddsWithscoresModifiersOnlyWhenOptionIsTrue()
+    {
+        $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => true));
+        $this->assertSame(array('zset', 0, 100, 'WITHSCORES'), $command->getArguments());
+
+        $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => 1));
+        $this->assertSame(array('zset', 0, 100, 'WITHSCORES'), $command->getArguments());
+
+        $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => false));
+        $this->assertSame(array('zset', 0, 100), $command->getArguments());
+
+        $command = $this->getCommandWithArguments('zset', 0, 100, array('withscores' => 0));
+        $this->assertSame(array('zset', 0, 100), $command->getArguments());
+    }
+
     /**
      * @group connected
      */