Remove controller reuse between testAction() calls.

Reusing controllers between various testAction calls would
mean that calls that needed to render views, would break on the
second call to testAction().  Controllers are rebuilt for each testAction()
call now.

Fixes #2142
This commit is contained in:
mark_story 2011-10-22 10:58:21 -04:00
parent b165acd926
commit c5ca10ca69
4 changed files with 54 additions and 5 deletions

View file

@ -259,6 +259,7 @@ class ControllerTestCaseTest extends CakeTestCase {
$result = $this->Case->controller->response->body(); $result = $this->Case->controller->response->body();
$this->assertPattern('/This is the TestsAppsController index view/', $result); $this->assertPattern('/This is the TestsAppsController index view/', $result);
$Controller = $this->Case->generate('TestsApps');
$this->Case->testAction('/tests_apps/redirect_to'); $this->Case->testAction('/tests_apps/redirect_to');
$results = $this->Case->headers; $results = $this->Case->headers;
$expected = array( $expected = array(
@ -341,7 +342,7 @@ class ControllerTestCaseTest extends CakeTestCase {
$result = $this->Case->testAction('/tests_apps/set_action', array( $result = $this->Case->testAction('/tests_apps/set_action', array(
'return' => 'view' 'return' => 'view'
)); ));
$this->assertEquals($result, 'This is the TestsAppsController index view'); $this->assertEquals($result, 'This is the TestsAppsController index view string');
$result = $this->Case->testAction('/tests_apps/set_action', array( $result = $this->Case->testAction('/tests_apps/set_action', array(
'return' => 'contents' 'return' => 'contents'
@ -464,7 +465,7 @@ class ControllerTestCaseTest extends CakeTestCase {
$result = $this->Case->testAction('/tests_apps/set_action', array( $result = $this->Case->testAction('/tests_apps/set_action', array(
'return' => 'view' 'return' => 'view'
)); ));
$this->assertEquals($result, 'This is the TestsAppsController index view'); $this->assertEquals($result, 'This is the TestsAppsController index view string');
$result = $this->Case->testAction('/tests_apps/set_action', array( $result = $this->Case->testAction('/tests_apps/set_action', array(
'return' => 'contents' 'return' => 'contents'
@ -474,4 +475,35 @@ class ControllerTestCaseTest extends CakeTestCase {
$this->assertPattern('/<\/html>/', $result); $this->assertPattern('/<\/html>/', $result);
} }
/**
* Test that controllers don't get reused.
*
* @return void
*/
public function testNoControllerReuse() {
$result = $this->Case->testAction('/tests_apps/index', array(
'data' => array('var' => 'first call'),
'method' => 'get',
'return' => 'contents',
));
$this->assertContains('<html', $result);
$this->assertContains('This is the TestsAppsController index view', $result);
$this->assertContains('first call', $result);
$this->assertContains('</html>', $result);
$result = $this->Case->testAction('/tests_apps/index', array(
'data' => array('var' => 'second call'),
'method' => 'get',
'return' => 'contents'
));
$this->assertContains('second call', $result);
$result = $this->Case->testAction('/tests_apps/index', array(
'data' => array('var' => 'third call'),
'method' => 'get',
'return' => 'contents'
));
$this->assertContains('third call', $result);
}
} }

View file

@ -21,6 +21,11 @@ class TestsAppsController extends AppController {
public $uses = array(); public $uses = array();
public function index() { public function index() {
$var = '';
if (isset($this->request->query['var'])) {
$var = $this->request->query['var'];
}
$this->set('var', $var);
} }
public function some_method() { public function some_method() {

View file

@ -1 +1 @@
This is the TestsAppsController index view This is the TestsAppsController index view <?php echo isset($var) ? $var : ''; ?>

View file

@ -156,6 +156,15 @@ abstract class ControllerTestCase extends CakeTestCase {
*/ */
public $headers = null; public $headers = null;
/**
* Flag for checking if the controller instance is dirty.
* Once a test has been run on a controller it should be rebuilt
* to clean up properties.
*
* @var boolean
*/
private $__dirtyController = false;
/** /**
* Used to enable calling ControllerTestCase::testAction() without the testing * Used to enable calling ControllerTestCase::testAction() without the testing
* framework thinking that it's a test case * framework thinking that it's a test case
@ -217,9 +226,10 @@ abstract class ControllerTestCase extends CakeTestCase {
$this->headers = Router::currentRoute()->response->header(); $this->headers = Router::currentRoute()->response->header();
return; return;
} }
if ($this->controller !== null && Inflector::camelize($request->params['controller']) !== $this->controller->name) { if ($this->__dirtyController) {
$this->controller = null; $this->controller = null;
} }
$plugin = empty($request->params['plugin']) ? '' : Inflector::camelize($request->params['plugin']) . '.'; $plugin = empty($request->params['plugin']) ? '' : Inflector::camelize($request->params['plugin']) . '.';
if ($this->controller === null && $this->autoMock) { if ($this->controller === null && $this->autoMock) {
$this->generate(Inflector::camelize($plugin . $request->params['controller'])); $this->generate(Inflector::camelize($plugin . $request->params['controller']));
@ -241,6 +251,7 @@ abstract class ControllerTestCase extends CakeTestCase {
} }
$this->contents = $this->controller->response->body(); $this->contents = $this->controller->response->body();
} }
$this->__dirtyController = true;
$this->headers = $Dispatch->response->header(); $this->headers = $Dispatch->response->header();
return $this->{$options['return']}; return $this->{$options['return']};
} }
@ -330,6 +341,7 @@ abstract class ControllerTestCase extends CakeTestCase {
} }
$_controller->constructClasses(); $_controller->constructClasses();
$this->__dirtyController = false;
$this->controller = $_controller; $this->controller = $_controller;
return $this->controller; return $this->controller;