From 03e231c05ddc7df17f915527b3093c76282c29f1 Mon Sep 17 00:00:00 2001 From: mark_story Date: Thu, 14 May 2009 23:17:42 -0400 Subject: [PATCH] Adding test case for controller task. Refactoring listAll() to use Model task which has the necessary code. --- cake/console/libs/tasks/controller.php | 39 ++---- .../console/libs/tasks/controller.test.php | 123 ++++++++++++++++++ 2 files changed, 134 insertions(+), 28 deletions(-) create mode 100644 cake/tests/cases/console/libs/tasks/controller.test.php diff --git a/cake/console/libs/tasks/controller.php b/cake/console/libs/tasks/controller.php index ec69bb21fc9..3248351c2ff 100644 --- a/cake/console/libs/tasks/controller.php +++ b/cake/console/libs/tasks/controller.php @@ -44,7 +44,7 @@ class ControllerTask extends Shell { * @var array * @access public */ - var $tasks = array('Project'); + var $tasks = array('Model', 'Project', 'Template'); /** * path to CONTROLLERS directory * @@ -105,12 +105,8 @@ function execute() { * @return void **/ function all() { - $ds = 'default'; - if (isset($this->params['connection'])) { - $ds = $this->params['connection']; - } $this->interactive = false; - $this->listAll($ds, false); + $this->listAll($this->connection, false); foreach ($this->__tables as $table) { $model = $this->_modelName($table); $controller = $this->_controllerName($model); @@ -509,40 +505,27 @@ function bakeTest($className) { $content = ""; return $this->createFile($path . $filename, $content); } + /** - * Outputs and gets the list of possible models or controllers from database + * Outputs and gets the list of possible controllers from database * * @param string $useDbConfig Database configuration name * @param boolean $interactive Whether you are using listAll interactively and want options output. * @return array Set of controllers * @access public */ - function listAll($useDbConfig = 'default', $interactive = true) { - $db =& ConnectionManager::getDataSource($useDbConfig); - $usePrefix = empty($db->config['prefix']) ? '' : $db->config['prefix']; - if ($usePrefix) { - $tables = array(); - foreach ($db->listSources() as $table) { - if (!strncmp($table, $usePrefix, strlen($usePrefix))) { - $tables[] = substr($table, strlen($usePrefix)); - } - } - } else { - $tables = $db->listSources(); - } - - if (empty($tables)) { - $this->err(__('Your database does not have any tables.', true)); - $this->_stop(); + function listAll($useDbConfig = null) { + if (is_null($useDbConfig)) { + $useDbConfig = $this->connection; } + $this->__tables = $this->Model->getAllTables($useDbConfig); - $this->__tables = $tables; - if ($interactive == true) { + if ($this->interactive == true) { $this->out('Possible Controllers based on your current database:'); $this->_controllerNames = array(); - $count = count($tables); + $count = count($this->__tables); for ($i = 0; $i < $count; $i++) { - $this->_controllerNames[] = $this->_controllerName($this->_modelName($tables[$i])); + $this->_controllerNames[] = $this->_controllerName($this->_modelName($this->__tables[$i])); $this->out($i + 1 . ". " . $this->_controllerNames[$i]); } return $this->_controllerNames; diff --git a/cake/tests/cases/console/libs/tasks/controller.test.php b/cake/tests/cases/console/libs/tasks/controller.test.php new file mode 100644 index 00000000000..dccddc275a9 --- /dev/null +++ b/cake/tests/cases/console/libs/tasks/controller.test.php @@ -0,0 +1,123 @@ +Dispatcher =& new TestControllerTaskMockShellDispatcher(); + $this->Task =& new MockControllerTask($this->Dispatcher); + $this->Task->Dispatch =& new $this->Dispatcher; + $this->Task->Dispatch->shellPaths = Configure::read('shellPaths'); + $this->Task->Template =& new TemplateTask($this->Task->Dispatch); + $this->Task->Model =& new ControllerMockModelTask($this->Task->Dispatch); + } + +/** + * tearDown method + * + * @return void + * @access public + */ + function endTest() { + unset($this->Task, $this->Dispatcher); + ClassRegistry::flush(); + } + +/** + * test ListAll + * + * @return void + **/ + function testListAll() { + $this->Task->connection = 'test_suite'; + $this->Task->interactive = true; + $this->Task->expectAt(1, 'out', array('1. Articles')); + $this->Task->expectAt(2, 'out', array('2. ArticlesTags')); + $this->Task->expectAt(3, 'out', array('3. Comments')); + $this->Task->expectAt(4, 'out', array('4. Tags')); + + $expected = array('Articles', 'ArticlesTags', 'Comments', 'Tags'); + $result = $this->Task->listAll('test_suite'); + $this->assertEqual($result, $expected); + + $this->Task->expectAt(6, 'out', array('1. Articles')); + $this->Task->expectAt(7, 'out', array('2. ArticlesTags')); + $this->Task->expectAt(8, 'out', array('4. Comments')); + $this->Task->expectAt(9, 'out', array('5. Tags')); + + $this->Task->interactive = false; + $result = $this->Task->listAll(); + + $expected = array('articles', 'articles_tags', 'comments', 'tags'); + $this->assertEqual($result, $expected); + } +} +?> \ No newline at end of file