Browse files

XmlView: configure top level element name

Allows the root element of the xml output to be changed by setting the
view variable '_rootElement'.
  • Loading branch information...
1 parent 54056fd commit d623d9ef21f8eb7e978cf50b5d4cbd619b73ed2a @larryb82 committed Nov 2, 2012
Showing with 19 additions and 22 deletions.
  1. +18 −20 lib/Cake/Test/Case/View/XmlViewTest.php
  2. +1 −2 lib/Cake/View/XmlView.php
View
38 lib/Cake/Test/Case/View/XmlViewTest.php
@@ -35,14 +35,6 @@ class XmlViewTest extends CakeTestCase {
* @return void
*/
public function testRenderWithoutView() {
- Configure::write('XmlView.RootNodeName', null);
- $this->__testRenderWithoutView('response');
-
- Configure::write('XmlView.RootNodeName', 'custom_name');
- $this->__testRenderWithoutView('custom_name');
- }
-
- private function __testRenderWithoutView($rootNodeName) {
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
@@ -70,40 +62,46 @@ private function __testRenderWithoutView($rootNodeName) {
$View = new XmlView($Controller);
$output = $View->render(false);
- $expected = Xml::build(array($rootNodeName => array('users' => $data)))->asXML();
+ $expected = Xml::build(array('response' => array('users' => $data)))->asXML();
$this->assertSame($expected, $output);
- }
+ $Controller->set('_rootElement', 'custom_name');
+ $View = new XmlView($Controller);
+ $output = $View->render(false);
+
+ $expected = Xml::build(array('custom_name' => array('users' => $data)))->asXML();
+ $this->assertSame($expected, $output);
+ }
/**
* Test render with an array in _serialize
*
* @return void
*/
public function testRenderWithoutViewMultiple() {
- Configure::write('XmlView.RootNodeName', null);
- $this->__testRenderWithoutViewMultiple('response');
-
- Configure::write('XmlView.RootNodeName', 'custom_name');
- $this->__testRenderWithoutViewMultiple('custom_name');
- }
-
- private function __testRenderWithoutViewMultiple($rootNodeName) {
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
$data = array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2'));
$Controller->set($data);
$Controller->set('_serialize', array('no', 'user'));
$View = new XmlView($Controller);
+ $this->assertSame('application/xml', $Response->type());
$output = $View->render(false);
+ $expected = array(
+ 'response' => array('no' => $data['no'], 'user' => $data['user'])
+ );
+ $this->assertSame(Xml::build($expected)->asXML(), $output);
+
+ $Controller->set('_rootElement', 'custom_name');
+ $View = new XmlView($Controller);
+ $output = $View->render(false);
$expected = array(
- $rootNodeName => array('no' => $data['no'], 'user' => $data['user'])
+ 'custom_name' => array('no' => $data['no'], 'user' => $data['user'])
);
$this->assertSame(Xml::build($expected)->asXML(), $output);
- $this->assertSame('application/xml', $Response->type());
}
/**
View
3 lib/Cake/View/XmlView.php
@@ -99,8 +99,7 @@ public function render($view = null, $layout = null) {
* @return string The serialized data
*/
protected function _serialize($serialize) {
- $root = Configure::read('XmlView.RootNodeName');
- $root = empty($root) ? 'response' : $root;
+ $root = isset($this->viewVars['_rootElement']) ? $this->viewVars['_rootElement'] : 'response';
if (is_array($serialize)) {
$data = array($root => array());

0 comments on commit d623d9e

Please sign in to comment.