|
@@ -29,7 +29,7 @@ class ArgvInputTest extends \PHPUnit_Framework_TestCase
|
|
|
$this->assertEquals(array('foo'), $p->getValue($input), '__construct() automatically get its input from the argv server variable');
|
|
|
}
|
|
|
|
|
|
- public function testParser()
|
|
|
+ public function testParseArguments()
|
|
|
{
|
|
|
$input = new ArgvInput(array('cli.php', 'foo'));
|
|
|
$input->bind(new InputDefinition(array(new InputArgument('name'))));
|
|
@@ -37,142 +37,185 @@ class ArgvInputTest extends \PHPUnit_Framework_TestCase
|
|
|
|
|
|
$input->bind(new InputDefinition(array(new InputArgument('name'))));
|
|
|
$this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() is stateless');
|
|
|
+ }
|
|
|
|
|
|
- $input = new ArgvInput(array('cli.php', '--foo'));
|
|
|
- $input->bind(new InputDefinition(array(new InputOption('foo'))));
|
|
|
- $this->assertEquals(array('foo' => true), $input->getOptions(), '->parse() parses long options without a value');
|
|
|
-
|
|
|
- $input = new ArgvInput(array('cli.php', '--foo=bar'));
|
|
|
- $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
|
|
|
- $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options with a required value (with a = separator)');
|
|
|
-
|
|
|
- $input = new ArgvInput(array('cli.php', '--foo', 'bar'));
|
|
|
- $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
|
|
|
- $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options with a required value (with a space separator)');
|
|
|
-
|
|
|
- try {
|
|
|
- $input = new ArgvInput(array('cli.php', '--foo'));
|
|
|
- $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
|
|
|
- $this->fail('->parse() throws a \RuntimeException if no value is passed to an option when it is required');
|
|
|
- } catch (\Exception $e) {
|
|
|
- $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if no value is passed to an option when it is required');
|
|
|
- $this->assertEquals('The "--foo" option requires a value.', $e->getMessage(), '->parse() throws a \RuntimeException if no value is passed to an option when it is required');
|
|
|
- }
|
|
|
-
|
|
|
- $input = new ArgvInput(array('cli.php', '-f'));
|
|
|
- $input->bind(new InputDefinition(array(new InputOption('foo', 'f'))));
|
|
|
- $this->assertEquals(array('foo' => true), $input->getOptions(), '->parse() parses short options without a value');
|
|
|
-
|
|
|
- $input = new ArgvInput(array('cli.php', '-fbar'));
|
|
|
- $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
|
|
|
- $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses short options with a required value (with no separator)');
|
|
|
-
|
|
|
- $input = new ArgvInput(array('cli.php', '-f', 'bar'));
|
|
|
- $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
|
|
|
- $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses short options with a required value (with a space separator)');
|
|
|
-
|
|
|
- $input = new ArgvInput(array('cli.php', '-f', ''));
|
|
|
- $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
|
|
|
- $this->assertEquals(array('foo' => ''), $input->getOptions(), '->parse() parses short options with an optional empty value');
|
|
|
-
|
|
|
- $input = new ArgvInput(array('cli.php', '-f', '', 'foo'));
|
|
|
- $input->bind(new InputDefinition(array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
|
|
|
- $this->assertEquals(array('foo' => ''), $input->getOptions(), '->parse() parses short options with an optional empty value followed by an argument');
|
|
|
-
|
|
|
- $input = new ArgvInput(array('cli.php', '-f', '', '-b'));
|
|
|
- $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b'))));
|
|
|
- $this->assertEquals(array('foo' => '', 'bar' => true), $input->getOptions(), '->parse() parses short options with an optional empty value followed by an option');
|
|
|
-
|
|
|
- $input = new ArgvInput(array('cli.php', '-f', '-b', 'foo'));
|
|
|
- $input->bind(new InputDefinition(array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b'))));
|
|
|
- $this->assertEquals(array('foo' => null, 'bar' => true), $input->getOptions(), '->parse() parses short options with an optional value which is not present');
|
|
|
-
|
|
|
- try {
|
|
|
- $input = new ArgvInput(array('cli.php', '-f'));
|
|
|
- $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
|
|
|
- $this->fail('->parse() throws a \RuntimeException if no value is passed to an option when it is required');
|
|
|
- } catch (\Exception $e) {
|
|
|
- $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if no value is passed to an option when it is required');
|
|
|
- $this->assertEquals('The "--foo" option requires a value.', $e->getMessage(), '->parse() throws a \RuntimeException if no value is passed to an option when it is required');
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- $input = new ArgvInput(array('cli.php', '-ffoo'));
|
|
|
- $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_NONE))));
|
|
|
- $this->fail('->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
|
|
|
- } catch (\Exception $e) {
|
|
|
- $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
|
|
|
- $this->assertEquals('The "-o" option does not exist.', $e->getMessage(), '->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- $input = new ArgvInput(array('cli.php', 'foo', 'bar'));
|
|
|
- $input->bind(new InputDefinition());
|
|
|
- $this->fail('->parse() throws a \RuntimeException if too many arguments are passed');
|
|
|
- } catch (\Exception $e) {
|
|
|
- $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if too many arguments are passed');
|
|
|
- $this->assertEquals('Too many arguments.', $e->getMessage(), '->parse() throws a \RuntimeException if too many arguments are passed');
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- $input = new ArgvInput(array('cli.php', '--foo'));
|
|
|
- $input->bind(new InputDefinition());
|
|
|
- $this->fail('->parse() throws a \RuntimeException if an unknown long option is passed');
|
|
|
- } catch (\Exception $e) {
|
|
|
- $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if an unknown long option is passed');
|
|
|
- $this->assertEquals('The "--foo" option does not exist.', $e->getMessage(), '->parse() throws a \RuntimeException if an unknown long option is passed');
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- $input = new ArgvInput(array('cli.php', '-f'));
|
|
|
- $input->bind(new InputDefinition());
|
|
|
- $this->fail('->parse() throws a \RuntimeException if an unknown short option is passed');
|
|
|
- } catch (\Exception $e) {
|
|
|
- $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if an unknown short option is passed');
|
|
|
- $this->assertEquals('The "-f" option does not exist.', $e->getMessage(), '->parse() throws a \RuntimeException if an unknown short option is passed');
|
|
|
- }
|
|
|
-
|
|
|
- $input = new ArgvInput(array('cli.php', '-fb'));
|
|
|
- $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b'))));
|
|
|
- $this->assertEquals(array('foo' => true, 'bar' => true), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one');
|
|
|
-
|
|
|
- $input = new ArgvInput(array('cli.php', '-fb', 'bar'));
|
|
|
- $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_REQUIRED))));
|
|
|
- $this->assertEquals(array('foo' => true, 'bar' => 'bar'), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and the last one has a required value');
|
|
|
-
|
|
|
- $input = new ArgvInput(array('cli.php', '-fb', 'bar'));
|
|
|
- $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL))));
|
|
|
- $this->assertEquals(array('foo' => true, 'bar' => 'bar'), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and the last one has an optional value');
|
|
|
+ /**
|
|
|
+ * @dataProvider provideOptions
|
|
|
+ */
|
|
|
+ public function testParseOptions($input, $options, $expectedOptions, $message)
|
|
|
+ {
|
|
|
+ $input = new ArgvInput($input);
|
|
|
+ $input->bind(new InputDefinition($options));
|
|
|
|
|
|
- $input = new ArgvInput(array('cli.php', '-fbbar'));
|
|
|
- $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL))));
|
|
|
- $this->assertEquals(array('foo' => true, 'bar' => 'bar'), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and the last one has an optional value with no separator');
|
|
|
+ $this->assertEquals($expectedOptions, $input->getOptions(), $message);
|
|
|
+ }
|
|
|
|
|
|
- $input = new ArgvInput(array('cli.php', '-fbbar'));
|
|
|
- $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL))));
|
|
|
- $this->assertEquals(array('foo' => 'bbar', 'bar' => null), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and one of them takes a value');
|
|
|
+ public function provideOptions()
|
|
|
+ {
|
|
|
+ return array(
|
|
|
+ array(
|
|
|
+ array('cli.php', '--foo'),
|
|
|
+ array(new InputOption('foo')),
|
|
|
+ array('foo' => true),
|
|
|
+ '->parse() parses long options without a value'
|
|
|
+ ),
|
|
|
+ array(
|
|
|
+ array('cli.php', '--foo=bar'),
|
|
|
+ array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)),
|
|
|
+ array('foo' => 'bar'),
|
|
|
+ '->parse() parses long options with a required value (with a = separator)'
|
|
|
+ ),
|
|
|
+ array(
|
|
|
+ array('cli.php', '--foo', 'bar'),
|
|
|
+ array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)),
|
|
|
+ array('foo' => 'bar'),
|
|
|
+ '->parse() parses long options with a required value (with a space separator)'
|
|
|
+ ),
|
|
|
+ array(
|
|
|
+ array('cli.php', '-f'),
|
|
|
+ array(new InputOption('foo', 'f')),
|
|
|
+ array('foo' => true),
|
|
|
+ '->parse() parses short options without a value'
|
|
|
+ ),
|
|
|
+ array(
|
|
|
+ array('cli.php', '-fbar'),
|
|
|
+ array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)),
|
|
|
+ array('foo' => 'bar'),
|
|
|
+ '->parse() parses short options with a required value (with no separator)'
|
|
|
+ ),
|
|
|
+ array(
|
|
|
+ array('cli.php', '-f', 'bar'),
|
|
|
+ array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)),
|
|
|
+ array('foo' => 'bar'),
|
|
|
+ '->parse() parses short options with a required value (with a space separator)'
|
|
|
+ ),
|
|
|
+ array(
|
|
|
+ array('cli.php', '-f', ''),
|
|
|
+ array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)),
|
|
|
+ array('foo' => ''),
|
|
|
+ '->parse() parses short options with an optional empty value'
|
|
|
+ ),
|
|
|
+ array(
|
|
|
+ array('cli.php', '-f', '', 'foo'),
|
|
|
+ array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)),
|
|
|
+ array('foo' => ''),
|
|
|
+ '->parse() parses short options with an optional empty value followed by an argument'
|
|
|
+ ),
|
|
|
+ array(
|
|
|
+ array('cli.php', '-f', '', '-b'),
|
|
|
+ array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b')),
|
|
|
+ array('foo' => '', 'bar' => true),
|
|
|
+ '->parse() parses short options with an optional empty value followed by an option'
|
|
|
+ ),
|
|
|
+ array(
|
|
|
+ array('cli.php', '-f', '-b', 'foo'),
|
|
|
+ array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b')),
|
|
|
+ array('foo' => null, 'bar' => true),
|
|
|
+ '->parse() parses short options with an optional value which is not present'
|
|
|
+ ),
|
|
|
+ array(
|
|
|
+ array('cli.php', '-fb'),
|
|
|
+ array(new InputOption('foo', 'f'), new InputOption('bar', 'b')),
|
|
|
+ array('foo' => true, 'bar' => true),
|
|
|
+ '->parse() parses short options when they are aggregated as a single one'
|
|
|
+ ),
|
|
|
+ array(
|
|
|
+ array('cli.php', '-fb', 'bar'),
|
|
|
+ array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_REQUIRED)),
|
|
|
+ array('foo' => true, 'bar' => 'bar'),
|
|
|
+ '->parse() parses short options when they are aggregated as a single one and the last one has a required value'
|
|
|
+ ),
|
|
|
+ array(
|
|
|
+ array('cli.php', '-fb', 'bar'),
|
|
|
+ array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL)),
|
|
|
+ array('foo' => true, 'bar' => 'bar'),
|
|
|
+ '->parse() parses short options when they are aggregated as a single one and the last one has an optional value'
|
|
|
+ ),
|
|
|
+ array(
|
|
|
+ array('cli.php', '-fbbar'),
|
|
|
+ array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL)),
|
|
|
+ array('foo' => true, 'bar' => 'bar'),
|
|
|
+ '->parse() parses short options when they are aggregated as a single one and the last one has an optional value with no separator'
|
|
|
+ ),
|
|
|
+ array(
|
|
|
+ array('cli.php', '-fbbar'),
|
|
|
+ array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL)),
|
|
|
+ array('foo' => 'bbar', 'bar' => null),
|
|
|
+ '->parse() parses short options when they are aggregated as a single one and one of them takes a value'
|
|
|
+ )
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @dataProvider provideInvalidInput
|
|
|
+ */
|
|
|
+ public function testInvalidInput($argv, $definition, $expectedExceptionMessage)
|
|
|
+ {
|
|
|
+ $this->setExpectedException('RuntimeException', $expectedExceptionMessage);
|
|
|
+
|
|
|
+ $input = new ArgvInput($argv);
|
|
|
+ $input->bind($definition);
|
|
|
+ }
|
|
|
|
|
|
- try {
|
|
|
- $input = new ArgvInput(array('cli.php', 'foo', 'bar', 'baz', 'bat'));
|
|
|
- $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::IS_ARRAY))));
|
|
|
- $this->assertEquals(array('name' => array('foo', 'bar', 'baz', 'bat')), $input->getArguments(), '->parse() parses array arguments');
|
|
|
- } catch (\RuntimeException $e) {
|
|
|
- $this->assertNotEquals('Too many arguments.', $e->getMessage(), '->parse() parses array arguments');
|
|
|
- }
|
|
|
+ public function provideInvalidInput()
|
|
|
+ {
|
|
|
+ return array(
|
|
|
+ array(
|
|
|
+ array('cli.php', '--foo'),
|
|
|
+ new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))),
|
|
|
+ 'The "--foo" option requires a value.'
|
|
|
+ ),
|
|
|
+ array(
|
|
|
+ array('cli.php', '-f'),
|
|
|
+ new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))),
|
|
|
+ 'The "--foo" option requires a value.'
|
|
|
+ ),
|
|
|
+ array(
|
|
|
+ array('cli.php', '-ffoo'),
|
|
|
+ new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_NONE))),
|
|
|
+ 'The "-o" option does not exist.'
|
|
|
+ ),
|
|
|
+ array(
|
|
|
+ array('cli.php', 'foo', 'bar'),
|
|
|
+ new InputDefinition(),
|
|
|
+ 'Too many arguments.'
|
|
|
+ ),
|
|
|
+ array(
|
|
|
+ array('cli.php', '--foo'),
|
|
|
+ new InputDefinition(),
|
|
|
+ 'The "--foo" option does not exist.'
|
|
|
+ ),
|
|
|
+ array(
|
|
|
+ array('cli.php', '-f'),
|
|
|
+ new InputDefinition(),
|
|
|
+ 'The "-f" option does not exist.'
|
|
|
+ ),
|
|
|
+ array(
|
|
|
+ array('cli.php', '-1'),
|
|
|
+ new InputDefinition(array(new InputArgument('number'))),
|
|
|
+ 'The "-1" option does not exist.'
|
|
|
+ )
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ public function testParseArrayArgument()
|
|
|
+ {
|
|
|
+ $input = new ArgvInput(array('cli.php', 'foo', 'bar', 'baz', 'bat'));
|
|
|
+ $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::IS_ARRAY))));
|
|
|
+
|
|
|
+ $this->assertEquals(array('name' => array('foo', 'bar', 'baz', 'bat')), $input->getArguments(), '->parse() parses array arguments');
|
|
|
+ }
|
|
|
|
|
|
+ public function testParseArrayOption()
|
|
|
+ {
|
|
|
$input = new ArgvInput(array('cli.php', '--name=foo', '--name=bar', '--name=baz'));
|
|
|
$input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
|
|
|
- $this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions());
|
|
|
|
|
|
- try {
|
|
|
- $input = new ArgvInput(array('cli.php', '-1'));
|
|
|
- $input->bind(new InputDefinition(array(new InputArgument('number'))));
|
|
|
- $this->fail('->parse() throws a \RuntimeException if an unknown option is passed');
|
|
|
- } catch (\Exception $e) {
|
|
|
- $this->assertInstanceOf('\RuntimeException', $e, '->parse() parses arguments with leading dashes as options without having encountered a double-dash sequence');
|
|
|
- $this->assertEquals('The "-1" option does not exist.', $e->getMessage(), '->parse() parses arguments with leading dashes as options without having encountered a double-dash sequence');
|
|
|
- }
|
|
|
+ $this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions());
|
|
|
+ }
|
|
|
|
|
|
+ public function testParseNegativeNumberAfterDoubleDash()
|
|
|
+ {
|
|
|
$input = new ArgvInput(array('cli.php', '--', '-1'));
|
|
|
$input->bind(new InputDefinition(array(new InputArgument('number'))));
|
|
|
$this->assertEquals(array('number' => '-1'), $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence');
|
|
@@ -181,9 +224,13 @@ class ArgvInputTest extends \PHPUnit_Framework_TestCase
|
|
|
$input->bind(new InputDefinition(array(new InputArgument('number'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
|
|
|
$this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses arguments with leading dashes as options before having encountered a double-dash sequence');
|
|
|
$this->assertEquals(array('number' => '-1'), $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence');
|
|
|
+ }
|
|
|
|
|
|
+ public function testParseEmptyStringArgument()
|
|
|
+ {
|
|
|
$input = new ArgvInput(array('cli.php', '-f', 'bar', ''));
|
|
|
$input->bind(new InputDefinition(array(new InputArgument('empty'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
|
|
|
+
|
|
|
$this->assertEquals(array('empty' => ''), $input->getArguments(), '->parse() parses empty string arguments');
|
|
|
}
|
|
|
|