Adding tests for task methods in runCommand.

Moving startup() call to the dispatcher so nested runCommand calls don't double output the startup content.
This commit is contained in:
mark_story 2010-10-09 23:30:56 -04:00
parent cea9dadaa2
commit 79d1739778
3 changed files with 18 additions and 5 deletions

View file

@ -329,8 +329,8 @@ class Shell extends Object {
* @return void * @return void
*/ */
public function runCommand($command, $argv) { public function runCommand($command, $argv) {
$this->startup();
if (!empty($command) && $this->hasTask($command)) { if (!empty($command) && $this->hasTask($command)) {
$command = Inflector::camelize($command);
return $this->{$command}->runCommand('execute', $argv); return $this->{$command}->runCommand('execute', $argv);
} }

View file

@ -269,6 +269,7 @@ class ShellDispatcher {
if ($Shell instanceof Shell) { if ($Shell instanceof Shell) {
$Shell->initialize(); $Shell->initialize();
$Shell->loadTasks(); $Shell->loadTasks();
$Shell->startup();
return $Shell->runCommand($command, $this->args); return $Shell->runCommand($command, $this->args);
} }
$methods = array_diff(get_class_methods($Shell), get_class_methods('Shell')); $methods = array_diff(get_class_methods($Shell), get_class_methods('Shell'));

View file

@ -613,7 +613,6 @@ class ShellTest extends CakeTestCase {
$Mock = $this->getMock('Shell', array('main', 'startup'), array(), '', false); $Mock = $this->getMock('Shell', array('main', 'startup'), array(), '', false);
$Mock->expects($this->once())->method('main')->will($this->returnValue(true)); $Mock->expects($this->once())->method('main')->will($this->returnValue(true));
$Mock->expects($this->once())->method('startup');
$result = $Mock->runCommand(null, array()); $result = $Mock->runCommand(null, array());
$this->assertTrue($result); $this->assertTrue($result);
} }
@ -628,7 +627,6 @@ class ShellTest extends CakeTestCase {
$methods = get_class_methods('Shell'); $methods = get_class_methods('Shell');
$Mock = $this->getMock('Shell', array('startup'), array(), '', false); $Mock = $this->getMock('Shell', array('startup'), array(), '', false);
$Mock->expects($this->once())->method('startup');
$Mock->expects($this->never())->method('hr'); $Mock->expects($this->never())->method('hr');
$result = $Mock->runCommand('hr', array()); $result = $Mock->runCommand('hr', array());
} }
@ -638,7 +636,7 @@ class ShellTest extends CakeTestCase {
* *
* @return void * @return void
*/ */
function testHelpParamTriggeringHelp() { function testRunCommandTriggeringHelp() {
$Parser = $this->getMock('ConsoleOptionParser', array(), array(), '', false); $Parser = $this->getMock('ConsoleOptionParser', array(), array(), '', false);
$Parser->expects($this->once())->method('parse') $Parser->expects($this->once())->method('parse')
->with(array('--help')) ->with(array('--help'))
@ -649,8 +647,22 @@ class ShellTest extends CakeTestCase {
$Shell->expects($this->once())->method('_getOptionParser') $Shell->expects($this->once())->method('_getOptionParser')
->will($this->returnValue($Parser)); ->will($this->returnValue($Parser));
$Shell->expects($this->once())->method('out'); $Shell->expects($this->once())->method('out');
$Shell->expects($this->once())->method('startup');
$Shell->runCommand(null, array('--help')); $Shell->runCommand(null, array('--help'));
} }
/**
* test that runCommand will call runCommand on the task.
*
* @return void
*/
function testRunCommandHittingTask() {
$Shell = $this->getMock('Shell', array('hasTask'), array(), '', false);
$task = $this->getMock('Shell', array('execute'), array(), '', false);
$Shell->tasks = array('RunCommand');
$Shell->expects($this->once())->method('hasTask')->will($this->returnValue(true));
$Shell->RunCommand = $task;
$Shell->runCommand('run_command', array());
}
} }