Make XmlView consistent with JsonView.

Both allow a view script or a serialize key with the name
of the variable to be serialized.
This commit is contained in:
mark_story 2011-11-07 22:40:39 -05:00
parent 5e9b1583e5
commit c1f5dc1e40
6 changed files with 38 additions and 27 deletions

View file

@ -35,17 +35,17 @@ class XmlViewTest extends CakeTestCase {
* @return void * @return void
*/ */
public function testRenderWithoutView() { public function testRenderWithoutView() {
$request = new CakeRequest(); $Request = new CakeRequest();
$response = new CakeResponse(); $Response = new CakeResponse();
$controller = new Controller($request, $response); $Controller = new Controller($Request, $Response);
$data = array('users' => array('user' => array('user1', 'user2'))); $data = array('users' => array('user' => array('user1', 'user2')));
$controller->set('serialize', $data); $Controller->set(array('users' => $data, 'serialize' => 'users'));
$view = new XmlView($controller); $View = new XmlView($Controller);
$output = $view->render(false); $output = $View->render(false);
$expected = '<?xml version="1.0" encoding="UTF-8"?><users><user>user1</user><user>user2</user></users>'; $expected = '<?xml version="1.0" encoding="UTF-8"?><users><user>user1</user><user>user2</user></users>';
$this->assertIdentical($expected, str_replace(array("\r", "\n"), '', $output)); $this->assertIdentical($expected, str_replace(array("\r", "\n"), '', $output));
$this->assertIdentical('application/xml', $response->type()); $this->assertIdentical('application/xml', $Response->type());
} }
/** /**
@ -55,12 +55,13 @@ class XmlViewTest extends CakeTestCase {
*/ */
public function testRenderWithView() { public function testRenderWithView() {
App::build(array('View' => array( App::build(array('View' => array(
CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Xml' . DS,
CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS
))); )));
$request = new CakeRequest(); $Request = new CakeRequest();
$response = new CakeResponse(); $Response = new CakeResponse();
$controller = new Controller($request, $response); $Controller = new Controller($Request, $Response);
$Controller->name = $Controller->viewPath = 'Posts';
$data = array( $data = array(
array( array(
'User' => array( 'User' => array(
@ -73,14 +74,14 @@ class XmlViewTest extends CakeTestCase {
) )
) )
); );
$controller->set('users', $data); $Controller->set('users', $data);
$view = new XmlView($controller); $View = new XmlView($Controller);
$output = $view->render('index', 'xml/xml_view'); $output = $View->render('index', 'xml/xml_view');
$expected = '<?xml version="1.0" encoding="UTF-8"?><users><user>user1</user><user>user2</user></users>'; $expected = '<?xml version="1.0" encoding="UTF-8"?><users><user>user1</user><user>user2</user></users>';
$this->assertIdentical($expected, str_replace(array("\r", "\n"), '', $output)); $this->assertIdentical($expected, str_replace(array("\r", "\n"), '', $output));
$this->assertIdentical('application/xml', $response->type()); $this->assertIdentical('application/xml', $Response->type());
$this->assertInstanceOf('HelperCollection', $view->Helpers); $this->assertInstanceOf('HelperCollection', $View->Helpers);
} }
} }

View file

@ -1,4 +0,0 @@
<?php echo '<?xml version="1.0" encoding="UTF-8"?>' ?>
<users>
<?php echo $content_for_layout; ?>
</users>

View file

@ -0,0 +1,6 @@
<?php
$data = array('users' => array('user' => array()));
foreach ($users as $user) {
$data['users']['user'][] = array('@' => $user['User']['username']);
}
echo Xml::fromArray($data)->saveXml();

View file

@ -1,3 +0,0 @@
<?php foreach ($users as $user): ?>
<user><?php echo $user['User']['username']; ?></user>
<?php endforeach; ?>

View file

@ -39,6 +39,13 @@ App::uses('Xml', 'Utility');
*/ */
class XmlView extends View { class XmlView extends View {
/**
* The subdirectory. XML views are always in xml.
*
* @var string
*/
public $subDir = 'xml';
/** /**
* Constructor * Constructor
* *
@ -65,10 +72,14 @@ class XmlView extends View {
* @return string The rendered view. * @return string The rendered view.
*/ */
public function render($view = null, $layout = null) { public function render($view = null, $layout = null) {
if (isset($this->viewVars['serialize']) && is_array($this->viewVars['serialize'])) { if (isset($this->viewVars['serialize'])) {
return $this->output = Xml::fromArray($this->viewVars['serialize'])->asXML(); $serialize = $this->viewVars['serialize'];
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
return $this->output = Xml::fromArray($data)->asXML();
}
if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
return $this->output = $this->_render($viewFileName);
} }
return parent::render($view, $layout);
} }
} }