Skip to content
Permalink
Browse files

Improving the usage of exceptions in Xml

  • Loading branch information...
lorenzo committed Aug 30, 2014
1 parent dc55818 commit 2c1fc6bd9bba727fe0e2ce8f19b9236234478c55
Showing with 18 additions and 27 deletions.
  1. +2 −2 src/Utility/Error/XmlException.php
  2. +11 −10 src/Utility/Xml.php
  3. +5 −15 tests/TestCase/Utility/XmlTest.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 {
}
@@ -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));
@@ -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 = '<derp';
$xml = Xml::build($input, array('return' => '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() {
<xxe>&payload;</xxe>
</request>
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);
}
}

0 comments on commit 2c1fc6b

Please sign in to comment.
You can’t perform that action at this time.