Not loading helpers and others stuffs not necessary to Json view. Tests added.

This commit is contained in:
Juan Basso 2011-10-28 22:01:33 -04:00 committed by mark_story
parent cfbc43671e
commit 873489053c
3 changed files with 152 additions and 8 deletions

View file

@ -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());
}
}

View file

@ -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 file

@ -1,7 +1,46 @@
<?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.
*
@ -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);
}
}