Browse Source

Change response parser for ZSCAN to match changes applied to ZRANGE.

Daniele Alessandri 10 years ago
parent
commit
f5ba47ad74

+ 7 - 3
CHANGELOG.md

@@ -19,9 +19,13 @@ v1.0.0 (2014-08-xx)
   is also more akin to how Redis replies to clients.
 
 - Commands `ZRANGE`, `ZRANGEBYSCORE`, `ZREVRANGE` and `ZREVRANGEBYSCORE` using
-  `WITHSCORE` return a named array of member => score instead of an array of
-  [member, score] elements. Insertion order is preserved anyway due to how PHP
-  works internally.
+  `WITHSCORE` return a named array of member => score instead of using an array
+  of [member, score] elements. Insertion order is preserved anyway due to how
+  PHP works internally.
+
+- The command `ZSCAN` returns a named array of member => score instead of using
+  an array of [member, score] elements. Insertion order is preserved anyway due
+  to how PHP works internally.
 
 - The rules for redis-cluster are now leveraged for empty key tags when using
   client-side sharding, which means that when one or the first occurrence of {}

+ 2 - 4
src/Collection/Iterator/SortedSetKey.php

@@ -49,9 +49,7 @@ class SortedSetKey extends CursorBasedIterator
      */
     protected function extractNext()
     {
-        $element = array_shift($this->elements);
-
-        $this->position = $element[0];
-        $this->current = $element[1];
+        $this->position = key($this->elements);
+        $this->current = array_shift($this->elements);
     }
 }

+ 0 - 1
src/Command/HashScan.php

@@ -69,7 +69,6 @@ class HashScan extends Command
     public function parseResponse($data)
     {
         if (is_array($data)) {
-            $data[0] = (int) $data[0];
             $fields = $data[1];
             $result = array();
 

+ 0 - 12
src/Command/KeyScan.php

@@ -62,16 +62,4 @@ class KeyScan extends Command
 
         return $normalized;
     }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function parseResponse($data)
-    {
-        if (is_array($data)) {
-            $data[0] = (int) $data[0];
-        }
-
-        return $data;
-    }
 }

+ 0 - 12
src/Command/SetScan.php

@@ -62,16 +62,4 @@ class SetScan extends Command
 
         return $normalized;
     }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function parseResponse($data)
-    {
-        if (is_array($data)) {
-            $data[0] = (int) $data[0];
-        }
-
-        return $data;
-    }
 }

+ 1 - 3
src/Command/ZSetScan.php

@@ -69,13 +69,11 @@ class ZSetScan extends Command
     public function parseResponse($data)
     {
         if (is_array($data)) {
-            $data[0] = (int) $data[0];
-
             $members = $data[1];
             $result = array();
 
             for ($i = 0; $i < count($members); $i++) {
-                $result[] = array($members[$i], (float) $members[++$i]);
+                $result[$members[$i]] = (float) $members[++$i];
             }
 
             $data[1] = $result;

+ 16 - 16
tests/Predis/Collection/Iterator/SortedSetKeyTest.php

@@ -70,7 +70,7 @@ class SortedSetKeyTest extends PredisTestCase
                ->method('zscan')
                ->with('key:zset', 0, array())
                ->will($this->returnValue(array(0, array(
-                    array('member:1st', 1.0), array('member:2nd', 2.0), array('member:3rd', 3.0),
+                    'member:1st' => 1.0, 'member:2nd' => 2.0, 'member:3rd' => 3.0,
                ))));
 
         $iterator = new SortedSetKey($client, 'key:zset');
@@ -108,13 +108,13 @@ class SortedSetKeyTest extends PredisTestCase
                ->method('zscan')
                ->with('key:zset', 0, array())
                ->will($this->returnValue(array(2, array(
-                    array('member:1st', 1.0), array('member:2nd', 2.0),
+                    'member:1st' => 1.0, 'member:2nd' => 2.0,
                ))));
         $client->expects($this->at(2))
                ->method('zscan')
                ->with('key:zset', 2, array())
                ->will($this->returnValue(array(0, array(
-                    array('member:3rd', 3.0),
+                    'member:3rd' => 3.0,
                ))));
 
         $iterator = new SortedSetKey($client, 'key:zset');
@@ -156,7 +156,7 @@ class SortedSetKeyTest extends PredisTestCase
                ->method('zscan')
                ->with('key:zset', 4, array())
                ->will($this->returnValue(array(0, array(
-                    array('member:1st', 1.0), array('member:2nd', 2.0),
+                    'member:1st' => 1.0, 'member:2nd' => 2.0,
                ))));
 
         $iterator = new SortedSetKey($client, 'key:zset');
@@ -189,7 +189,7 @@ class SortedSetKeyTest extends PredisTestCase
                ->method('zscan')
                ->with('key:zset', 0, array())
                ->will($this->returnValue(array(2, array(
-                    array('member:1st', 1.0), array('member:2nd', 2.0),
+                    'member:1st' => 1.0, 'member:2nd' => 2.0,
                ))));
         $client->expects($this->at(2))
                ->method('zscan')
@@ -199,7 +199,7 @@ class SortedSetKeyTest extends PredisTestCase
                ->method('zscan')
                ->with('key:zset', 5, array())
                ->will($this->returnValue(array(0, array(
-                    array('member:3rd', 3.0)
+                    'member:3rd' => 3.0
                ))));
 
         $iterator = new SortedSetKey($client, 'key:zset');
@@ -237,7 +237,7 @@ class SortedSetKeyTest extends PredisTestCase
                ->method('zscan')
                ->with('key:zset', 0, array('MATCH' => 'member:*'))
                ->will($this->returnValue(array(2, array(
-                    array('member:1st', 1.0), array('member:2nd', 2.0),
+                    'member:1st' => 1.0, 'member:2nd' => 2.0,
                ))));
 
         $iterator = new SortedSetKey($client, 'key:zset', 'member:*');
@@ -270,13 +270,13 @@ class SortedSetKeyTest extends PredisTestCase
                ->method('zscan')
                ->with('key:zset', 0, array('MATCH' => 'member:*'))
                ->will($this->returnValue(array(1, array(
-                    array('member:1st', 1.0),
+                    'member:1st' => 1.0,
                 ))));
         $client->expects($this->at(2))
                ->method('zscan')
                ->with('key:zset', 1, array('MATCH' => 'member:*'))
                ->will($this->returnValue(array(0, array(
-                    array('member:2nd', 2.0),
+                    'member:2nd' => 2.0,
                 ))));
 
         $iterator = new SortedSetKey($client, 'key:zset', 'member:*');
@@ -309,7 +309,7 @@ class SortedSetKeyTest extends PredisTestCase
                ->method('zscan')
                ->with('key:zset', 0, array('COUNT' => 2))
                ->will($this->returnValue(array(0, array(
-                    array('member:1st', 1.0), array('member:2nd', 2.0),
+                    'member:1st' => 1.0, 'member:2nd' => 2.0,
                ))));
 
         $iterator = new SortedSetKey($client, 'key:zset', null, 2);
@@ -342,13 +342,13 @@ class SortedSetKeyTest extends PredisTestCase
                ->method('zscan')
                ->with('key:zset', 0, array('COUNT' => 1))
                ->will($this->returnValue(array(1, array(
-                    array('member:1st', 1.0),
+                    'member:1st' => 1.0,
                 ))));
         $client->expects($this->at(2))
                ->method('zscan')
                ->with('key:zset', 1, array('COUNT' => 1))
                ->will($this->returnValue(array(0, array(
-                    array('member:2nd', 2.0),
+                    'member:2nd' => 2.0,
                 ))));
 
         $iterator = new SortedSetKey($client, 'key:zset', null, 1);
@@ -381,7 +381,7 @@ class SortedSetKeyTest extends PredisTestCase
                ->method('zscan')
                ->with('key:zset', 0, array('MATCH' => 'member:*', 'COUNT' => 2))
                ->will($this->returnValue(array(0, array(
-                    array('member:1st', 1.0), array('member:2nd', 2.0),
+                    'member:1st' => 1.0, 'member:2nd' => 2.0,
                ))));
 
         $iterator = new SortedSetKey($client, 'key:zset', 'member:*', 2);
@@ -414,13 +414,13 @@ class SortedSetKeyTest extends PredisTestCase
                ->method('zscan')
                ->with('key:zset', 0, array('MATCH' => 'member:*', 'COUNT' => 1))
                ->will($this->returnValue(array(1, array(
-                    array('member:1st', 1.0),
+                    'member:1st' => 1.0,
                 ))));
         $client->expects($this->at(2))
                ->method('zscan')
                ->with('key:zset', 1, array('MATCH' => 'member:*', 'COUNT' => 1))
                ->will($this->returnValue(array(0, array(
-                    array('member:2nd', 2.0),
+                    'member:2nd' => 2.0,
                 ))));
 
         $iterator = new SortedSetKey($client, 'key:zset', 'member:*', 1);
@@ -453,7 +453,7 @@ class SortedSetKeyTest extends PredisTestCase
                ->method('zscan')
                ->with('key:zset', 0, array())
                ->will($this->returnValue(array(0, array(
-                    array('member:1st', 1.0), array('member:2nd', 2.0),
+                    'member:1st' => 1.0, 'member:2nd' => 2.0,
                ))));
 
         $iterator = new SortedSetKey($client, 'key:zset');

+ 3 - 3
tests/Predis/Command/HashScanTest.php

@@ -81,7 +81,7 @@ class HashScanTest extends PredisCommandTestCase
     public function testParseResponse()
     {
         $raw = array('3', array('field:1', '1', 'field:2', '2', 'field:3', '3'));
-        $expected = array(3, array('field:1' => '1', 'field:2' => '2', 'field:3' => '3'));
+        $expected = array('3', array('field:1' => '1', 'field:2' => '2', 'field:3' => '3'));
 
         $command = $this->getCommand();
 
@@ -101,7 +101,7 @@ class HashScanTest extends PredisCommandTestCase
 
         $response = $redis->hscan('key', 0);
 
-        $this->assertSame(0, $response[0]);
+        $this->assertSame('0', $response[0]);
         $this->assertSame($expectedFields, array_keys($response[1]));
         $this->assertSame($expectedValues, array_values($response[1]));
     }
@@ -130,7 +130,7 @@ class HashScanTest extends PredisCommandTestCase
 
         $response = $redis->hscan('key', 0, 'MATCH', 'nofield:*');
 
-        $this->assertSame(0, $response[0]);
+        $this->assertSame('0', $response[0]);
         $this->assertEmpty($response[1]);
     }
 }

+ 3 - 3
tests/Predis/Command/KeyScanTest.php

@@ -81,7 +81,7 @@ class KeyScanTest extends PredisCommandTestCase
     public function testParseResponse()
     {
         $raw = array('3', array('key:1', 'key:2', 'key:3'));
-        $expected = array(3, array('key:1', 'key:2', 'key:3'));
+        $expected = array('3', array('key:1', 'key:2', 'key:3'));
 
         $command = $this->getCommand();
 
@@ -113,7 +113,7 @@ class KeyScanTest extends PredisCommandTestCase
         $redis = $this->getClient();
         $redis->mset($kvs);
 
-        $response = $redis->scan(0, 'MATCH', 'key:t*');
+        $response = $redis->scan('0', 'MATCH', 'key:t*');
 
         $this->assertSameValues(array('key:two', 'key:three'), $response[1]);
     }
@@ -130,7 +130,7 @@ class KeyScanTest extends PredisCommandTestCase
 
         $response = $redis->scan(0, 'MATCH', 'nokey:*');
 
-        $this->assertSame(0, $response[0]);
+        $this->assertSame('0', $response[0]);
         $this->assertEmpty($response[1]);
     }
 }

+ 3 - 3
tests/Predis/Command/SetScanTest.php

@@ -81,7 +81,7 @@ class SetScanTest extends PredisCommandTestCase
     public function testParseResponse()
     {
         $raw = array('3', array('member:1', 'member:2', 'member:3'));
-        $expected = array(3, array('member:1', 'member:2', 'member:3'));
+        $expected = array('3', array('member:1', 'member:2', 'member:3'));
 
         $command = $this->getCommand();
 
@@ -98,7 +98,7 @@ class SetScanTest extends PredisCommandTestCase
 
         $response = $redis->sscan('key', 0);
 
-        $this->assertSame(0, $response[0]);
+        $this->assertSame('0', $response[0]);
         $this->assertSameValues($members, $response[1]);
     }
 
@@ -125,7 +125,7 @@ class SetScanTest extends PredisCommandTestCase
 
         $response = $redis->sscan('key', 0, 'MATCH', 'nomember:*');
 
-        $this->assertSame(0, $response[0]);
+        $this->assertSame('0', $response[0]);
         $this->assertEmpty($response[1]);
     }
 }

+ 7 - 7
tests/Predis/Command/ZSetScanTest.php

@@ -81,7 +81,7 @@ class ZSetScanTest extends PredisCommandTestCase
     public function testParseResponse()
     {
         $raw = array('3', array('member:1', '1', 'member:2', '2', 'member:3', '3'));
-        $expected = array(3, array(array('member:1' , 1.0), array('member:2', 2.0), array('member:3' , 3.0)));
+        $expected = array('3', array('member:1' => 1.0, 'member:2' => 2.0, 'member:3' => 3.0));
 
         $command = $this->getCommand();
 
@@ -101,9 +101,9 @@ class ZSetScanTest extends PredisCommandTestCase
 
         $response = $redis->zscan('key', 0);
 
-        $this->assertSame(0, $response[0]);
-        $this->assertSame($expectedMembers, array_map(function ($e) { return $e[0]; }, $response[1]));
-        $this->assertSame($expectedScores, array_map(function ($e) { return $e[1]; }, $response[1]));
+        $this->assertSame('0', $response[0]);
+        $this->assertSame($expectedMembers, array_keys($response[1]));
+        $this->assertSame($expectedScores, array_values($response[1]));
     }
 
     /**
@@ -116,8 +116,8 @@ class ZSetScanTest extends PredisCommandTestCase
 
         $response = $redis->zscan('key', 0, 'MATCH', 'member:t*');
 
-        $this->assertSame(array('member:two', 'member:three'), array_map(function ($e) { return $e[0]; }, $response[1]));
-        $this->assertSame(array(2.0, 3.0), array_map(function ($e) { return $e[1]; }, $response[1]));
+        $this->assertSame(array('member:two', 'member:three'), array_keys($response[1]));
+        $this->assertSame(array(2.0, 3.0), array_values($response[1]));
     }
 
     /**
@@ -130,7 +130,7 @@ class ZSetScanTest extends PredisCommandTestCase
 
         $response = $redis->zscan('key', 0, 'MATCH', 'nomember:*');
 
-        $this->assertSame(0, $response[0]);
+        $this->assertSame('0', $response[0]);
         $this->assertEmpty($response[1]);
     }
 }