瀏覽代碼

New command: COMMAND (Redis 3.0.0).

The command returns the response as is, but it would probably be not a
bad idea to parse it to restructure returned info using named arrays,
making it easier to access single commands and their info.

We will keep it as is for now, and investigate this change later.
Daniele Alessandri 11 年之前
父節點
當前提交
0235ba1a30

+ 2 - 0
CHANGELOG.md

@@ -4,6 +4,8 @@ v0.8.7 (2014-xx-xx)
 - Added `3.0` in the server profiles aliases list for Redis 3.0. `2.8` is still
   the default server profile and `dev` still targets Redis 3.0.
 
+- Added `COMMAND` to the server profile for Redis 3.0.
+
 - Switched internally to the `CLUSTER SLOTS` command instead of `CLUSTER NODES`
   to fetch the updated slots map from redis-cluster. This change requires users
   to upgrade Redis nodes to >= 3.0.0b7.

+ 27 - 0
lib/Predis/Command/ServerCommand.php

@@ -0,0 +1,27 @@
+<?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/command
+ * @author Daniele Alessandri <suppakilla@gmail.com>
+ */
+class ServerCommand extends AbstractCommand
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function getId()
+    {
+        return 'COMMAND';
+    }
+}

+ 3 - 0
lib/Predis/Profile/ServerVersion30.php

@@ -252,6 +252,9 @@ class ServerVersion30 extends ServerProfile
             'pfmerge'                   => 'Predis\Command\HyperLogLogMerge',
 
             /* ---------------- Redis 3.0 ---------------- */
+
+            /* remote server control commands */
+            'command'                   => 'Predis\Command\ServerCommand',
         );
     }
 }

+ 115 - 0
tests/Predis/Command/ServerCommandTest.php

@@ -0,0 +1,115 @@
+<?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-server
+ */
+class ServerCommandTest extends PredisCommandTestCase
+{
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedCommand()
+    {
+        return 'Predis\Command\ServerCommand';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedId()
+    {
+        return 'COMMAND';
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testFilterArguments()
+    {
+        $arguments = array('INFO', 'DEL');
+        $expected = array('INFO', 'DEL');
+
+        $command = $this->getCommand();
+        $command->setArguments($arguments);
+
+        $this->assertSame($expected, $command->getArguments());
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testParseResponse()
+    {
+        $raw = array(
+            array('get', 2, array('readonly', 'fast'), 1, 1, 1),
+            array('set', -3, array('write', 'denyoom'), 1, 1, 1),
+            array('watch', -2, array('readonly', 'noscript', 'fast'), 1, -1, 1),
+            array('unwatch', 1, array('readonly', 'noscript', 'fast'), 0, 0, 0),
+            array('info', -1, array('readonly', 'loading', 'stale'), 0, 0, 0),
+        );
+
+        $expected = $raw;
+
+        $command = $this->getCommand();
+
+        $this->assertSame($expected, $command->parseResponse($raw));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testParseEmptyResponse()
+    {
+        $raw = array(null);
+        $expected = array(null);
+
+        $command = $this->getCommand();
+
+        $this->assertSame($expected, $command->parseResponse($raw));
+    }
+
+    /**
+     * @group connected
+     */
+    public function testReturnsEmptyCommandInfoOnNonExistingCommand()
+    {
+        $redis = $this->getClient();
+
+        $this->assertCount(1, $response = $redis->command('INFO', 'FOOBAR'));
+        $this->assertSame(array(null), $response);
+    }
+
+    /**
+     * @group connected
+     */
+    public function testReturnsCommandInfoOnExistingCommand()
+    {
+        $redis = $this->getClient();
+
+        $expected = array(array('command', 0, array('readonly', 'loading', 'stale'), 0, 0, 0));
+        $this->assertCount(1, $response = $redis->command('INFO', 'COMMAND'));
+        $this->assertSame($expected, $response);
+    }
+
+    /**
+     * @group connected
+     */
+    public function testReturnsListOfCommandInfoWithNoArguments()
+    {
+        $redis = $this->getClient();
+
+        $this->assertGreaterThan(100, count($response = $redis->command()));
+    }
+}

+ 1 - 0
tests/Predis/Profile/ServerVersion30Test.php

@@ -183,6 +183,7 @@ class ServerVersion30Test extends PredisProfileTestCase
             142 => 'pfadd',
             143 => 'pfcount',
             144 => 'pfmerge',
+            145 => 'command',
         );
     }
 }

+ 1 - 0
tests/Predis/Profile/ServerVersionNextTest.php

@@ -183,6 +183,7 @@ class ServerVersionNextTest extends PredisProfileTestCase
             142 => 'pfadd',
             143 => 'pfcount',
             144 => 'pfmerge',
+            145 => 'command',
         );
     }
 }