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 PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Input\InputDefinition;
16 use Symfony\Component\Console\Input\InputArgument;
17 use Symfony\Component\Console\Input\InputOption;
19 class InputDefinitionTest extends TestCase
21 protected static $fixtures;
28 public static function setUpBeforeClass()
30 self::$fixtures = __DIR__.'/../Fixtures/';
33 public function testConstructorArguments()
35 $this->initializeArguments();
37 $definition = new InputDefinition();
38 $this->assertEquals(array(), $definition->getArguments(), '__construct() creates a new InputDefinition object');
40 $definition = new InputDefinition(array($this->foo, $this->bar));
41 $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getArguments(), '__construct() takes an array of InputArgument objects as its first argument');
44 public function testConstructorOptions()
46 $this->initializeOptions();
48 $definition = new InputDefinition();
49 $this->assertEquals(array(), $definition->getOptions(), '__construct() creates a new InputDefinition object');
51 $definition = new InputDefinition(array($this->foo, $this->bar));
52 $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getOptions(), '__construct() takes an array of InputOption objects as its first argument');
55 public function testSetArguments()
57 $this->initializeArguments();
59 $definition = new InputDefinition();
60 $definition->setArguments(array($this->foo));
61 $this->assertEquals(array('foo' => $this->foo), $definition->getArguments(), '->setArguments() sets the array of InputArgument objects');
62 $definition->setArguments(array($this->bar));
64 $this->assertEquals(array('bar' => $this->bar), $definition->getArguments(), '->setArguments() clears all InputArgument objects');
67 public function testAddArguments()
69 $this->initializeArguments();
71 $definition = new InputDefinition();
72 $definition->addArguments(array($this->foo));
73 $this->assertEquals(array('foo' => $this->foo), $definition->getArguments(), '->addArguments() adds an array of InputArgument objects');
74 $definition->addArguments(array($this->bar));
75 $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getArguments(), '->addArguments() does not clear existing InputArgument objects');
78 public function testAddArgument()
80 $this->initializeArguments();
82 $definition = new InputDefinition();
83 $definition->addArgument($this->foo);
84 $this->assertEquals(array('foo' => $this->foo), $definition->getArguments(), '->addArgument() adds a InputArgument object');
85 $definition->addArgument($this->bar);
86 $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getArguments(), '->addArgument() adds a InputArgument object');
90 * @expectedException \LogicException
91 * @expectedExceptionMessage An argument with name "foo" already exists.
93 public function testArgumentsMustHaveDifferentNames()
95 $this->initializeArguments();
97 $definition = new InputDefinition();
98 $definition->addArgument($this->foo);
99 $definition->addArgument($this->foo1);
103 * @expectedException \LogicException
104 * @expectedExceptionMessage Cannot add an argument after an array argument.
106 public function testArrayArgumentHasToBeLast()
108 $this->initializeArguments();
110 $definition = new InputDefinition();
111 $definition->addArgument(new InputArgument('fooarray', InputArgument::IS_ARRAY));
112 $definition->addArgument(new InputArgument('anotherbar'));
116 * @expectedException \LogicException
117 * @expectedExceptionMessage Cannot add a required argument after an optional one.
119 public function testRequiredArgumentCannotFollowAnOptionalOne()
121 $this->initializeArguments();
123 $definition = new InputDefinition();
124 $definition->addArgument($this->foo);
125 $definition->addArgument($this->foo2);
128 public function testGetArgument()
130 $this->initializeArguments();
132 $definition = new InputDefinition();
133 $definition->addArguments(array($this->foo));
134 $this->assertEquals($this->foo, $definition->getArgument('foo'), '->getArgument() returns a InputArgument by its name');
138 * @expectedException \InvalidArgumentException
139 * @expectedExceptionMessage The "bar" argument does not exist.
141 public function testGetInvalidArgument()
143 $this->initializeArguments();
145 $definition = new InputDefinition();
146 $definition->addArguments(array($this->foo));
147 $definition->getArgument('bar');
150 public function testHasArgument()
152 $this->initializeArguments();
154 $definition = new InputDefinition();
155 $definition->addArguments(array($this->foo));
157 $this->assertTrue($definition->hasArgument('foo'), '->hasArgument() returns true if a InputArgument exists for the given name');
158 $this->assertFalse($definition->hasArgument('bar'), '->hasArgument() returns false if a InputArgument exists for the given name');
161 public function testGetArgumentRequiredCount()
163 $this->initializeArguments();
165 $definition = new InputDefinition();
166 $definition->addArgument($this->foo2);
167 $this->assertEquals(1, $definition->getArgumentRequiredCount(), '->getArgumentRequiredCount() returns the number of required arguments');
168 $definition->addArgument($this->foo);
169 $this->assertEquals(1, $definition->getArgumentRequiredCount(), '->getArgumentRequiredCount() returns the number of required arguments');
172 public function testGetArgumentCount()
174 $this->initializeArguments();
176 $definition = new InputDefinition();
177 $definition->addArgument($this->foo2);
178 $this->assertEquals(1, $definition->getArgumentCount(), '->getArgumentCount() returns the number of arguments');
179 $definition->addArgument($this->foo);
180 $this->assertEquals(2, $definition->getArgumentCount(), '->getArgumentCount() returns the number of arguments');
183 public function testGetArgumentDefaults()
185 $definition = new InputDefinition(array(
186 new InputArgument('foo1', InputArgument::OPTIONAL),
187 new InputArgument('foo2', InputArgument::OPTIONAL, '', 'default'),
188 new InputArgument('foo3', InputArgument::OPTIONAL | InputArgument::IS_ARRAY),
189 // new InputArgument('foo4', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, '', array(1, 2)),
191 $this->assertEquals(array('foo1' => null, 'foo2' => 'default', 'foo3' => array()), $definition->getArgumentDefaults(), '->getArgumentDefaults() return the default values for each argument');
193 $definition = new InputDefinition(array(
194 new InputArgument('foo4', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, '', array(1, 2)),
196 $this->assertEquals(array('foo4' => array(1, 2)), $definition->getArgumentDefaults(), '->getArgumentDefaults() return the default values for each argument');
199 public function testSetOptions()
201 $this->initializeOptions();
203 $definition = new InputDefinition(array($this->foo));
204 $this->assertEquals(array('foo' => $this->foo), $definition->getOptions(), '->setOptions() sets the array of InputOption objects');
205 $definition->setOptions(array($this->bar));
206 $this->assertEquals(array('bar' => $this->bar), $definition->getOptions(), '->setOptions() clears all InputOption objects');
210 * @expectedException \InvalidArgumentException
211 * @expectedExceptionMessage The "-f" option does not exist.
213 public function testSetOptionsClearsOptions()
215 $this->initializeOptions();
217 $definition = new InputDefinition(array($this->foo));
218 $definition->setOptions(array($this->bar));
219 $definition->getOptionForShortcut('f');
222 public function testAddOptions()
224 $this->initializeOptions();
226 $definition = new InputDefinition(array($this->foo));
227 $this->assertEquals(array('foo' => $this->foo), $definition->getOptions(), '->addOptions() adds an array of InputOption objects');
228 $definition->addOptions(array($this->bar));
229 $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getOptions(), '->addOptions() does not clear existing InputOption objects');
232 public function testAddOption()
234 $this->initializeOptions();
236 $definition = new InputDefinition();
237 $definition->addOption($this->foo);
238 $this->assertEquals(array('foo' => $this->foo), $definition->getOptions(), '->addOption() adds a InputOption object');
239 $definition->addOption($this->bar);
240 $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getOptions(), '->addOption() adds a InputOption object');
244 * @expectedException \LogicException
245 * @expectedExceptionMessage An option named "foo" already exists.
247 public function testAddDuplicateOption()
249 $this->initializeOptions();
251 $definition = new InputDefinition();
252 $definition->addOption($this->foo);
253 $definition->addOption($this->foo2);
257 * @expectedException \LogicException
258 * @expectedExceptionMessage An option with shortcut "f" already exists.
260 public function testAddDuplicateShortcutOption()
262 $this->initializeOptions();
264 $definition = new InputDefinition();
265 $definition->addOption($this->foo);
266 $definition->addOption($this->foo1);
269 public function testGetOption()
271 $this->initializeOptions();
273 $definition = new InputDefinition(array($this->foo));
274 $this->assertEquals($this->foo, $definition->getOption('foo'), '->getOption() returns a InputOption by its name');
278 * @expectedException \InvalidArgumentException
279 * @expectedExceptionMessage The "--bar" option does not exist.
281 public function testGetInvalidOption()
283 $this->initializeOptions();
285 $definition = new InputDefinition(array($this->foo));
286 $definition->getOption('bar');
289 public function testHasOption()
291 $this->initializeOptions();
293 $definition = new InputDefinition(array($this->foo));
294 $this->assertTrue($definition->hasOption('foo'), '->hasOption() returns true if a InputOption exists for the given name');
295 $this->assertFalse($definition->hasOption('bar'), '->hasOption() returns false if a InputOption exists for the given name');
298 public function testHasShortcut()
300 $this->initializeOptions();
302 $definition = new InputDefinition(array($this->foo));
303 $this->assertTrue($definition->hasShortcut('f'), '->hasShortcut() returns true if a InputOption exists for the given shortcut');
304 $this->assertFalse($definition->hasShortcut('b'), '->hasShortcut() returns false if a InputOption exists for the given shortcut');
307 public function testGetOptionForShortcut()
309 $this->initializeOptions();
311 $definition = new InputDefinition(array($this->foo));
312 $this->assertEquals($this->foo, $definition->getOptionForShortcut('f'), '->getOptionForShortcut() returns a InputOption by its shortcut');
315 public function testGetOptionForMultiShortcut()
317 $this->initializeOptions();
319 $definition = new InputDefinition(array($this->multi));
320 $this->assertEquals($this->multi, $definition->getOptionForShortcut('m'), '->getOptionForShortcut() returns a InputOption by its shortcut');
321 $this->assertEquals($this->multi, $definition->getOptionForShortcut('mmm'), '->getOptionForShortcut() returns a InputOption by its shortcut');
325 * @expectedException \InvalidArgumentException
326 * @expectedExceptionMessage The "-l" option does not exist.
328 public function testGetOptionForInvalidShortcut()
330 $this->initializeOptions();
332 $definition = new InputDefinition(array($this->foo));
333 $definition->getOptionForShortcut('l');
336 public function testGetOptionDefaults()
338 $definition = new InputDefinition(array(
339 new InputOption('foo1', null, InputOption::VALUE_NONE),
340 new InputOption('foo2', null, InputOption::VALUE_REQUIRED),
341 new InputOption('foo3', null, InputOption::VALUE_REQUIRED, '', 'default'),
342 new InputOption('foo4', null, InputOption::VALUE_OPTIONAL),
343 new InputOption('foo5', null, InputOption::VALUE_OPTIONAL, '', 'default'),
344 new InputOption('foo6', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY),
345 new InputOption('foo7', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, '', array(1, 2)),
354 'foo7' => array(1, 2),
356 $this->assertSame($defaults, $definition->getOptionDefaults(), '->getOptionDefaults() returns the default values for all options');
360 * @dataProvider getGetSynopsisData
362 public function testGetSynopsis(InputDefinition $definition, $expectedSynopsis, $message = null)
364 $this->assertEquals($expectedSynopsis, $definition->getSynopsis(), $message ? '->getSynopsis() '.$message : '');
367 public function getGetSynopsisData()
370 array(new InputDefinition(array(new InputOption('foo'))), '[--foo]', 'puts optional options in square brackets'),
371 array(new InputDefinition(array(new InputOption('foo', 'f'))), '[-f|--foo]', 'separates shortcut with a pipe'),
372 array(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))), '[-f|--foo FOO]', 'uses shortcut as value placeholder'),
373 array(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))), '[-f|--foo [FOO]]', 'puts optional values in square brackets'),
375 array(new InputDefinition(array(new InputArgument('foo', InputArgument::REQUIRED))), '<foo>', 'puts arguments in angle brackets'),
376 array(new InputDefinition(array(new InputArgument('foo'))), '[<foo>]', 'puts optional arguments in square brackets'),
377 array(new InputDefinition(array(new InputArgument('foo', InputArgument::IS_ARRAY))), '[<foo>]...', 'uses an ellipsis for array arguments'),
378 array(new InputDefinition(array(new InputArgument('foo', InputArgument::REQUIRED | InputArgument::IS_ARRAY))), '<foo> (<foo>)...', 'uses parenthesis and ellipsis for required array arguments'),
380 array(new InputDefinition(array(new InputOption('foo'), new InputArgument('foo', InputArgument::REQUIRED))), '[--foo] [--] <foo>', 'puts [--] between options and arguments'),
384 public function testGetShortSynopsis()
386 $definition = new InputDefinition(array(new InputOption('foo'), new InputOption('bar'), new InputArgument('cat')));
387 $this->assertEquals('[options] [--] [<cat>]', $definition->getSynopsis(true), '->getSynopsis(true) groups options in [options]');
393 public function testLegacyAsText()
395 $definition = new InputDefinition(array(
396 new InputArgument('foo', InputArgument::OPTIONAL, 'The foo argument'),
397 new InputArgument('baz', InputArgument::OPTIONAL, 'The baz argument', true),
398 new InputArgument('bar', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The bar argument', array('http://foo.com/')),
399 new InputOption('foo', 'f', InputOption::VALUE_REQUIRED, 'The foo option'),
400 new InputOption('baz', null, InputOption::VALUE_OPTIONAL, 'The baz option', false),
401 new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL, 'The bar option', 'bar'),
402 new InputOption('qux', '', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The qux option', array('http://foo.com/', 'bar')),
403 new InputOption('qux2', '', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The qux2 option', array('foo' => 'bar')),
406 $this->assertStringEqualsFile(self::$fixtures.'/definition_astext.txt', $definition->asText(), '->asText() returns a textual representation of the InputDefinition');
412 public function testLegacyAsXml()
414 $definition = new InputDefinition(array(
415 new InputArgument('foo', InputArgument::OPTIONAL, 'The foo argument'),
416 new InputArgument('baz', InputArgument::OPTIONAL, 'The baz argument', true),
417 new InputArgument('bar', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The bar argument', array('bar')),
418 new InputOption('foo', 'f', InputOption::VALUE_REQUIRED, 'The foo option'),
419 new InputOption('baz', null, InputOption::VALUE_OPTIONAL, 'The baz option', false),
420 new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL, 'The bar option', 'bar'),
422 $this->assertXmlStringEqualsXmlFile(self::$fixtures.'/definition_asxml.txt', $definition->asXml(), '->asXml() returns an XML representation of the InputDefinition');
425 protected function initializeArguments()
427 $this->foo = new InputArgument('foo');
428 $this->bar = new InputArgument('bar');
429 $this->foo1 = new InputArgument('foo');
430 $this->foo2 = new InputArgument('foo2', InputArgument::REQUIRED);
433 protected function initializeOptions()
435 $this->foo = new InputOption('foo', 'f');
436 $this->bar = new InputOption('bar', 'b');
437 $this->foo1 = new InputOption('fooBis', 'f');
438 $this->foo2 = new InputOption('foo', 'p');
439 $this->multi = new InputOption('multi', 'm|mm|mmm');