Browse Source

Instantiate the current class when subclassing Client.

Previously the getClientFor() method in a subclass of Predis\Client
returned an instance of Predis\Client instead of a new instance of
the subclass. The new behaviour is more correct.
Daniele Alessandri 12 năm trước cách đây
mục cha
commit
9901233fa1
3 tập tin đã thay đổi với 16 bổ sung1 xóa
  1. 3 0
      CHANGELOG.md
  2. 1 1
      lib/Predis/Client.php
  3. 12 0
      tests/Predis/ClientTest.php

+ 3 - 0
CHANGELOG.md

@@ -14,6 +14,9 @@ v0.8.1 (201x-xx-xx)
   ignored when retrying to execute a Lua script by falling back to `EVAL` after
   a `-NOSCRIPT` error (ISSUE #94).
 
+- __FIX__: When subclassing Predis\Client, the `getClientFor()` method returns
+  a new instance of the subclass instead of a new instance of Predis\Client.
+
 
 v0.8.0 (2012-10-23)
 ===============================================================================

+ 1 - 1
lib/Predis/Client.php

@@ -142,7 +142,7 @@ class Client implements ClientInterface
             throw new \InvalidArgumentException("Invalid connection ID: '$connectionID'");
         }
 
-        return new Client($connection, $this->options);
+        return new static($connection, $this->options);
     }
 
     /**

+ 12 - 0
tests/Predis/ClientTest.php

@@ -462,10 +462,22 @@ class ClientTest extends StandardTestCase
 
         $clientNode02 = $client->getClientFor('node02');
 
+        $this->assertInstanceOf('Predis\Client', $clientNode02);
         $this->assertSame($node02, $clientNode02->getConnection());
         $this->assertSame($client->getOptions(), $clientNode02->getOptions());
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testGetClientForReturnsInstanceOfSubclass()
+    {
+        $nodes = array('tcp://host1?alias=node01', 'tcp://host2?alias=node02');
+        $client = $this->getMock('Predis\Client', array('dummy'), array($nodes), 'SubclassedClient');
+
+        $this->assertInstanceOf('SubclassedClient', $client->getClientFor('node02'));
+    }
+
     /**
      * @group disconnected
      */