Yaffs site version 1.1
[yaffs-website] / vendor / symfony / console / Tests / Input / InputDefinitionTest.php
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
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;
18
19 class InputDefinitionTest extends TestCase
20 {
21     protected static $fixtures;
22
23     protected $foo;
24     protected $bar;
25     protected $foo1;
26     protected $foo2;
27
28     public static function setUpBeforeClass()
29     {
30         self::$fixtures = __DIR__.'/../Fixtures/';
31     }
32
33     public function testConstructorArguments()
34     {
35         $this->initializeArguments();
36
37         $definition = new InputDefinition();
38         $this->assertEquals(array(), $definition->getArguments(), '__construct() creates a new InputDefinition object');
39
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');
42     }
43
44     public function testConstructorOptions()
45     {
46         $this->initializeOptions();
47
48         $definition = new InputDefinition();
49         $this->assertEquals(array(), $definition->getOptions(), '__construct() creates a new InputDefinition object');
50
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');
53     }
54
55     public function testSetArguments()
56     {
57         $this->initializeArguments();
58
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));
63
64         $this->assertEquals(array('bar' => $this->bar), $definition->getArguments(), '->setArguments() clears all InputArgument objects');
65     }
66
67     public function testAddArguments()
68     {
69         $this->initializeArguments();
70
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');
76     }
77
78     public function testAddArgument()
79     {
80         $this->initializeArguments();
81
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');
87     }
88
89     /**
90      * @expectedException        \LogicException
91      * @expectedExceptionMessage An argument with name "foo" already exists.
92      */
93     public function testArgumentsMustHaveDifferentNames()
94     {
95         $this->initializeArguments();
96
97         $definition = new InputDefinition();
98         $definition->addArgument($this->foo);
99         $definition->addArgument($this->foo1);
100     }
101
102     /**
103      * @expectedException        \LogicException
104      * @expectedExceptionMessage Cannot add an argument after an array argument.
105      */
106     public function testArrayArgumentHasToBeLast()
107     {
108         $this->initializeArguments();
109
110         $definition = new InputDefinition();
111         $definition->addArgument(new InputArgument('fooarray', InputArgument::IS_ARRAY));
112         $definition->addArgument(new InputArgument('anotherbar'));
113     }
114
115     /**
116      * @expectedException        \LogicException
117      * @expectedExceptionMessage Cannot add a required argument after an optional one.
118      */
119     public function testRequiredArgumentCannotFollowAnOptionalOne()
120     {
121         $this->initializeArguments();
122
123         $definition = new InputDefinition();
124         $definition->addArgument($this->foo);
125         $definition->addArgument($this->foo2);
126     }
127
128     public function testGetArgument()
129     {
130         $this->initializeArguments();
131
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');
135     }
136
137     /**
138      * @expectedException        \InvalidArgumentException
139      * @expectedExceptionMessage The "bar" argument does not exist.
140      */
141     public function testGetInvalidArgument()
142     {
143         $this->initializeArguments();
144
145         $definition = new InputDefinition();
146         $definition->addArguments(array($this->foo));
147         $definition->getArgument('bar');
148     }
149
150     public function testHasArgument()
151     {
152         $this->initializeArguments();
153
154         $definition = new InputDefinition();
155         $definition->addArguments(array($this->foo));
156
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');
159     }
160
161     public function testGetArgumentRequiredCount()
162     {
163         $this->initializeArguments();
164
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');
170     }
171
172     public function testGetArgumentCount()
173     {
174         $this->initializeArguments();
175
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');
181     }
182
183     public function testGetArgumentDefaults()
184     {
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)),
190         ));
191         $this->assertEquals(array('foo1' => null, 'foo2' => 'default', 'foo3' => array()), $definition->getArgumentDefaults(), '->getArgumentDefaults() return the default values for each argument');
192
193         $definition = new InputDefinition(array(
194             new InputArgument('foo4', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, '', array(1, 2)),
195         ));
196         $this->assertEquals(array('foo4' => array(1, 2)), $definition->getArgumentDefaults(), '->getArgumentDefaults() return the default values for each argument');
197     }
198
199     public function testSetOptions()
200     {
201         $this->initializeOptions();
202
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');
207     }
208
209     /**
210      * @expectedException        \InvalidArgumentException
211      * @expectedExceptionMessage The "-f" option does not exist.
212      */
213     public function testSetOptionsClearsOptions()
214     {
215         $this->initializeOptions();
216
217         $definition = new InputDefinition(array($this->foo));
218         $definition->setOptions(array($this->bar));
219         $definition->getOptionForShortcut('f');
220     }
221
222     public function testAddOptions()
223     {
224         $this->initializeOptions();
225
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');
230     }
231
232     public function testAddOption()
233     {
234         $this->initializeOptions();
235
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');
241     }
242
243     /**
244      * @expectedException        \LogicException
245      * @expectedExceptionMessage An option named "foo" already exists.
246      */
247     public function testAddDuplicateOption()
248     {
249         $this->initializeOptions();
250
251         $definition = new InputDefinition();
252         $definition->addOption($this->foo);
253         $definition->addOption($this->foo2);
254     }
255
256     /**
257      * @expectedException        \LogicException
258      * @expectedExceptionMessage An option with shortcut "f" already exists.
259      */
260     public function testAddDuplicateShortcutOption()
261     {
262         $this->initializeOptions();
263
264         $definition = new InputDefinition();
265         $definition->addOption($this->foo);
266         $definition->addOption($this->foo1);
267     }
268
269     public function testGetOption()
270     {
271         $this->initializeOptions();
272
273         $definition = new InputDefinition(array($this->foo));
274         $this->assertEquals($this->foo, $definition->getOption('foo'), '->getOption() returns a InputOption by its name');
275     }
276
277     /**
278      * @expectedException        \InvalidArgumentException
279      * @expectedExceptionMessage The "--bar" option does not exist.
280      */
281     public function testGetInvalidOption()
282     {
283         $this->initializeOptions();
284
285         $definition = new InputDefinition(array($this->foo));
286         $definition->getOption('bar');
287     }
288
289     public function testHasOption()
290     {
291         $this->initializeOptions();
292
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');
296     }
297
298     public function testHasShortcut()
299     {
300         $this->initializeOptions();
301
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');
305     }
306
307     public function testGetOptionForShortcut()
308     {
309         $this->initializeOptions();
310
311         $definition = new InputDefinition(array($this->foo));
312         $this->assertEquals($this->foo, $definition->getOptionForShortcut('f'), '->getOptionForShortcut() returns a InputOption by its shortcut');
313     }
314
315     public function testGetOptionForMultiShortcut()
316     {
317         $this->initializeOptions();
318
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');
322     }
323
324     /**
325      * @expectedException        \InvalidArgumentException
326      * @expectedExceptionMessage The "-l" option does not exist.
327      */
328     public function testGetOptionForInvalidShortcut()
329     {
330         $this->initializeOptions();
331
332         $definition = new InputDefinition(array($this->foo));
333         $definition->getOptionForShortcut('l');
334     }
335
336     public function testGetOptionDefaults()
337     {
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)),
346         ));
347         $defaults = array(
348             'foo1' => false,
349             'foo2' => null,
350             'foo3' => 'default',
351             'foo4' => null,
352             'foo5' => 'default',
353             'foo6' => array(),
354             'foo7' => array(1, 2),
355         );
356         $this->assertSame($defaults, $definition->getOptionDefaults(), '->getOptionDefaults() returns the default values for all options');
357     }
358
359     /**
360      * @dataProvider getGetSynopsisData
361      */
362     public function testGetSynopsis(InputDefinition $definition, $expectedSynopsis, $message = null)
363     {
364         $this->assertEquals($expectedSynopsis, $definition->getSynopsis(), $message ? '->getSynopsis() '.$message : '');
365     }
366
367     public function getGetSynopsisData()
368     {
369         return array(
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'),
374
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'),
379
380             array(new InputDefinition(array(new InputOption('foo'), new InputArgument('foo', InputArgument::REQUIRED))), '[--foo] [--] <foo>', 'puts [--] between options and arguments'),
381         );
382     }
383
384     public function testGetShortSynopsis()
385     {
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]');
388     }
389
390     /**
391      * @group legacy
392      */
393     public function testLegacyAsText()
394     {
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')),
404         ));
405
406         $this->assertStringEqualsFile(self::$fixtures.'/definition_astext.txt', $definition->asText(), '->asText() returns a textual representation of the InputDefinition');
407     }
408
409     /**
410      * @group legacy
411      */
412     public function testLegacyAsXml()
413     {
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'),
421         ));
422         $this->assertXmlStringEqualsXmlFile(self::$fixtures.'/definition_asxml.txt', $definition->asXml(), '->asXml() returns an XML representation of the InputDefinition');
423     }
424
425     protected function initializeArguments()
426     {
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);
431     }
432
433     protected function initializeOptions()
434     {
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');
440     }
441 }