Skip to content

Commit

Permalink
Making ShellDispatcher use exceptions instead of returning false and …
Browse files Browse the repository at this point in the history
…doing other goofy things.

Adding MissingShellMethodException, MissingShellClassException and MissingShellFileException for use with ShellDispatcher.
Removing duplicated tests, and refactoring them into separate tests with expected exceptions.
  • Loading branch information
markstory committed Oct 14, 2010
1 parent ffbb4e6 commit 317e32f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 183 deletions.
15 changes: 3 additions & 12 deletions cake/console/shell_dispatcher.php
Expand Up @@ -307,12 +307,6 @@ public function dispatch() {

$Shell = $this->_getShell($plugin);

if (!$Shell) {
$title = sprintf(__('Error: Class %s could not be loaded.'), $this->shellClass);
$this->stderr($title . "\n");
return false;
}

$methods = array();

if (is_a($Shell, 'Shell')) {
Expand Down Expand Up @@ -360,10 +354,7 @@ public function dispatch() {
}
}

$title = sprintf(__('Error: Unknown %1$s command %2$s.'), $this->shellName, $arg);
$message = sprintf(__('For usage try `cake %s help`'), $this->shell);
$this->stderr($title . "\n" . $message . "\n");
return false;
throw new MissingShellMethodException(array('shell' => $this->shell, 'method' => $arg));
}

/**
Expand All @@ -386,7 +377,7 @@ protected function _getShell($plugin = null) {
}
}
if (!isset($loaded)) {
return false;
throw new MissingShellFileException(array('shell' => $this->shell . '.php'));
}

if (!class_exists('Shell')) {
Expand All @@ -397,7 +388,7 @@ protected function _getShell($plugin = null) {
require $this->shellPath;
}
if (!class_exists($this->shellClass)) {
return false;
throw new MissingShellClassException(array('shell' => $this->shell));
}
$Shell = new $this->shellClass($this);
return $Shell;
Expand Down
27 changes: 27 additions & 0 deletions cake/libs/exceptions.php
Expand Up @@ -313,6 +313,33 @@ class MissingTaskClassException extends CakeException {
protected $_messageTemplate = 'Task class "%s" is missing.';
}

/**
* Used when a shell method cannot be found.
*
* @package cake.libs
*/
class MissingShellMethodException extends CakeException {
protected $_messageTemplate = "Unknown command %1\$s %2\$s.\nFor usage try `cake %1\$s help`";
}

/**
* Used when a shell class cannot be found.
*
* @package cake.libs
*/
class MissingShellClassException extends CakeException {
protected $_messageTemplate = "Shell class %s could not be loaded.";
}

/**
* Used when a shell class cannot be found.
*
* @package cake.libs
*/
class MissingShellFileException extends CakeException {
protected $_messageTemplate = "Shell file %s could not be loaded.";
}

/**
* Exception class to be thrown when a database table is not found in the datasource
*
Expand Down
2 changes: 1 addition & 1 deletion cake/tests/cases/console/console_output.test.php
@@ -1,6 +1,6 @@
<?php
/**
* ShellDispatcherTest file
* ConsoleOutputTest file
*
* PHP 5
*
Expand Down
215 changes: 45 additions & 170 deletions cake/tests/cases/console/shell_dispatcher.test.php
Expand Up @@ -519,53 +519,67 @@ public function testDispatchShellWithMain() {
$result = $Dispatcher->dispatch();
$this->assertNull($result);
$this->assertEqual($Dispatcher->args, array());
}

$Shell = new MockWithMainShell($Dispatcher);
$this->mockObjects[] = $Shell;
/**
* test missing shell exceptions on underscored (private methods)
*
* @expectedException MissingShellMethodException
* @return void
*/
function testMissingShellMethodExceptionPrivateMethod() {
$Dispatcher = new TestShellDispatcher();

$Shell->expects($this->once())->method('main')->will($this->returnValue(true));
$Shell->expects($this->never())->method('hr');
$Shell->expects($this->once())->method('startup');
$Shell->expects($this->once())->method('main');
$methods = get_class_methods('Shell');
array_push($methods, 'main', '_secret');

$Shell = $this->getMock('Shell', $methods, array(&$Dispatcher), 'MissingShellPrivateMethod');
$Shell->expects($this->never())->method('main');
$Shell->expects($this->never())->method('startup');
$Shell->expects($this->never())->method('_secret');
$Dispatcher->TestShell = $Shell;

$Dispatcher->args = array('mock_with_main', 'hr');
$Dispatcher->args = array('missing_shell_private_method', '_secret');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
$this->assertEqual($Dispatcher->args, array('hr'));
}

$Shell = new MockWithMainShell($Dispatcher);
$this->mockObjects[] = $Shell;
$Shell->expects($this->once())->method('main')->will($this->returnValue(true));
$Shell->expects($this->once())->method('startup');
/**
* test exception when calling shell class methods.
*
* @expectedException MissingShellMethodException
* @return void
*/
function testMissingShellMethodBaseClassMethod() {
$Dispatcher = new TestShellDispatcher();

$Shell = $this->getMock('Shell', array(), array(&$Dispatcher), 'MissingShellBaseClass');
$Shell->expects($this->never())->method('main');
$Shell->expects($this->never())->method('startup');
$Shell->expects($this->never())->method('hr');
$Dispatcher->TestShell = $Shell;

$Dispatcher->args = array('mock_with_main', 'dispatch');
$Dispatcher->args = array('missing_shell_base_class', 'hr');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
$this->assertEqual($Dispatcher->args, array('dispatch'));
}

$Shell = new MockWithMainShell($Dispatcher);
$this->mockObjects[] = $Shell;
$Shell->expects($this->once())->method('main')->will($this->returnValue(true));
$Shell->expects($this->once())->method('startup');
$Dispatcher->TestShell = $Shell;
/**
* test missing shell exception on missing method.
*
* @expectedException MissingShellMethodException
* @return void
*/
function testMissingShellMethodExceptionMissingMethod() {
$Dispatcher = new TestShellDispatcher();

$Dispatcher->args = array('mock_with_main', 'idontexist');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
$this->assertEqual($Dispatcher->args, array('idontexist'));
$methods = get_class_methods('Shell');

$Shell = new MockWithMainShell($Dispatcher);
$this->mockObjects[] = $Shell;
$Shell = $this->getMock('Shell', $methods, array(&$Dispatcher), 'MissingShellNoMethod');
$Shell->expects($this->never())->method('main');
$Shell->expects($this->never())->method('startup');
$Shell->expects($this->never())->method('_secret');
$Dispatcher->TestShell = $Shell;

$Dispatcher->args = array('mock_with_main', '_secret');
$Dispatcher->args = array('missing_shell_method_no_method', 'idontexist');
$result = $Dispatcher->dispatch();
$this->assertFalse($result);
}

/**
Expand All @@ -579,16 +593,6 @@ public function testDispatchShellWithoutMain() {
array_push($methods, 'initDb', '_secret');
$Shell = $this->getMock('Shell', $methods, array(&$Dispatcher), 'MockWithoutMainShell');

$Shell->expects($this->once())->method('initialize');
$Shell->expects($this->once())->method('loadTasks');
$Shell->expects($this->never())->method('startup');
$Dispatcher->TestShell = $Shell;

$Dispatcher->args = array('mock_without_main');
$result = $Dispatcher->dispatch();
$this->assertFalse($result);
$this->assertEqual($Dispatcher->args, array());

$Shell = new MockWithoutMainShell($Dispatcher);
$this->mockObjects[] = $Shell;
$Shell->expects($this->once())->method('initDb')->will($this->returnValue(true));
Expand All @@ -601,45 +605,6 @@ public function testDispatchShellWithoutMain() {
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
$this->assertEqual($Dispatcher->args, array());

$Shell = new MockWithoutMainShell($Dispatcher);
$this->mockObjects[] = $Shell;
$Shell->expects($this->never())->method('hr');
$Shell->expects($this->never())->method('startup');
$Dispatcher->TestShell = $Shell;

$Dispatcher->args = array('mock_without_main', 'hr');
$result = $Dispatcher->dispatch();
$this->assertFalse($result);
$this->assertEqual($Dispatcher->args, array('hr'));

$Shell = new MockWithoutMainShell($Dispatcher);
$this->mockObjects[] = $Shell;
$Shell->expects($this->never())->method('startup');
$Dispatcher->TestShell = $Shell;

$Dispatcher->args = array('mock_without_main', 'dispatch');
$result = $Dispatcher->dispatch();
$this->assertFalse($result);

$Shell = new MockWithoutMainShell($Dispatcher);
$this->mockObjects[] = $Shell;
$Shell->expects($this->never())->method('startup');
$Dispatcher->TestShell = $Shell;

$Dispatcher->args = array('mock_without_main', 'idontexist');
$result = $Dispatcher->dispatch();
$this->assertFalse($result);

$Shell = new MockWithoutMainShell($Dispatcher);
$this->mockObjects[] = $Shell;
$Shell->expects($this->never())->method('startup');
$Shell->expects($this->never())->method('_secret');
$Dispatcher->TestShell = $Shell;

$Dispatcher->args = array('mock_without_main', '_secret');
$result = $Dispatcher->dispatch();
$this->assertFalse($result);
}

/**
Expand All @@ -651,7 +616,7 @@ public function testDispatchNotAShellWithMain() {
$Dispatcher = new TestShellDispatcher();
$methods = get_class_methods('Object');
array_push($methods, 'main', 'initdb', 'initialize', 'loadTasks', 'startup', '_secret');
$Shell = $this->getMock('Object', $methods, array(&$Dispatcher), 'MockWithMainNotAShell');
$Shell = $this->getMock('Object', $methods, array(), 'MockWithMainNotAShell');

$Shell->expects($this->never())->method('initialize');
$Shell->expects($this->never())->method('loadTasks');
Expand All @@ -673,51 +638,6 @@ public function testDispatchNotAShellWithMain() {
$Dispatcher->args = array('mock_with_main_not_a', 'initdb');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);

$Shell = new MockWithMainNotAShell($Dispatcher);
$this->mockObjects[] = $Shell;
$Shell->expects($this->once())->method('main')->will($this->returnValue(true));
$Shell->expects($this->once())->method('startup');
$Dispatcher->TestShell = $Shell;

$Dispatcher->args = array('mock_with_main_not_a', 'hr');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
$this->assertEqual($Dispatcher->args, array('hr'));


$Shell = new MockWithMainNotAShell($Dispatcher);
$this->mockObjects[] = $Shell;
$Shell->expects($this->once())->method('main')->will($this->returnValue(true));
$Shell->expects($this->once())->method('startup');
$Dispatcher->TestShell = $Shell;

$Dispatcher->args = array('mock_with_main_not_a', 'dispatch');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
$this->assertEqual($Dispatcher->args, array('dispatch'));

$Shell = new MockWithMainNotAShell($Dispatcher);
$this->mockObjects[] = $Shell;
$Shell->expects($this->once())->method('main')->will($this->returnValue(true));
$Shell->expects($this->once())->method('startup');
$Dispatcher->TestShell = $Shell;

$Dispatcher->args = array('mock_with_main_not_a', 'idontexist');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
$this->assertEqual($Dispatcher->args, array('idontexist'));

$Shell = new MockWithMainNotAShell($Dispatcher);
$this->mockObjects[] = $Shell;
$Shell->expects($this->never())->method('_secret');
$Shell->expects($this->never())->method('main');
$Shell->expects($this->never())->method('startup');
$Dispatcher->TestShell = $Shell;

$Dispatcher->args = array('mock_with_main_not_a', '_secret');
$result = $Dispatcher->dispatch();
$this->assertFalse($result);
}

/**
Expand Down Expand Up @@ -751,51 +671,6 @@ public function testDispatchNotAShellWithoutMain() {
$Dispatcher->args = array('mock_without_main_not_a', 'initdb');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);

$Shell = new MockWithoutMainNotAShell($Dispatcher);
$this->mockObjects[] = $Shell;
$Shell->expects($this->once())->method('main')->will($this->returnValue(true));
$Shell->expects($this->once())->method('startup');
$Dispatcher->TestShell = $Shell;

$Dispatcher->args = array('mock_without_main_not_a', 'hr');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
$this->assertEqual($Dispatcher->args, array('hr'));


$Shell = new MockWithoutMainNotAShell($Dispatcher);
$this->mockObjects[] = $Shell;
$Shell->expects($this->once())->method('main')->will($this->returnValue(true));
$Shell->expects($this->once())->method('startup');
$Dispatcher->TestShell = $Shell;

$Dispatcher->args = array('mock_without_main_not_a', 'dispatch');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
$this->assertEqual($Dispatcher->args, array('dispatch'));

$Shell = new MockWithoutMainNotAShell($Dispatcher);
$this->mockObjects[] = $Shell;
$Shell->expects($this->once())->method('main')->will($this->returnValue(true));
$Shell->expects($this->once())->method('startup');
$Dispatcher->TestShell = $Shell;

$Dispatcher->args = array('mock_without_main_not_a', 'idontexist');
$result = $Dispatcher->dispatch();
$this->assertTrue($result);
$this->assertEqual($Dispatcher->args, array('idontexist'));

$Shell = new MockWithoutMainNotAShell($Dispatcher);
$this->mockObjects[] = $Shell;
$Shell->expects($this->never())->method('_secret');
$Shell->expects($this->never())->method('main');
$Shell->expects($this->never())->method('startup');
$Dispatcher->TestShell = $Shell;

$Dispatcher->args = array('mock_without_main_not_a', '_secret');
$result = $Dispatcher->dispatch();
$this->assertFalse($result);
}

/**
Expand Down

0 comments on commit 317e32f

Please sign in to comment.