Skip to content
Permalink
Browse files

Fixing ShellDispatcher tests and making ShellDispatcher/Shell use Tas…

…kCollection to loadTasks.
  • Loading branch information...
markstory committed Jul 8, 2010
1 parent cb3c7fe commit e0acd2131f7d580038279629923810c7ade2041f
@@ -119,6 +119,13 @@ class ShellDispatcher {
*/
public $shellName = null;
/**
* TaskCollection object for the command
*
* @var TaskCollection
*/
protected $_Tasks;
/**
* Constructor
*
@@ -422,6 +429,18 @@ protected function _getShell($plugin = null) {
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.
*
@@ -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 @@ function __construct(&$dispatch) {
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 @@ protected function _loadModels() {
* @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);
}
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();
}
$tasks = TaskCollection::normalizeObjectArray((array)$this->tasks);
foreach ($tasks as $task => $properties) {
$this->{$task} = $this->Tasks->load($properties['class'], $properties['settings']);
$this->taskNames[] = $task;
}
return true;
}
@@ -562,7 +562,7 @@ public function testDispatchShellWithMain() {
$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 @@ public function testDispatchShellWithMain() {
$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 @@ public function testDispatchShellWithoutMain() {
$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 @@ public function testDispatchShellWithoutMain() {
$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 @@ public function testDispatchShellWithoutMain() {
$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 @@ public function testDispatchShellWithoutMain() {
$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 @@ public function testDispatchShellWithoutMain() {
$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 @@ public function testDispatchTask() {
$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');
@@ -360,7 +360,7 @@ public function testError() {
*
* @return void
*/
public function testLoadTasks() {
public function testLoadTasks() {
$this->assertTrue($this->Shell->loadTasks());
$this->Shell->tasks = null;
@@ -375,12 +375,7 @@ public function testLoadTasks() {
$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');

0 comments on commit e0acd21

Please sign in to comment.
You can’t perform that action at this time.