Fixing ShellDispatcher tests and making ShellDispatcher/Shell use TaskCollection to loadTasks.

This commit is contained in:
mark_story 2010-07-07 21:35:03 -04:00
parent cb3c7feb5c
commit e0acd2131f
4 changed files with 43 additions and 54 deletions

View file

@ -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.
*

View file

@ -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;
}

View file

@ -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');

View file

@ -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');