From bb566c3841ce8b0011e754562a289c114a231453 Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 4 Jun 2009 23:35:36 -0400 Subject: [PATCH] Adding test cases for various permutations of execute() Allowing admin methods to be baked separately of regular methods. --- cake/console/libs/tasks/view.php | 40 ++++---- .../cases/console/libs/tasks/view.test.php | 93 ++++++++++++++++++- 2 files changed, 108 insertions(+), 25 deletions(-) diff --git a/cake/console/libs/tasks/view.php b/cake/console/libs/tasks/view.php index fc7237ab9..b51829b82 100644 --- a/cake/console/libs/tasks/view.php +++ b/cake/console/libs/tasks/view.php @@ -130,7 +130,6 @@ class ViewTask extends Shell { } else { $vars = $this->__loadController(); if ($vars) { - $methods = array_diff( array_map('strtolower', get_class_methods($this->controllerName . 'Controller')), array_map('strtolower', get_class_methods('appcontroller')) @@ -142,7 +141,7 @@ class ViewTask extends Shell { $adminRoute = Configure::read('Routing.admin'); if (!empty($adminRoute)) { - $adminDelete = $adminRoute.'_delete'; + $adminDelete = $adminRoute . '_delete'; } foreach ($methods as $method) { if ($method{0} != '_' && !in_array($method, array('delete', $adminDelete))) { @@ -191,10 +190,6 @@ class ViewTask extends Shell { $this->connection = $this->DbConfig->getConfig(); } - $wannaDoAdmin = 'n'; - $wannaDoScaffold = 'y'; - $admin = false; - $this->Controller->connection = $this->connection; $this->controllerName = $this->Controller->getName(); @@ -208,27 +203,25 @@ class ViewTask extends Shell { } $prompt = __("Would you like to create some CRUD views\n(index, add, view, edit) for this controller?\nNOTE: Before doing so, you'll need to create your controller\nand model classes (including associated models).", true); - $wannaDoScaffold = $this->in($prompt, array('y','n'), 'n'); + $wannaDoScaffold = $this->in($prompt, array('y','n'), 'y'); - if (strtolower($wannaDoScaffold) == 'y') { - $wannaDoAdmin = $this->in(__("Would you like to create the views for admin routing?", true), array('y','n'), 'y'); - } + $wannaDoAdmin = $this->in(__("Would you like to create the views for admin routing?", true), array('y','n'), 'n'); - if (strtolower($wannaDoAdmin) == 'y') { - $admin = $this->getAdmin(); - } - - if (strtolower($wannaDoScaffold) == 'y') { - $actions = $this->scaffoldActions; - if ($admin) { - foreach ($actions as $action) { - $actions[] = $admin . $action; - } - } + if (strtolower($wannaDoScaffold) == 'y' || strtolower($wannaDoAdmin) == 'y') { $vars = $this->__loadController(); - if ($vars) { + if (strtolower($wannaDoScaffold) == 'y') { + $actions = $this->scaffoldActions; $this->bakeActions($actions, $vars); } + if (strtolower($wannaDoAdmin) == 'y') { + $admin = $this->getAdmin(); + $regularActions = $this->scaffoldActions; + $adminActions = array(); + foreach ($regularActions as $action) { + $adminActions[] = $admin . $action; + } + $this->bakeActions($adminActions, $vars); + } $this->hr(); $this->out(''); $this->out(__("View Scaffolding Complete.\n", true)); @@ -347,7 +340,7 @@ class ViewTask extends Shell { */ function bake($action, $content = '') { if ($content === true) { - $content = $this->getContent(); + $content = $this->getContent($action); } $filename = $this->path . $this->controllerPath . DS . Inflector::underscore($action) . '.ctp'; $Folder =& new Folder($this->path . $this->controllerPath, true); @@ -427,6 +420,7 @@ class ViewTask extends Shell { $this->out(""); $this->_stop(); } + /** * Returns associations for controllers models. * diff --git a/cake/tests/cases/console/libs/tasks/view.test.php b/cake/tests/cases/console/libs/tasks/view.test.php index 6e6b35a3e..aaae32239 100644 --- a/cake/tests/cases/console/libs/tasks/view.test.php +++ b/cake/tests/cases/console/libs/tasks/view.test.php @@ -58,10 +58,29 @@ Mock::generate('ControllerTask', 'ViewTaskMockControllerTask'); class ViewTaskComment extends Model { var $name = 'ViewTaskComment'; var $useTable = 'comments'; + + var $belongsTo = array( + 'Article' => array( + 'className' => 'ViewTaskArticle', + 'foreignKey' => 'article_id' + ) + ); +} + +class ViewTaskArticle extends Model { + var $name = 'ViewTaskArticle'; + var $useTable = 'articles'; } class ViewTaskCommentsController extends Controller { var $name = 'ViewTaskComments'; + + function index() { + + } + function add() { + + } } @@ -139,13 +158,19 @@ class ViewTaskTest extends CakeTestCase { $this->Task->controllerName = 'ViewTaskComments'; $this->Task->controllerPath = 'view_task_comments'; - $this->Task->expectAt(0, 'createFile', array(TMP . 'view_task_comments' . DS . 'view.ctp', '*')); + $this->Task->expectAt(0, 'createFile', array( + TMP . 'view_task_comments' . DS . 'view.ctp', + new PatternExpectation('/View Task Articles/') + )); $this->Task->bake('view', true); $this->Task->expectAt(1, 'createFile', array(TMP . 'view_task_comments' . DS . 'edit.ctp', '*')); $this->Task->bake('edit', true); - $this->Task->expectAt(2, 'createFile', array(TMP . 'view_task_comments' . DS . 'index.ctp', '*')); + $this->Task->expectAt(2, 'createFile', array( + TMP . 'view_task_comments' . DS . 'index.ctp', + new PatternExpectation('/\$viewTaskComment\[\'Article\'\]\[\'title\'\]/') + )); $this->Task->bake('index', true); @rmdir(TMP . 'view_task_comments'); @@ -166,6 +191,7 @@ class ViewTaskTest extends CakeTestCase { $this->Task->expectAt(2, 'createFile', array(TMP . 'view_task_comments' . DS . 'index.ctp', '*')); $this->Task->bakeActions(array('view', 'edit', 'index'), array()); + @rmdir(TMP . 'view_task_comments'); } @@ -186,6 +212,7 @@ class ViewTaskTest extends CakeTestCase { $this->Task->expectAt(0, 'createFile', array(TMP . 'view_task_comments' . DS . 'my_action.ctp', '*')); $this->Task->customAction(); + @rmdir(TMP . 'view_task_comments'); } @@ -201,14 +228,76 @@ class ViewTaskTest extends CakeTestCase { $this->Task->Controller->setReturnValue('listAll', array('view_task_comments')); $this->Task->Controller->expectOnce('listAll'); + $this->Task->expectCallCount('createFile', 4); $this->Task->expectAt(0, 'createFile', array(TMP . 'view_task_comments' . DS . 'index.ctp', '*')); $this->Task->expectAt(1, 'createFile', array(TMP . 'view_task_comments' . DS . 'view.ctp', '*')); $this->Task->expectAt(2, 'createFile', array(TMP . 'view_task_comments' . DS . 'add.ctp', '*')); $this->Task->expectAt(3, 'createFile', array(TMP . 'view_task_comments' . DS . 'edit.ctp', '*')); $this->Task->execute(); + @rmdir(TMP . 'view_task_comments'); } +/** + * test `cake bake view $controller view` + * + * @return void + **/ + function testExecuteWithActionParam() { + $this->Task->path = TMP; + $this->Task->args[0] = 'ViewTaskComments'; + $this->Task->args[1] = 'view'; + + $this->Task->expectCallCount('createFile', 1); + $this->Task->expectAt(0, 'createFile', array(TMP . 'view_task_comments' . DS . 'view.ctp', '*')); + $this->Task->execute(); + + @rmdir(TMP . 'view_task_comments'); + } + +/** + * test `cake bake view $controller` + * + * @return void + **/ + function testExecuteWithController() { + $this->Task->path = TMP; + $this->Task->args[0] = 'ViewTaskComments'; + + $this->Task->expectCallCount('createFile', 2); + $this->Task->expectAt(0, 'createFile', array(TMP . 'view_task_comments' . DS . 'index.ctp', '*')); + $this->Task->expectAt(1, 'createFile', array(TMP . 'view_task_comments' . DS . 'add.ctp', '*')); + $this->Task->execute(); + + @rmdir(TMP . 'view_task_comments'); + } + +/** + * test execute into interactive. + * + * @return void + **/ + function testExecuteInteractive() { + $this->Task->path = TMP; + $this->Task->connection = 'test_suite'; + $this->Task->args = array(); + + $this->Task->Controller->setReturnValue('getName', 'ViewTaskComments'); + $this->Task->setReturnValue('in', 'y'); + $this->Task->setReturnValueAt(0, 'in', 'y'); + $this->Task->setReturnValueAt(1, 'in', 'y'); + $this->Task->setReturnValueAt(2, 'in', 'n'); + + $this->Task->expectCallCount('createFile', 4); + $this->Task->expectAt(0, 'createFile', array(TMP . 'view_task_comments' . DS . 'index.ctp', '*')); + $this->Task->expectAt(1, 'createFile', array(TMP . 'view_task_comments' . DS . 'view.ctp', '*')); + $this->Task->expectAt(2, 'createFile', array(TMP . 'view_task_comments' . DS . 'add.ctp', '*')); + $this->Task->expectAt(3, 'createFile', array(TMP . 'view_task_comments' . DS . 'edit.ctp', '*')); + + $this->Task->execute(); + + @rmdir(TMP . 'view_task_comments'); + } } ?> \ No newline at end of file