From a6c1aafcc43d4bf6393c8b8243144022d0856b6e Mon Sep 17 00:00:00 2001 From: Juan Basso Date: Sat, 29 Oct 2011 16:51:44 -0400 Subject: [PATCH] Added XmlView. --- lib/Cake/Test/Case/View/XmlViewTest.php | 86 +++++++++++++++++++ .../test_app/View/Layouts/xml/xml_view.ctp | 4 + lib/Cake/Test/test_app/View/Xml/index.ctp | 3 + lib/Cake/View/XmlView.php | 62 +++++++++++++ 4 files changed, 155 insertions(+) create mode 100644 lib/Cake/Test/Case/View/XmlViewTest.php create mode 100644 lib/Cake/Test/test_app/View/Layouts/xml/xml_view.ctp create mode 100644 lib/Cake/Test/test_app/View/Xml/index.ctp create mode 100644 lib/Cake/View/XmlView.php diff --git a/lib/Cake/Test/Case/View/XmlViewTest.php b/lib/Cake/Test/Case/View/XmlViewTest.php new file mode 100644 index 00000000000..0defd751913 --- /dev/null +++ b/lib/Cake/Test/Case/View/XmlViewTest.php @@ -0,0 +1,86 @@ + + * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) + * + * Licensed under The MIT License + * Redistributions of files must retain the above copyright notice + * + * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) + * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests + * @package Cake.Test.Case.View + * @since CakePHP(tm) v 2.1.0 + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) + */ + +App::uses('Controller', 'Controller'); +App::uses('CakeRequest', 'Network'); +App::uses('CakeResponse', 'Network'); +App::uses('XmlView', 'View'); + +/** + * XmlViewTest + * + * @package Cake.Test.Case.View + */ +class XmlViewTest extends CakeTestCase { + +/** + * testRenderWithoutView method + * + * @return void + */ + public function testRenderWithoutView() { + $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); + + $expected = 'user1user2'; + $this->assertIdentical($expected, str_replace(array("\r", "\n"), '', $output)); + $this->assertIdentical('application/xml', $response->type()); + } + +/** + * testRenderWithView method + * + * @return void + */ + 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); + $data = array( + array( + 'User' => array( + 'username' => 'user1' + ) + ), + array( + 'User' => array( + 'username' => 'user2' + ) + ) + ); + $controller->set('users', $data); + $view = new XmlView($controller); + $output = $view->render('index', 'xml/xml_view'); + + $expected = 'user1user2'; + $this->assertIdentical($expected, str_replace(array("\r", "\n"), '', $output)); + $this->assertIdentical('application/xml', $response->type()); + $this->assertInstanceOf('HelperCollection', $view->Helpers); + } + +} diff --git a/lib/Cake/Test/test_app/View/Layouts/xml/xml_view.ctp b/lib/Cake/Test/test_app/View/Layouts/xml/xml_view.ctp new file mode 100644 index 00000000000..d57f328fdd9 --- /dev/null +++ b/lib/Cake/Test/test_app/View/Layouts/xml/xml_view.ctp @@ -0,0 +1,4 @@ + + + + diff --git a/lib/Cake/Test/test_app/View/Xml/index.ctp b/lib/Cake/Test/test_app/View/Xml/index.ctp new file mode 100644 index 00000000000..1a91fca019f --- /dev/null +++ b/lib/Cake/Test/test_app/View/Xml/index.ctp @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/lib/Cake/View/XmlView.php b/lib/Cake/View/XmlView.php new file mode 100644 index 00000000000..a948180b3e6 --- /dev/null +++ b/lib/Cake/View/XmlView.php @@ -0,0 +1,62 @@ +response->type('xml'); + } + } + +/** + * Render a XML view. + * + * Uses the special 'serialize' parameter to convert a set of + * view variables into a XML response. Makes generating simple + * XML responses very easy. You can omit the 'serialize' parameter, + * and use a normal view + layout as well. + * + * @param string $view The view being rendered. + * @param string $layout The layout being rendered. + * @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(); + } + return parent::render($view, $layout); + } + +}