Skip to content

Commit

Permalink
Port Xml::fromArray() fix from 2.x
Browse files Browse the repository at this point in the history
Port fix for zero values from 2.x

Refs #9870
  • Loading branch information
markstory committed Dec 14, 2016
1 parent 50ea948 commit 82d3473
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Utility/Xml.php
Expand Up @@ -335,7 +335,7 @@ protected static function _createChild($data)
$childNS = $value['xmlns:'];
unset($value['xmlns:']);
}
} elseif (!empty($value) || $value === 0) {
} elseif (!empty($value) || $value === 0 || $value === '0') {
$childValue = (string)$value;
}

Expand Down
26 changes: 22 additions & 4 deletions tests/TestCase/Utility/XmlTest.php
Expand Up @@ -377,17 +377,35 @@ public function testFromArray()
$obj = Xml::fromArray($xml, 'attributes');
$xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?><tags><tag id="1">defect</tag></tags>';
$this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
}

$xml = [
'tag' => [
/**
* Test fromArray() with zero values.
*
* @return void
*/
public function testFromArrayZeroValue()
{
$xml = array(
'tag' => array(
'@' => 0,
'@test' => 'A test'
]
];
)
);
$obj = Xml::fromArray($xml);
$xmlText = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<tag test="A test">0</tag>
XML;
$this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());

$xml = array(
'tag' => array('0')
);
$obj = Xml::fromArray($xml);
$xmlText = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<tag>0</tag>
XML;
$this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML());
}
Expand Down

0 comments on commit 82d3473

Please sign in to comment.