Browse Source

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 10 years ago
parent
commit
eb8de52269

+ 27 - 0
src/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 Command
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function getId()
+    {
+        return 'COMMAND';
+    }
+}

+ 3 - 0
src/Profile/RedisVersion300.php

@@ -256,6 +256,9 @@ class RedisVersion300 extends RedisProfile
             'PFMERGE'                   => 'Predis\Command\HyperLogLogMerge',
 
             /* ---------------- Redis 3.0 ---------------- */
+
+            /* remote server control commands */
+            'COMMAND'                   => 'Predis\Command\ServerCommand',
         );
     }
 }

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

@@ -0,0 +1,122 @@
+<?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;
+
+use Predis\Response\Status;
+
+/**
+ * @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(new Status('readonly'), new Status('fast')), 1, 1, 1),
+            array('set', -3, array(new Status('write'), new Status('denyoom')), 1, 1, 1),
+            array('watch', -2, array(new Status('readonly'), new Status('noscript'), new Status('fast')), 1, -1, 1),
+            array('unwatch', 1, array(new Status('readonly'), new Status('noscript'), new Status('fast')), 0, 0, 0),
+            array('info', -1, array(new Status('readonly'), new Status('loading'), new Status('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();
+
+        // NOTE: we use assertEquals instead of assertSame because Redis returns
+        // flags as +STATUS responses, represented by Predis with instances of
+        // Predis\Response\Status instead of plain strings. This class responds
+        // to __toString() so the string conversion is implicit, but assertSame
+        // checks for strict equality while assertEquals is loose.
+        $expected = array(array('command', 0, array('readonly', 'loading', 'stale'), 0, 0, 0));
+        $this->assertCount(1, $response = $redis->command('INFO', 'COMMAND'));
+        $this->assertEquals($expected, $response);
+    }
+
+    /**
+     * @group connected
+     */
+    public function testReturnsListOfCommandInfoWithNoArguments()
+    {
+        $redis = $this->getClient();
+
+        $this->assertGreaterThan(100, count($response = $redis->command()));
+    }
+}

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

@@ -185,6 +185,7 @@ class RedisUnstableTest extends PredisProfileTestCase
             144 => 'PFADD',
             145 => 'PFCOUNT',
             146 => 'PFMERGE',
+            147 => 'COMMAND',
         );
     }
 }

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

@@ -185,6 +185,7 @@ class RedisVersion300Test extends PredisProfileTestCase
             144 => 'PFADD',
             145 => 'PFCOUNT',
             146 => 'PFMERGE',
+            147 => 'COMMAND',
         );
     }
 }