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
*/
public function testRenderWithoutView() {
$request = new CakeRequest();
$response = new CakeResponse();
$controller = new Controller($request, $response);
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
$data = array('users' => array('user' => array('user1', 'user2')));
$controller->set('serialize', $data);
$view = new XmlView($controller);
$output = $view->render(false);
$Controller->set(array('users' => $data, 'serialize' => 'users'));
$View = new XmlView($Controller);
$output = $View->render(false);
$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('application/xml', $response->type());
$this->assertIdentical('application/xml', $Response->type());
}
/**
@ -55,12 +55,13 @@ class XmlViewTest extends CakeTestCase {
*/
public function testRenderWithView() {
App::build(array('View' => array(
CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Xml' . DS,
CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS
)));
$request = new CakeRequest();
$response = new CakeResponse();
$controller = new Controller($request, $response);
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
$Controller->name = $Controller->viewPath = 'Posts';
$data = array(
array(
'User' => array(
@ -73,14 +74,14 @@ class XmlViewTest extends CakeTestCase {
)
)
);
$controller->set('users', $data);
$view = new XmlView($controller);
$output = $view->render('index', 'xml/xml_view');
$Controller->set('users', $data);
$View = new XmlView($Controller);
$output = $View->render('index', 'xml/xml_view');
$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('application/xml', $response->type());
$this->assertInstanceOf('HelperCollection', $view->Helpers);
$this->assertIdentical('application/xml', $Response->type());
$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

@ -331,4 +331,4 @@ class Xml {
}
}
}
}

View file

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