fixes #5384, Xml::toArray()

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7555 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2008-09-04 15:34:08 +00:00
parent ff7428e676
commit 2a84ec9944
2 changed files with 35 additions and 13 deletions

View file

@ -678,7 +678,7 @@ class XmlNode extends Object {
if ($child->attributes) { if ($child->attributes) {
$value = array_merge(array('value' => $value), $child->attributes); $value = array_merge(array('value' => $value), $child->attributes);
} }
if (isset($out[$child->name])) { if (isset($out[$child->name]) || isset($multi[$key])) {
if (!isset($multi)) { if (!isset($multi)) {
$multi = array($key => array($out[$child->name])); $multi = array($key => array($out[$child->name]));
unset($out[$child->name]); unset($out[$child->name]);

View file

@ -35,12 +35,12 @@ App::import('Core', 'Xml');
* @subpackage cake.tests.cases.libs * @subpackage cake.tests.cases.libs
*/ */
class XmlTest extends CakeTestCase { class XmlTest extends CakeTestCase {
function setUp() { function setUp() {
$manager =& new XmlManager(); $manager =& new XmlManager();
$manager->namespaces = array(); $manager->namespaces = array();
} }
function KgetTests() { function KgetTests() {
return array('testRootTagParsing'); return array('testRootTagParsing');
} }
@ -277,13 +277,13 @@ class XmlTest extends CakeTestCase {
); );
$xml =& new Xml($input, array('format' => 'tags')); $xml =& new Xml($input, array('format' => 'tags'));
$node =& $xml->children[0]->children[0]; $node =& $xml->children[0]->children[0];
$nextSibling =& $node->nextSibling(); $nextSibling =& $node->nextSibling();
$this->assertEqual($nextSibling, $xml->children[0]->children[1]); $this->assertEqual($nextSibling, $xml->children[0]->children[1]);
$nextSibling2 =& $nextSibling->nextSibling(); $nextSibling2 =& $nextSibling->nextSibling();
$this->assertEqual($nextSibling2, $xml->children[0]->children[2]); $this->assertEqual($nextSibling2, $xml->children[0]->children[2]);
$noFriends =& $xml->children[0]->children[12]; $noFriends =& $xml->children[0]->children[12];
$this->assertNull($noFriends->nextSibling()); $this->assertNull($noFriends->nextSibling());
} }
@ -310,10 +310,10 @@ class XmlTest extends CakeTestCase {
); );
$xml =& new Xml($input, array('format' => 'tags')); $xml =& new Xml($input, array('format' => 'tags'));
$node =& $xml->children[0]->children[1]; $node =& $xml->children[0]->children[1];
$prevSibling =& $node->previousSibling(); $prevSibling =& $node->previousSibling();
$this->assertEqual($prevSibling, $xml->children[0]->children[0]); $this->assertEqual($prevSibling, $xml->children[0]->children[0]);
$this->assertNull($prevSibling->previousSibling()); $this->assertNull($prevSibling->previousSibling());
} }
/** /**
@ -325,7 +325,7 @@ class XmlTest extends CakeTestCase {
function testAddAndRemoveAttributes() { function testAddAndRemoveAttributes() {
$node =& new XmlElement('myElement', 'superValue'); $node =& new XmlElement('myElement', 'superValue');
$this->assertTrue(empty($node->attributes)); $this->assertTrue(empty($node->attributes));
$attrs = array( $attrs = array(
'id' => 'test', 'id' => 'test',
'show' => 1, 'show' => 1,
@ -333,11 +333,11 @@ class XmlTest extends CakeTestCase {
); );
$node->addAttribute($attrs); $node->addAttribute($attrs);
$this->assertEqual($node->attributes, $attrs); $this->assertEqual($node->attributes, $attrs);
$node =& new XmlElement('myElement', 'superValue'); $node =& new XmlElement('myElement', 'superValue');
$node->addAttribute('test', 'value'); $node->addAttribute('test', 'value');
$this->assertTrue(isset($node->attributes['test'])); $this->assertTrue(isset($node->attributes['test']));
$node =& new XmlElement('myElement', 'superValue'); $node =& new XmlElement('myElement', 'superValue');
$obj =& new StdClass(); $obj =& new StdClass();
$obj->class = 'info'; $obj->class = 'info';
@ -352,7 +352,7 @@ class XmlTest extends CakeTestCase {
$result = $node->removeAttribute('class'); $result = $node->removeAttribute('class');
$this->assertTrue($result); $this->assertTrue($result);
$this->assertFalse(isset($node->attributes['class'])); $this->assertFalse(isset($node->attributes['class']));
$result = $node->removeAttribute('missing'); $result = $node->removeAttribute('missing');
$this->assertFalse($result); $this->assertFalse($result);
} }
@ -862,6 +862,28 @@ class XmlTest extends CakeTestCase {
) )
)); ));
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$text = "<?xml version='1.0' encoding='utf-8'?>
<course>
<comps>
<comp>1</comp>
<comp>2</comp>
<comp>3</comp>
<comp>4</comp>
</comps>
</course>";
$xml = new Xml($text);
$result = $xml->toArray();
$expected = array('Course' => array(
'Comps' => array(
'Comp' => array(
1, 2, 3, 4
)
)
));
$this->assertEqual($result, $expected);
} }
@ -876,7 +898,7 @@ class XmlTest extends CakeTestCase {
$parentNode->append($string); $parentNode->append($string);
$last =& $parentNode->last(); $last =& $parentNode->last();
$this->assertEqual($last->name, 'ourChildNode'); $this->assertEqual($last->name, 'ourChildNode');
$this->expectError(); $this->expectError();
$parentNode->append($parentNode); $parentNode->append($parentNode);
} }