Преглед на файлове

Allow passing an array for the list of source keys to be used in ZUNIONSTORE/ZINTERSTORE.

Daniele Alessandri преди 14 години
родител
ревизия
38e76cbbc8
променени са 2 файла, в които са добавени 43 реда и са изтрити 4 реда
  1. 9 4
      lib/Predis/Commands/ZSetUnionStore.php
  2. 34 0
      test/RedisCommandsTest.php

+ 9 - 4
lib/Predis/Commands/ZSetUnionStore.php

@@ -6,12 +6,17 @@ class ZSetUnionStore extends Command {
     public function getId() { return 'ZUNIONSTORE'; }
     public function filterArguments(Array $arguments) {
         $options = array();
-        $argc    = count($arguments);
-        if ($argc > 1 && is_array($arguments[$argc - 1])) {
+        $argc = count($arguments);
+        if ($argc > 2 && is_array($arguments[$argc - 1])) {
             $options = $this->prepareOptions(array_pop($arguments));
         }
-        $args = is_array($arguments[0]) ? $arguments[0] : $arguments;
-        return  array_merge($args, $options);
+        if (is_array($arguments[1])) {
+            $arguments = array_merge(
+                array($arguments[0], count($arguments[1])),
+                $arguments[1]
+            );
+        }
+        return array_merge($arguments, $options);
     }
     private function prepareOptions($options) {
         $opts = array_change_key_case($options, CASE_UPPER);

+ 34 - 0
test/RedisCommandsTest.php

@@ -1504,6 +1504,23 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
             $this->redis->zrange('zsetc', 0, -1, 'withscores')
         );
 
+        // using an array to pass the list of source keys
+        $sourceKeys = array('zseta', 'zsetb');
+
+        $this->assertEquals(4, $this->redis->zunionstore('zsetc', $sourceKeys));
+        $this->assertEquals(
+            array(array('a', 1), array('b', 3), array('d', 3), array('c', 5)),
+            $this->redis->zrange('zsetc', 0, -1, 'withscores')
+        );
+
+        // using an array to pass the list of source keys + options array
+        $options = array('weights' => array(2, 3));
+        $this->assertEquals(4, $this->redis->zunionstore('zsetc', $sourceKeys, $options));
+        $this->assertEquals(
+            array(array('a', 2), array('b', 7), array('d', 9), array('c', 12)),
+            $this->redis->zrange('zsetc', 0, -1, 'withscores')
+        );
+
         RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
             $test->redis->set('zsetFake', 'fake');
             $test->redis->zunionstore('zsetc', 2, 'zseta', 'zsetFake');
@@ -1549,6 +1566,23 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
             $this->redis->zrange('zsetc', 0, -1, 'withscores')
         );
 
+        // using an array to pass the list of source keys
+        $sourceKeys = array('zseta', 'zsetb');
+
+        $this->assertEquals(2, $this->redis->zinterstore('zsetc', $sourceKeys));
+        $this->assertEquals(
+            array(array('b', 3), array('c', 5)),
+            $this->redis->zrange('zsetc', 0, -1, 'withscores')
+        );
+
+        // using an array to pass the list of source keys + options array
+        $options = array('weights' => array(2, 3));
+        $this->assertEquals(2, $this->redis->zinterstore('zsetc', $sourceKeys, $options));
+        $this->assertEquals(
+            array(array('b', 7), array('c', 12)),
+            $this->redis->zrange('zsetc', 0, -1, 'withscores')
+        );
+
         RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function($test) {
             $test->redis->set('zsetFake', 'fake');
             $test->redis->zinterstore('zsetc', 2, 'zseta', 'zsetFake');