From e0acd2131f7d580038279629923810c7ade2041f Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 7 Jul 2010 21:35:03 -0400 Subject: [PATCH] Fixing ShellDispatcher tests and making ShellDispatcher/Shell use TaskCollection to loadTasks. --- cake/console/cake.php | 19 +++++++ cake/console/libs/shell.php | 52 ++++++-------------- cake/tests/cases/console/cake.test.php | 17 +++---- cake/tests/cases/console/libs/shell.test.php | 9 +--- 4 files changed, 43 insertions(+), 54 deletions(-) diff --git a/cake/console/cake.php b/cake/console/cake.php index 7bd13bb43..4e4673cfc 100644 --- a/cake/console/cake.php +++ b/cake/console/cake.php @@ -119,6 +119,13 @@ class ShellDispatcher { */ public $shellName = null; +/** + * TaskCollection object for the command + * + * @var TaskCollection + */ + protected $_Tasks; + /** * Constructor * @@ -422,6 +429,18 @@ class ShellDispatcher { return $Shell; } +/** + * Returns a TaskCollection object for Shells to use when loading their tasks. + * + * @return TaskCollection object. + */ + public function getTaskCollection() { + if (empty($this->_Tasks)) { + $this->_Tasks = new TaskCollection($this); + } + return $this->_Tasks; + } + /** * Prompts the user for input, and returns it. * diff --git a/cake/console/libs/shell.php b/cake/console/libs/shell.php index bfd2b019e..132612d86 100644 --- a/cake/console/libs/shell.php +++ b/cake/console/libs/shell.php @@ -17,6 +17,7 @@ * @since CakePHP(tm) v 1.2.0.5012 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ +App::import('Shell', 'TaskCollection'); /** * Base class for command-line utilities for automating programmer chores. @@ -131,6 +132,13 @@ class Shell extends Object { */ public $uses = array(); +/** + * Task Collection for the command, used to create Tasks. + * + * @var TaskCollection + */ + public $Tasks; + /** * Constructs this Shell instance. * @@ -153,11 +161,8 @@ class Shell extends Object { if ($this->alias == null) { $this->alias = $this->name; } - - ClassRegistry::addObject($this->name, $this); - ClassRegistry::map($this->name, $this->alias); - $this->Dispatch =& $dispatch; + $this->Tasks = $dispatch->getTaskCollection(); } /** @@ -252,43 +257,14 @@ class Shell extends Object { * @return bool */ public function loadTasks() { - if ($this->tasks === null || $this->tasks === false || $this->tasks === true || empty($this->tasks)) { + if ($this->tasks === true || empty($this->tasks) || empty($this->Tasks)) { return true; } - - $tasks = $this->tasks; - if (!is_array($tasks)) { - $tasks = array($tasks); + $tasks = TaskCollection::normalizeObjectArray((array)$this->tasks); + foreach ($tasks as $task => $properties) { + $this->{$task} = $this->Tasks->load($properties['class'], $properties['settings']); + $this->taskNames[] = $task; } - - foreach ($tasks as $taskName) { - $task = Inflector::underscore($taskName); - $taskClass = Inflector::camelize($taskName . 'Task'); - - if (!class_exists($taskClass)) { - foreach ($this->Dispatch->shellPaths as $path) { - $taskPath = $path . 'tasks' . DS . $task . '.php'; - if (file_exists($taskPath)) { - require_once $taskPath; - break; - } - } - } - $taskClassCheck = $taskClass; - if (ClassRegistry::isKeySet($taskClassCheck)) { - $this->taskNames[] = $taskName; - $this->{$taskName} = ClassRegistry::getObject($taskClassCheck); - } else { - $this->taskNames[] = $taskName; - $this->{$taskName} = new $taskClass($this->Dispatch); - } - - if (!isset($this->{$taskName})) { - $this->err("Task `{$taskName}` could not be loaded"); - $this->_stop(); - } - } - return true; } diff --git a/cake/tests/cases/console/cake.test.php b/cake/tests/cases/console/cake.test.php index eaa55c3c6..8ed92c6e4 100644 --- a/cake/tests/cases/console/cake.test.php +++ b/cake/tests/cases/console/cake.test.php @@ -562,7 +562,7 @@ class ShellDispatcherTest extends CakeTestCase { $this->assertTrue($result); $this->assertEqual($Dispatcher->args, array('dispatch')); - $Shell = new MockWithMainShell($Dispather); + $Shell = new MockWithMainShell($Dispatcher); $this->mockObjects[] = $Shell; $Shell->expects($this->once())->method('main')->will($this->returnValue(true)); $Shell->expects($this->once())->method('startup'); @@ -573,7 +573,7 @@ class ShellDispatcherTest extends CakeTestCase { $this->assertTrue($result); $this->assertEqual($Dispatcher->args, array('idontexist')); - $Shell = new MockWithMainShell($Dispather); + $Shell = new MockWithMainShell($Dispatcher); $this->mockObjects[] = $Shell; $Shell->expects($this->never())->method('main'); $Shell->expects($this->never())->method('startup'); @@ -606,8 +606,7 @@ class ShellDispatcherTest extends CakeTestCase { $this->assertFalse($result); $this->assertEqual($Dispatcher->args, array()); - - $Shell = new MockWithoutMainShell($Dispather); + $Shell = new MockWithoutMainShell($Dispatcher); $this->mockObjects[] = $Shell; $Shell->expects($this->once())->method('initDb')->will($this->returnValue(true)); $Shell->expects($this->once())->method('initialize'); @@ -620,7 +619,7 @@ class ShellDispatcherTest extends CakeTestCase { $this->assertTrue($result); $this->assertEqual($Dispatcher->args, array()); - $Shell = new MockWithoutMainShell($Dispather); + $Shell = new MockWithoutMainShell($Dispatcher); $this->mockObjects[] = $Shell; $Shell->expects($this->never())->method('hr'); $Shell->expects($this->never())->method('startup'); @@ -631,7 +630,7 @@ class ShellDispatcherTest extends CakeTestCase { $this->assertFalse($result); $this->assertEqual($Dispatcher->args, array('hr')); - $Shell = new MockWithoutMainShell($Dispather); + $Shell = new MockWithoutMainShell($Dispatcher); $this->mockObjects[] = $Shell; $Shell->expects($this->never())->method('startup'); $Dispatcher->TestShell = $Shell; @@ -640,7 +639,7 @@ class ShellDispatcherTest extends CakeTestCase { $result = $Dispatcher->dispatch(); $this->assertFalse($result); - $Shell = new MockWithoutMainShell($Dispather); + $Shell = new MockWithoutMainShell($Dispatcher); $this->mockObjects[] = $Shell; $Shell->expects($this->never())->method('startup'); $Dispatcher->TestShell = $Shell; @@ -649,7 +648,7 @@ class ShellDispatcherTest extends CakeTestCase { $result = $Dispatcher->dispatch(); $this->assertFalse($result); - $Shell = new MockWithoutMainShell($Dispather); + $Shell = new MockWithoutMainShell($Dispatcher); $this->mockObjects[] = $Shell; $Shell->expects($this->never())->method('startup'); $Shell->expects($this->never())->method('_secret'); @@ -831,7 +830,7 @@ class ShellDispatcherTest extends CakeTestCase { $Week = $this->getMock('Shell', $mainMethods, array(&$Dispatcher), 'MockWeekShell'); $Sunday = $this->getMock('Shell', $executeMethods, array(&$Dispatcher), 'MockOnSundayTask'); - $Shell = new MockWeekShell($Dispather); + $Shell = new MockWeekShell($Dispatcher); $this->mockObjects[] = $Shell; $Shell->expects($this->once())->method('initialize'); $Shell->expects($this->once())->method('loadTasks'); diff --git a/cake/tests/cases/console/libs/shell.test.php b/cake/tests/cases/console/libs/shell.test.php index fdebb0fe2..bc711fe75 100644 --- a/cake/tests/cases/console/libs/shell.test.php +++ b/cake/tests/cases/console/libs/shell.test.php @@ -360,7 +360,7 @@ class ShellTest extends CakeTestCase { * * @return void */ - public function testLoadTasks() { + public function testLoadTasks() { $this->assertTrue($this->Shell->loadTasks()); $this->Shell->tasks = null; @@ -375,12 +375,7 @@ class ShellTest extends CakeTestCase { $this->Shell->tasks = array(); $this->assertTrue($this->Shell->loadTasks()); - // Fatal Error - // $this->Shell->tasks = 'TestIDontExist'; - // $this->assertFalse($this->Shell->loadTasks()); - // $this->assertFalse(isset($this->Shell->TestIDontExist)); - - $this->Shell->tasks = 'TestApple'; + $this->Shell->tasks = array('TestApple'); $this->assertTrue($this->Shell->loadTasks()); $this->assertIsA($this->Shell->TestApple, 'TestAppleTask');