4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Tests\Input;
14 use Symfony\Component\Console\Input\ArgvInput;
15 use Symfony\Component\Console\Input\InputDefinition;
16 use Symfony\Component\Console\Input\InputArgument;
17 use Symfony\Component\Console\Input\InputOption;
19 class ArgvInputTest extends \PHPUnit_Framework_TestCase
21 public function testConstructor()
23 $_SERVER['argv'] = array('cli.php', 'foo');
24 $input = new ArgvInput();
25 $r = new \ReflectionObject($input);
26 $p = $r->getProperty('tokens');
27 $p->setAccessible(true);
29 $this->assertEquals(array('foo'), $p->getValue($input), '__construct() automatically get its input from the argv server variable');
32 public function testParser()
34 $input = new ArgvInput(array('cli.php', 'foo'));
35 $input->bind(new InputDefinition(array(new InputArgument('name'))));
36 $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() parses required arguments');
38 $input->bind(new InputDefinition(array(new InputArgument('name'))));
39 $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() is stateless');
41 $input = new ArgvInput(array('cli.php', '--foo'));
42 $input->bind(new InputDefinition(array(new InputOption('foo'))));
43 $this->assertEquals(array('foo' => true), $input->getOptions(), '->parse() parses long options without a value');
45 $input = new ArgvInput(array('cli.php', '--foo=bar'));
46 $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
47 $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options with a required value (with a = separator)');
49 $input = new ArgvInput(array('cli.php', '--foo', 'bar'));
50 $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
51 $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options with a required value (with a space separator)');
54 $input = new ArgvInput(array('cli.php', '--foo'));
55 $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
56 $this->fail('->parse() throws a \RuntimeException if no value is passed to an option when it is required');
57 } catch (\Exception $e) {
58 $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if no value is passed to an option when it is required');
59 $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');
62 $input = new ArgvInput(array('cli.php', '-f'));
63 $input->bind(new InputDefinition(array(new InputOption('foo', 'f'))));
64 $this->assertEquals(array('foo' => true), $input->getOptions(), '->parse() parses short options without a value');
66 $input = new ArgvInput(array('cli.php', '-fbar'));
67 $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
68 $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses short options with a required value (with no separator)');
70 $input = new ArgvInput(array('cli.php', '-f', 'bar'));
71 $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
72 $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses short options with a required value (with a space separator)');
74 $input = new ArgvInput(array('cli.php', '-f', '-b', 'foo'));
75 $input->bind(new InputDefinition(array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b'))));
76 $this->assertEquals(array('foo' => null, 'bar' => true), $input->getOptions(), '->parse() parses short options with an optional value which is not present');
79 $input = new ArgvInput(array('cli.php', '-f'));
80 $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
81 $this->fail('->parse() throws a \RuntimeException if no value is passed to an option when it is required');
82 } catch (\Exception $e) {
83 $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if no value is passed to an option when it is required');
84 $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');
88 $input = new ArgvInput(array('cli.php', '-ffoo'));
89 $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_NONE))));
90 $this->fail('->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
91 } catch (\Exception $e) {
92 $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
93 $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');
97 $input = new ArgvInput(array('cli.php', 'foo', 'bar'));
98 $input->bind(new InputDefinition());
99 $this->fail('->parse() throws a \RuntimeException if too many arguments are passed');
100 } catch (\Exception $e) {
101 $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if too many arguments are passed');
102 $this->assertEquals('Too many arguments.', $e->getMessage(), '->parse() throws a \RuntimeException if too many arguments are passed');
106 $input = new ArgvInput(array('cli.php', '--foo'));
107 $input->bind(new InputDefinition());
108 $this->fail('->parse() throws a \RuntimeException if an unknown long option is passed');
109 } catch (\Exception $e) {
110 $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if an unknown long option is passed');
111 $this->assertEquals('The "--foo" option does not exist.', $e->getMessage(), '->parse() throws a \RuntimeException if an unknown long option is passed');
115 $input = new ArgvInput(array('cli.php', '-f'));
116 $input->bind(new InputDefinition());
117 $this->fail('->parse() throws a \RuntimeException if an unknown short option is passed');
118 } catch (\Exception $e) {
119 $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if an unknown short option is passed');
120 $this->assertEquals('The "-f" option does not exist.', $e->getMessage(), '->parse() throws a \RuntimeException if an unknown short option is passed');
123 $input = new ArgvInput(array('cli.php', '-fb'));
124 $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b'))));
125 $this->assertEquals(array('foo' => true, 'bar' => true), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one');
127 $input = new ArgvInput(array('cli.php', '-fb', 'bar'));
128 $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_REQUIRED))));
129 $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');
131 $input = new ArgvInput(array('cli.php', '-fb', 'bar'));
132 $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL))));
133 $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');
135 $input = new ArgvInput(array('cli.php', '-fbbar'));
136 $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL))));
137 $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');
139 $input = new ArgvInput(array('cli.php', '-fbbar'));
140 $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL))));
141 $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');
144 $input = new ArgvInput(array('cli.php', 'foo', 'bar', 'baz', 'bat'));
145 $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::IS_ARRAY))));
146 $this->assertEquals(array('name' => array('foo', 'bar', 'baz', 'bat')), $input->getArguments(), '->parse() parses array arguments');
147 } catch (\RuntimeException $e) {
148 $this->assertNotEquals('Too many arguments.', $e->getMessage(), '->parse() parses array arguments');
151 $input = new ArgvInput(array('cli.php', '--name=foo', '--name=bar', '--name=baz'));
152 $input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
153 $this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions());
156 $input = new ArgvInput(array('cli.php', '-1'));
157 $input->bind(new InputDefinition(array(new InputArgument('number'))));
158 $this->fail('->parse() throws a \RuntimeException if an unknown option is passed');
159 } catch (\Exception $e) {
160 $this->assertInstanceOf('\RuntimeException', $e, '->parse() parses arguments with leading dashes as options without having encountered a double-dash sequence');
161 $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');
164 $input = new ArgvInput(array('cli.php', '--', '-1'));
165 $input->bind(new InputDefinition(array(new InputArgument('number'))));
166 $this->assertEquals(array('number' => '-1'), $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence');
168 $input = new ArgvInput(array('cli.php', '-f', 'bar', '--', '-1'));
169 $input->bind(new InputDefinition(array(new InputArgument('number'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
170 $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses arguments with leading dashes as options before having encountered a double-dash sequence');
171 $this->assertEquals(array('number' => '-1'), $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence');
173 $input = new ArgvInput(array('cli.php', '-f', 'bar', ''));
174 $input->bind(new InputDefinition(array(new InputArgument('empty'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
175 $this->assertEquals(array('empty' => ''), $input->getArguments(), '->parse() parses empty string arguments');
178 public function testGetFirstArgument()
180 $input = new ArgvInput(array('cli.php', '-fbbar'));
181 $this->assertEquals('', $input->getFirstArgument(), '->getFirstArgument() returns the first argument from the raw input');
183 $input = new ArgvInput(array('cli.php', '-fbbar', 'foo'));
184 $this->assertEquals('foo', $input->getFirstArgument(), '->getFirstArgument() returns the first argument from the raw input');
187 public function testHasParameterOption()
189 $input = new ArgvInput(array('cli.php', '-f', 'foo'));
190 $this->assertTrue($input->hasParameterOption('-f'), '->hasParameterOption() returns true if the given short option is in the raw input');
192 $input = new ArgvInput(array('cli.php', '--foo', 'foo'));
193 $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if the given short option is in the raw input');
195 $input = new ArgvInput(array('cli.php', 'foo'));
196 $this->assertFalse($input->hasParameterOption('--foo'), '->hasParameterOption() returns false if the given short option is not in the raw input');
200 * @dataProvider provideGetParameterOptionValues
202 public function testGetParameterOptionEqualSign($argv, $key, $expected)
204 $input = new ArgvInput($argv);
205 $this->assertEquals($expected, $input->getParameterOption($key), '->getParameterOption() returns the expected value');
208 public function provideGetParameterOptionValues()
211 array(array('app/console', 'foo:bar', '-e', 'dev'), '-e', 'dev'),
212 array(array('app/console', 'foo:bar', '--env=dev'), '--env', 'dev'),
213 array(array('app/console', 'foo:bar', '-e', 'dev'), array('-e', '--env'), 'dev'),
214 array(array('app/console', 'foo:bar', '--env=dev'), array('-e', '--env'), 'dev'),