Skip to content

Commit

Permalink
Moving response construction into Controller, this will let controlle…
Browse files Browse the repository at this point in the history
…r classes more easily modify the response subclass they want to use.

Test case updated.
  • Loading branch information
markstory committed Aug 22, 2010
1 parent 44f1687 commit 7221f9c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
32 changes: 29 additions & 3 deletions cake/libs/controller/controller.php
Expand Up @@ -118,6 +118,21 @@ class Controller extends Object {
*/
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
* in this array are: 'conditions', 'fields', 'order', 'limit', 'page', and 'recursive',
Expand Down Expand Up @@ -322,9 +337,8 @@ class Controller extends Object {
*
* @param CakeRequest $request Request object for this controller can be null for testing.
* 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) {
$r = null;
if (!preg_match('/(.*)Controller/i', get_class($this), $r)) {
Expand All @@ -349,7 +363,7 @@ public function __construct($request = null, $response = null) {
if ($request instanceof CakeRequest) {
$this->_setRequest($request);
}
$this->response = $response;
$this->getResponse();
parent::__construct();
}

Expand Down Expand Up @@ -501,6 +515,18 @@ public function constructClasses() {
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.
* Fire the Component and Controller callbacks in the correct order.
Expand Down
23 changes: 15 additions & 8 deletions cake/tests/cases/libs/controller/controller.test.php
Expand Up @@ -856,7 +856,8 @@ function testPaginateOrderVirtualField() {
function testFlash() {
$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');
$result = $Controller->response->body();

Expand All @@ -880,7 +881,8 @@ function testFlash() {
$this->assertEqual($result, $expected);

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');
$result = $Controller->response->body();
$this->assertPattern('/Ajax!/', $result);
Expand Down Expand Up @@ -1030,11 +1032,12 @@ public static function statusCodeProvider() {
* @return void
*/
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->init($Controller);

$Controller->response->expects($this->once())->method('statusCode')
->with($code);
$Controller->response->expects($this->once())->method('header')
Expand All @@ -1051,7 +1054,8 @@ function testRedirectByCode($code, $msg) {
* @return void
*/
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->init($Controller);
Expand All @@ -1072,7 +1076,8 @@ function testRedirectByMessage($code, $msg) {
* @return void
*/
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->expects($this->once())->method('beforeRedirect')->will($this->returnValue(null));
Expand All @@ -1092,7 +1097,8 @@ function testRedirectTriggeringComponentsReturnNull() {
* @return void
*/
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->expects($this->once())->method('beforeRedirect')
Expand Down Expand Up @@ -1414,7 +1420,8 @@ function testRequestHandlerPrefers(){
* @return void
*/
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(1))->method('httpCodes')->with(100);
$Controller->httpCodes();
Expand Down

0 comments on commit 7221f9c

Please sign in to comment.