Fix incorrect usage of instance method in static method.

We also added a test to check that the socket-based connection backend
throws an exception when unable to resolve hostnames.
This commit is contained in:
Daniele Alessandri
2013-12-22 12:38:54 +01:00
parent 5065541cc6
commit b9b899eaf0
2 changed files with 19 additions and 4 deletions
@@ -229,7 +229,7 @@ class PhpiredisSocketConnection extends AbstractConnection
* @param ParametersInterface $parameters Parameters used to initialize the connection.
* @return string
*/
private static function getAddress(ParametersInterface $parameters)
protected static function getAddress(ParametersInterface $parameters)
{
if ($parameters->scheme === 'unix') {
return $parameters->path;
@@ -238,8 +238,8 @@ class PhpiredisSocketConnection 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)];
@@ -256,7 +256,10 @@ class PhpiredisSocketConnection extends AbstractConnection
*/
private function connectWithTimeout(ParametersInterface $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);
@@ -86,6 +86,18 @@ class PhpiredisSocketConnectionTest 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 PhpiredisSocketConnection($parameters);
$connection->connect();
}
/**
* @group connected
* @expectedException Predis\Protocol\ProtocolException