From f19fa11eb2e46fa6d88162c8356e2e902cc8734e Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 17 Apr 2014 23:27:02 -0400 Subject: [PATCH] Update bake tasks to use method arguments over $args property. Use the new positional arguments feature to clean up bake a bit. --- src/Console/Command/BakeShell.php | 8 +++--- src/Console/Command/Task/ControllerTask.php | 8 +++--- src/Console/Command/Task/FixtureTask.php | 8 +++--- src/Console/Command/Task/ModelTask.php | 8 +++--- src/Console/Command/Task/PluginTask.php | 6 ++--- src/Console/Command/Task/SimpleBakeTask.php | 4 +-- src/Console/Command/Task/TestTask.php | 11 ++++---- src/Console/Command/Task/ViewTask.php | 19 +++++--------- .../Console/Command/BakeShellTest.php | 8 +++--- .../Command/Task/ControllerTaskTest.php | 6 ++--- .../Console/Command/Task/FixtureTaskTest.php | 11 +++----- .../Console/Command/Task/ModelTaskTest.php | 12 +++------ .../Console/Command/Task/PluginTaskTest.php | 4 +-- .../Console/Command/Task/ProjectTaskTest.php | 5 ++-- .../Command/Task/SimpleBakeTaskTest.php | 3 +-- .../Console/Command/Task/TestTaskTest.php | 6 ++--- .../Console/Command/Task/ViewTaskTest.php | 26 +++++++++---------- 17 files changed, 62 insertions(+), 91 deletions(-) diff --git a/src/Console/Command/BakeShell.php b/src/Console/Command/BakeShell.php index 5e7dc88430e..c0535345e60 100644 --- a/src/Console/Command/BakeShell.php +++ b/src/Console/Command/BakeShell.php @@ -190,7 +190,7 @@ protected function _findTaskClasses($files) { * * @return void */ - public function all() { + public function all($name = null) { $this->out('Bake All'); $this->hr(); @@ -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) { @@ -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', 'Bake All complete'), 1, Shell::QUIET); return true; diff --git a/src/Console/Command/Task/ControllerTask.php b/src/Console/Command/Task/ControllerTask.php index a5333c4bad2..3e10297af6f 100644 --- a/src/Console/Command/Task/ControllerTask.php +++ b/src/Console/Command/Task/ControllerTask.php @@ -44,14 +44,14 @@ 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)); @@ -59,11 +59,11 @@ public function execute() { 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); } diff --git a/src/Console/Command/Task/FixtureTask.php b/src/Console/Command/Task/FixtureTask.php index 8c2ff2c4744..75332579091 100644 --- a/src/Console/Command/Task/FixtureTask.php +++ b/src/Console/Command/Task/FixtureTask.php @@ -90,13 +90,13 @@ 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)); @@ -104,14 +104,14 @@ public function execute() { 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); } diff --git a/src/Console/Command/Task/ModelTask.php b/src/Console/Command/Task/ModelTask.php index 21160e5b634..55ca2843088 100644 --- a/src/Console/Command/Task/ModelTask.php +++ b/src/Console/Command/Task/ModelTask.php @@ -75,14 +75,14 @@ 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)); @@ -90,11 +90,11 @@ public function execute() { 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)); } /** diff --git a/src/Console/Command/Task/PluginTask.php b/src/Console/Command/Task/PluginTask.php index 683d53ed3a6..30189d5adcb 100644 --- a/src/Console/Command/Task/PluginTask.php +++ b/src/Console/Command/Task/PluginTask.php @@ -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('You must provide a plugin name in CamelCase format.'); $this->err('To make an "Example" plugin, run Console/cake bake plugin Example.'); 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)); diff --git a/src/Console/Command/Task/SimpleBakeTask.php b/src/Console/Command/Task/SimpleBakeTask.php index 2dcb486965a..ea9400a1f3b 100644 --- a/src/Console/Command/Task/SimpleBakeTask.php +++ b/src/Console/Command/Task/SimpleBakeTask.php @@ -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); } diff --git a/src/Console/Command/Task/TestTask.php b/src/Console/Command/Task/TestTask.php index bbfe56aac0e..a01a3ca63fb 100644 --- a/src/Console/Command/Task/TestTask.php +++ b/src/Console/Command/Task/TestTask.php @@ -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('Done'); } } diff --git a/src/Console/Command/Task/ViewTask.php b/src/Console/Command/Task/ViewTask.php index b40c29f27c8..b69d3cbce1f 100644 --- a/src/Console/Command/Task/ViewTask.php +++ b/src/Console/Command/Task/ViewTask.php @@ -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) { @@ -114,8 +114,7 @@ public function execute() { return true; } - $action = null; - if (strtolower($this->args[0]) === 'all') { + if (strtolower($name) === 'all') { return $this->all(); } @@ -123,13 +122,10 @@ public function execute() { 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; @@ -223,8 +219,7 @@ public function all() { $tables = $this->Model->listAll(); foreach ($tables as $table) { - $this->args[0] = $table; - $this->execute(); + $this->execute($table); } } diff --git a/tests/TestCase/Console/Command/BakeShellTest.php b/tests/TestCase/Console/Command/BakeShellTest.php index 2dcdddf1a77..c755febc1b9 100644 --- a/tests/TestCase/Console/Command/BakeShellTest.php +++ b/tests/TestCase/Console/Command/BakeShellTest.php @@ -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') @@ -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'); } /** diff --git a/tests/TestCase/Console/Command/Task/ControllerTaskTest.php b/tests/TestCase/Console/Command/Task/ControllerTaskTest.php index baad3a38a83..318cfa3b210 100644 --- a/tests/TestCase/Console/Command/Task/ControllerTaskTest.php +++ b/tests/TestCase/Console/Command/Task/ControllerTaskTest.php @@ -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()) @@ -317,7 +316,7 @@ public function testExecuteIntoAll() { )) ->will($this->returnValue(true)); - $this->Task->execute(); + $this->Task->execute('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); } } diff --git a/tests/TestCase/Console/Command/Task/FixtureTaskTest.php b/tests/TestCase/Console/Command/Task/FixtureTaskTest.php index 8dd72b19bfa..dc691060593 100644 --- a/tests/TestCase/Console/Command/Task/FixtureTaskTest.php +++ b/tests/TestCase/Console/Command/Task/FixtureTaskTest.php @@ -179,7 +179,6 @@ 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'; @@ -187,7 +186,7 @@ public function testExecuteWithTableOption() { ->method('createFile') ->with($filename, $this->stringContains("public \$table = 'comments';")); - $this->Task->execute(); + $this->Task->execute('article'); } /** @@ -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'); } /** @@ -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'))); @@ -229,7 +226,7 @@ public function testExecuteIntoAll() { ->method('createFile') ->with($filename, $this->stringContains('class CommentFixture')); - $this->Task->execute(); + $this->Task->execute('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') @@ -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') diff --git a/tests/TestCase/Console/Command/Task/ModelTaskTest.php b/tests/TestCase/Console/Command/Task/ModelTaskTest.php index 5249d713a8e..cba31116bea 100644 --- a/tests/TestCase/Console/Command/Task/ModelTaskTest.php +++ b/tests/TestCase/Console/Command/Task/ModelTaskTest.php @@ -751,7 +751,6 @@ public function testExecuteNoArgs() { */ public function testExecuteWithNamedModel() { $this->Task->connection = 'test'; - $this->Task->args = ['BakeArticles']; $tableFile = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php'); $this->Task->expects($this->at(0)) @@ -763,7 +762,7 @@ public function testExecuteWithNamedModel() { ->method('createFile') ->with($entityFile, $this->stringContains('class BakeArticle extends Entity')); - $this->Task->execute(); + $this->Task->execute('BakeArticles'); } /** @@ -786,13 +785,12 @@ public static function nameVariations() { public function testExecuteWithNamedModelVariations($name) { $this->Task->connection = 'test'; - $this->Task->args = array($name); $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php'); $this->Task->expects($this->at(0)) ->method('createFile') ->with($filename, $this->stringContains('class BakeArticlesTable extends Table {')); - $this->Task->execute(); + $this->Task->execute($name); } /** @@ -807,7 +805,6 @@ public function testExecuteIntoAll() { } $this->Task->connection = 'test'; - $this->Task->args = ['all']; $this->Task->Fixture->expects($this->exactly($count)) ->method('bake'); @@ -864,7 +861,7 @@ public function testExecuteIntoAll() { ->method('createFile') ->with($filename, $this->stringContains('class CategoryThread extends')); - $this->Task->execute(); + $this->Task->execute('all'); } /** @@ -879,7 +876,6 @@ public function testSkipTablesAndAll() { } $this->Task->connection = 'test'; - $this->Task->args = ['all']; $this->Task->skipTables = ['bake_tags']; $this->Task->Fixture->expects($this->exactly(7)) @@ -917,7 +913,7 @@ public function testSkipTablesAndAll() { ->method('createFile') ->with($filename); - $this->Task->execute(); + $this->Task->execute('all'); } } diff --git a/tests/TestCase/Console/Command/Task/PluginTaskTest.php b/tests/TestCase/Console/Command/Task/PluginTaskTest.php index 5f6e5c241d8..650baaeecd2 100644 --- a/tests/TestCase/Console/Command/Task/PluginTaskTest.php +++ b/tests/TestCase/Console/Command/Task/PluginTaskTest.php @@ -124,7 +124,6 @@ public function testExecuteWithNoArgs() { $this->Task->expects($this->never()) ->method('createFile'); - $this->Task->args = array(); $this->Task->execute(); $Folder = new Folder($path); @@ -153,8 +152,7 @@ public function testExecuteWithOneArg() { $this->Task->expects($this->at(3))->method('createFile') ->with($file, new \PHPUnit_Framework_Constraint_IsAnything()); - $this->Task->args = array('BakeTestPlugin'); - $this->Task->execute(); + $this->Task->execute('BakeTestPlugin'); $Folder = new Folder($this->Task->path . 'BakeTestPlugin'); $Folder->delete(); diff --git a/tests/TestCase/Console/Command/Task/ProjectTaskTest.php b/tests/TestCase/Console/Command/Task/ProjectTaskTest.php index f2f8bc0cdf9..5a177511797 100644 --- a/tests/TestCase/Console/Command/Task/ProjectTaskTest.php +++ b/tests/TestCase/Console/Command/Task/ProjectTaskTest.php @@ -72,12 +72,11 @@ protected function _setupTestProject() { */ public function testExecuteWithAbsolutePath() { $this->markTestIncomplete('Need to figure this out'); - $path = $this->Task->args[0] = TMP . 'BakeTestApp'; $this->Task->params['skel'] = CAKE . 'Console/Templates/skel'; $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y')); - $this->Task->execute(); + $this->Task->execute(TMP . 'BakeTestApp'); - $this->assertTrue(is_dir($this->Task->args[0]), 'No project dir'); + $this->assertTrue(is_dir(TMP . 'BakeTestApp'), 'No project dir'); $File = new File($path . DS . 'Config/paths.php'); $contents = $File->read(); $this->assertRegExp('/define\(\'CAKE_CORE_INCLUDE_PATH\', .*?DS/', $contents); diff --git a/tests/TestCase/Console/Command/Task/SimpleBakeTaskTest.php b/tests/TestCase/Console/Command/Task/SimpleBakeTaskTest.php index 0a0f70f0ec6..187384fe5e2 100644 --- a/tests/TestCase/Console/Command/Task/SimpleBakeTaskTest.php +++ b/tests/TestCase/Console/Command/Task/SimpleBakeTaskTest.php @@ -67,7 +67,6 @@ public function setUp() { * @return void */ public function testExecute() { - $this->Task->args = ['Example']; $this->Task->expects($this->once()) ->method('createFile') ->with( @@ -78,7 +77,7 @@ public function testExecute() { ->method('bake') ->with('behavior', 'Example'); - $this->Task->execute(); + $this->Task->execute('Example'); } /** diff --git a/tests/TestCase/Console/Command/Task/TestTaskTest.php b/tests/TestCase/Console/Command/Task/TestTaskTest.php index 1cfdbbcc171..34a0dc74cf5 100644 --- a/tests/TestCase/Console/Command/Task/TestTaskTest.php +++ b/tests/TestCase/Console/Command/Task/TestTaskTest.php @@ -123,8 +123,7 @@ public function testExecuteOneArgPrintsClassOptions() { $this->Task->expects($this->once()) ->method('outputClassChoices'); - $this->Task->args = ['Entity']; - $this->Task->execute(); + $this->Task->execute('Entity'); } /** @@ -133,13 +132,12 @@ public function testExecuteOneArgPrintsClassOptions() { * @return void */ public function testExecuteWithTwoArgs() { - $this->Task->args = ['Table', 'TestTaskTag']; $this->Task->expects($this->once())->method('createFile') ->with( $this->stringContains('TestCase' . DS . 'Model' . DS . 'Table' . DS . 'TestTaskTagTableTest.php'), $this->stringContains('class TestTaskTagTableTest extends TestCase') ); - $this->Task->execute(); + $this->Task->execute('Table', 'TestTaskTag'); } /** diff --git a/tests/TestCase/Console/Command/Task/ViewTaskTest.php b/tests/TestCase/Console/Command/Task/ViewTaskTest.php index b16e8ec5c2c..0bdb3ac403c 100644 --- a/tests/TestCase/Console/Command/Task/ViewTaskTest.php +++ b/tests/TestCase/Console/Command/Task/ViewTaskTest.php @@ -475,12 +475,11 @@ public function testExecuteNoArgs() { */ public function testExecuteIntoAll() { $this->_setupTask(['in', 'err', 'createFile', 'all', '_stop']); - $this->Task->args[0] = 'all'; $this->Task->expects($this->once()) ->method('all'); - $this->Task->execute(); + $this->Task->execute('all'); } /** @@ -497,9 +496,14 @@ public function testAllCallsExecute() { $this->Task->expects($this->exactly(2)) ->method('execute'); + $this->Task->expects($this->at(0)) + ->method('execute') + ->with('comments'); + $this->Task->expects($this->at(1)) + ->method('execute') + ->with('articles'); $this->Task->all(); - $this->assertEquals('articles', $this->Task->args[0]); } /** @@ -509,14 +513,12 @@ public function testAllCallsExecute() { */ public function testExecuteWithActionParam() { $this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']); - $this->Task->args[0] = 'ViewTaskComments'; - $this->Task->args[1] = 'view'; $this->Task->expects($this->once()) ->method('bake') ->with('view', true); - $this->Task->execute(); + $this->Task->execute('ViewTaskComments', 'view'); } /** @@ -527,7 +529,6 @@ public function testExecuteWithActionParam() { */ public function testExecuteWithController() { $this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']); - $this->Task->args[0] = 'ViewTaskComments'; $this->Task->expects($this->exactly(4)) ->method('bake'); @@ -548,7 +549,7 @@ public function testExecuteWithController() { ->method('bake') ->with('edit', $this->anything()); - $this->Task->execute(); + $this->Task->execute('ViewTaskComments'); } /** @@ -566,7 +567,6 @@ public static function nameVariations() { * @return void */ public function testExecuteWithControllerFlag() { - $this->Task->args[0] = 'Posts'; $this->Task->params['controller'] = 'Blog'; $this->Task->expects($this->exactly(4)) @@ -580,7 +580,7 @@ public function testExecuteWithControllerFlag() { $this->anything() ); } - $this->Task->execute(); + $this->Task->execute('Posts'); } /** @@ -589,7 +589,6 @@ public function testExecuteWithControllerFlag() { * @return void */ public function testExecuteWithControllerAndAdminFlag() { - $this->Task->args[0] = 'Posts'; $this->Task->params['prefix'] = 'Admin'; $this->Task->expects($this->exactly(2)) @@ -603,7 +602,7 @@ public function testExecuteWithControllerAndAdminFlag() { $this->anything() ); } - $this->Task->execute(); + $this->Task->execute('Posts'); } /** @@ -615,13 +614,12 @@ public function testExecuteWithAlternateTemplates() { $this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']); $this->Task->connection = 'test'; - $this->Task->args = ['ViewTaskComments', 'index', 'list']; $this->Task->params = []; $this->Task->expects($this->once()) ->method('bake') ->with('list', true); - $this->Task->execute(); + $this->Task->execute('ViewTaskComments', 'index', 'list'); } /**