diff --git a/library/PSX/Console/CommandCommand.php b/library/PSX/Console/CommandCommand.php index 1149707e..e48ed907 100644 --- a/library/PSX/Console/CommandCommand.php +++ b/library/PSX/Console/CommandCommand.php @@ -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; @@ -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); } } diff --git a/tests/PSX/Console/CommandCommandTest.php b/tests/PSX/Console/CommandCommandTest.php index 50954fd1..07766688 100644 --- a/tests/PSX/Console/CommandCommandTest.php +++ b/tests/PSX/Console/CommandCommandTest.php @@ -24,6 +24,7 @@ namespace PSX\Console; use PSX\Command\Output; +use PSX\Test\CommandTestCase; use Symfony\Component\Console\Tester\CommandTester; /** @@ -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(); @@ -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(); @@ -63,7 +104,7 @@ public function testCommand() /** * @expectedException PSX\Command\MissingParameterException */ - public function testCommandEmptyBody() + public function testCommandStdinEmptyBody() { $stream = fopen('php://memory', 'r+'); fwrite($stream, ''); @@ -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'); @@ -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, )); } }