Skip to content

Commit

Permalink
Add rootName propagation from runner -> shell -> help
Browse files Browse the repository at this point in the history
Push custom root names all the way down to the help text for commands.
  • Loading branch information
markstory committed Jun 30, 2017
1 parent 261cdc3 commit 3d3c29c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Console/CommandRunner.php
Expand Up @@ -169,6 +169,7 @@ protected function getShell(ConsoleIo $io, CommandCollection $commands, $name)
if (method_exists($instance, 'setCommandCollection')) {
$instance->setCommandCollection($commands);
}
$instance->setRootName($this->root);

return $instance;
}
Expand Down
24 changes: 23 additions & 1 deletion src/Console/Shell.php
Expand Up @@ -162,6 +162,13 @@ class Shell
*/
protected $_io;

/**
* The root command name used when generating help output.
*
* @var string
*/
protected $rootName = 'cake';

/**
* Constructs this Shell instance.
*
Expand Down Expand Up @@ -190,6 +197,19 @@ public function __construct(ConsoleIo $io = null)
}
}

/**
* Set the root command name for help output.
*
* @param string $name The name of the root command.
* @return $this
*/
public function setRootName($name)
{
$this->rootName = (string)$name;

return $this;
}

/**
* Get the io object for this shell.
*
Expand Down Expand Up @@ -550,8 +570,10 @@ protected function _displayHelp($command)
public function getOptionParser()
{
$name = ($this->plugin ? $this->plugin . '.' : '') . $this->name;
$parser = new ConsoleOptionParser($name);
$parser->setRootName($this->rootName);

return new ConsoleOptionParser($name);
return $parser;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/TestCase/Console/CommandRunnerTest.php
Expand Up @@ -241,6 +241,30 @@ public function testRunValidCommandReturnInteger()
$this->assertSame(99, $result);
}

/**
* Ensure that the root command name propagates to shell help
*
* @return void
*/
public function testRunRootNamePropagates()
{
$app = $this->getMockBuilder(BaseApplication::class)
->setMethods(['middleware', 'bootstrap', 'console'])
->setConstructorArgs([$this->config])
->getMock();

$commands = new CommandCollection(['sample' => SampleShell::class]);
$app->method('console')->will($this->returnValue($commands));

$output = new ConsoleOutput();

$runner = new CommandRunner($app, 'widget');
$runner->run(['widget', 'sample', '-h'], $this->getMockIo($output));
$result = implode("\n", $output->messages());
$this->assertContains('widget sample [-h]', $result);
$this->assertNotContains('cake sample [-h]', $result);
}

protected function getMockIo($output)
{
$io = $this->getMockBuilder(ConsoleIo::class)
Expand Down
11 changes: 11 additions & 0 deletions tests/TestCase/Console/ShellTest.php
Expand Up @@ -1339,6 +1339,17 @@ public function testGetSetIo()
$this->assertSame($this->Shell->getIo(), $this->io);
}

/**
* Test setRootName filters into the option parser help text.
*
* @return void
*/
public function testSetRootNamePropagatesToHelpText()
{
$this->assertSame($this->Shell, $this->Shell->setRootName('tool'), 'is chainable');
$this->assertContains('tool shell_test_shell [-h]', $this->Shell->getOptionParser()->help());
}

/**
* Tests __debugInfo
*
Expand Down

0 comments on commit 3d3c29c

Please sign in to comment.