Browse Source

Pass positional arguments into shell methods.

Much like controllers, shells now get positional arguments as method
parameters. This makes it easier to work with positional arguments, as
you don't need to use $this->args.

Refs #3325
mark_story 12 years ago
parent
commit
9e0da22a58
2 changed files with 20 additions and 12 deletions
  1. 2 2
      src/Console/Shell.php
  2. 18 10
      tests/TestCase/Console/ShellTest.php

+ 2 - 2
src/Console/Shell.php

@@ -374,10 +374,10 @@ class Shell extends Object {
 			return $this->{$command}->runCommand('execute', $argv);
 		}
 		if ($isMethod) {
-			return $this->{$command}();
+			return call_user_func_array([$this, $command], $this->args);
 		}
 		if ($isMain) {
-			return $this->main();
+			return call_user_func_array([$this, 'main'], $this->args);
 		}
 		$this->out($this->OptionParser->help($command));
 		return false;

+ 18 - 10
tests/TestCase/Console/ShellTest.php

@@ -523,10 +523,14 @@ class ShellTest extends TestCase {
  * @return void
  */
 	public function testRunCommandMain() {
-		$Mock = $this->getMock('Cake\Console\Shell', array('main', 'startup'), array(), '', false);
+		$io = $this->getMock('Cake\Console\ConsoleIo');
+		$Mock = $this->getMock('Cake\Console\Shell', ['main', 'startup'], [$io]);
 
-		$Mock->expects($this->once())->method('main')->will($this->returnValue(true));
-		$result = $Mock->runCommand(null, array());
+		$Mock->expects($this->once())->method('startup');
+		$Mock->expects($this->once())->method('main')
+			->with('cakes')
+			->will($this->returnValue(true));
+		$result = $Mock->runCommand(null, ['cakes', '--verbose']);
 		$this->assertTrue($result);
 	}
 
@@ -536,10 +540,14 @@ class ShellTest extends TestCase {
  * @return void
  */
 	public function testRunCommandWithMethod() {
-		$Mock = $this->getMock('Cake\Console\Shell', array('hit_me', 'startup'), array(), '', false);
+		$io = $this->getMock('Cake\Console\ConsoleIo');
+		$Mock = $this->getMock('Cake\Console\Shell', ['hit_me', 'startup'], [$io]);
 
-		$Mock->expects($this->once())->method('hit_me')->will($this->returnValue(true));
-		$result = $Mock->runCommand('hit_me', array());
+		$Mock->expects($this->once())->method('startup');
+		$Mock->expects($this->once())->method('hit_me')
+			->with('cakes')
+			->will($this->returnValue(true));
+		$result = $Mock->runCommand('hit_me', ['hit_me', 'cakes', '--verbose']);
 		$this->assertTrue($result);
 	}
 
@@ -606,11 +614,11 @@ class ShellTest extends TestCase {
  * @return void
  */
 	public function testRunCommandHittingTask() {
-		$Shell = $this->getMock('Cake\Console\Shell', array('hasTask', 'startup'), array(), '', false);
-		$task = $this->getMock('Cake\Console\Shell', array('execute', 'runCommand'), array(), '', false);
+		$Shell = $this->getMock('Cake\Console\Shell', ['hasTask', 'startup'], [], '', false);
+		$task = $this->getMock('Cake\Console\Shell', ['execute', 'runCommand'], [], '', false);
 		$task->expects($this->any())
 			->method('runCommand')
-			->with('execute', array('one', 'value'));
+			->with('execute', ['one', 'value']);
 
 		$Shell->expects($this->once())->method('startup');
 		$Shell->expects($this->any())
@@ -619,7 +627,7 @@ class ShellTest extends TestCase {
 
 		$Shell->RunCommand = $task;
 
-		$Shell->runCommand('run_command', array('run_command', 'one', 'value'));
+		$Shell->runCommand('run_command', ['run_command', 'one', 'value']);
 	}
 
 /**