Browse Source

Add new command: HSTRLEN (Redis 3.2.0).

Also bump the unstable profile version to 3.2.
Daniele Alessandri 10 years ago
parent
commit
0d25e7b0bb

+ 1 - 0
src/Cluster/ClusterStrategy.php

@@ -154,6 +154,7 @@ abstract class ClusterStrategy implements StrategyInterface
             'HSETNX' => $getKeyFromFirstArgument,
             'HSETNX' => $getKeyFromFirstArgument,
             'HVALS' => $getKeyFromFirstArgument,
             'HVALS' => $getKeyFromFirstArgument,
             'HSCAN' => $getKeyFromFirstArgument,
             'HSCAN' => $getKeyFromFirstArgument,
+            'HSTRLEN' => $getKeyFromFirstArgument,
 
 
             /* commands operating on HyperLogLog */
             /* commands operating on HyperLogLog */
             'PFADD' => $getKeyFromFirstArgument,
             'PFADD' => $getKeyFromFirstArgument,

+ 28 - 0
src/Command/HashStringLength.php

@@ -0,0 +1,28 @@
+<?php
+
+/*
+ * This file is part of the Predis package.
+ *
+ * (c) Daniele Alessandri <suppakilla@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Predis\Command;
+
+/**
+ * @link http://redis.io/commands/hstrlen
+ *
+ * @author Daniele Alessandri <suppakilla@gmail.com>
+ */
+class HashStringLength extends Command
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function getId()
+    {
+        return 'HSTRLEN';
+    }
+}

+ 2 - 0
src/Command/Processor/KeyPrefixProcessor.php

@@ -157,6 +157,8 @@ class KeyPrefixProcessor implements ProcessorInterface
             'ZREMRANGEBYLEX' => 'self::first',
             'ZREMRANGEBYLEX' => 'self::first',
             'ZREVRANGEBYLEX' => 'self::first',
             'ZREVRANGEBYLEX' => 'self::first',
             'BITPOS' => 'self::first',
             'BITPOS' => 'self::first',
+            /* ---------------- Redis 3.2 ---------------- */
+            'HSTRLEN' => 'self::first',
         );
         );
     }
     }
 
 

+ 7 - 2
src/Profile/RedisUnstable.php

@@ -23,7 +23,7 @@ class RedisUnstable extends RedisVersion300
      */
      */
     public function getVersion()
     public function getVersion()
     {
     {
-        return '3.0';
+        return '3.2';
     }
     }
 
 
     /**
     /**
@@ -31,6 +31,11 @@ class RedisUnstable extends RedisVersion300
      */
      */
     public function getSupportedCommands()
     public function getSupportedCommands()
     {
     {
-        return array_merge(parent::getSupportedCommands(), array());
+        return array_merge(parent::getSupportedCommands(), array(
+            /* ---------------- Redis 3.2 ---------------- */
+
+            /* commands operating on hashes */
+            'HSTRLEN' => 'Predis\Command\HashStringLength',
+        ));
     }
     }
 }
 }

+ 1 - 0
src/Replication/ReplicationStrategy.php

@@ -220,6 +220,7 @@ class ReplicationStrategy
             'HVALS' => true,
             'HVALS' => true,
             'HGETALL' => true,
             'HGETALL' => true,
             'HSCAN' => true,
             'HSCAN' => true,
+            'HSTRLEN' => true,
             'PING' => true,
             'PING' => true,
             'AUTH' => true,
             'AUTH' => true,
             'SELECT' => true,
             'SELECT' => true,

+ 1 - 0
tests/Predis/Cluster/PredisStrategyTest.php

@@ -366,6 +366,7 @@ class PredisStrategyTest extends PredisTestCase
             'HSETNX' => 'keys-first',
             'HSETNX' => 'keys-first',
             'HVALS' => 'keys-first',
             'HVALS' => 'keys-first',
             'HSCAN' => 'keys-first',
             'HSCAN' => 'keys-first',
+            'HSTRLEN' => 'keys-first',
 
 
             /* commands operating on HyperLogLog */
             /* commands operating on HyperLogLog */
             'PFADD' => 'keys-first',
             'PFADD' => 'keys-first',

+ 1 - 0
tests/Predis/Cluster/RedisStrategyTest.php

@@ -376,6 +376,7 @@ class RedisStrategyTest extends PredisTestCase
             'HSETNX' => 'keys-first',
             'HSETNX' => 'keys-first',
             'HVALS' => 'keys-first',
             'HVALS' => 'keys-first',
             'HSCAN' => 'keys-first',
             'HSCAN' => 'keys-first',
+            'HSTRLEN' => 'keys-first',
 
 
             /* commands operating on HyperLogLog */
             /* commands operating on HyperLogLog */
             'PFADD' => 'keys-first',
             'PFADD' => 'keys-first',

+ 94 - 0
tests/Predis/Command/HashStringLengthTest.php

@@ -0,0 +1,94 @@
+<?php
+
+/*
+ * This file is part of the Predis package.
+ *
+ * (c) Daniele Alessandri <suppakilla@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Predis\Command;
+
+/**
+ * @group commands
+ * @group realm-hash
+ */
+class HashStringLengthTest extends PredisCommandTestCase
+{
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedCommand()
+    {
+        return 'Predis\Command\HashStringLength';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedId()
+    {
+        return 'HSTRLEN';
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testFilterArguments()
+    {
+        $arguments = array('key', 'field');
+        $expected = array('key', 'field');
+
+        $command = $this->getCommand();
+        $command->setArguments($arguments);
+
+        $this->assertSame($expected, $command->getArguments());
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testParseResponse()
+    {
+        $command = $this->getCommand();
+
+        $this->assertSame(0, $command->parseResponse(0));
+        $this->assertSame(1, $command->parseResponse(1));
+        $this->assertSame(10, $command->parseResponse(10));
+    }
+
+    /**
+     * @group connected
+     */
+    public function testReturnsStringLengthOfSpecifiedField()
+    {
+        $redis = $this->getClient();
+
+        $redis->hmset('metavars', 'foo', 'bar', 'hoge', 'piyo');
+
+        // Existing key and field
+        $this->assertSame(3, $redis->hstrlen('metavars', 'foo'));
+        $this->assertSame(4, $redis->hstrlen('metavars', 'hoge'));
+
+        // Existing key but non existing field
+        $this->assertSame(0, $redis->hstrlen('metavars', 'foofoo'));
+
+        // Non existing key
+        $this->assertSame(0, $redis->hstrlen('unknown', 'foo'));
+    }
+
+    /**
+     * @group connected
+     * @expectedException \Predis\Response\ServerException
+     * @expectedExceptionMessage Operation against a key holding the wrong kind of value
+     */
+    public function testThrowsExceptionOnWrongType()
+    {
+        $redis = $this->getClient();
+
+        $redis->set('metavars', 'foo');
+        $redis->hstrlen('metavars', 'foo');
+    }
+}

+ 1 - 1
tests/Predis/Profile/FactoryTest.php

@@ -19,7 +19,7 @@ use PredisTestCase;
 class FactoryTest extends PredisTestCase
 class FactoryTest extends PredisTestCase
 {
 {
     const DEFAULT_PROFILE_VERSION = '3.0';
     const DEFAULT_PROFILE_VERSION = '3.0';
-    const DEVELOPMENT_PROFILE_VERSION = '3.0';
+    const DEVELOPMENT_PROFILE_VERSION = '3.2';
 
 
     /**
     /**
      * @group disconnected
      * @group disconnected

+ 2 - 1
tests/Predis/Profile/RedisUnstableTest.php

@@ -29,7 +29,7 @@ class RedisUnstableTest extends PredisProfileTestCase
      */
      */
     public function getExpectedVersion()
     public function getExpectedVersion()
     {
     {
-        return '3.0';
+        return '3.2';
     }
     }
 
 
     /**
     /**
@@ -189,6 +189,7 @@ class RedisUnstableTest extends PredisProfileTestCase
             148 => 'PFCOUNT',
             148 => 'PFCOUNT',
             149 => 'PFMERGE',
             149 => 'PFMERGE',
             150 => 'COMMAND',
             150 => 'COMMAND',
+            151 => 'HSTRLEN',
         );
         );
     }
     }
 }
 }

+ 1 - 0
tests/Predis/Replication/ReplicationStrategyTest.php

@@ -382,6 +382,7 @@ class ReplicationStrategyTest extends PredisTestCase
             'HSETNX' => 'write',
             'HSETNX' => 'write',
             'HVALS' => 'read',
             'HVALS' => 'read',
             'HSCAN' => 'read',
             'HSCAN' => 'read',
+            'HSTRLEN' => 'read',
 
 
             /* commands operating on HyperLogLog */
             /* commands operating on HyperLogLog */
             'PFADD' => 'write',
             'PFADD' => 'write',