Ver Fonte

Merge branch 'v0.9/add-commands'

Daniele Alessandri há 11 anos atrás
pai
commit
ce706a375c

+ 3 - 0
CHANGELOG.md

@@ -3,6 +3,9 @@ v0.9.0 (201x-xx-xx)
 
 
 - The default server profile for Redis is now `2.8`.
 - The default server profile for Redis is now `2.8`.
 
 
+- Added `SENTINEL` to the profile for Redis 2.6 and `PUBSUB` to the profile for
+  Redis 2.8.
+
 - Added the `aggregate` client option, useful to fully customize how the client
 - Added the `aggregate` client option, useful to fully customize how the client
   should aggregate multiple connections when an array of connection parameters
   should aggregate multiple connections when an array of connection parameters
   is passed to `Predis\Client::__construct()`.
   is passed to `Predis\Client::__construct()`.

+ 59 - 0
lib/Predis/Command/PubSubPubsub.php

@@ -0,0 +1,59 @@
+<?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/pubsub
+ * @author Daniele Alessandri <suppakilla@gmail.com>
+ */
+class PubSubPubsub extends AbstractCommand
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function getId()
+    {
+        return 'PUBSUB';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function parseResponse($data)
+    {
+        switch (strtolower($this->getArgument(0))) {
+            case 'numsub':
+                return self::processNumsub($data);
+
+            default:
+                return $data;
+        }
+    }
+
+    /**
+     * Returns the processed response to PUBSUB NUMSUB.
+     *
+     * @param array List of channels
+     * @return array
+     */
+    protected static function processNumsub(array $channels)
+    {
+        $processed = array();
+        $count = count($channels);
+
+        for ($i = 0; $i < $count; $i++) {
+            $processed[$channels[$i]] = $channels[++$i];
+        }
+
+        return $processed;
+    }
+}

+ 64 - 0
lib/Predis/Command/ServerSentinel.php

@@ -0,0 +1,64 @@
+<?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/topics/sentinel
+ * @author Daniele Alessandri <suppakilla@gmail.com>
+ */
+class ServerSentinel extends AbstractCommand
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function getId()
+    {
+        return 'SENTINEL';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function parseResponse($data)
+    {
+        switch (strtolower($this->getArgument(0))) {
+            case 'masters':
+            case 'slaves':
+                return self::processMastersOrSlaves($data);
+
+            default:
+                return $data;
+        }
+    }
+
+    /**
+     * Returns a processed response to SENTINEL MASTERS or SENTINEL SLAVES.
+     *
+     * @param array List of Redis servers.
+     * @return array
+     */
+    protected static function processMastersOrSlaves(array $servers)
+    {
+        foreach ($servers as $idx => $node) {
+            $processed = array();
+            $count = count($node);
+
+            for ($i = 0; $i < $count; $i++) {
+                $processed[$node[$i]] = $node[++$i];
+            }
+
+            $servers[$idx] = $processed;
+        }
+
+        return $servers;
+    }
+}

+ 1 - 0
lib/Predis/Profile/ServerVersion26.php

@@ -233,6 +233,7 @@ class ServerVersion26 extends ServerProfile
             /* remote server control commands */
             /* remote server control commands */
             'info'                      => 'Predis\Command\ServerInfoV26x',
             'info'                      => 'Predis\Command\ServerInfoV26x',
             'time'                      => 'Predis\Command\ServerTime',
             'time'                      => 'Predis\Command\ServerTime',
+            'sentinel'                  => 'Predis\Command\ServerSentinel',
         );
         );
     }
     }
 }
 }

+ 4 - 0
lib/Predis/Profile/ServerVersion28.php

@@ -233,6 +233,7 @@ class ServerVersion28 extends ServerProfile
             /* remote server control commands */
             /* remote server control commands */
             'info'                      => 'Predis\Command\ServerInfoV26x',
             'info'                      => 'Predis\Command\ServerInfoV26x',
             'time'                      => 'Predis\Command\ServerTime',
             'time'                      => 'Predis\Command\ServerTime',
+            'sentinel'                  => 'Predis\Command\ServerSentinel',
 
 
 
 
             /* ---------------- Redis 2.8 ---------------- */
             /* ---------------- Redis 2.8 ---------------- */
@@ -248,6 +249,9 @@ class ServerVersion28 extends ServerProfile
 
 
             /* commands operating on hashes */
             /* commands operating on hashes */
             'hscan'                     => 'Predis\Command\HashScan',
             'hscan'                     => 'Predis\Command\HashScan',
+
+            /* publish - subscribe */
+            'pubsub'                    => 'Predis\Command\PubSubPubsub',
         );
         );
     }
     }
 }
 }

+ 90 - 0
tests/Predis/Command/PubSubPubsubTest.php

@@ -0,0 +1,90 @@
+<?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 PHPUnit_Framework_TestCase as StandardTestCase;
+
+/**
+ * @group commands
+ * @group realm-pubsub
+ */
+class PubSubPubsubTest extends CommandTestCase
+{
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedCommand()
+    {
+        return 'Predis\Command\PubSubPubsub';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedId()
+    {
+        return 'PUBSUB';
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testFilterArguments()
+    {
+        $arguments = array('channels', 'predis:*');
+        $expected = array('channels', 'predis:*');
+
+        $command = $this->getCommand();
+        $command->setArguments($arguments);
+
+        $this->assertSame($expected, $command->getArguments());
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testParseResponse()
+    {
+        $response = array('predis:incoming', 'predis:outgoing');
+        $expected = array('predis:incoming', 'predis:outgoing');
+
+        $command = $this->getCommandWithArguments('channels', 'predis:*');
+
+        $this->assertSame($expected, $command->parseResponse($response));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testPubsubNumsub()
+    {
+        $response = array('predis:incoming', '10', 'predis:outgoing', '8');
+        $expected = array('predis:incoming' => '10', 'predis:outgoing' => '8');
+
+        $command = $this->getCommandWithArguments('numsub', 'predis:incoming', 'predis:outgoing');
+
+        $this->assertSame($expected, $command->parseResponse($response));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testPubsubNumpat()
+    {
+        $response = 6;
+        $expected = 6;
+
+        $command = $this->getCommandWithArguments('numpat');
+
+        $this->assertSame($expected, $command->parseResponse($response));
+    }
+}

+ 192 - 0
tests/Predis/Command/ServerSentinelTest.php

@@ -0,0 +1,192 @@
+<?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 PHPUnit_Framework_TestCase as StandardTestCase;
+
+/**
+ * @group commands
+ * @group realm-server
+ */
+class ServerSentinelTest extends CommandTestCase
+{
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedCommand()
+    {
+        return 'Predis\Command\ServerSentinel';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function getExpectedId()
+    {
+        return 'SENTINEL';
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testFilterArguments()
+    {
+        $arguments = array('get-master-addr-by-name', 'predis:master');
+        $expected = array('get-master-addr-by-name', 'predis:master');
+
+        $command = $this->getCommandWithArgumentsArray($arguments);
+
+        $this->assertSame($expected, $command->getArguments());
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testParseResponse()
+    {
+        $expected = array('127.0.0.1', '6379');
+        $command = $this->getCommand();
+
+        $this->assertSame($expected, $command->parseResponse($expected));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testSentinelMastersResponse()
+    {
+        $response = array(
+            array(
+                "name", "predis:master",
+                "ip", "127.0.0.1",
+                "port", "6379",
+                "runid", "89f6128a7e5780aa6ef7d4d7022cfafbf799b3ab",
+                "flags", "master",
+                "pending-commands", "0",
+                "last-ok-ping-reply", "386",
+                "last-ping-reply", "386",
+                "info-refresh", "9926",
+                "num-slaves", "1",
+                "num-other-sentinels", "0",
+                "quorum", "2",
+            ),
+        );
+
+        $expected = array(
+            array(
+                "name" => "predis:master",
+                "ip" => "127.0.0.1",
+                "port" => "6379",
+                "runid" => "89f6128a7e5780aa6ef7d4d7022cfafbf799b3ab",
+                "flags" => "master",
+                "pending-commands" => "0",
+                "last-ok-ping-reply" => "386",
+                "last-ping-reply" => "386",
+                "info-refresh" => "9926",
+                "num-slaves" => "1",
+                "num-other-sentinels" => "0",
+                "quorum" => "2",
+            ),
+        );
+
+        $command = $this->getCommandWithArguments('masters');
+
+        $this->assertSame($expected, $command->parseResponse($response));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testSentinelSlavesResponse()
+    {
+        $response = array(
+            array(
+                "name", "127.0.0.1:6380",
+                "ip", "127.0.0.1",
+                "port", "6380",
+                "runid", "92aea60e4fead2507cccd6574e4c7139d401d0ae",
+                "flags", "slave",
+                "pending-commands", "0",
+                "last-ok-ping-reply", "1011",
+                "last-ping-reply", "1011",
+                "info-refresh", "4366",
+                "master-link-down-time", "0",
+                "master-link-status", "ok",
+                "master-host", "127.0.0.1",
+                "master-port", "6379",
+                "slave-priority", "100",
+            )
+        );
+
+        $expected = array(
+            array(
+                "name" => "127.0.0.1:6380",
+                "ip" => "127.0.0.1",
+                "port" => "6380",
+                "runid" => "92aea60e4fead2507cccd6574e4c7139d401d0ae",
+                "flags" => "slave",
+                "pending-commands" => "0",
+                "last-ok-ping-reply" => "1011",
+                "last-ping-reply" => "1011",
+                "info-refresh" => "4366",
+                "master-link-down-time" => "0",
+                "master-link-status" => "ok",
+                "master-host" => "127.0.0.1",
+                "master-port" => "6379",
+                "slave-priority" => "100",
+            )
+        );
+
+        $command = $this->getCommandWithArguments('slaves', 'predis:master');
+
+        $this->assertSame($expected, $command->parseResponse($response));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testSentinelIsMasterDownByAddr()
+    {
+        $response = array("0", "7388832d5fdee6a2e301d6bbc5052bd1526d741c");
+        $expected = array("0", "7388832d5fdee6a2e301d6bbc5052bd1526d741c");
+
+        $command = $this->getCommandWithArguments('is-master-down-by-addr', '127.0.0.1', '6379');
+
+        $this->assertSame($expected, $command->parseResponse($response));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testSentinelGetMasterAddrByName()
+    {
+        $response = array("127.0.0.1", "6379");
+        $expected = array("127.0.0.1", "6379");
+
+        $command = $this->getCommandWithArguments('get-master-addr-by-name', 'predis:master');
+
+        $this->assertSame($expected, $command->parseResponse($response));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testSentinelReset()
+    {
+        $response = 1;
+        $expected = 1;
+
+        $command = $this->getCommandWithArguments('reset', 'predis:*');
+
+        $this->assertSame($expected, $command->parseResponse($response));
+    }
+}

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

@@ -173,6 +173,7 @@ class ServerVersion26Test extends ServerVersionTestCase
             132 => 'evalsha',
             132 => 'evalsha',
             133 => 'script',
             133 => 'script',
             134 => 'time',
             134 => 'time',
+            135 => 'sentinel',
         );
         );
     }
     }
 }
 }

+ 6 - 4
tests/Predis/Profile/ServerVersion28Test.php

@@ -173,10 +173,12 @@ class ServerVersion28Test extends ServerVersionTestCase
             132 => 'evalsha',
             132 => 'evalsha',
             133 => 'script',
             133 => 'script',
             134 => 'time',
             134 => 'time',
-            135 => 'scan',
-            136 => 'sscan',
-            137 => 'zscan',
-            138 => 'hscan',
+            135 => 'sentinel',
+            136 => 'scan',
+            137 => 'sscan',
+            138 => 'zscan',
+            139 => 'hscan',
+            140 => 'pubsub',
         );
         );
     }
     }
 }
 }

+ 6 - 4
tests/Predis/Profile/ServerVersionNextTest.php

@@ -173,10 +173,12 @@ class ServerVersionNextTest extends ServerVersionTestCase
             132 => 'evalsha',
             132 => 'evalsha',
             133 => 'script',
             133 => 'script',
             134 => 'time',
             134 => 'time',
-            135 => 'scan',
-            136 => 'sscan',
-            137 => 'zscan',
-            138 => 'hscan',
+            135 => 'sentinel',
+            136 => 'scan',
+            137 => 'sscan',
+            138 => 'zscan',
+            139 => 'hscan',
+            140 => 'pubsub',
         );
         );
     }
     }
 }
 }