Browse Source

Do not throw exception when connection has been already established.

The base abstract connection class now returns a bool to indicate when
the actual connect() operation has been performed on the underlying
resource. This return value is not part of the interface so extending
classes can decide to not return any value.
Daniele Alessandri 11 years ago
parent
commit
7ba465048f

+ 4 - 0
CHANGELOG.md

@@ -12,6 +12,10 @@ v0.9.0 (201x-xx-xx)
   values carrying the original payload, so one can do `$response == 'OK'` which
   is also more akin to how Redis replies to clients.
 
+- Invoking `Predis\Client::connect()` when the underlying connection has been
+  already established does not throw any exception anymore, now the connection
+  simply does not attempt to perform any operation.
+
 - Added the `aggregate` client option, useful to fully customize how the client
   should aggregate multiple connections when an array of connection parameters
   is passed to `Predis\Client::__construct()`.

+ 5 - 3
lib/Predis/Connection/AbstractConnection.php

@@ -88,11 +88,13 @@ abstract class AbstractConnection implements SingleConnectionInterface
      */
     public function connect()
     {
-        if ($this->isConnected()) {
-            throw new ClientException('Connection already estabilished.');
+        if (!$this->isConnected()) {
+            $this->resource = $this->createResource();
+
+            return true;
         }
 
-        $this->resource = $this->createResource();
+        return false;
     }
 
     /**

+ 6 - 6
lib/Predis/Connection/PhpiredisSocketConnection.php

@@ -296,13 +296,13 @@ class PhpiredisSocketConnection extends AbstractConnection
      */
     public function connect()
     {
-        parent::connect();
+        if (parent::connect()) {
+            $this->connectWithTimeout($this->parameters);
 
-        $this->connectWithTimeout($this->parameters);
-
-        if ($this->initCommands) {
-            foreach ($this->initCommands as $command) {
-                $this->executeCommand($command);
+            if ($this->initCommands) {
+                foreach ($this->initCommands as $command) {
+                    $this->executeCommand($command);
+                }
             }
         }
     }

+ 1 - 3
lib/Predis/Connection/StreamConnection.php

@@ -129,9 +129,7 @@ class StreamConnection extends AbstractConnection
      */
     public function connect()
     {
-        parent::connect();
-
-        if ($this->initCommands) {
+        if (parent::connect() && $this->initCommands) {
             foreach ($this->initCommands as $command) {
                 $this->executeCommand($command);
             }

+ 4 - 3
tests/PHPUnit/PredisConnectionTestCase.php

@@ -49,15 +49,16 @@ abstract class PredisConnectionTestCase extends PredisTestCase
 
     /**
      * @group connected
-     * @expectedException Predis\ClientException
-     * @expectedExceptionMessage Connection already estabilished.
      */
-    public function testThrowsExceptionOnConnectWhenAlreadyConnected()
+    public function testDoesNotThrowExceptionOnConnectWhenAlreadyConnected()
     {
         $connection = $this->getConnection();
 
         $connection->connect();
+        $this->assertTrue($connection->isConnected());
+
         $connection->connect();
+        $this->assertTrue($connection->isConnected());
     }
 
     /**