TYPO3  7.6
InputDefinitionTest.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 
17 
18 class InputDefinitionTest extends \PHPUnit_Framework_TestCase
19 {
20  protected static $fixtures;
21 
22  protected $foo, $bar, $foo1, $foo2;
23 
24  public static function setUpBeforeClass()
25  {
26  self::$fixtures = __DIR__.'/../Fixtures/';
27  }
28 
29  public function testConstructorArguments()
30  {
31  $this->initializeArguments();
32 
33  $definition = new InputDefinition();
34  $this->assertEquals(array(), $definition->getArguments(), '__construct() creates a new InputDefinition object');
35 
36  $definition = new InputDefinition(array($this->foo, $this->bar));
37  $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getArguments(), '__construct() takes an array of InputArgument objects as its first argument');
38  }
39 
40  public function testConstructorOptions()
41  {
42  $this->initializeOptions();
43 
44  $definition = new InputDefinition();
45  $this->assertEquals(array(), $definition->getOptions(), '__construct() creates a new InputDefinition object');
46 
47  $definition = new InputDefinition(array($this->foo, $this->bar));
48  $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getOptions(), '__construct() takes an array of InputOption objects as its first argument');
49  }
50 
51  public function testSetArguments()
52  {
53  $this->initializeArguments();
54 
55  $definition = new InputDefinition();
56  $definition->setArguments(array($this->foo));
57  $this->assertEquals(array('foo' => $this->foo), $definition->getArguments(), '->setArguments() sets the array of InputArgument objects');
58  $definition->setArguments(array($this->bar));
59 
60  $this->assertEquals(array('bar' => $this->bar), $definition->getArguments(), '->setArguments() clears all InputArgument objects');
61  }
62 
63  public function testAddArguments()
64  {
65  $this->initializeArguments();
66 
67  $definition = new InputDefinition();
68  $definition->addArguments(array($this->foo));
69  $this->assertEquals(array('foo' => $this->foo), $definition->getArguments(), '->addArguments() adds an array of InputArgument objects');
70  $definition->addArguments(array($this->bar));
71  $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getArguments(), '->addArguments() does not clear existing InputArgument objects');
72  }
73 
74  public function testAddArgument()
75  {
76  $this->initializeArguments();
77 
78  $definition = new InputDefinition();
79  $definition->addArgument($this->foo);
80  $this->assertEquals(array('foo' => $this->foo), $definition->getArguments(), '->addArgument() adds a InputArgument object');
81  $definition->addArgument($this->bar);
82  $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getArguments(), '->addArgument() adds a InputArgument object');
83  }
84 
90  {
91  $this->initializeArguments();
92 
93  $definition = new InputDefinition();
94  $definition->addArgument($this->foo);
95  $definition->addArgument($this->foo1);
96  }
97 
103  {
104  $this->initializeArguments();
105 
106  $definition = new InputDefinition();
107  $definition->addArgument(new InputArgument('fooarray', InputArgument::IS_ARRAY));
108  $definition->addArgument(new InputArgument('anotherbar'));
109  }
110 
116  {
117  $this->initializeArguments();
118 
119  $definition = new InputDefinition();
120  $definition->addArgument($this->foo);
121  $definition->addArgument($this->foo2);
122  }
123 
124  public function testGetArgument()
125  {
126  $this->initializeArguments();
127 
128  $definition = new InputDefinition();
129  $definition->addArguments(array($this->foo));
130  $this->assertEquals($this->foo, $definition->getArgument('foo'), '->getArgument() returns a InputArgument by its name');
131  }
132 
137  public function testGetInvalidArgument()
138  {
139  $this->initializeArguments();
140 
141  $definition = new InputDefinition();
142  $definition->addArguments(array($this->foo));
143  $definition->getArgument('bar');
144  }
145 
146  public function testHasArgument()
147  {
148  $this->initializeArguments();
149 
150  $definition = new InputDefinition();
151  $definition->addArguments(array($this->foo));
152 
153  $this->assertTrue($definition->hasArgument('foo'), '->hasArgument() returns true if a InputArgument exists for the given name');
154  $this->assertFalse($definition->hasArgument('bar'), '->hasArgument() returns false if a InputArgument exists for the given name');
155  }
156 
158  {
159  $this->initializeArguments();
160 
161  $definition = new InputDefinition();
162  $definition->addArgument($this->foo2);
163  $this->assertEquals(1, $definition->getArgumentRequiredCount(), '->getArgumentRequiredCount() returns the number of required arguments');
164  $definition->addArgument($this->foo);
165  $this->assertEquals(1, $definition->getArgumentRequiredCount(), '->getArgumentRequiredCount() returns the number of required arguments');
166  }
167 
168  public function testGetArgumentCount()
169  {
170  $this->initializeArguments();
171 
172  $definition = new InputDefinition();
173  $definition->addArgument($this->foo2);
174  $this->assertEquals(1, $definition->getArgumentCount(), '->getArgumentCount() returns the number of arguments');
175  $definition->addArgument($this->foo);
176  $this->assertEquals(2, $definition->getArgumentCount(), '->getArgumentCount() returns the number of arguments');
177  }
178 
179  public function testGetArgumentDefaults()
180  {
181  $definition = new InputDefinition(array(
183  new InputArgument('foo2', InputArgument::OPTIONAL, '', 'default'),
185  // new InputArgument('foo4', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, '', array(1, 2)),
186  ));
187  $this->assertEquals(array('foo1' => null, 'foo2' => 'default', 'foo3' => array()), $definition->getArgumentDefaults(), '->getArgumentDefaults() return the default values for each argument');
188 
189  $definition = new InputDefinition(array(
190  new InputArgument('foo4', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, '', array(1, 2)),
191  ));
192  $this->assertEquals(array('foo4' => array(1, 2)), $definition->getArgumentDefaults(), '->getArgumentDefaults() return the default values for each argument');
193  }
194 
195  public function testSetOptions()
196  {
197  $this->initializeOptions();
198 
199  $definition = new InputDefinition(array($this->foo));
200  $this->assertEquals(array('foo' => $this->foo), $definition->getOptions(), '->setOptions() sets the array of InputOption objects');
201  $definition->setOptions(array($this->bar));
202  $this->assertEquals(array('bar' => $this->bar), $definition->getOptions(), '->setOptions() clears all InputOption objects');
203  }
204 
209  public function testSetOptionsClearsOptions()
210  {
211  $this->initializeOptions();
212 
213  $definition = new InputDefinition(array($this->foo));
214  $definition->setOptions(array($this->bar));
215  $definition->getOptionForShortcut('f');
216  }
217 
218  public function testAddOptions()
219  {
220  $this->initializeOptions();
221 
222  $definition = new InputDefinition(array($this->foo));
223  $this->assertEquals(array('foo' => $this->foo), $definition->getOptions(), '->addOptions() adds an array of InputOption objects');
224  $definition->addOptions(array($this->bar));
225  $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getOptions(), '->addOptions() does not clear existing InputOption objects');
226  }
227 
228  public function testAddOption()
229  {
230  $this->initializeOptions();
231 
232  $definition = new InputDefinition();
233  $definition->addOption($this->foo);
234  $this->assertEquals(array('foo' => $this->foo), $definition->getOptions(), '->addOption() adds a InputOption object');
235  $definition->addOption($this->bar);
236  $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getOptions(), '->addOption() adds a InputOption object');
237  }
238 
243  public function testAddDuplicateOption()
244  {
245  $this->initializeOptions();
246 
247  $definition = new InputDefinition();
248  $definition->addOption($this->foo);
249  $definition->addOption($this->foo2);
250  }
251 
257  {
258  $this->initializeOptions();
259 
260  $definition = new InputDefinition();
261  $definition->addOption($this->foo);
262  $definition->addOption($this->foo1);
263  }
264 
265  public function testGetOption()
266  {
267  $this->initializeOptions();
268 
269  $definition = new InputDefinition(array($this->foo));
270  $this->assertEquals($this->foo, $definition->getOption('foo'), '->getOption() returns a InputOption by its name');
271  }
272 
277  public function testGetInvalidOption()
278  {
279  $this->initializeOptions();
280 
281  $definition = new InputDefinition(array($this->foo));
282  $definition->getOption('bar');
283  }
284 
285  public function testHasOption()
286  {
287  $this->initializeOptions();
288 
289  $definition = new InputDefinition(array($this->foo));
290  $this->assertTrue($definition->hasOption('foo'), '->hasOption() returns true if a InputOption exists for the given name');
291  $this->assertFalse($definition->hasOption('bar'), '->hasOption() returns false if a InputOption exists for the given name');
292  }
293 
294  public function testHasShortcut()
295  {
296  $this->initializeOptions();
297 
298  $definition = new InputDefinition(array($this->foo));
299  $this->assertTrue($definition->hasShortcut('f'), '->hasShortcut() returns true if a InputOption exists for the given shortcut');
300  $this->assertFalse($definition->hasShortcut('b'), '->hasShortcut() returns false if a InputOption exists for the given shortcut');
301  }
302 
303  public function testGetOptionForShortcut()
304  {
305  $this->initializeOptions();
306 
307  $definition = new InputDefinition(array($this->foo));
308  $this->assertEquals($this->foo, $definition->getOptionForShortcut('f'), '->getOptionForShortcut() returns a InputOption by its shortcut');
309  }
310 
312  {
313  $this->initializeOptions();
314 
315  $definition = new InputDefinition(array($this->multi));
316  $this->assertEquals($this->multi, $definition->getOptionForShortcut('m'), '->getOptionForShortcut() returns a InputOption by its shortcut');
317  $this->assertEquals($this->multi, $definition->getOptionForShortcut('mmm'), '->getOptionForShortcut() returns a InputOption by its shortcut');
318  }
319 
325  {
326  $this->initializeOptions();
327 
328  $definition = new InputDefinition(array($this->foo));
329  $definition->getOptionForShortcut('l');
330  }
331 
332  public function testGetOptionDefaults()
333  {
334  $definition = new InputDefinition(array(
335  new InputOption('foo1', null, InputOption::VALUE_NONE),
336  new InputOption('foo2', null, InputOption::VALUE_REQUIRED),
337  new InputOption('foo3', null, InputOption::VALUE_REQUIRED, '', 'default'),
338  new InputOption('foo4', null, InputOption::VALUE_OPTIONAL),
339  new InputOption('foo5', null, InputOption::VALUE_OPTIONAL, '', 'default'),
341  new InputOption('foo7', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, '', array(1, 2)),
342  ));
343  $defaults = array(
344  'foo1' => false,
345  'foo2' => null,
346  'foo3' => 'default',
347  'foo4' => null,
348  'foo5' => 'default',
349  'foo6' => array(),
350  'foo7' => array(1, 2),
351  );
352  $this->assertSame($defaults, $definition->getOptionDefaults(), '->getOptionDefaults() returns the default values for all options');
353  }
354 
358  public function testGetSynopsis(InputDefinition $definition, $expectedSynopsis, $message = null)
359  {
360  $this->assertEquals($expectedSynopsis, $definition->getSynopsis(), $message ? '->getSynopsis() '.$message : '');
361  }
362 
363  public function getGetSynopsisData()
364  {
365  return array(
366  array(new InputDefinition(array(new InputOption('foo'))), '[--foo]', 'puts optional options in square brackets'),
367  array(new InputDefinition(array(new InputOption('foo', 'f'))), '[-f|--foo]', 'separates shortcut with a pipe'),
368  array(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))), '[-f|--foo FOO]', 'uses shortcut as value placeholder'),
369  array(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))), '[-f|--foo [FOO]]', 'puts optional values in square brackets'),
370 
371  array(new InputDefinition(array(new InputArgument('foo', InputArgument::REQUIRED))), '<foo>', 'puts arguments in angle brackets'),
372  array(new InputDefinition(array(new InputArgument('foo'))), '[<foo>]', 'puts optional arguments in square brackets'),
373  array(new InputDefinition(array(new InputArgument('foo', InputArgument::IS_ARRAY))), '[<foo>]...', 'uses an ellipsis for array arguments'),
374  array(new InputDefinition(array(new InputArgument('foo', InputArgument::REQUIRED | InputArgument::IS_ARRAY))), '<foo> (<foo>)...', 'uses parenthesis and ellipsis for required array arguments'),
375 
376  array(new InputDefinition(array(new InputOption('foo'), new InputArgument('foo', InputArgument::REQUIRED))), '[--foo] [--] <foo>', 'puts [--] between options and arguments'),
377  );
378  }
379 
380  public function testGetShortSynopsis()
381  {
382  $definition = new InputDefinition(array(new InputOption('foo'), new InputOption('bar'), new InputArgument('cat')));
383  $this->assertEquals('[options] [--] [<cat>]', $definition->getSynopsis(true), '->getSynopsis(true) groups options in [options]');
384  }
385 
389  public function testLegacyAsText()
390  {
391  $definition = new InputDefinition(array(
392  new InputArgument('foo', InputArgument::OPTIONAL, 'The foo argument'),
393  new InputArgument('baz', InputArgument::OPTIONAL, 'The baz argument', true),
394  new InputArgument('bar', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The bar argument', array('http://foo.com/')),
395  new InputOption('foo', 'f', InputOption::VALUE_REQUIRED, 'The foo option'),
396  new InputOption('baz', null, InputOption::VALUE_OPTIONAL, 'The baz option', false),
397  new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL, 'The bar option', 'bar'),
398  new InputOption('qux', '', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The qux option', array('http://foo.com/', 'bar')),
399  new InputOption('qux2', '', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The qux2 option', array('foo' => 'bar')),
400  ));
401 
402  $this->assertStringEqualsFile(self::$fixtures.'/definition_astext.txt', $definition->asText(), '->asText() returns a textual representation of the InputDefinition');
403  }
404 
408  public function testLegacyAsXml()
409  {
410  $definition = new InputDefinition(array(
411  new InputArgument('foo', InputArgument::OPTIONAL, 'The foo argument'),
412  new InputArgument('baz', InputArgument::OPTIONAL, 'The baz argument', true),
413  new InputArgument('bar', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The bar argument', array('bar')),
414  new InputOption('foo', 'f', InputOption::VALUE_REQUIRED, 'The foo option'),
415  new InputOption('baz', null, InputOption::VALUE_OPTIONAL, 'The baz option', false),
416  new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL, 'The bar option', 'bar'),
417  ));
418  $this->assertXmlStringEqualsXmlFile(self::$fixtures.'/definition_asxml.txt', $definition->asXml(), '->asXml() returns an XML representation of the InputDefinition');
419  }
420 
421  protected function initializeArguments()
422  {
423  $this->foo = new InputArgument('foo');
424  $this->bar = new InputArgument('bar');
425  $this->foo1 = new InputArgument('foo');
426  $this->foo2 = new InputArgument('foo2', InputArgument::REQUIRED);
427  }
428 
429  protected function initializeOptions()
430  {
431  $this->foo = new InputOption('foo', 'f');
432  $this->bar = new InputOption('bar', 'b');
433  $this->foo1 = new InputOption('fooBis', 'f');
434  $this->foo2 = new InputOption('foo', 'p');
435  $this->multi = new InputOption('multi', 'm|mm|mmm');
436  }
437 }