فهرست منبع

Throw when command sent in connect() returns error.

Common failures are the use of SELECT with a database index outside
the bound of the configured number of databases in redis.conf or the
use of a wrong password for authentication with AUTH.

This resolves #322.
Daniele Alessandri 8 سال پیش
والد
کامیت
973e8592e3

+ 6 - 1
src/Connection/PhpiredisSocketConnection.php

@@ -14,6 +14,7 @@ namespace Predis\Connection;
 use Predis\Command\CommandInterface;
 use Predis\NotSupportedException;
 use Predis\Response\Error as ErrorResponse;
+use Predis\Response\ErrorInterface as ErrorResponseInterface;
 use Predis\Response\Status as StatusResponse;
 
 /**
@@ -316,7 +317,11 @@ class PhpiredisSocketConnection extends AbstractConnection
     {
         if (parent::connect() && $this->initCommands) {
             foreach ($this->initCommands as $command) {
-                $this->executeCommand($command);
+                $response = $this->executeCommand($command);
+
+                if ($response instanceof ErrorResponseInterface) {
+                    $this->onConnectionError("`{$command->getId()}` failed: $response", 0);
+                }
             }
         }
     }

+ 6 - 1
src/Connection/StreamConnection.php

@@ -13,6 +13,7 @@ namespace Predis\Connection;
 
 use Predis\Command\CommandInterface;
 use Predis\Response\Error as ErrorResponse;
+use Predis\Response\ErrorInterface as ErrorResponseInterface;
 use Predis\Response\Status as StatusResponse;
 
 /**
@@ -256,7 +257,11 @@ class StreamConnection extends AbstractConnection
     {
         if (parent::connect() && $this->initCommands) {
             foreach ($this->initCommands as $command) {
-                $this->executeCommand($command);
+                $response = $this->executeCommand($command);
+
+                if ($response instanceof ErrorResponseInterface) {
+                    $this->onConnectionError("`{$command->getId()}` failed: $response", 0);
+                }
             }
         }
     }

+ 30 - 0
tests/Predis/Connection/PhpiredisSocketConnectionTest.php

@@ -11,6 +11,9 @@
 
 namespace Predis\Connection;
 
+use Predis\Command\RawCommand;
+use Predis\Response\Error as ErrorResponse;
+
 /**
  * @group ext-phpiredis
  * @requires extension phpiredis
@@ -43,6 +46,33 @@ class PhpiredisSocketConnectionTest extends PredisConnectionTestCase
         $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
     }
 
+    /**
+     * @group disconnected
+     * @expectedException \Predis\Connection\ConnectionException
+     * @expectedExceptionMessage `SELECT` failed: ERR invalid DB index [tcp://127.0.0.1:6379]
+     */
+    public function testThrowsExceptionOnInitializationCommandFailure()
+    {
+        $cmdSelect = RawCommand::create('SELECT', '1000');
+        $responseError =
+
+        $connection = $this->getMockBuilder(static::CONNECTION_CLASS)
+                           ->setMethods(array('executeCommand', 'createResource'))
+                           ->setConstructorArgs(array(new Parameters()))
+                           ->getMock();
+
+        $connection->method('executeCommand')
+                   ->with($cmdSelect)
+                   ->will($this->returnValue(
+                       new ErrorResponse("ERR invalid DB index")
+                   ));
+
+        $connection->method('createResource');
+
+        $connection->addConnectCommand($cmdSelect);
+        $connection->connect();
+    }
+
     // ******************************************************************** //
     // ---- INTEGRATION TESTS --------------------------------------------- //
     // ******************************************************************** //

+ 30 - 0
tests/Predis/Connection/PhpiredisStreamConnectionTest.php

@@ -11,6 +11,9 @@
 
 namespace Predis\Connection;
 
+use Predis\Command\RawCommand;
+use Predis\Response\Error as ErrorResponse;
+
 /**
  * @group ext-phpiredis
  * @requires extension phpiredis
@@ -43,6 +46,33 @@ class PhpiredisStreamConnectionTest extends PredisConnectionTestCase
         $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
     }
 
+    /**
+     * @group disconnected
+     * @expectedException \Predis\Connection\ConnectionException
+     * @expectedExceptionMessage `SELECT` failed: ERR invalid DB index [tcp://127.0.0.1:6379]
+     */
+    public function testThrowsExceptionOnInitializationCommandFailure()
+    {
+        $cmdSelect = RawCommand::create('SELECT', '1000');
+        $responseError =
+
+        $connection = $this->getMockBuilder(static::CONNECTION_CLASS)
+                           ->setMethods(array('executeCommand', 'createResource'))
+                           ->setConstructorArgs(array(new Parameters()))
+                           ->getMock();
+
+        $connection->method('executeCommand')
+                   ->with($cmdSelect)
+                   ->will($this->returnValue(
+                       new ErrorResponse("ERR invalid DB index")
+                   ));
+
+        $connection->method('createResource');
+
+        $connection->addConnectCommand($cmdSelect);
+        $connection->connect();
+    }
+
     // ******************************************************************** //
     // ---- INTEGRATION TESTS --------------------------------------------- //
     // ******************************************************************** //

+ 30 - 0
tests/Predis/Connection/StreamConnectionTest.php

@@ -11,6 +11,9 @@
 
 namespace Predis\Connection;
 
+use Predis\Command\RawCommand;
+use Predis\Response\Error as ErrorResponse;
+
 /**
  *
  */
@@ -18,6 +21,33 @@ class StreamConnectionTest extends PredisConnectionTestCase
 {
     const CONNECTION_CLASS = 'Predis\Connection\StreamConnection';
 
+    /**
+     * @group disconnected
+     * @expectedException \Predis\Connection\ConnectionException
+     * @expectedExceptionMessage `SELECT` failed: ERR invalid DB index [tcp://127.0.0.1:6379]
+     */
+    public function testThrowsExceptionOnInitializationCommandFailure()
+    {
+        $cmdSelect = RawCommand::create('SELECT', '1000');
+        $responseError =
+
+        $connection = $this->getMockBuilder(static::CONNECTION_CLASS)
+                           ->setMethods(array('executeCommand', 'createResource'))
+                           ->setConstructorArgs(array(new Parameters()))
+                           ->getMock();
+
+        $connection->method('executeCommand')
+                   ->with($cmdSelect)
+                   ->will($this->returnValue(
+                       new ErrorResponse("ERR invalid DB index")
+                   ));
+
+        $connection->method('createResource');
+
+        $connection->addConnectCommand($cmdSelect);
+        $connection->connect();
+    }
+
     // ******************************************************************** //
     // ---- INTEGRATION TESTS --------------------------------------------- //
     // ******************************************************************** //