Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make XmlView consistent with JsonView.

Both allow a view script or a serialize key with the name
of the variable to be serialized.
  • Loading branch information...
commit c1f5dc1e40f2c2d691990ec4baf47bcd1b412ac1 1 parent 5e9b158
@markstory markstory authored
View
33 lib/Cake/Test/Case/View/XmlViewTest.php
@@ -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 @@ public function testRenderWithoutView() {
*/
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 @@ public function testRenderWithView() {
)
)
);
- $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
4 lib/Cake/Test/test_app/View/Layouts/xml/xml_view.ctp
@@ -1,4 +0,0 @@
-<?php echo '<?xml version="1.0" encoding="UTF-8"?>' ?>
-<users>
-<?php echo $content_for_layout; ?>
-</users>
View
6 lib/Cake/Test/test_app/View/Posts/xml/index.ctp
@@ -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
3  lib/Cake/Test/test_app/View/Xml/index.ctp
@@ -1,3 +0,0 @@
-<?php foreach ($users as $user): ?>
-<user><?php echo $user['User']['username']; ?></user>
-<?php endforeach; ?>
View
2  lib/Cake/Utility/Xml.php
@@ -331,4 +331,4 @@ protected static function _toArray($xml, &$parentData, $ns, $namespaces) {
}
}
-}
+}
View
17 lib/Cake/View/XmlView.php
@@ -40,6 +40,13 @@
class XmlView extends View {
/**
+ * The subdirectory. XML views are always in xml.
+ *
+ * @var string
+ */
+ public $subDir = 'xml';
+
+/**
* Constructor
*
* @param Controller $controller
@@ -65,10 +72,14 @@ public function __construct($controller) {
* @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);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.