Skip to content

Commit

Permalink
Added tests around argument and option parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed Jun 23, 2017
1 parent 2f7def1 commit 5b05257
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tests/TestCase/TestSuite/ConsoleIntegrationTestCaseTest.php
Expand Up @@ -46,6 +46,34 @@ public function testExecCoreCommand()
$this->assertExitCode(Shell::CODE_SUCCESS);
}

/**
* tests exec with an arg and an option
*
* @return void
*/
public function testExecWithArgsAndOption()
{
$this->exec('integration args_and_options arg --opt="some string"');

$this->assertOutputContains('arg: arg');
$this->assertOutputContains('opt: some string');
$this->assertExitCode(Shell::CODE_SUCCESS);
}

/**
* tests exec with missing required argument
*
* @return void
*/
public function testExecWithMissingRequiredArg()
{
$this->exec('integration args_and_options');

$this->assertErrorContains('Missing required arguments');
$this->assertErrorContains('arg is required');
$this->assertExitCode(Shell::CODE_ERROR);
}

/**
* tests exec with input
*
Expand Down
37 changes: 37 additions & 0 deletions tests/test_app/TestApp/Shell/IntegrationShell.php
Expand Up @@ -20,11 +20,37 @@
*/
namespace TestApp\Shell;

use Cake\Console\ConsoleOptionParser;
use Cake\Console\Shell;

class IntegrationShell extends Shell
{

/**
* Option parser
*
* @return ConsoleOptionParser
*/
public function getOptionParser()
{
$parser = new ConsoleOptionParser();
$argAndOptionParser = (new ConsoleOptionParser())
->addArgument('arg', [
'required' => true
])
->addOption('opt', [
'short' => 'o'
]);

$parser
->addSubcommand('argsAndOptions', [
'parser' => $argAndOptionParser
])
->addSubcommand('bridge');

return $parser;
}

/**
* Bridge of Death question
*
Expand All @@ -48,4 +74,15 @@ public function bridge()

$this->out('You may pass.');
}

/**
* A sub command that requires an argument and has an option
*
* @return void
*/
public function argsAndOptions()
{
$this->out('arg: ' . $this->args[0]);
$this->out('opt: ' . $this->param('opt'));
}
}

0 comments on commit 5b05257

Please sign in to comment.