Просмотр исходного кода

Modify ctor signature of Predis\Command\RawCommand.

Daniele Alessandri 8 лет назад
Родитель
Сommit
e4872af747

+ 2 - 0
CHANGELOG.md

@@ -8,6 +8,8 @@ v2.0.0 (201x-xx-xx)
   used by Predis is `Predis\Command\RedisFactory` and it still allows developers
   to define or override commands with their own implementations.
 
+- Changed the signature for the constructor of `Predis\Command\RawCommand`.
+
 - Classes for Redis commands have been moved into the new `Predis\Command\Redis`
   namespace.
 

+ 2 - 1
src/Client.php

@@ -289,9 +289,10 @@ class Client implements ClientInterface, \IteratorAggregate
     public function executeRaw(array $arguments, &$error = null)
     {
         $error = false;
+        $commandID = array_shift($arguments);
 
         $response = $this->connection->executeCommand(
-            new RawCommand($arguments)
+            new RawCommand($commandID, $arguments)
         );
 
         if ($response instanceof ResponseInterface) {

+ 6 - 13
src/Command/RawCommand.php

@@ -28,20 +28,13 @@ class RawCommand implements CommandInterface
     private $arguments;
 
     /**
-     * @param array $arguments Command ID and its arguments.
-     *
-     * @throws \InvalidArgumentException
+     * @param string $commandID Command ID.
+     * @param array  $arguments Command arguments.
      */
-    public function __construct(array $arguments)
+    public function __construct($commandID, array $arguments = array())
     {
-        if (!$arguments) {
-            throw new \InvalidArgumentException(
-                'The arguments array must contain at least the command ID.'
-            );
-        }
-
-        $this->commandID = strtoupper(array_shift($arguments));
-        $this->arguments = $arguments;
+        $this->commandID = strtoupper($commandID);
+        $this->setArguments($arguments);
     }
 
     /**
@@ -55,7 +48,7 @@ class RawCommand implements CommandInterface
     public static function create($commandID /* [ $arg, ... */)
     {
         $arguments = func_get_args();
-        $command = new self($arguments);
+        $command = new self(array_shift($arguments), $arguments);
 
         return $command;
     }

+ 2 - 2
src/Connection/Factory.php

@@ -175,13 +175,13 @@ class Factory implements FactoryInterface
 
         if (isset($parameters->password)) {
             $connection->addConnectCommand(
-                new RawCommand(array('AUTH', $parameters->password))
+                new RawCommand('AUTH', array($parameters->password))
             );
         }
 
         if (isset($parameters->database)) {
             $connection->addConnectCommand(
-                new RawCommand(array('SELECT', $parameters->database))
+                new RawCommand('SELECT', array($parameters->database))
             );
         }
     }

+ 1 - 1
tests/Predis/Command/Processor/KeyPrefixProcessorTest.php

@@ -344,7 +344,7 @@ class KeyPrefixProcessorTest extends PredisTestCase
 
     public function getCommandInstance($commandID, array $arguments)
     {
-        $command = new RawCommand(array($commandID));
+        $command = new RawCommand($commandID);
         $command->setRawArguments($arguments);
 
         return $command;

+ 7 - 17
tests/Predis/Command/RawCommandTest.php

@@ -24,7 +24,7 @@ class RawCommandTest extends PredisTestCase
     public function testConstructorWithCommandID()
     {
         $commandID = 'PING';
-        $command = new RawCommand(array($commandID));
+        $command = new RawCommand($commandID);
 
         $this->assertSame($commandID, $command->getId());
         $this->assertEmpty($command->getArguments());
@@ -38,7 +38,7 @@ class RawCommandTest extends PredisTestCase
         $commandID = 'SET';
         $commandArgs = array('foo', 'bar');
 
-        $command = new RawCommand(array_merge((array) $commandID, $commandArgs));
+        $command = new RawCommand($commandID, $commandArgs);
 
         $this->assertSame($commandID, $command->getId());
         $this->assertSame($commandArgs, $command->getArguments());
@@ -58,16 +58,6 @@ class RawCommandTest extends PredisTestCase
         $this->assertSame(array('foo', 'bar'), $command->getArguments());
     }
 
-    /**
-     * @group disconnected
-     * @expectedException \InvalidArgumentException
-     * @expectedExceptionMessage The arguments array must contain at least the command ID.
-     */
-    public function testExceptionOnMissingCommandID()
-    {
-        new RawCommand(array());
-    }
-
     /**
      * The signature of RawCommand::create() requires one argument which is the
      * ID of the command (other arguments are fetched dinamically). If the first
@@ -87,7 +77,7 @@ class RawCommandTest extends PredisTestCase
     public function testSetArguments()
     {
         $commandID = 'SET';
-        $command = new RawCommand(array($commandID));
+        $command = new RawCommand($commandID);
 
         $command->setArguments($commandArgs = array('foo', 'bar'));
         $this->assertSame($commandArgs, $command->getArguments());
@@ -102,7 +92,7 @@ class RawCommandTest extends PredisTestCase
     public function testSetRawArguments()
     {
         $commandID = 'SET';
-        $command = new RawCommand(array($commandID));
+        $command = new RawCommand($commandID);
 
         $command->setRawArguments($commandArgs = array('foo', 'bar'));
         $this->assertSame($commandArgs, $command->getArguments());
@@ -117,8 +107,8 @@ class RawCommandTest extends PredisTestCase
     public function testSetAndGetHash()
     {
         $slot = 1024;
-        $arguments = array('SET', 'key', 'value');
-        $command = new RawCommand($arguments);
+        $arguments = array('key', 'value');
+        $command = new RawCommand('SET', $arguments);
 
         $this->assertNull($command->getSlot());
 
@@ -134,7 +124,7 @@ class RawCommandTest extends PredisTestCase
      */
     public function testNormalizesCommandIdentifiersToUppercase()
     {
-        $command = new RawCommand(array('set', 'key', 'value'));
+        $command = new RawCommand('set', array('key', 'value'));
 
         $this->assertSame('SET', $command->getId());
     }