TYPO3  7.6
ArgvInputTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Symfony\Component\Console\Tests\Input;
13 
18 
19 class ArgvInputTest extends \PHPUnit_Framework_TestCase
20 {
21  public function testConstructor()
22  {
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);
28 
29  $this->assertEquals(array('foo'), $p->getValue($input), '__construct() automatically get its input from the argv server variable');
30  }
31 
32  public function testParseArguments()
33  {
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');
37 
38  $input->bind(new InputDefinition(array(new InputArgument('name'))));
39  $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() is stateless');
40  }
41 
45  public function testParseOptions($input, $options, $expectedOptions, $message)
46  {
47  $input = new ArgvInput($input);
48  $input->bind(new InputDefinition($options));
49 
50  $this->assertEquals($expectedOptions, $input->getOptions(), $message);
51  }
52 
53  public function provideOptions()
54  {
55  return array(
56  array(
57  array('cli.php', '--foo'),
58  array(new InputOption('foo')),
59  array('foo' => true),
60  '->parse() parses long options without a value',
61  ),
62  array(
63  array('cli.php', '--foo=bar'),
64  array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)),
65  array('foo' => 'bar'),
66  '->parse() parses long options with a required value (with a = separator)',
67  ),
68  array(
69  array('cli.php', '--foo', 'bar'),
70  array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)),
71  array('foo' => 'bar'),
72  '->parse() parses long options with a required value (with a space separator)',
73  ),
74  array(
75  array('cli.php', '-f'),
76  array(new InputOption('foo', 'f')),
77  array('foo' => true),
78  '->parse() parses short options without a value',
79  ),
80  array(
81  array('cli.php', '-fbar'),
82  array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)),
83  array('foo' => 'bar'),
84  '->parse() parses short options with a required value (with no separator)',
85  ),
86  array(
87  array('cli.php', '-f', 'bar'),
88  array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)),
89  array('foo' => 'bar'),
90  '->parse() parses short options with a required value (with a space separator)',
91  ),
92  array(
93  array('cli.php', '-f', ''),
94  array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)),
95  array('foo' => ''),
96  '->parse() parses short options with an optional empty value',
97  ),
98  array(
99  array('cli.php', '-f', '', 'foo'),
100  array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)),
101  array('foo' => ''),
102  '->parse() parses short options with an optional empty value followed by an argument',
103  ),
104  array(
105  array('cli.php', '-f', '', '-b'),
106  array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b')),
107  array('foo' => '', 'bar' => true),
108  '->parse() parses short options with an optional empty value followed by an option',
109  ),
110  array(
111  array('cli.php', '-f', '-b', 'foo'),
112  array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b')),
113  array('foo' => null, 'bar' => true),
114  '->parse() parses short options with an optional value which is not present',
115  ),
116  array(
117  array('cli.php', '-fb'),
118  array(new InputOption('foo', 'f'), new InputOption('bar', 'b')),
119  array('foo' => true, 'bar' => true),
120  '->parse() parses short options when they are aggregated as a single one',
121  ),
122  array(
123  array('cli.php', '-fb', 'bar'),
124  array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_REQUIRED)),
125  array('foo' => true, 'bar' => 'bar'),
126  '->parse() parses short options when they are aggregated as a single one and the last one has a required value',
127  ),
128  array(
129  array('cli.php', '-fb', 'bar'),
130  array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL)),
131  array('foo' => true, 'bar' => 'bar'),
132  '->parse() parses short options when they are aggregated as a single one and the last one has an optional value',
133  ),
134  array(
135  array('cli.php', '-fbbar'),
136  array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL)),
137  array('foo' => true, 'bar' => 'bar'),
138  '->parse() parses short options when they are aggregated as a single one and the last one has an optional value with no separator',
139  ),
140  array(
141  array('cli.php', '-fbbar'),
142  array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL)),
143  array('foo' => 'bbar', 'bar' => null),
144  '->parse() parses short options when they are aggregated as a single one and one of them takes a value',
145  ),
146  );
147  }
148 
152  public function testInvalidInput($argv, $definition, $expectedExceptionMessage)
153  {
154  $this->setExpectedException('RuntimeException', $expectedExceptionMessage);
155 
156  $input = new ArgvInput($argv);
157  $input->bind($definition);
158  }
159 
160  public function provideInvalidInput()
161  {
162  return array(
163  array(
164  array('cli.php', '--foo'),
165  new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))),
166  'The "--foo" option requires a value.',
167  ),
168  array(
169  array('cli.php', '-f'),
170  new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))),
171  'The "--foo" option requires a value.',
172  ),
173  array(
174  array('cli.php', '-ffoo'),
175  new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_NONE))),
176  'The "-o" option does not exist.',
177  ),
178  array(
179  array('cli.php', '--foo=bar'),
180  new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_NONE))),
181  'The "--foo" option does not accept a value.',
182  ),
183  array(
184  array('cli.php', 'foo', 'bar'),
185  new InputDefinition(),
186  'Too many arguments.',
187  ),
188  array(
189  array('cli.php', '--foo'),
190  new InputDefinition(),
191  'The "--foo" option does not exist.',
192  ),
193  array(
194  array('cli.php', '-f'),
195  new InputDefinition(),
196  'The "-f" option does not exist.',
197  ),
198  array(
199  array('cli.php', '-1'),
200  new InputDefinition(array(new InputArgument('number'))),
201  'The "-1" option does not exist.',
202  ),
203  );
204  }
205 
206  public function testParseArrayArgument()
207  {
208  $input = new ArgvInput(array('cli.php', 'foo', 'bar', 'baz', 'bat'));
209  $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::IS_ARRAY))));
210 
211  $this->assertEquals(array('name' => array('foo', 'bar', 'baz', 'bat')), $input->getArguments(), '->parse() parses array arguments');
212  }
213 
214  public function testParseArrayOption()
215  {
216  $input = new ArgvInput(array('cli.php', '--name=foo', '--name=bar', '--name=baz'));
217  $input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
218 
219  $this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions(), '->parse() parses array options ("--option=value" syntax)');
220 
221  $input = new ArgvInput(array('cli.php', '--name', 'foo', '--name', 'bar', '--name', 'baz'));
222  $input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
223  $this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions(), '->parse() parses array options ("--option value" syntax)');
224 
225  $input = new ArgvInput(array('cli.php', '--name=foo', '--name=bar', '--name='));
226  $input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
227  $this->assertSame(array('name' => array('foo', 'bar', null)), $input->getOptions(), '->parse() parses empty array options as null ("--option=value" syntax)');
228 
229  $input = new ArgvInput(array('cli.php', '--name', 'foo', '--name', 'bar', '--name', '--anotherOption'));
230  $input->bind(new InputDefinition(array(
232  new InputOption('anotherOption', null, InputOption::VALUE_NONE),
233  )));
234  $this->assertSame(array('name' => array('foo', 'bar', null), 'anotherOption' => true), $input->getOptions(), '->parse() parses empty array options as null ("--option value" syntax)');
235  }
236 
238  {
239  $input = new ArgvInput(array('cli.php', '--', '-1'));
240  $input->bind(new InputDefinition(array(new InputArgument('number'))));
241  $this->assertEquals(array('number' => '-1'), $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence');
242 
243  $input = new ArgvInput(array('cli.php', '-f', 'bar', '--', '-1'));
244  $input->bind(new InputDefinition(array(new InputArgument('number'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
245  $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses arguments with leading dashes as options before having encountered a double-dash sequence');
246  $this->assertEquals(array('number' => '-1'), $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence');
247  }
248 
250  {
251  $input = new ArgvInput(array('cli.php', '-f', 'bar', ''));
252  $input->bind(new InputDefinition(array(new InputArgument('empty'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
253 
254  $this->assertEquals(array('empty' => ''), $input->getArguments(), '->parse() parses empty string arguments');
255  }
256 
257  public function testGetFirstArgument()
258  {
259  $input = new ArgvInput(array('cli.php', '-fbbar'));
260  $this->assertNull($input->getFirstArgument(), '->getFirstArgument() returns null when there is no arguments');
261 
262  $input = new ArgvInput(array('cli.php', '-fbbar', 'foo'));
263  $this->assertEquals('foo', $input->getFirstArgument(), '->getFirstArgument() returns the first argument from the raw input');
264  }
265 
266  public function testHasParameterOption()
267  {
268  $input = new ArgvInput(array('cli.php', '-f', 'foo'));
269  $this->assertTrue($input->hasParameterOption('-f'), '->hasParameterOption() returns true if the given short option is in the raw input');
270 
271  $input = new ArgvInput(array('cli.php', '--foo', 'foo'));
272  $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if the given short option is in the raw input');
273 
274  $input = new ArgvInput(array('cli.php', 'foo'));
275  $this->assertFalse($input->hasParameterOption('--foo'), '->hasParameterOption() returns false if the given short option is not in the raw input');
276 
277  $input = new ArgvInput(array('cli.php', '--foo=bar'));
278  $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if the given option with provided value is in the raw input');
279  }
280 
281  public function testToString()
282  {
283  $input = new ArgvInput(array('cli.php', '-f', 'foo'));
284  $this->assertEquals('-f foo', (string) $input);
285 
286  $input = new ArgvInput(array('cli.php', '-f', '--bar=foo', 'a b c d', "A\nB'C"));
287  $this->assertEquals('-f --bar=foo '.escapeshellarg('a b c d').' '.escapeshellarg("A\nB'C"), (string) $input);
288  }
289 
293  public function testGetParameterOptionEqualSign($argv, $key, $expected)
294  {
295  $input = new ArgvInput($argv);
296  $this->assertEquals($expected, $input->getParameterOption($key), '->getParameterOption() returns the expected value');
297  }
298 
300  {
301  return array(
302  array(array('app/console', 'foo:bar', '-e', 'dev'), '-e', 'dev'),
303  array(array('app/console', 'foo:bar', '--env=dev'), '--env', 'dev'),
304  array(array('app/console', 'foo:bar', '-e', 'dev'), array('-e', '--env'), 'dev'),
305  array(array('app/console', 'foo:bar', '--env=dev'), array('-e', '--env'), 'dev'),
306  array(array('app/console', 'foo:bar', '--env=dev', '--en=1'), array('--en'), '1'),
307  array(array('app/console', 'foo:bar', '--env=dev', '', '--en=1'), array('--en'), '1'),
308  );
309  }
310 
312  {
313  $input = new ArgvInput(array('cli.php', '-'));
314  $input->bind(new InputDefinition(array(new InputArgument('file'))));
315  $this->assertEquals(array('file' => '-'), $input->getArguments(), '->parse() parses single dash as an argument');
316  }
317 }