Skip to content

Commit

Permalink
Update bake tasks to use method arguments over $args property.
Browse files Browse the repository at this point in the history
Use the new positional arguments feature to clean up bake a bit.
  • Loading branch information
markstory committed Apr 18, 2014
1 parent 9e0da22 commit f19fa11
Show file tree
Hide file tree
Showing 17 changed files with 62 additions and 91 deletions.
8 changes: 3 additions & 5 deletions src/Console/Command/BakeShell.php
Expand Up @@ -190,7 +190,7 @@ protected function _findTaskClasses($files) {
*
* @return void
*/
public function all() {
public function all($name = null) {
$this->out('Bake All');
$this->hr();

Expand All @@ -199,7 +199,7 @@ public function all() {
$this->connection = $this->params['connection'];
}

if (empty($this->args)) {
if (empty($name)) {
$this->Model->connection = $this->connection;
$this->out(__d('cake_console', 'Possible model names based on your database'));
foreach ($this->Model->listAll() as $table) {
Expand All @@ -213,14 +213,12 @@ public function all() {
$this->{$task}->connection = $this->connection;
}

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

$this->Model->bake($name);
$this->Controller->bake($name);

$this->View->args = [$name];
$this->View->execute();
$this->View->execute($name);

$this->out(__d('cake_console', '<success>Bake All complete</success>'), 1, Shell::QUIET);
return true;
Expand Down
8 changes: 4 additions & 4 deletions src/Console/Command/Task/ControllerTask.php
Expand Up @@ -44,26 +44,26 @@ class ControllerTask extends BakeTask {
*
* @return void
*/
public function execute() {
public function execute($name = null) {
parent::execute();

if (!isset($this->connection)) {
$this->connection = 'default';
}

if (empty($this->args)) {
if (empty($name)) {
$this->out(__d('cake_console', 'Possible controllers based on your current database:'));
foreach ($this->listAll() as $table) {
$this->out('- ' . $this->_controllerName($table));
}
return true;
}

if (strtolower($this->args[0]) === 'all') {
if (strtolower($name) === 'all') {
return $this->all();
}

$controller = $this->_controllerName($this->args[0]);
$controller = $this->_controllerName($name);
$this->bake($controller);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Console/Command/Task/FixtureTask.php
Expand Up @@ -90,28 +90,28 @@ public function getOptionParser() {
*
* @return void
*/
public function execute() {
public function execute($name = null) {
parent::execute();
if (!isset($this->connection)) {
$this->connection = 'default';
}

if (empty($this->args)) {
if (empty($name)) {
$this->out(__d('cake_console', 'Choose a fixture to bake from the following:'));
foreach ($this->Model->listAll() as $table) {
$this->out('- ' . $this->_modelName($table));
}
return true;
}

if (strtolower($this->args[0]) === 'all') {
if (strtolower($name) === 'all') {
return $this->all();
}
$table = null;
if (isset($this->params['table'])) {
$table = $this->params['table'];
}
$model = $this->_modelName($this->args[0]);
$model = $this->_modelName($name);
$this->bake($model, $table);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Console/Command/Task/ModelTask.php
Expand Up @@ -75,26 +75,26 @@ class ModelTask extends BakeTask {
*
* @return void
*/
public function execute() {
public function execute($name = null) {
parent::execute();

if (!isset($this->connection)) {
$this->connection = 'default';
}

if (empty($this->args)) {
if (empty($name)) {
$this->out(__d('cake_console', 'Choose a model to bake from the following:'));
foreach ($this->listAll() as $table) {
$this->out('- ' . $this->_modelName($table));
}
return true;
}

if (strtolower($this->args[0]) === 'all') {
if (strtolower($name) === 'all') {
return $this->all();
}

$this->bake($this->_modelName($this->args[0]));
$this->bake($this->_modelName($name));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Console/Command/Task/PluginTask.php
Expand Up @@ -56,13 +56,13 @@ public function initialize() {
*
* @return void
*/
public function execute() {
if (empty($this->args[0])) {
public function execute($name = null) {
if (empty($name)) {
$this->err('<error>You must provide a plugin name in CamelCase format.</error>');
$this->err('To make an "Example" plugin, run <info>Console/cake bake plugin Example</info>.');
return false;
}
$plugin = $this->_camelize($this->args[0]);
$plugin = $this->_camelize($name);
$pluginPath = $this->_pluginPath($plugin);
if (is_dir($pluginPath)) {
$this->out(__d('cake_console', 'Plugin: %s already exists, no action taken', $plugin));
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Command/Task/SimpleBakeTask.php
Expand Up @@ -72,9 +72,9 @@ public function templateData() {
*
* @return void
*/
public function execute() {
public function execute($name = null) {
parent::execute();
$name = Inflector::classify($this->args[0]);
$name = Inflector::classify($name);
$this->bake($name);
$this->bakeTest($name);
}
Expand Down
11 changes: 5 additions & 6 deletions src/Console/Command/Task/TestTask.php
Expand Up @@ -79,18 +79,17 @@ class TestTask extends BakeTask {
*
* @return void
*/
public function execute() {
public function execute($type = null, $name = null) {
parent::execute();
if (empty($this->args)) {
if (empty($type) && empty($name)) {
return $this->outputTypeChoices();
}

$count = count($this->args);
if ($count === 1) {
return $this->outputClassChoices($this->args[0]);
if (empty($name)) {
return $this->outputClassChoices($type);
}

if ($this->bake($this->args[0], $this->args[1])) {
if ($this->bake($type, $name)) {
$this->out('<success>Done</success>');
}
}
Expand Down
19 changes: 7 additions & 12 deletions src/Console/Command/Task/ViewTask.php
Expand Up @@ -98,14 +98,14 @@ public function initialize() {
*
* @return mixed
*/
public function execute() {
public function execute($name = null, $template = null, $action = null) {
parent::execute();

if (!isset($this->connection)) {
$this->connection = 'default';
}

if (empty($this->args)) {
if (empty($name)) {
$this->out(__d('cake_console', 'Possible tables to bake views for based on your current database:'));
$this->Model->connection = $this->connection;
foreach ($this->Model->listAll() as $table) {
Expand All @@ -114,22 +114,18 @@ public function execute() {
return true;
}

$action = null;
if (strtolower($this->args[0]) === 'all') {
if (strtolower($name) === 'all') {
return $this->all();
}

$controller = null;
if (!empty($this->params['controller'])) {
$controller = $this->params['controller'];
}
$this->controller($this->args[0], $controller);
$this->controller($name, $controller);

if (isset($this->args[1])) {
$this->template = $this->args[1];
}
if (isset($this->args[2])) {
$action = $this->args[2];
if (isset($template)) {
$this->template = $template;
}
if (!$action) {
$action = $this->template;
Expand Down Expand Up @@ -223,8 +219,7 @@ public function all() {
$tables = $this->Model->listAll();

foreach ($tables as $table) {
$this->args[0] = $table;
$this->execute();
$this->execute($table);
}
}

Expand Down
8 changes: 3 additions & 5 deletions tests/TestCase/Console/Command/BakeShellTest.php
Expand Up @@ -78,7 +78,8 @@ public function testAllWithModelName() {
->will($this->returnValue(true));

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

$this->Shell->expects($this->at(0))
->method('out')
Expand All @@ -90,10 +91,7 @@ public function testAllWithModelName() {

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

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

/**
Expand Down
6 changes: 2 additions & 4 deletions tests/TestCase/Console/Command/Task/ControllerTaskTest.php
Expand Up @@ -302,7 +302,6 @@ public function testExecuteIntoAll() {
$this->markTestSkipped('Additional tables detected.');
}
$this->Task->connection = 'test';
$this->Task->args = ['all'];
$this->Task->params = ['helpers' => 'Time,Text'];

$this->Task->Test->expects($this->atLeastOnce())
Expand All @@ -317,7 +316,7 @@ public function testExecuteIntoAll() {
))
->will($this->returnValue(true));

$this->Task->execute();
$this->Task->execute('all');
}

/**
Expand All @@ -339,13 +338,12 @@ public static function nameVariations() {
*/
public function testExecuteWithControllerNameVariations($name) {
$this->Task->connection = 'test';
$this->Task->args = [$name];

$filename = APP . 'Controller/BakeArticlesController.php';
$this->Task->expects($this->once())
->method('createFile')
->with($filename, $this->stringContains('public function index()'));
$this->Task->execute();
$this->Task->execute($name);
}

}
11 changes: 3 additions & 8 deletions tests/TestCase/Console/Command/Task/FixtureTaskTest.php
Expand Up @@ -179,15 +179,14 @@ public function testImportRecordsNoEscaping() {
*/
public function testExecuteWithTableOption() {
$this->Task->connection = 'test';
$this->Task->args = array('article');
$this->Task->params = ['table' => 'comments'];
$filename = ROOT . '/Test/Fixture/ArticleFixture.php';

$this->Task->expects($this->at(0))
->method('createFile')
->with($filename, $this->stringContains("public \$table = 'comments';"));

$this->Task->execute();
$this->Task->execute('article');
}

/**
Expand All @@ -197,14 +196,13 @@ public function testExecuteWithTableOption() {
*/
public function testExecuteWithNamedModel() {
$this->Task->connection = 'test';
$this->Task->args = array('article');
$filename = ROOT . '/Test/Fixture/ArticleFixture.php';

$this->Task->expects($this->at(0))
->method('createFile')
->with($filename, $this->stringContains('class ArticleFixture'));

$this->Task->execute();
$this->Task->execute('article');
}

/**
Expand All @@ -214,7 +212,6 @@ public function testExecuteWithNamedModel() {
*/
public function testExecuteIntoAll() {
$this->Task->connection = 'test';
$this->Task->args = array('all');
$this->Task->Model->expects($this->any())
->method('listAll')
->will($this->returnValue(array('articles', 'comments')));
Expand All @@ -229,7 +226,7 @@ public function testExecuteIntoAll() {
->method('createFile')
->with($filename, $this->stringContains('class CommentFixture'));

$this->Task->execute();
$this->Task->execute('all');
}

/**
Expand All @@ -239,7 +236,6 @@ public function testExecuteIntoAll() {
*/
public function testAllWithCountAndRecordsFlags() {
$this->Task->connection = 'test';
$this->Task->args = ['all'];
$this->Task->params = ['count' => 10, 'records' => true];

$this->Task->Model->expects($this->any())->method('listAll')
Expand All @@ -266,7 +262,6 @@ public function testAllWithCountAndRecordsFlags() {
*/
public function testAllWithSchemaImport() {
$this->Task->connection = 'test';
$this->Task->args = array('all');
$this->Task->params = array('schema' => true);

$this->Task->Model->expects($this->any())->method('listAll')
Expand Down

0 comments on commit f19fa11

Please sign in to comment.