Skip to content

Commit

Permalink
pass command parameters as arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Nov 29, 2014
1 parent b676a12 commit fe87dfd
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 15 deletions.
36 changes: 30 additions & 6 deletions library/PSX/Console/CommandCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use PSX\Command\Executor;
use PSX\Command\ParameterParser;
use RuntimeException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -60,19 +61,42 @@ protected function configure()
{
$this
->setName('command')
->setDescription('Executes an PSX command through the console. The parameters must be provided as JSON via stdin')
->addArgument('cmd', InputArgument::REQUIRED, 'Name of the command');
->setDescription('Executes an PSX command through the console. The parameters must be either provided as JSON via stdin or per parameter')
->addArgument('cmd', InputArgument::REQUIRED, 'Name of the command')
->addArgument('parameters', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Needed parameters for the command')
->addOption('stdin', 's', InputOption::VALUE_NONE, 'Whether to read from stdin');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$body = $this->reader->read();
$command = $input->getArgument('cmd');
$parameters = $input->getArgument('parameters');

if(empty($body))
if($input->getOption('stdin'))
{
$body = '{}';
$body = $this->reader->read();
if(empty($body))
{
$body = '{}';
}

$parser = new ParameterParser\Json($command, $body);
}
else
{
$map = array();
foreach($parameters as $parameter)
{
$parts = explode(':', $parameter, 2);
$key = $parts[0];
$value = isset($parts[1]) ? $parts[1] : null;

$map[$key] = $value;
}

$parser = new ParameterParser\Map($command, $map);
}

$this->executor->run(new ParameterParser\Json($input->getArgument('cmd'), $body));
$this->executor->run($parser);
}
}
59 changes: 50 additions & 9 deletions tests/PSX/Console/CommandCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace PSX\Console;

use PSX\Command\Output;
use PSX\Test\CommandTestCase;
use Symfony\Component\Console\Tester\CommandTester;

/**
Expand All @@ -33,9 +34,48 @@
* @license http://www.gnu.org/licenses/gpl.html GPLv3
* @link http://phpsx.org
*/
class CommandCommandTest extends \PHPUnit_Framework_TestCase
class CommandCommandTest extends CommandTestCase
{
public function testCommand()
public function testCommandParameter()
{
$memory = new Output\Memory();

getContainer()->set('command_output', $memory);

$command = getContainer()->get('console')->find('command');

$commandTester = new CommandTester($command);
$commandTester->execute(array(
'cmd' => 'PSX\Command\Foo\Command\FooCommand',
'parameters' => array('foo:test'),
));

$messages = $memory->getMessages();

$this->assertEquals(2, count($messages));
$this->assertEquals('Some foo informations', rtrim($messages[0]));
$this->assertEquals('Hello test', rtrim($messages[1]));
}

/**
* @expectedException PSX\Command\MissingParameterException
*/
public function testCommandParameterEmpty()
{
$memory = new Output\Memory();

getContainer()->set('command_output', $memory);

$command = getContainer()->get('console')->find('command');

$commandTester = new CommandTester($command);
$commandTester->execute(array(
'cmd' => 'PSX\Command\Foo\Command\FooCommand',
'parameters' => array(),
));
}

public function testCommandStdin()
{
$memory = new Output\Memory();

Expand All @@ -50,7 +90,8 @@ public function testCommand()

$commandTester = new CommandTester($command);
$commandTester->execute(array(
'cmd' => 'PSX\Command\Foo\Command\FooCommand',
'cmd' => 'PSX\Command\Foo\Command\FooCommand',
'--stdin' => true,
));

$messages = $memory->getMessages();
Expand All @@ -63,7 +104,7 @@ public function testCommand()
/**
* @expectedException PSX\Command\MissingParameterException
*/
public function testCommandEmptyBody()
public function testCommandStdinEmptyBody()
{
$stream = fopen('php://memory', 'r+');
fwrite($stream, '');
Expand All @@ -74,15 +115,15 @@ public function testCommandEmptyBody()

$commandTester = new CommandTester($command);
$commandTester->execute(array(
'command' => $command->getName(),
'cmd' => 'PSX\Command\Foo\Command\FooCommand',
'cmd' => 'PSX\Command\Foo\Command\FooCommand',
'--stdin' => true,
));
}

/**
* @expectedException PSX\Exception
*/
public function testCommandInvalidJson()
public function testCommandStdinInvalidJson()
{
$stream = fopen('php://memory', 'r+');
fwrite($stream, 'foobar');
Expand All @@ -93,8 +134,8 @@ public function testCommandInvalidJson()

$commandTester = new CommandTester($command);
$commandTester->execute(array(
'command' => $command->getName(),
'cmd' => 'PSX\Command\Foo\Command\FooCommand',
'cmd' => 'PSX\Command\Foo\Command\FooCommand',
'--stdin' => true,
));
}
}

0 comments on commit fe87dfd

Please sign in to comment.