Skip to content

Commit

Permalink
Improving the usage of exceptions in Xml
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Aug 30, 2014
1 parent dc55818 commit 2c1fc6b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/Utility/Error/XmlException.php
Expand Up @@ -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 {
}
21 changes: 11 additions & 10 deletions src/Utility/Xml.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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.');
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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) {
Expand All @@ -287,7 +288,7 @@ protected static function _fromArray($dom, $node, &$data, $format) {
}
}
} else {
throw new Error\XmlException('Invalid array');
throw new XmlException('Invalid array');
}
}
}
Expand Down Expand Up @@ -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));
Expand Down
20 changes: 5 additions & 15 deletions tests/TestCase/Utility/XmlTest.php
Expand Up @@ -168,7 +168,7 @@ public static function invalidDataProvider() {
* testBuildInvalidData
*
* @dataProvider invalidDataProvider
* @expectedException \Cake\Core\Exception\Exception
* @expectedException RuntimeException
* @return void
*/
public function testBuildInvalidData($value) {
Expand All @@ -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'));
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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.