From 341c0d17f0f3d5bd7488edb9c8153d276d520831 Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 19 Feb 2013 20:53:28 -0500 Subject: [PATCH] Attempt to fix failures in travisci. 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. --- lib/Cake/Test/Case/Network/CakeSocketTest.php | 66 ++++++++++++------- lib/Cake/Utility/Xml.php | 12 ++-- 2 files changed, 50 insertions(+), 28 deletions(-) diff --git a/lib/Cake/Test/Case/Network/CakeSocketTest.php b/lib/Cake/Test/Case/Network/CakeSocketTest.php index acd664b2d0a..df4656afd98 100644 --- a/lib/Cake/Test/Case/Network/CakeSocketTest.php +++ b/lib/Cake/Test/Case/Network/CakeSocketTest.php @@ -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; + } } /** @@ -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; + } } /** @@ -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; + } } /** diff --git a/lib/Cake/Utility/Xml.php b/lib/Cake/Utility/Xml.php index 285ddd94950..f822abea83f 100644 --- a/lib/Cake/Utility/Xml.php +++ b/lib/Cake/Utility/Xml.php @@ -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.')); }