bringing xmlhelper to 100% code coverage
fixing small bugs in XmlHelper::__parseContent

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6838 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
DarkAngelBGE 2008-05-13 07:56:33 +00:00
parent a5510c2ebb
commit 88a2f0936e
3 changed files with 136 additions and 7 deletions

View file

@ -74,7 +74,11 @@ class XmlHelper extends AppHelper {
if (Configure::read('App.encoding') !== null) { if (Configure::read('App.encoding') !== null) {
$this->encoding = Configure::read('App.encoding'); $this->encoding = Configure::read('App.encoding');
} }
if (is_array($attrib)) {
$attrib = array_merge(array('version' => '1.0', 'encoding' => $this->encoding), $attrib); $attrib = array_merge(array('version' => '1.0', 'encoding' => $this->encoding), $attrib);
}
return $this->output('<' . '?xml' . $this->__composeAttributes($attrib) . ' ?' . '>'); return $this->output('<' . '?xml' . $this->__composeAttributes($attrib) . ' ?' . '>');
} }
/** /**
@ -178,17 +182,19 @@ class XmlHelper extends AppHelper {
$out = ''; $out = '';
$keys = array_keys($content); $keys = array_keys($content);
$count = count($keys); $count = count($keys);
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
if (is_numeric($keys[$i])) { if (is_numeric($content[$keys[$i]])) {
$out .= $this->__composeContent($content[$keys[$i]]); $out .= $this->__composeContent($content[$keys[$i]]);
} elseif (is_array($content[$keys[$i]])) { } elseif (is_array($content[$keys[$i]])) {
$attr = $child = array(); $attr = $child = array();
if (Set::countDim($content[$keys[$i]]) >= 2) { if (Set::countDim($content[$keys[$i]]) >= 2) {
trigger_error(__('Dimension for XmlHelper::__composeContent is too high (>= 2). Please use an array with less dimension.', true), E_USER_WARNING);
} else { } else {
$out .= $this->__composeContent($content[$keys[$i]]);
} }
//$out .= $this->elem($keys[$i] } elseif (is_string($content[$keys[$i]])) {
$out .= $this->elem($content[$keys[$i]]);
} }
} }
return $out; return $out;
@ -196,8 +202,6 @@ class XmlHelper extends AppHelper {
return $content->toString(); return $content->toString();
} elseif (is_object($content) && method_exists($content, 'toString')) { } elseif (is_object($content) && method_exists($content, 'toString')) {
return $content->toString(); return $content->toString();
} elseif (is_object($content) && method_exists($content, 'toString')) {
return $content->toString();
} else { } else {
return $content; return $content;
} }

View file

@ -556,6 +556,7 @@ class XmlNode extends Object {
$d .= '<' . $this->name(); $d .= '<' . $this->name();
if (is_array($this->attributes) && count($this->attributes) > 0) { if (is_array($this->attributes) && count($this->attributes) > 0) {
foreach ($this->attributes as $key => $val) { foreach ($this->attributes as $key => $val) {
$val = str_replace('"', '\"', $val);
$d .= " $key=\"$val\""; $d .= " $key=\"$val\"";
} }
} }

View file

@ -32,6 +32,17 @@ if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
uses('view'.DS.'helpers'.DS.'app_helper', 'controller'.DS.'controller', 'model'.DS.'model', 'view'.DS.'helper', 'view'.DS.'helpers'.DS.'xml'); uses('view'.DS.'helpers'.DS.'app_helper', 'controller'.DS.'controller', 'model'.DS.'model', 'view'.DS.'helper', 'view'.DS.'helpers'.DS.'xml');
class TestXml extends Object {
var $content = '';
function __construct($content) {
$this->content = $content;
}
function toString() {
return $this->content;
}
}
/** /**
* Short description for class. * Short description for class.
* *
@ -71,6 +82,119 @@ class XmlHelperTest extends UnitTestCase {
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
} }
function testRenderElementWithNamespace() {
$result = $this->Xml->elem('count', array('namespace' => 'myNameSpace'), 'content');
$expected = '<myNameSpace:count>content</count>';
$this->assertEqual($result, $expected);
$result = $this->Xml->elem('count', array('namespace' => 'myNameSpace'), 'content', false);
$expected = '<myNameSpace:count>content';
$this->assertEqual($result, $expected);
}
function testSerialize() {
$data = array(
'test1' => 'test with no quotes',
'test2' => 'test with "double quotes"'
);
$result = $this->Xml->serialize($data);
$expected = '<std_class test1="test with no quotes" test2="test with \"double quotes\"" />';
$this->assertIdentical($result, $expected);
$data = array(
'test1' => 'test with no quotes',
'test2' => 'test without double quotes'
);
$result = $this->Xml->serialize($data);
$expected = '<std_class test1="test with no quotes" test2="test without double quotes" />';
$this->assertIdentical($result, $expected);
}
function testHeader() {
$attrib = array();
$result = $this->Xml->header($attrib);
$expected = '<?xml version="1.0" encoding="UTF-8" ?>';
$this->assertIdentical($result, $expected);
$attrib = array(
'encoding' => 'UTF-8',
'version' => '1.1'
);
$result = $this->Xml->header($attrib);
$expected = '<?xml version="1.1" encoding="UTF-8" ?>';
$this->assertIdentical($result, $expected);
$attrib = array(
'encoding' => 'UTF-8',
'version' => '1.2',
'additional' => 'attribute'
);
$result = $this->Xml->header($attrib);
$expected = '<?xml version="1.2" encoding="UTF-8" additional="attribute" ?>';
$this->assertIdentical($result, $expected);
$attrib = 'encoding="UTF-8" someOther="value"';
$result = $this->Xml->header($attrib);
$expected = '<?xml encoding="UTF-8" someOther="value" ?>';
$this->assertIdentical($result, $expected);
}
function test__ComposeContent() {
$content = 'some String';
$result = $this->Xml->__composeContent($content);
$expected = 'some String';
$this->assertIdentical($result, $expected);
$content = array('some String', 'some Other String');
$result = $this->Xml->__composeContent($content);
$expected = '<some String /><some Other String />';
$this->assertIdentical($result, $expected);
$content = array(1, 'some Other String');
$result = $this->Xml->__composeContent($content);
$expected = '1<some Other String />';
$this->assertIdentical($result, $expected);
$content = array(
array('some String'),
array('some Other String')
);
$result = $this->Xml->__composeContent($content);
$expected = '<some String /><some Other String />';
$this->assertIdentical($result, $expected);
$content = array(
array(array('some String')),
array(('some Other String'))
);
$result = $this->Xml->__composeContent($content);
$this->assertError();
$xml = new Xml(null, array());
$result = $xml->load('<para><note>simple note</note></para>');
$result = $this->Xml->__composeContent($xml);
$expected = '<para><note><![CDATA[simple note]]></note></para>';
$this->assertIdentical($result, $expected);
$xml = new TestXml('<para><note>simple note</note></para>');
$result = $this->Xml->__composeContent($xml);
$expected = '<para><note>simple note</note></para>';
$this->assertIdentical($result, $expected);
}
function test__prepareNamespaces() {
$this->Xml->__namespaces = array('namespace1', 'namespace2');
$result = $this->Xml->__prepareNamespaces();
$expected = array('xmlns:0' => 'namespace1', 'xmlns:1' => 'namespace2');
$this->assertIdentical($result, $expected);
$this->Xml->__namespaces = array();
$result = $this->Xml->__prepareNamespaces();
$expected = array();
$this->assertIdentical($result, $expected);
}
function tearDown() { function tearDown() {
unset($this->Xml); unset($this->Xml);
} }