mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Fixing notice errors in ShellDispatcher
Removing ShellDispatcher tests that are now inside Shell. Changing exception type to match the one used in ShellDispatcher.
This commit is contained in:
parent
e70089891d
commit
193721a0eb
4 changed files with 45 additions and 159 deletions
|
@ -329,9 +329,19 @@ class Shell extends Object {
|
|||
* @return void
|
||||
*/
|
||||
public function runCommand($command, $argv) {
|
||||
if (!empty($command) && $this->hasTask($command)) {
|
||||
$command = Inflector::camelize($command);
|
||||
$isTask = $this->hasTask($command);
|
||||
$isMethod = $this->hasMethod($command);
|
||||
$isMain = $this->hasMethod('main');
|
||||
|
||||
if ($isTask || $isMethod) {
|
||||
array_shift($argv);
|
||||
}
|
||||
if ($isTask || $isMethod || $isMain) {
|
||||
$this->startup();
|
||||
}
|
||||
|
||||
if ($isTask) {
|
||||
$command = Inflector::camelize($command);
|
||||
return $this->{$command}->runCommand('execute', $argv);
|
||||
}
|
||||
|
||||
|
@ -340,14 +350,14 @@ class Shell extends Object {
|
|||
if (isset($this->params['help'])) {
|
||||
return $this->out($this->parser->help());
|
||||
}
|
||||
if ($this->hasMethod($command)) {
|
||||
array_shift($argv);
|
||||
|
||||
if ($isMethod) {
|
||||
return $this->{$command}();
|
||||
}
|
||||
if ($this->hasMethod('main')) {
|
||||
if ($isMain) {
|
||||
return $this->main();
|
||||
}
|
||||
throw new RuntimeException(sprintf(__('Unhandled method `%s`'), $command));
|
||||
throw new MissingShellMethodException(array('shell' => get_class($this), 'method' => $command));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -269,12 +269,11 @@ class ShellDispatcher {
|
|||
if ($Shell instanceof Shell) {
|
||||
$Shell->initialize();
|
||||
$Shell->loadTasks();
|
||||
$Shell->startup();
|
||||
return $Shell->runCommand($command, $this->args);
|
||||
}
|
||||
$methods = array_diff(get_class_methods($Shell), get_class_methods('Shell'));
|
||||
$added = in_array($command, $methods);
|
||||
$private = $arg[0] == '_' && method_exists($Shell, $command);
|
||||
$private = $command[0] == '_' && method_exists($Shell, $command);
|
||||
|
||||
if (!$private) {
|
||||
if ($added) {
|
||||
|
|
|
@ -620,7 +620,7 @@ class ShellTest extends CakeTestCase {
|
|||
/**
|
||||
* test run command causing exception on Shell method.
|
||||
*
|
||||
* @expectedException RuntimeException
|
||||
* @expectedException MissingShellMethodException
|
||||
* @return void
|
||||
*/
|
||||
function testRunCommandBaseclassMethod() {
|
||||
|
@ -631,6 +631,20 @@ class ShellTest extends CakeTestCase {
|
|||
$result = $Mock->runCommand('hr', array());
|
||||
}
|
||||
|
||||
/**
|
||||
* test run command causing exception on Shell method.
|
||||
*
|
||||
* @expectedException MissingShellMethodException
|
||||
* @return void
|
||||
*/
|
||||
function testRunCommandMissingMethod() {
|
||||
$methods = get_class_methods('Shell');
|
||||
$Mock = $this->getMock('Shell', array('startup'), array(), '', false);
|
||||
|
||||
$Mock->expects($this->never())->method('idontexist');
|
||||
$result = $Mock->runCommand('idontexist', array());
|
||||
}
|
||||
|
||||
/**
|
||||
* test that a --help causes help to show.
|
||||
*
|
||||
|
@ -657,11 +671,12 @@ class ShellTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
function testRunCommandHittingTask() {
|
||||
$Shell = $this->getMock('Shell', array('hasTask'), array(), '', false);
|
||||
$Shell = $this->getMock('Shell', array('hasTask', 'startup'), array(), '', false);
|
||||
$task = $this->getMock('Shell', array('execute', 'runCommand'), array(), '', false);
|
||||
$task->expects($this->any())->method('runCommand')
|
||||
->with('execute', array('one', 'value'));
|
||||
|
||||
$Shell->expects($this->once())->method('startup');
|
||||
$Shell->expects($this->any())->method('hasTask')->will($this->returnValue(true));
|
||||
$Shell->RunCommand = $task;
|
||||
|
||||
|
|
|
@ -438,103 +438,20 @@ class ShellDispatcherTest extends CakeTestCase {
|
|||
*/
|
||||
public function testDispatchShellWithMain() {
|
||||
$Dispatcher = new TestShellDispatcher();
|
||||
$methods = get_class_methods('Shell');
|
||||
array_push($methods, 'main', '_secret');
|
||||
$Mock = $this->getMock('Shell', $methods, array(&$Dispatcher), 'MockWithMainShell');
|
||||
$Mock = $this->getMock('Shell', array(), array(&$Dispatcher), 'MockWithMainShell');
|
||||
|
||||
$Mock->expects($this->once())->method('main')->will($this->returnValue(true));
|
||||
$Mock->expects($this->once())->method('initialize');
|
||||
$Mock->expects($this->once())->method('loadTasks');
|
||||
$Mock->expects($this->once())->method('startup');
|
||||
$Mock->expects($this->once())->method('runCommand')
|
||||
->with(null, array())
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$Dispatcher->TestShell = $Mock;
|
||||
|
||||
$Dispatcher->args = array('mock_with_main');
|
||||
$result = $Dispatcher->dispatch();
|
||||
$this->assertTrue($result);
|
||||
$this->assertEqual($Dispatcher->args, array());
|
||||
|
||||
$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', 'initdb');
|
||||
$result = $Dispatcher->dispatch();
|
||||
$this->assertTrue($result);
|
||||
$this->assertEqual($Dispatcher->args, array('initdb'));
|
||||
|
||||
$Shell = new MockWithMainShell($Dispatcher);
|
||||
$this->mockObjects[] = $Shell;
|
||||
$Shell->expects($this->once())->method('startup');
|
||||
$Shell->expects($this->once())->method('help');
|
||||
$Dispatcher->TestShell = $Shell;
|
||||
|
||||
$Dispatcher->args = array('mock_with_main', 'help');
|
||||
$result = $Dispatcher->dispatch();
|
||||
$this->assertNull($result);
|
||||
$this->assertEqual($Dispatcher->args, array());
|
||||
}
|
||||
|
||||
/**
|
||||
* test missing shell exceptions on underscored (private methods)
|
||||
*
|
||||
* @expectedException MissingShellMethodException
|
||||
* @return void
|
||||
*/
|
||||
function testMissingShellMethodExceptionPrivateMethod() {
|
||||
$Dispatcher = new TestShellDispatcher();
|
||||
|
||||
$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('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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -544,22 +461,22 @@ class ShellDispatcherTest extends CakeTestCase {
|
|||
*/
|
||||
public function testDispatchShellWithoutMain() {
|
||||
$Dispatcher = new TestShellDispatcher();
|
||||
$methods = get_class_methods('Shell');
|
||||
array_push($methods, 'initDb', '_secret');
|
||||
$Shell = $this->getMock('Shell', $methods, array(&$Dispatcher), 'MockWithoutMainShell');
|
||||
$Shell = $this->getMock('Shell', array(), array(&$Dispatcher), 'MockWithoutMainShell');
|
||||
|
||||
$Shell = new MockWithoutMainShell($Dispatcher);
|
||||
$this->mockObjects[] = $Shell;
|
||||
$Shell->expects($this->once())->method('initDb')->will($this->returnValue(true));
|
||||
|
||||
$Shell->expects($this->once())->method('initialize');
|
||||
$Shell->expects($this->once())->method('loadTasks');
|
||||
$Shell->expects($this->once())->method('startup');
|
||||
$Shell->expects($this->once())->method('runCommand')
|
||||
->with('initdb', array('initdb'))
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$Dispatcher->TestShell = $Shell;
|
||||
|
||||
$Dispatcher->args = array('mock_without_main', 'initdb');
|
||||
$result = $Dispatcher->dispatch();
|
||||
$this->assertTrue($result);
|
||||
$this->assertEqual($Dispatcher->args, array());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -628,61 +545,6 @@ class ShellDispatcherTest extends CakeTestCase {
|
|||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that a task is called instead of the shell if the first arg equals
|
||||
* the name of the task
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDispatchTask() {
|
||||
$Dispatcher = new TestShellDispatcher();
|
||||
$mainMethods = $executeMethods = get_class_methods('Shell');
|
||||
array_push($mainMethods, 'main');
|
||||
array_push($executeMethods, 'execute');
|
||||
|
||||
$Week = $this->getMock('Shell', $mainMethods, array(&$Dispatcher), 'MockWeekShell');
|
||||
$Sunday = $this->getMock('Shell', $executeMethods, array(&$Dispatcher), 'MockOnSundayTask');
|
||||
|
||||
$Shell = new MockWeekShell($Dispatcher);
|
||||
$this->mockObjects[] = $Shell;
|
||||
$Shell->expects($this->once())->method('initialize');
|
||||
$Shell->expects($this->once())->method('loadTasks');
|
||||
$Shell->expects($this->never())->method('startup');
|
||||
$Shell->expects($this->never())->method('main');
|
||||
|
||||
$Task = new MockOnSundayTask($Dispatcher);
|
||||
$this->mockObjects[] = $Task;
|
||||
$Task->expects($this->once())->method('execute')->will($this->returnValue(true));
|
||||
$Task->expects($this->once())->method('initialize');;
|
||||
$Task->expects($this->once())->method('loadTasks');
|
||||
$Task->expects($this->once())->method('startup');
|
||||
$Task->expects($this->once())->method('execute');
|
||||
|
||||
$Shell->MockOnSunday = $Task;
|
||||
$Shell->taskNames = array('MockOnSunday');
|
||||
$Dispatcher->TestShell = $Shell;
|
||||
|
||||
$Dispatcher->args = array('mock_week', 'mock_on_sunday');
|
||||
$result = $Dispatcher->dispatch();
|
||||
$this->assertTrue($result);
|
||||
$this->assertEqual($Dispatcher->args, array());
|
||||
|
||||
$Shell = new MockWeekShell($Dispatcher);
|
||||
$Task = new MockOnSundayTask($Dispatcher);
|
||||
array_push($this->mockObjects, $Shell, $Task);
|
||||
|
||||
$Task->expects($this->never())->method('execute');
|
||||
$Task->expects($this->once())->method('help');
|
||||
|
||||
$Shell->MockOnSunday = $Task;
|
||||
$Shell->taskNames = array('MockOnSunday');
|
||||
$Dispatcher->TestShell = $Shell;
|
||||
|
||||
$Dispatcher->args = array('mock_week', 'mock_on_sunday', 'help');
|
||||
$result = $Dispatcher->dispatch();
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify shifting of arguments
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue