Permalink
Browse files

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
  • Loading branch information...
1 parent b165acd commit c5ca10ca6932b76f505fec39f6e78b34dfcc5b9e @markstory markstory committed Oct 22, 2011
@@ -259,6 +259,7 @@ public function testTestAction() {
$result = $this->Case->controller->response->body();
$this->assertPattern('/This is the TestsAppsController index view/', $result);
+ $Controller = $this->Case->generate('TestsApps');
$this->Case->testAction('/tests_apps/redirect_to');
$results = $this->Case->headers;
$expected = array(
@@ -341,7 +342,7 @@ public function testBCSetReturn() {
$result = $this->Case->testAction('/tests_apps/set_action', array(
'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(
'return' => 'contents'
@@ -464,7 +465,7 @@ public function testNoMocking() {
$result = $this->Case->testAction('/tests_apps/set_action', array(
'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(
'return' => 'contents'
@@ -474,4 +475,35 @@ public function testNoMocking() {
$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);
+ }
+
}
@@ -21,6 +21,11 @@ class TestsAppsController extends AppController {
public $uses = array();
public function index() {
+ $var = '';
+ if (isset($this->request->query['var'])) {
+ $var = $this->request->query['var'];
+ }
+ $this->set('var', $var);
}
public function some_method() {
@@ -1 +1 @@
-This is the TestsAppsController index view
+This is the TestsAppsController index view <?php echo isset($var) ? $var : ''; ?>
@@ -157,6 +157,15 @@ public function afterRender($viewFile) {
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
* framework thinking that it's a test case
*
@@ -217,9 +226,10 @@ protected function _testAction($url = '', $options = array()) {
$this->headers = Router::currentRoute()->response->header();
return;
}
- if ($this->controller !== null && Inflector::camelize($request->params['controller']) !== $this->controller->name) {
+ if ($this->__dirtyController) {
$this->controller = null;
}
+
$plugin = empty($request->params['plugin']) ? '' : Inflector::camelize($request->params['plugin']) . '.';
if ($this->controller === null && $this->autoMock) {
$this->generate(Inflector::camelize($plugin . $request->params['controller']));
@@ -241,6 +251,7 @@ protected function _testAction($url = '', $options = array()) {
}
$this->contents = $this->controller->response->body();
}
+ $this->__dirtyController = true;
$this->headers = $Dispatch->response->header();
return $this->{$options['return']};
}
@@ -324,12 +335,13 @@ public function generate($controller, $mocks = array()) {
throw new MissingComponentException(array(
'class' => $componentClass
));
- }
+ }
$_component = $this->getMock($componentClass, $methods, array(), '', false);
$_controller->Components->set($name, $_component);
}
$_controller->constructClasses();
+ $this->__dirtyController = false;
$this->controller = $_controller;
return $this->controller;

0 comments on commit c5ca10c

Please sign in to comment.