Skip to content

Commit

Permalink
Fix prefixed controllers not working with testAction().
Browse files Browse the repository at this point in the history
The generated controller class needs to be aware that it is in a prefix
and adjust the classname + viewPath accordingly.

Refs #3636
  • Loading branch information
markstory committed Jun 6, 2014
1 parent bb9e851 commit 7395d5d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/TestSuite/ControllerTestCase.php
Expand Up @@ -283,9 +283,16 @@ protected function _testAction($url = '', $options = array()) {
if ($this->_dirtyController) {
$this->controller = null;
}
$plugin = empty($request->params['plugin']) ? '' : Inflector::camelize($request->params['plugin']) . '.';
if ($this->controller === null && $this->autoMock) {
$this->generate($plugin . Inflector::camelize($request->params['controller']));
$plugin = '';
if (!empty($request->params['plugin'])) {
$plugin = Inflector::camelize($request->params['plugin']) . '.';
}
$controllerName = Inflector::camelize($request->params['controller']);
if (!empty($request->params['prefix'])) {
$controllerName = Inflector::camelize($request->params['prefix']) . '/' . $controllerName;
}
$this->generate($plugin . $controllerName, [], $request);
}
$params = array();
if ($options['return'] === 'result') {
Expand Down Expand Up @@ -313,9 +320,7 @@ protected function _testAction($url = '', $options = array()) {
}

/**
* Generates a mocked controller and mocks any classes passed to `$mocks`. By
* default, `stop()` is stubbed as is sending the response headers, so to not
* interfere with testing.
* Generates a mocked controller and mocks any classes passed to `$mocks`.
*
* ### Mocks:
*
Expand All @@ -329,11 +334,13 @@ protected function _testAction($url = '', $options = array()) {
*
* @param string $controller Controller name
* @param array $mocks List of classes and methods to mock
* @param \Cake\Network\Request $request A request object to build the controller with.
* This parameter is required when mocking prefixed controllers.
* @return \Cake\Controller\Controller Mocked controller
* @throws \Cake\Controller\Error\MissingControllerException When controllers could not be created.
* @throws \Cake\Controller\Error\MissingComponentException When components could not be created.
*/
public function generate($controller, array $mocks = array()) {
public function generate($controller, array $mocks = array(), $request = null) {
$className = App::className($controller, 'Controller', 'Controller');
if (!$className) {
list($plugin, $controller) = pluginSplit($controller);
Expand All @@ -343,15 +350,15 @@ public function generate($controller, array $mocks = array()) {
));
}

$mocks = array_merge(array(
$mocks += [
'methods' => null,
'models' => array(),
'components' => array()
), $mocks);
'models' => [],
'components' => []
];
list(, $controllerName) = namespaceSplit($className);
$name = substr($controllerName, 0, -10);

$request = $this->getMock('Cake\Network\Request');
$request = $request ?: $this->getMock('Cake\Network\Request');
$response = $this->getMock('Cake\Network\Response', array('_sendHeader', 'stop'));
$controller = $this->getMock(
$className,
Expand Down
18 changes: 18 additions & 0 deletions tests/TestCase/TestSuite/ControllerTestCaseTest.php
Expand Up @@ -198,6 +198,24 @@ public function testTestAction() {
$this->assertEquals($expected, $results);
}

/**
* Test testAction() with prefix routes.
*
* @return void
*/
public function testActionWithPrefix() {
Configure::write('Routing.prefixes', ['admin']);
Plugin::load('TestPlugin');

$result = $this->Case->testAction('/admin/posts/index', ['return' => 'view']);
$expected = '<h1>Admin Post Index</h1>';
$this->assertContains($expected, $result);

$result = $this->Case->testAction('/admin/test_plugin/comments/index', ['return' => 'view']);
$expected = '<h1>TestPlugin Admin Comments</h1>';
$this->assertContains($expected, $result);
}

/**
* Make sure testAction() can hit plugin controllers.
*
Expand Down
@@ -0,0 +1 @@
<h1>TestPlugin Admin Comments</h1>
1 change: 1 addition & 0 deletions tests/test_app/TestApp/Template/Admin/Posts/index.ctp
@@ -0,0 +1 @@
<h1>Admin Post Index</h1>

0 comments on commit 7395d5d

Please sign in to comment.