Skip to content

Commit

Permalink
Update BakeShell.
Browse files Browse the repository at this point in the history
Update bakeshell to use new task code and simplified internals.
  • Loading branch information
markstory committed Apr 1, 2014
1 parent a50a7e9 commit 6fb7ea7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 140 deletions.
125 changes: 25 additions & 100 deletions src/Console/Command/BakeShell.php
@@ -1,11 +1,5 @@
<?php
/**
* Command-line code generation utility to automate programmer chores.
*
* Bake is CakePHP's code generation script, which can help you kickstart
* application development by writing fully functional skeleton controllers,
* models, and views. Going further, Bake can also write Unit Tests for you.
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand Down Expand Up @@ -35,7 +29,7 @@
* application development by writing fully functional skeleton controllers,
* models, and views. Going further, Bake can also write Unit Tests for you.
*
* @link http://book.cakephp.org/2.0/en/console-and-shells/code-generation-with-bake.html
* @link http://book.cakephp.org/3.0/en/console-and-shells/code-generation-with-bake.html
*/
class BakeShell extends Shell {

Expand All @@ -44,7 +38,7 @@ class BakeShell extends Shell {
*
* @var array
*/
public $tasks = ['Project', 'DbConfig', 'Model', 'Controller', 'View', 'Plugin', 'Fixture', 'Test'];
public $tasks = ['Project', 'Model', 'Controller', 'View', 'Plugin', 'Fixture', 'Test'];

/**
* The connection being used.
Expand All @@ -64,7 +58,7 @@ public function startup() {
Cache::disable();

$task = Inflector::classify($this->command);
if (isset($this->{$task}) && !in_array($task, ['Project', 'DbConfig'])) {
if (isset($this->{$task}) && !in_array($task, ['Project'])) {
if (isset($this->params['connection'])) {
$this->{$task}->connection = $this->params['connection'];
}
Expand All @@ -80,62 +74,21 @@ public function startup() {
* @return mixed
*/
public function main() {
if (!is_dir($this->DbConfig->path)) {
$path = $this->Project->execute();
if (!empty($path)) {
$this->DbConfig->path = $path . 'Config/';
} else {
return false;
}
}

$connections = ConnectionManager::configured();
if (empty($connections)) {
$this->out(__d('cake_console', 'Your database configuration was not found. Take a moment to create one.'));
$this->args = null;
return $this->DbConfig->execute();
$this->out(__d('cake_console', 'Your database configuration was not found.'));
$this->out(__d('cake_console', 'Add your database connection information to App/Config/app.php.'));
return false;
}
$this->out(__d('cake_console', 'Interactive Bake Shell'));
$this->out(__d('cake_console', 'Bake Shell'));
$this->hr();
$this->out(__d('cake_console', '[D]atabase Configuration'));
$this->out(__d('cake_console', '[M]odel'));
$this->out(__d('cake_console', '[V]iew'));
$this->out(__d('cake_console', '[C]ontroller'));
$this->out(__d('cake_console', '[P]roject'));
$this->out(__d('cake_console', '[F]ixture'));
$this->out(__d('cake_console', '[T]est case'));
$this->out(__d('cake_console', '[Q]uit'));

$classToBake = strtoupper($this->in(__d('cake_console', 'What would you like to Bake?'), ['D', 'M', 'V', 'C', 'P', 'F', 'T', 'Q']));
switch ($classToBake) {
case 'D':
$this->DbConfig->execute();
break;
case 'M':
$this->Model->execute();
break;
case 'V':
$this->View->execute();
break;
case 'C':
$this->Controller->execute();
break;
case 'P':
$this->Project->execute();
break;
case 'F':
$this->Fixture->execute();
break;
case 'T':
$this->Test->execute();
break;
case 'Q':
return $this->_stop();
default:
$this->out(__d('cake_console', 'You have made an invalid selection. Please choose a type of class to Bake by entering D, M, V, F, T, or C.'));
}
$this->hr();
$this->main();
}

/**
Expand All @@ -147,64 +100,36 @@ public function all() {
$this->out('Bake All');
$this->hr();

if (!isset($this->params['connection']) && empty($this->connection)) {
$this->connection = $this->DbConfig->getConfig();
$this->connection = 'default';
if (!empty($this->params['connection'])) {
$this->connection = $this->params['connection'];
}

if (empty($this->args)) {
$this->Model->interactive = true;
$name = $this->Model->getName($this->connection);
$this->Model->connection = $this->connection;
$this->out(__d('cake_console', 'Possible model names based on your database'));
foreach ($this->Model->listAll() as $table) {
$this->out('- ' . $table);
}
$this->out(__d('cake_console', 'Run <info>cake bake all [name]</info>. To generate skeleton files.'));
return false;
}

foreach (['Model', 'Controller', 'View'] as $task) {
$this->{$task}->connection = $this->connection;
$this->{$task}->interactive = false;
}

if (!empty($this->args[0])) {
$name = $this->args[0];
}

$modelExists = false;
$model = $this->_modelName($name);
$name = $this->args[0];
$name = $this->_modelName($name);

$model = App::classname($model, 'Model');
if (class_exists($model)) {
$object = new $model();
$modelExists = true;
} else {
$object = new Model(['name' => $name, 'ds' => $this->connection]);
}
$this->Model->bake($name);
$this->Controller->bake($name);

$modelBaked = $this->Model->bake($object, false);
$this->View->args = [$name];
$this->View->execute();

if ($modelBaked && $modelExists === false) {
if ($this->_checkUnitTest()) {
$this->Model->bakeFixture($model);
$this->Model->bakeTest($model);
}
$modelExists = true;
}

if ($modelExists === true) {
$controller = $this->_controllerName($name);
if ($this->Controller->bake($controller, $this->Controller->bakeActions($controller))) {
if ($this->_checkUnitTest()) {
$this->Controller->bakeTest($controller);
}
}
$controller = App::classname($controller, 'Controller', 'Controller');
if ($controller) {
$this->View->args = [$name];
$this->View->execute();
}
$this->out('', 1, Shell::QUIET);
$this->out(__d('cake_console', '<success>Bake All complete</success>'), 1, Shell::QUIET);
array_shift($this->args);
} else {
$this->error(__d('cake_console', 'Bake All could not continue without a valid model'));
}
return $this->_stop();
$this->out(__d('cake_console', '<success>Bake All complete</success>'), 1, Shell::QUIET);
return true;
}

/**
Expand Down
51 changes: 11 additions & 40 deletions tests/TestCase/Console/Command/BakeShellTest.php
@@ -1,7 +1,5 @@
<?php
/**
* BakeShell Test Case
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand Down Expand Up @@ -43,7 +41,7 @@ public function setUp() {

$this->Shell = $this->getMock(
'Cake\Console\Command\BakeShell',
['in', 'out', 'hr', 'err', 'createFile', '_stop', '_checkUnitTest'],
['in', 'out', 'hr', 'err', 'createFile', '_stop'],
[$out, $out, $in]
);
Configure::write('App.namespace', 'TestApp');
Expand All @@ -65,64 +63,37 @@ public function tearDown() {
* @return void
*/
public function testAllWithModelName() {
$this->markTestIncomplete('Baking with models is not working right now.');
$dispatcher =& $this->Dispatcher;

$this->Shell->Model = $this->getMock(
'Cake\Console\Command\Task\ModelTask',
[],
[$dispatcher]
);
$this->Shell->Controller = $this->getMock(
'Cake\Console\Command\Task\ControllerTask',
[],
[$dispatcher]
);
$this->Shell->View = $this->getMock(
'Cake\Console\Command\Task\ModelTask',
[],
[$dispatcher]
);
$this->Shell->DbConfig = $this->getMock(
'Cake\Console\Command\Task\DbConfigTask',
[],
[$dispatcher]
);

$this->Shell->DbConfig->expects($this->once())
->method('getConfig')
->will($this->returnValue('test'));

$this->Shell->Model->expects($this->never())
->method('getName');
$this->Shell->Model = $this->getMock('Cake\Console\Command\Task\ModelTask');
$this->Shell->Controller = $this->getMock('Cake\Console\Command\Task\ControllerTask');
$this->Shell->View = $this->getMock('Cake\Console\Command\Task\ModelTask');

$this->Shell->Model->expects($this->once())
->method('bake')
->with('Comments')
->will($this->returnValue(true));

$this->Shell->Controller->expects($this->once())
->method('bake')
->with('Comments')
->will($this->returnValue(true));

$this->Shell->View->expects($this->once())
->method('execute');

$this->Shell->expects($this->once())
->method('_stop');

$this->Shell->expects($this->at(0))
->method('out')
->with('Bake All');

$this->Shell->expects($this->at(4))
$this->Shell->expects($this->at(2))
->method('out')
->with('<success>Bake All complete</success>');

$this->Shell->connection = '';
$this->Shell->params = array();
$this->Shell->args = array('Comment');
$this->Shell->params = [];
$this->Shell->args = ['Comment'];
$this->Shell->all();

$this->assertEquals('Comment', $this->Shell->View->args[0]);
$this->assertEquals('Comments', $this->Shell->View->args[0]);
}

}

0 comments on commit 6fb7ea7

Please sign in to comment.