Skip to content

Commit

Permalink
Add App::uses to baked controllers.
Browse files Browse the repository at this point in the history
Controllers should load their dependencies.
Bake should help with the best practice.
Fixes #1971
  • Loading branch information
markstory committed Sep 10, 2011
1 parent 7c08c96 commit 3cb3424
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
8 changes: 6 additions & 2 deletions lib/Cake/Console/Command/Task/ControllerTask.php
Expand Up @@ -290,7 +290,8 @@ public function bakeActions($controllerName, $admin = null, $wannaUseSession = t
$displayField = $modelObj->displayField;
$primaryKey = $modelObj->primaryKey;

$this->Template->set(compact('plugin', 'admin', 'controllerPath', 'pluralName', 'singularName',
$this->Template->set(compact(
'plugin', 'admin', 'controllerPath', 'pluralName', 'singularName',
'singularHumanName', 'pluralHumanName', 'modelObj', 'wannaUseSession', 'currentModelName',
'displayField', 'primaryKey'
));
Expand All @@ -312,7 +313,10 @@ public function bake($controllerName, $actions = '', $helpers = null, $component

$isScaffold = ($actions === 'scaffold') ? true : false;

$this->Template->set('plugin', $this->plugin);
$this->Template->set(array(
'plugin' => $this->plugin,
'pluginPath' => empty($this->plugin) ? '' : $this->plugin . '.'
));
$this->Template->set(compact('controllerName', 'actions', 'helpers', 'components', 'isScaffold'));
$contents = $this->Template->generate('classes', 'controller');

Expand Down
1 change: 1 addition & 0 deletions lib/Cake/Console/Templates/default/classes/controller.ctp
Expand Up @@ -20,6 +20,7 @@
*/

echo "<?php\n";
echo "App::uses('{$plugin}AppController', '{$pluginPath}Controller');\n";
?>
/**
* <?php echo $controllerName; ?> Controller
Expand Down
Expand Up @@ -315,15 +315,18 @@ public function testBakeWithPlugin() {
$this->Task->expects($this->at(3))->method('createFile')->with(
$path,
new PHPUnit_Framework_Constraint_PCREMatch('/ArticlesController extends ControllerTestAppController/')
);
)->will($this->returnValue(true));

$this->Task->bake('Articles', '--actions--', array(), array(), array());

$this->Task->plugin = 'ControllerTest';
$path = APP . 'Plugin' . DS . 'ControllerTest' . DS . 'Controller' . DS . 'ArticlesController.php';
$this->Task->bake('Articles', '--actions--', array(), array(), array());
$result = $this->Task->bake('Articles', '--actions--', array(), array(), array());

$this->assertContains("App::uses('ControllerTestAppController', 'ControllerTest.Controller');", $result);
$this->assertEquals('ControllerTest', $this->Task->Template->templateVars['plugin']);
$this->assertEquals('ControllerTest.', $this->Task->Template->templateVars['pluginPath']);

$this->assertEqual($this->Task->Template->templateVars['plugin'], 'ControllerTest');
CakePlugin::unload();
}

Expand Down

0 comments on commit 3cb3424

Please sign in to comment.