Skip to content

Commit

Permalink
Attempt to fix failures in travisci.
Browse files Browse the repository at this point in the history
Network tests have recently started failing, make those skip conditions
instead. Convert SocketExceptions in Xml::build() into XmlExceptions.
This is the documented behavior so we should try to honour that.
  • Loading branch information
markstory committed Feb 20, 2013
1 parent fd7158c commit 341c0d1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 28 deletions.
66 changes: 42 additions & 24 deletions lib/Cake/Test/Case/Network/CakeSocketTest.php
Expand Up @@ -87,16 +87,22 @@ public function testSocketConnection() {
$this->assertFalse($this->Socket->connected);
$this->Socket->disconnect();
$this->assertFalse($this->Socket->connected);
$this->Socket->connect();
$this->assertTrue($this->Socket->connected);
$this->Socket->connect();
$this->assertTrue($this->Socket->connected);

$this->Socket->disconnect();
$config = array('persistent' => true);
$this->Socket = new CakeSocket($config);
$this->Socket->connect();
$this->assertTrue($this->Socket->connected);
try {
$this->Socket->connect();
$this->assertTrue($this->Socket->connected);
$this->Socket->connect();
$this->assertTrue($this->Socket->connected);

$this->Socket->disconnect();
$config = array('persistent' => true);
$this->Socket = new CakeSocket($config);
$this->Socket->connect();
$this->assertTrue($this->Socket->connected);
} catch (SocketException $e) {
$connectionRefused = stripos($e->getMessage(), 'Connection refused') !== false;
$this->skipIf($connectionRefused, 'Cannot test network, skipping.');
throw $e;
}
}

/**
Expand Down Expand Up @@ -129,19 +135,25 @@ public function testInvalidConnection($data) {
* @return void
*/
public function testSocketHost() {
$this->Socket = new CakeSocket();
$this->Socket->connect();
$this->assertEquals('127.0.0.1', $this->Socket->address());
$this->assertEquals(gethostbyaddr('127.0.0.1'), $this->Socket->host());
$this->assertEquals(null, $this->Socket->lastError());
$this->assertTrue(in_array('127.0.0.1', $this->Socket->addresses()));

$this->Socket = new CakeSocket(array('host' => '127.0.0.1'));
$this->Socket->connect();
$this->assertEquals('127.0.0.1', $this->Socket->address());
$this->assertEquals(gethostbyaddr('127.0.0.1'), $this->Socket->host());
$this->assertEquals(null, $this->Socket->lastError());
$this->assertTrue(in_array('127.0.0.1', $this->Socket->addresses()));
try {
$this->Socket = new CakeSocket();
$this->Socket->connect();
$this->assertEquals('127.0.0.1', $this->Socket->address());
$this->assertEquals(gethostbyaddr('127.0.0.1'), $this->Socket->host());
$this->assertEquals(null, $this->Socket->lastError());
$this->assertTrue(in_array('127.0.0.1', $this->Socket->addresses()));

$this->Socket = new CakeSocket(array('host' => '127.0.0.1'));
$this->Socket->connect();
$this->assertEquals('127.0.0.1', $this->Socket->address());
$this->assertEquals(gethostbyaddr('127.0.0.1'), $this->Socket->host());
$this->assertEquals(null, $this->Socket->lastError());
$this->assertTrue(in_array('127.0.0.1', $this->Socket->addresses()));
} catch (SocketException $e) {
$connectionRefused = stripos($e->getMessage(), 'Connection refused') !== false;
$this->skipIf($connectionRefused, 'Cannot test network, skipping.');
throw $e;
}
}

/**
Expand Down Expand Up @@ -256,7 +268,13 @@ protected function _connectSocketToSslTls() {
$this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
$configSslTls = array('host' => 'smtp.gmail.com', 'port' => 465, 'timeout' => 5);
$this->Socket = new CakeSocket($configSslTls);
$this->Socket->connect();
try {
$this->Socket->connect();
} catch (SocketException $e) {
$connectionRefused = stripos($e->getMessage(), 'Connection refused') !== false;
$this->skipIf($connectionRefused, 'Cannot test network, skipping.');
throw $e;
}
}

/**
Expand Down
12 changes: 8 additions & 4 deletions lib/Cake/Utility/Xml.php
Expand Up @@ -102,12 +102,16 @@ public static function build($input, $options = array()) {
} elseif (file_exists($input)) {
return self::_loadXml(file_get_contents($input), $options);
} elseif (strpos($input, 'http://') === 0 || strpos($input, 'https://') === 0) {
$socket = new HttpSocket(array('request' => array('redirect' => 10)));
$response = $socket->get($input);
if (!$response->isOk()) {
try {
$socket = new HttpSocket(array('request' => array('redirect' => 10)));
$response = $socket->get($input);
if (!$response->isOk()) {
throw new XmlException(__d('cake_dev', 'XML cannot be read.'));
}
return self::_loadXml($response->body, $options);
} catch (SocketException $e) {
throw new XmlException(__d('cake_dev', 'XML cannot be read.'));
}
return self::_loadXml($response->body, $options);
} elseif (!is_string($input)) {
throw new XmlException(__d('cake_dev', 'Invalid input.'));
}
Expand Down

0 comments on commit 341c0d1

Please sign in to comment.