mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
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.
This commit is contained in:
parent
ffbb4e6b45
commit
317e32f07b
4 changed files with 84 additions and 191 deletions
|
@ -307,12 +307,6 @@ class ShellDispatcher {
|
||||||
|
|
||||||
$Shell = $this->_getShell($plugin);
|
$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();
|
$methods = array();
|
||||||
|
|
||||||
if (is_a($Shell, 'Shell')) {
|
if (is_a($Shell, 'Shell')) {
|
||||||
|
@ -360,10 +354,7 @@ class ShellDispatcher {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$title = sprintf(__('Error: Unknown %1$s command %2$s.'), $this->shellName, $arg);
|
throw new MissingShellMethodException(array('shell' => $this->shell, 'method' => $arg));
|
||||||
$message = sprintf(__('For usage try `cake %s help`'), $this->shell);
|
|
||||||
$this->stderr($title . "\n" . $message . "\n");
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -386,7 +377,7 @@ class ShellDispatcher {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!isset($loaded)) {
|
if (!isset($loaded)) {
|
||||||
return false;
|
throw new MissingShellFileException(array('shell' => $this->shell . '.php'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!class_exists('Shell')) {
|
if (!class_exists('Shell')) {
|
||||||
|
@ -397,7 +388,7 @@ class ShellDispatcher {
|
||||||
require $this->shellPath;
|
require $this->shellPath;
|
||||||
}
|
}
|
||||||
if (!class_exists($this->shellClass)) {
|
if (!class_exists($this->shellClass)) {
|
||||||
return false;
|
throw new MissingShellClassException(array('shell' => $this->shell));
|
||||||
}
|
}
|
||||||
$Shell = new $this->shellClass($this);
|
$Shell = new $this->shellClass($this);
|
||||||
return $Shell;
|
return $Shell;
|
||||||
|
|
|
@ -313,6 +313,33 @@ class MissingTaskClassException extends CakeException {
|
||||||
protected $_messageTemplate = 'Task class "%s" is missing.';
|
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
|
* Exception class to be thrown when a database table is not found in the datasource
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* ShellDispatcherTest file
|
* ConsoleOutputTest file
|
||||||
*
|
*
|
||||||
* PHP 5
|
* PHP 5
|
||||||
*
|
*
|
||||||
|
|
|
@ -519,53 +519,67 @@ class ShellDispatcherTest extends CakeTestCase {
|
||||||
$result = $Dispatcher->dispatch();
|
$result = $Dispatcher->dispatch();
|
||||||
$this->assertNull($result);
|
$this->assertNull($result);
|
||||||
$this->assertEqual($Dispatcher->args, array());
|
$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));
|
$methods = get_class_methods('Shell');
|
||||||
$Shell->expects($this->never())->method('hr');
|
array_push($methods, 'main', '_secret');
|
||||||
$Shell->expects($this->once())->method('startup');
|
|
||||||
$Shell->expects($this->once())->method('main');
|
|
||||||
$Dispatcher->TestShell = $Shell;
|
|
||||||
|
|
||||||
$Dispatcher->args = array('mock_with_main', 'hr');
|
$Shell = $this->getMock('Shell', $methods, array(&$Dispatcher), 'MissingShellPrivateMethod');
|
||||||
$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->expects($this->never())->method('main');
|
$Shell->expects($this->never())->method('main');
|
||||||
$Shell->expects($this->never())->method('startup');
|
$Shell->expects($this->never())->method('startup');
|
||||||
$Shell->expects($this->never())->method('_secret');
|
$Shell->expects($this->never())->method('_secret');
|
||||||
$Dispatcher->TestShell = $Shell;
|
$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();
|
$result = $Dispatcher->dispatch();
|
||||||
$this->assertFalse($result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -579,16 +593,6 @@ class ShellDispatcherTest extends CakeTestCase {
|
||||||
array_push($methods, 'initDb', '_secret');
|
array_push($methods, 'initDb', '_secret');
|
||||||
$Shell = $this->getMock('Shell', $methods, array(&$Dispatcher), 'MockWithoutMainShell');
|
$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);
|
$Shell = new MockWithoutMainShell($Dispatcher);
|
||||||
$this->mockObjects[] = $Shell;
|
$this->mockObjects[] = $Shell;
|
||||||
$Shell->expects($this->once())->method('initDb')->will($this->returnValue(true));
|
$Shell->expects($this->once())->method('initDb')->will($this->returnValue(true));
|
||||||
|
@ -601,45 +605,6 @@ class ShellDispatcherTest extends CakeTestCase {
|
||||||
$result = $Dispatcher->dispatch();
|
$result = $Dispatcher->dispatch();
|
||||||
$this->assertTrue($result);
|
$this->assertTrue($result);
|
||||||
$this->assertEqual($Dispatcher->args, array());
|
$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();
|
$Dispatcher = new TestShellDispatcher();
|
||||||
$methods = get_class_methods('Object');
|
$methods = get_class_methods('Object');
|
||||||
array_push($methods, 'main', 'initdb', 'initialize', 'loadTasks', 'startup', '_secret');
|
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('initialize');
|
||||||
$Shell->expects($this->never())->method('loadTasks');
|
$Shell->expects($this->never())->method('loadTasks');
|
||||||
|
@ -673,51 +638,6 @@ class ShellDispatcherTest extends CakeTestCase {
|
||||||
$Dispatcher->args = array('mock_with_main_not_a', 'initdb');
|
$Dispatcher->args = array('mock_with_main_not_a', 'initdb');
|
||||||
$result = $Dispatcher->dispatch();
|
$result = $Dispatcher->dispatch();
|
||||||
$this->assertTrue($result);
|
$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');
|
$Dispatcher->args = array('mock_without_main_not_a', 'initdb');
|
||||||
$result = $Dispatcher->dispatch();
|
$result = $Dispatcher->dispatch();
|
||||||
$this->assertTrue($result);
|
$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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue