Merge branch 2.0-xml of github.com:cakephp/cakephp into 2.0-xml

This commit is contained in:
Juan Basso 2010-08-23 01:46:31 -03:00
commit 65726841cd
2 changed files with 99 additions and 96 deletions

View file

@ -25,8 +25,45 @@ class Xml {
/**
* Initialize SimpleXMLElement from a given XML string, file path, URL or array.
*
* ### Usage:
*
* Building XML from a string:
*
* `$xml = Xml::build('<example>text</example>');`
*
* Building XML from a file path:
*
* `$xml = Xml::build('/path/to/an/xml/file.xml');`
*
* Building from a remote URL:
*
* `$xml = Xml::build('http://example.com/example.xml');`
*
* Building from an array:
*
* {{{
* $value = array(
* 'tags' => array(
* 'tag' => array(
* array(
* 'id' => '1',
* 'name' => 'defect'
* ),
* array(
* 'id' => '2',
* 'name' => 'enhancement'
* )
* )
* )
* );
* $xml = Xml::build($value);
* }}}
*
* When building XML from an array ensure that there is only one top level element.
*
* @param mixed $input XML string, a path to a file, an URL or an array
* @return object SimpleXMLElement
* @throws Exception
*/
public static function build($input) {
if (is_array($input) || is_object($input)) {
@ -42,10 +79,31 @@ class Xml {
}
/**
* Transform an array in a SimpleXMLElement
* Transform an array into a SimpleXMLElement
*
* Using the following data:
*
* {{{
* $value = array(
* 'root' => array(
* 'tag' => array(
* 'id' => 1,
* 'value' => 'defect'
* )
* )
* );
* }}}
*
* Calling `Xml::fromArray($value, 'tags');` Will generate:
*
* `<root><tag><id>1</id><value>defect</value></tag></root>`
*
* And calling `Xml::fromArray($value, 'attribute');` Will generate:
*
* `<root><tag id="1">defect</tag></root>`
*
* @param array $input Array with data
* @param string $format If create childs ('tags') or attributes ('attribute')
* @param string $format If create childs ('tags') or attributes ('attribute').
* @return object SimpleXMLElement
*/
public static function fromArray($input, $format = 'attribute') {
@ -74,8 +132,8 @@ class Xml {
* Recursive method to create SimpleXMLElement from array
*
* @param object $node Handler to SimpleXMLElement
* @param array $array
* @param string $format
* @param array $array Array of data to append to the $node.
* @param string $format Either 'attribute' or 'tags'. This determines where nested keys go.
* @return void
*/
protected static function _fromArrayRecursive(&$node, &$array, $format = 'attribute') {

View file

@ -56,34 +56,29 @@ class XmlTest extends CakeTestCase {
$this->assertEqual((string)$obj, 'value');
}
/**
* data provider function for testBuildInvalidData
*
* @return void
*/
public static function invalidDataProvider() {
return array(
array(null),
array(false),
array(''),
array('<tag>')
);
}
/**
* testBuildInvalidData
*
* @dataProvider invalidDataProvider
* @expectedException Exception
* return void
*/
function testBuildInvalidData() {
Xml::build(false);
}
/**
* testBuildEmptyData
*
* @expectedException Exception
* return void
*/
function testBuildEmptyData() {
Xml::build('');
}
/**
* testBuildInvalidTag
*
* @expectedException Exception
* return void
*/
function testBuildInvalidTag() {
Xml::build('<tag>');
function testBuildInvalidData($value) {
Xml::build($value);
}
/**
@ -215,49 +210,20 @@ class XmlTest extends CakeTestCase {
}
/**
* testFromArrayFail method
* data provider for fromArray() failures
*
* @access public
* @return void
*/
function testFromArrayFail() {
try {
Xml::fromArray(false);
$this->fail('No exception thrown');
} catch (Exception $e) {
$this->assertTrue(true);
}
try {
Xml::fromArray(array());
$this->fail('No exception thrown');
} catch (Exception $e) {
$this->assertTrue(true);
}
try {
Xml::fromArray(array('numeric key as root'));
$this->fail('No exception thrown');
} catch (Exception $e) {
$this->assertTrue(true);
}
try {
Xml::fromArray(array('item1' => '', 'item2' => ''));
$this->fail('No exception thrown');
} catch (Exception $e) {
$this->assertTrue(true);
}
try {
Xml::fromArray(array('items' => array('item1', 'item2')));
$this->fail('No exception thrown');
} catch (Exception $e) {
$this->assertTrue(true);
}
try {
$xml = array(
public static function invalidArrayDataProvider() {
return array(
array(''),
array(null),
array(false),
array(array()),
array('numeric key as root'),
array('item1' => '', 'item2' => ''),
array('items' => array('item1', 'item2')),
array(array(
'tags' => array(
'tag' => array(
array(
@ -267,15 +233,8 @@ class XmlTest extends CakeTestCase {
)
)
)
);
Xml::fromArray($xml);
$this->fail('No exception thrown');
} catch (Exception $e) {
$this->assertEqual($e->getMessage(), __('Invalid array'));
}
try {
$xml = array(
)),
array(array(
'tags' => array(
'@tag' => array(
array(
@ -288,33 +247,19 @@ class XmlTest extends CakeTestCase {
)
)
)
);
$obj = Xml::fromArray($xml);
$this->fail('No exception thrown');
} catch (Exception $e) {
$this->assertEqual($e->getMessage(), __('Invalid array'));
}
)),
array(new DateTime())
);
}
/**
* testToArrayInvalidObject
* testFromArrayFail method
*
* @dataProvider invalidArrayDataProvider
* @expectedException Exception
* @return void
*/
function testToArrayInvalidObject() {
$obj = new DateTime();
Xml::toArray($obj);
}
/**
* testToArrayNoObject
*
* @expectedException Exception
* @return void
*/
function testToArrayNoObject() {
Xml::toArray(array());
function testFromArrayFail($value) {
Xml::fromArray($value);
}
/**