From 317e32f07bc877b00f42ba38d14c66d704417bba Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 3 Oct 2010 23:00:48 -0400 Subject: [PATCH] Making ShellDispatcher use exceptions instead of returning false and 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. --- cake/console/shell_dispatcher.php | 15 +- cake/libs/exceptions.php | 27 ++ .../cases/console/console_output.test.php | 2 +- .../cases/console/shell_dispatcher.test.php | 231 ++++-------------- 4 files changed, 84 insertions(+), 191 deletions(-) diff --git a/cake/console/shell_dispatcher.php b/cake/console/shell_dispatcher.php index d604f15d8..d5d0bc233 100644 --- a/cake/console/shell_dispatcher.php +++ b/cake/console/shell_dispatcher.php @@ -307,12 +307,6 @@ class ShellDispatcher { $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')) { @@ -360,10 +354,7 @@ class ShellDispatcher { } } - $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)); } /** @@ -386,7 +377,7 @@ class ShellDispatcher { } } if (!isset($loaded)) { - return false; + throw new MissingShellFileException(array('shell' => $this->shell . '.php')); } if (!class_exists('Shell')) { @@ -397,7 +388,7 @@ class ShellDispatcher { require $this->shellPath; } if (!class_exists($this->shellClass)) { - return false; + throw new MissingShellClassException(array('shell' => $this->shell)); } $Shell = new $this->shellClass($this); return $Shell; diff --git a/cake/libs/exceptions.php b/cake/libs/exceptions.php index daa1e7f39..7fdd069a9 100644 --- a/cake/libs/exceptions.php +++ b/cake/libs/exceptions.php @@ -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 * diff --git a/cake/tests/cases/console/console_output.test.php b/cake/tests/cases/console/console_output.test.php index 7509dfbe0..00cf0822e 100644 --- a/cake/tests/cases/console/console_output.test.php +++ b/cake/tests/cases/console/console_output.test.php @@ -1,6 +1,6 @@ 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'); - $Dispatcher->TestShell = $Shell; + $methods = get_class_methods('Shell'); + array_push($methods, 'main', '_secret'); - $Dispatcher->args = array('mock_with_main', 'hr'); - $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'); - $Dispatcher->TestShell = $Shell; - - $Dispatcher->args = array('mock_with_main', 'dispatch'); - $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; - - $Dispatcher->args = array('mock_with_main', 'idontexist'); - $result = $Dispatcher->dispatch(); - $this->assertTrue($result); - $this->assertEqual($Dispatcher->args, array('idontexist')); - - $Shell = new MockWithMainShell($Dispatcher); - $this->mockObjects[] = $Shell; + $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', '_secret'); + $Dispatcher->args = array('missing_shell_private_method', '_secret'); + $result = $Dispatcher->dispatch(); + } + +/** + * 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('missing_shell_base_class', 'hr'); + $result = $Dispatcher->dispatch(); + } + +/** + * test missing shell exception on missing method. + * + * @expectedException MissingShellMethodException + * @return void + */ + function testMissingShellMethodExceptionMissingMethod() { + $Dispatcher = new TestShellDispatcher(); + + $methods = get_class_methods('Shell'); + + $Shell = $this->getMock('Shell', $methods, array(&$Dispatcher), 'MissingShellNoMethod'); + $Shell->expects($this->never())->method('main'); + $Shell->expects($this->never())->method('startup'); + $Dispatcher->TestShell = $Shell; + + $Dispatcher->args = array('missing_shell_method_no_method', 'idontexist'); $result = $Dispatcher->dispatch(); - $this->assertFalse($result); } /** @@ -579,16 +593,6 @@ class ShellDispatcherTest extends CakeTestCase { 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)); @@ -601,45 +605,6 @@ class ShellDispatcherTest extends CakeTestCase { $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); } /** @@ -651,7 +616,7 @@ class ShellDispatcherTest extends CakeTestCase { $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'); @@ -673,51 +638,6 @@ class ShellDispatcherTest extends CakeTestCase { $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); } /** @@ -751,51 +671,6 @@ class ShellDispatcherTest extends CakeTestCase { $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); } /**