Permalink
Browse files

Not loading helpers and others stuffs not necessary to Json view. Tes…

…ts added.
  • Loading branch information...
1 parent cfbc436 commit 873489053c00ac814222490b8bd8aeefd47e7940 @jrbasso jrbasso committed with markstory Oct 29, 2011
Showing with 152 additions and 8 deletions.
  1. +78 −0 lib/Cake/Test/Case/View/JsonViewTest.php
  2. +27 −0 lib/Cake/Test/test_app/View/Json/index.ctp
  3. +47 −8 lib/Cake/View/JsonView.php
@@ -0,0 +1,78 @@
+<?php
+/**
+ * JsonViewTest file
+ *
+ * PHP 5
+ *
+ * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
+ * 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());
+ }
+
+}
@@ -0,0 +1,27 @@
+<?php
+/**
+ *
+ * PHP 5
+ *
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * 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://cakephp.org CakePHP(tm) Project
+ * @package Cake.Test.test_app.View.Json
+ * @since CakePHP(tm) v 2.1.0
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+
+$formatted = array(
+ 'user' => $user['User']['username'],
+ 'list' => array()
+);
+foreach ($user['Item'] as $item) {
+ $formatted['list'][] = $item['name'];
+}
+
+$this->set('serialize', $formatted);
View
@@ -1,8 +1,47 @@
<?php
+/**
+ * A custom view class that is used for JSON responses
+ *
+ * PHP 5
+ *
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * 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://cakephp.org CakePHP(tm) Project
+ * @package Cake.View
+ * @since CakePHP(tm) v 2.1.0
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+
+App::uses('View', 'View');
+/**
+ * JsonView
+ *
+ * @package Cake.View
+ */
class JsonView extends View {
/**
+ * Constructor
+ *
+ * @param Controller $controller
+ */
+ public function __construct($controller) {
+ if (is_object($controller)) {
+ foreach (array('viewVars', 'viewPath', 'view', 'response') as $var) {
+ $this->{$var} = $controller->{$var};
+ }
+ $this->response->type('json');
+ }
+ Object::__construct();
+ }
+
+/**
* Render a JSON view.
*
* Uses the special 'serialize' parameter to convert a set of
@@ -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);
}
+
}

0 comments on commit 8734890

Please sign in to comment.