diff --git a/lib/Cake/Test/Case/View/JsonViewTest.php b/lib/Cake/Test/Case/View/JsonViewTest.php new file mode 100644 index 000000000..a560ef085 --- /dev/null +++ b/lib/Cake/Test/Case/View/JsonViewTest.php @@ -0,0 +1,78 @@ + + * 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('JsonView', 'View'); + +/** + * JsonViewTest + * + * @package Cake.Test.Case.View + */ +class JsonViewTest extends CakeTestCase { + +/** + * testRenderWithoutView method + * + * @return void + */ + public function testRenderWithoutView() { + $request = new CakeRequest(); + $response = new CakeResponse(); + $controller = new Controller($request, $response); + $data = array('user' => 'fake', 'list' => array('item1', 'item2')); + $controller->set('serialize', $data); + $view = new JsonView($controller); + $output = $view->render(false); + + $this->assertIdentical(json_encode($data), $output); + $this->assertIdentical('application/json', $response->type()); + } + +/** + * testRenderWithView method + * + * @return void + */ + public function testRenderWithView() { + App::build(array('View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Json'))); + $request = new CakeRequest(); + $response = new CakeResponse(); + $controller = new Controller($request, $response); + $data = array( + 'User' => array( + 'username' => 'fake' + ), + 'Item' => array( + array('name' => 'item1'), + array('name' => 'item2') + ) + ); + $controller->set('user', $data); + $view = new JsonView($controller); + $output = $view->render('index'); + + $expected = json_encode(array('user' => 'fake', 'list' => array('item1', 'item2'))); + $this->assertIdentical($expected, $output); + $this->assertIdentical('application/json', $response->type()); + } + +} diff --git a/lib/Cake/Test/test_app/View/Json/index.ctp b/lib/Cake/Test/test_app/View/Json/index.ctp new file mode 100644 index 000000000..13e0269b5 --- /dev/null +++ b/lib/Cake/Test/test_app/View/Json/index.ctp @@ -0,0 +1,27 @@ + $user['User']['username'], + 'list' => array() +); +foreach ($user['Item'] as $item) { + $formatted['list'][] = $item['name']; +} + +$this->set('serialize', $formatted); diff --git a/lib/Cake/View/JsonView.php b/lib/Cake/View/JsonView.php index a058d03a1..2e590e8d9 100644 --- a/lib/Cake/View/JsonView.php +++ b/lib/Cake/View/JsonView.php @@ -1,7 +1,46 @@ {$var} = $controller->{$var}; + } + $this->response->type('json'); + } + Object::__construct(); + } + /** * Render a JSON view. * @@ -12,16 +51,16 @@ class JsonView extends View { * * @param string $view The view being rendered. * @param string $layout The layout being rendered. - * @return The rendered view. + * @return string The rendered view. */ public function render($view = null, $layout = null) { - if (isset($this->viewVars['serialize'])) { - $vars = array_intersect_key( - $this->viewVars, - array_flip($this->viewVars['serialize']) - ); - return json_encode($vars); + if ($view !== false && $viewFileName = $this->_getViewFileName($view)) { + $this->_render($viewFileName); } - return parent::render($view, $layout); + + $data = isset($this->viewVars['serialize']) ? $this->viewVars['serialize'] : null; + + return $this->output = json_encode($data); } + }