From 2c1fc6bd9bba727fe0e2ce8f19b9236234478c55 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Sat, 30 Aug 2014 17:18:24 +0200 Subject: [PATCH] Improving the usage of exceptions in Xml --- src/Utility/Error/XmlException.php | 4 ++-- src/Utility/Xml.php | 21 +++++++++++---------- tests/TestCase/Utility/XmlTest.php | 20 +++++--------------- 3 files changed, 18 insertions(+), 27 deletions(-) diff --git a/src/Utility/Error/XmlException.php b/src/Utility/Error/XmlException.php index 951b27d66e0..c2c5fdf70e3 100644 --- a/src/Utility/Error/XmlException.php +++ b/src/Utility/Error/XmlException.php @@ -13,12 +13,12 @@ */ namespace Cake\Utility\Error; -use Cake\Core\Exception\Exception; +use RuntimeException; /** * Exception class for Xml. This exception will be thrown from Xml when it * encounters an error. * */ -class XmlException extends Exception { +class XmlException extends RuntimeException { } diff --git a/src/Utility/Xml.php b/src/Utility/Xml.php index b0491f960a8..a4957a14ff4 100644 --- a/src/Utility/Xml.php +++ b/src/Utility/Xml.php @@ -16,6 +16,7 @@ use Cake\Core\Configure; use Cake\Network\Error\SocketException; +use Cake\Utility\Error\XmlException; use Cake\Network\Http\Client; use \DOMDocument; @@ -100,16 +101,16 @@ public static function build($input, array $options = []) { $socket = new Client(['redirect' => 10]); $response = $socket->get($input); if (!$response->isOk()) { - throw new Error\XmlException('XML cannot be read.'); + throw new XmlException('XML cannot be read.'); } return static::_loadXml($response->body, $options); } catch (SocketException $e) { - throw new Error\XmlException('XML cannot be read.'); + throw new XmlException('XML cannot be read.'); } } elseif (!is_string($input)) { - throw new Error\XmlException('Invalid input.'); + throw new XmlException('Invalid input.'); } - throw new Error\XmlException('XML cannot be read.'); + throw new XmlException('XML cannot be read.'); } /** @@ -141,7 +142,7 @@ protected static function _loadXml($input, $options) { } libxml_use_internal_errors($internalErrors); if ($xml === null) { - throw new Error\XmlException('Xml cannot be read.'); + throw new XmlException('Xml cannot be read.'); } return $xml; } @@ -189,11 +190,11 @@ public static function fromArray($input, $options = array()) { $input = $input->toArray(); } if (!is_array($input) || count($input) !== 1) { - throw new Error\XmlException('Invalid input.'); + throw new XmlException('Invalid input.'); } $key = key($input); if (is_int($key)) { - throw new Error\XmlException('The key of input must be alphanumeric'); + throw new XmlException('The key of input must be alphanumeric'); } if (!is_array($options)) { @@ -274,7 +275,7 @@ protected static function _fromArray($dom, $node, &$data, $format) { } } else { if ($key[0] === '@') { - throw new Error\XmlException('Invalid array'); + throw new XmlException('Invalid array'); } if (is_numeric(implode('', array_keys($value)))) { // List foreach ($value as $item) { @@ -287,7 +288,7 @@ protected static function _fromArray($dom, $node, &$data, $format) { } } } else { - throw new Error\XmlException('Invalid array'); + throw new XmlException('Invalid array'); } } } @@ -341,7 +342,7 @@ public static function toArray($obj) { $obj = simplexml_import_dom($obj); } if (!($obj instanceof \SimpleXMLElement)) { - throw new Error\XmlException('The input is not instance of SimpleXMLElement, DOMDocument or DOMNode.'); + throw new XmlException('The input is not instance of SimpleXMLElement, DOMDocument or DOMNode.'); } $result = array(); $namespaces = array_merge(array('' => ''), $obj->getNamespaces(true)); diff --git a/tests/TestCase/Utility/XmlTest.php b/tests/TestCase/Utility/XmlTest.php index 63db6d8ef2e..49cc9740c5c 100644 --- a/tests/TestCase/Utility/XmlTest.php +++ b/tests/TestCase/Utility/XmlTest.php @@ -168,7 +168,7 @@ public static function invalidDataProvider() { * testBuildInvalidData * * @dataProvider invalidDataProvider - * @expectedException \Cake\Core\Exception\Exception + * @expectedException RuntimeException * @return void */ public function testBuildInvalidData($value) { @@ -183,7 +183,7 @@ public function testBuildInvalidData($value) { */ public function testBuildInvalidDataSimpleXml() { $input = ' 'simplexml')); + Xml::build($input, array('return' => 'simplexml')); } /** @@ -536,15 +536,11 @@ public static function invalidArrayDataProvider() { * testFromArrayFail method * * @dataProvider invalidArrayDataProvider + * @expectedException Exception * @return void */ public function testFromArrayFail($value) { - try { - Xml::fromArray($value); - $this->fail('No exception.'); - } catch (\Exception $e) { - $this->assertTrue(true, 'Caught exception.'); - } + Xml::fromArray($value); } /** @@ -1143,12 +1139,6 @@ public function testNoEntityLoading() { &payload; XML; - try { - $result = Xml::build($xml); - $this->assertEquals('', (string)$result->xxe); - } catch (Exception $e) { - $this->assertTrue(true, 'A warning was raised meaning external entities were not loaded'); - } + $result = Xml::build($xml); } - }