Moving response construction into Controller, this will let controller classes more easily modify the response subclass they want to use.

Test case updated.
This commit is contained in:
mark_story 2010-08-22 12:31:55 -04:00
parent 44f1687f5b
commit 7221f9c3b9
2 changed files with 44 additions and 11 deletions

View file

@ -118,6 +118,21 @@ class Controller extends Object {
*/ */
public $request; public $request;
/**
* An instance of a CakeResponse object that contains information about the impending response
*
* @var CakeResponse
*/
public $response;
/**
* The classname to use for creating the response object.
*
* @var string
*/
protected $_responseClass = 'CakeResponse';
/** /**
* Holds pagination defaults for controller actions. The keys that can be included * Holds pagination defaults for controller actions. The keys that can be included
* in this array are: 'conditions', 'fields', 'order', 'limit', 'page', and 'recursive', * in this array are: 'conditions', 'fields', 'order', 'limit', 'page', and 'recursive',
@ -322,9 +337,8 @@ class Controller extends Object {
* *
* @param CakeRequest $request Request object for this controller can be null for testing. * @param CakeRequest $request Request object for this controller can be null for testing.
* But expect that features that use the params will not work. * But expect that features that use the params will not work.
* @param CakeResponse $response Response object for this controller
*/ */
public function __construct($request = null, $response = null) { public function __construct($request = null) {
if ($this->name === null) { if ($this->name === null) {
$r = null; $r = null;
if (!preg_match('/(.*)Controller/i', get_class($this), $r)) { if (!preg_match('/(.*)Controller/i', get_class($this), $r)) {
@ -349,7 +363,7 @@ class Controller extends Object {
if ($request instanceof CakeRequest) { if ($request instanceof CakeRequest) {
$this->_setRequest($request); $this->_setRequest($request);
} }
$this->response = $response; $this->getResponse();
parent::__construct(); parent::__construct();
} }
@ -501,6 +515,18 @@ class Controller extends Object {
return true; return true;
} }
/**
* Gets the response object for this controller. Will construct the response if it has not already been built.
*
* @return CakeResponse
*/
public function getResponse() {
if (empty($this->response)) {
$this->response = new $this->_responseClass(array('charset' => Configure::read('App.encoding')));
}
return $this->response;
}
/** /**
* Perform the startup process for this controller. * Perform the startup process for this controller.
* Fire the Component and Controller callbacks in the correct order. * Fire the Component and Controller callbacks in the correct order.

View file

@ -856,7 +856,8 @@ class ControllerTest extends CakeTestCase {
function testFlash() { function testFlash() {
$request = new CakeRequest('controller_posts/index'); $request = new CakeRequest('controller_posts/index');
$Controller = new Controller($request, $this->getMock('CakeResponse', array('_sendHeader'))); $Controller = new Controller($request);
$Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
$Controller->flash('this should work', '/flash'); $Controller->flash('this should work', '/flash');
$result = $Controller->response->body(); $result = $Controller->response->body();
@ -880,7 +881,8 @@ class ControllerTest extends CakeTestCase {
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
App::build(array('views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS))); App::build(array('views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)));
$Controller = new Controller(null, $this->getMock('CakeResponse', array('_sendHeader'))); $Controller = new Controller(null);
$Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
$Controller->flash('this should work', '/flash', 1, 'ajax2'); $Controller->flash('this should work', '/flash', 1, 'ajax2');
$result = $Controller->response->body(); $result = $Controller->response->body();
$this->assertPattern('/Ajax!/', $result); $this->assertPattern('/Ajax!/', $result);
@ -1030,11 +1032,12 @@ class ControllerTest extends CakeTestCase {
* @return void * @return void
*/ */
function testRedirectByCode($code, $msg) { function testRedirectByCode($code, $msg) {
$Controller = new Controller(null, $this->getMock('CakeResponse', array('header', 'statusCode'))); $Controller = new Controller(null);
$Controller->response = $this->getMock('CakeResponse', array('header', 'statusCode'));
$Controller->Component = new Component(); $Controller->Component = new Component();
$Controller->Component->init($Controller); $Controller->Component->init($Controller);
$Controller->response->expects($this->once())->method('statusCode') $Controller->response->expects($this->once())->method('statusCode')
->with($code); ->with($code);
$Controller->response->expects($this->once())->method('header') $Controller->response->expects($this->once())->method('header')
@ -1051,7 +1054,8 @@ class ControllerTest extends CakeTestCase {
* @return void * @return void
*/ */
function testRedirectByMessage($code, $msg) { function testRedirectByMessage($code, $msg) {
$Controller = new Controller(null, $this->getMock('CakeResponse', array('header', 'statusCode'))); $Controller = new Controller(null);
$Controller->response = $this->getMock('CakeResponse', array('header', 'statusCode'));
$Controller->Component = new Component(); $Controller->Component = new Component();
$Controller->Component->init($Controller); $Controller->Component->init($Controller);
@ -1072,7 +1076,8 @@ class ControllerTest extends CakeTestCase {
* @return void * @return void
*/ */
function testRedirectTriggeringComponentsReturnNull() { function testRedirectTriggeringComponentsReturnNull() {
$Controller = new Controller(null, $this->getMock('CakeResponse', array('header', 'statusCode'))); $Controller = new Controller(null);
$Controller->response = $this->getMock('CakeResponse', array('header', 'statusCode'));
$Controller->Component = $this->getMock('Component'); $Controller->Component = $this->getMock('Component');
$Controller->Component->expects($this->once())->method('beforeRedirect')->will($this->returnValue(null)); $Controller->Component->expects($this->once())->method('beforeRedirect')->will($this->returnValue(null));
@ -1092,7 +1097,8 @@ class ControllerTest extends CakeTestCase {
* @return void * @return void
*/ */
function testRedirectBeforeRedirectModifyingParams() { function testRedirectBeforeRedirectModifyingParams() {
$Controller = new Controller(null, $this->getMock('CakeResponse', array('header', 'statusCode'))); $Controller = new Controller(null);
$Controller->response = $this->getMock('CakeResponse', array('header', 'statusCode'));
$Controller->Component = $this->getMock('Component'); $Controller->Component = $this->getMock('Component');
$Controller->Component->expects($this->once())->method('beforeRedirect') $Controller->Component->expects($this->once())->method('beforeRedirect')
@ -1414,7 +1420,8 @@ class ControllerTest extends CakeTestCase {
* @return void * @return void
*/ */
function testControllerHttpCodes() { function testControllerHttpCodes() {
$Controller = new Controller(null, $this->getMock('CakeResponse', array('httpCodes'))); $Controller = new Controller(null);
$Controller->response = $this->getMock('CakeResponse', array('httpCodes'));
$Controller->response->expects($this->at(0))->method('httpCodes')->with(null); $Controller->response->expects($this->at(0))->method('httpCodes')->with(null);
$Controller->response->expects($this->at(1))->method('httpCodes')->with(100); $Controller->response->expects($this->at(1))->method('httpCodes')->with(100);
$Controller->httpCodes(); $Controller->httpCodes();