Преглед на файлове

Make PhpiredisConnection::getAddress() static and protected.

This method is mostly an utility method which is the main reason for
it bein static, but can be useful to have it available when extending
this class.

This change was inspired by b9b899e (master) even though the original
commit is meant to fix an issue not affecting v0.8.
Daniele Alessandri преди 11 години
родител
ревизия
d88a280977
променени са 2 файла, в които са добавени 19 реда и са изтрити 4 реда
  1. 7 4
      lib/Predis/Connection/PhpiredisConnection.php
  2. 12 0
      tests/Predis/Connection/PhpiredisConnectionTest.php

+ 7 - 4
lib/Predis/Connection/PhpiredisConnection.php

@@ -227,7 +227,7 @@ class PhpiredisConnection extends AbstractConnection
      * @param  ConnectionParametersInterface $parameters Parameters used to initialize the connection.
      * @return string
      */
-    private function getAddress(ConnectionParametersInterface $parameters)
+    protected static function getAddress(ConnectionParametersInterface $parameters)
     {
         if ($parameters->scheme === 'unix') {
             return $parameters->path;
@@ -236,8 +236,8 @@ class PhpiredisConnection extends AbstractConnection
         $host = $parameters->host;
 
         if (ip2long($host) === false) {
-            if (($addresses = gethostbynamel($host)) === false) {
-                $this->onConnectionError("Cannot resolve the address of $host");
+            if (false === $addresses = gethostbynamel($host)) {
+                return false;
             }
 
             return $addresses[array_rand($addresses)];
@@ -254,7 +254,10 @@ class PhpiredisConnection extends AbstractConnection
      */
     private function connectWithTimeout(ConnectionParametersInterface $parameters)
     {
-        $host = self::getAddress($parameters);
+        if (false === $host = self::getAddress($parameters)) {
+            $this->onConnectionError("Cannot resolve the address of '$parameters->host'.");
+        }
+
         $socket = $this->getResource();
 
         socket_set_nonblock($socket);

+ 12 - 0
tests/Predis/Connection/PhpiredisConnectionTest.php

@@ -86,6 +86,18 @@ class PhpiredisConnectionTest extends PredisConnectionTestCase
         $this->assertSame(array('foo', 'hoge', 'lol'), $connection->executeCommand($cmdLrange));
     }
 
+     /**
+      * @group connected
+      * @expectedException Predis\Connection\ConnectionException
+      * @expectedExceptionMessage Cannot resolve the address of 'bogus.tld'.
+      */
+     public function testThrowsExceptionOnUnresolvableHostname()
+     {
+         $parameters = $this->getParameters(array('host' => 'bogus.tld'));
+         $connection = new PhpiredisConnection($parameters);
+         $connection->connect();
+     }
+
     /**
      * @group connected
      * @expectedException Predis\Protocol\ProtocolException