Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / symfony / console / Symfony / Component / 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 Symfony\Component\Console\Input\InputDefinition;
15 use Symfony\Component\Console\Input\InputArgument;
16 use Symfony\Component\Console\Input\InputOption;
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 testConstructor()
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         $this->initializeOptions();
40
41         $definition = new InputDefinition();
42         $this->assertEquals(array(), $definition->getOptions(), '__construct() creates a new InputDefinition object');
43
44         $definition = new InputDefinition(array($this->foo, $this->bar));
45         $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getOptions(), '__construct() takes an array of InputOption objects as its first argument');
46     }
47
48     public function testSetArguments()
49     {
50         $this->initializeArguments();
51
52         $definition = new InputDefinition();
53         $definition->setArguments(array($this->foo));
54         $this->assertEquals(array('foo' => $this->foo), $definition->getArguments(), '->setArguments() sets the array of InputArgument objects');
55         $definition->setArguments(array($this->bar));
56
57         $this->assertEquals(array('bar' => $this->bar), $definition->getArguments(), '->setArguments() clears all InputArgument objects');
58     }
59
60     public function testAddArguments()
61     {
62         $this->initializeArguments();
63
64         $definition = new InputDefinition();
65         $definition->addArguments(array($this->foo));
66         $this->assertEquals(array('foo' => $this->foo), $definition->getArguments(), '->addArguments() adds an array of InputArgument objects');
67         $definition->addArguments(array($this->bar));
68         $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getArguments(), '->addArguments() does not clear existing InputArgument objects');
69     }
70
71     public function testAddArgument()
72     {
73         $this->initializeArguments();
74
75         $definition = new InputDefinition();
76         $definition->addArgument($this->foo);
77         $this->assertEquals(array('foo' => $this->foo), $definition->getArguments(), '->addArgument() adds a InputArgument object');
78         $definition->addArgument($this->bar);
79         $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getArguments(), '->addArgument() adds a InputArgument object');
80
81         // arguments must have different names
82         try {
83             $definition->addArgument($this->foo1);
84             $this->fail('->addArgument() throws a Exception if another argument is already registered with the same name');
85         } catch (\Exception $e) {
86             $this->assertInstanceOf('\Exception', $e, '->addArgument() throws a Exception if another argument is already registered with the same name');
87             $this->assertEquals('An argument with name "foo" already exists.', $e->getMessage());
88         }
89
90         // cannot add a parameter after an array parameter
91         $definition->addArgument(new InputArgument('fooarray', InputArgument::IS_ARRAY));
92         try {
93             $definition->addArgument(new InputArgument('anotherbar'));
94             $this->fail('->addArgument() throws a Exception if there is an array parameter already registered');
95         } catch (\Exception $e) {
96             $this->assertInstanceOf('\Exception', $e, '->addArgument() throws a Exception if there is an array parameter already registered');
97             $this->assertEquals('Cannot add an argument after an array argument.', $e->getMessage());
98         }
99
100         // cannot add a required argument after an optional one
101         $definition = new InputDefinition();
102         $definition->addArgument($this->foo);
103         try {
104             $definition->addArgument($this->foo2);
105             $this->fail('->addArgument() throws an exception if you try to add a required argument after an optional one');
106         } catch (\Exception $e) {
107             $this->assertInstanceOf('\Exception', $e, '->addArgument() throws an exception if you try to add a required argument after an optional one');
108             $this->assertEquals('Cannot add a required argument after an optional one.', $e->getMessage());
109         }
110     }
111
112     public function testGetArgument()
113     {
114         $this->initializeArguments();
115
116         $definition = new InputDefinition();
117         $definition->addArguments(array($this->foo));
118         $this->assertEquals($this->foo, $definition->getArgument('foo'), '->getArgument() returns a InputArgument by its name');
119         try {
120             $definition->getArgument('bar');
121             $this->fail('->getArgument() throws an exception if the InputArgument name does not exist');
122         } catch (\Exception $e) {
123             $this->assertInstanceOf('\Exception', $e, '->getArgument() throws an exception if the InputArgument name does not exist');
124             $this->assertEquals('The "bar" argument does not exist.', $e->getMessage());
125         }
126     }
127
128     public function testHasArgument()
129     {
130         $this->initializeArguments();
131
132         $definition = new InputDefinition();
133         $definition->addArguments(array($this->foo));
134         $this->assertTrue($definition->hasArgument('foo'), '->hasArgument() returns true if a InputArgument exists for the given name');
135         $this->assertFalse($definition->hasArgument('bar'), '->hasArgument() returns false if a InputArgument exists for the given name');
136     }
137
138     public function testGetArgumentRequiredCount()
139     {
140         $this->initializeArguments();
141
142         $definition = new InputDefinition();
143         $definition->addArgument($this->foo2);
144         $this->assertEquals(1, $definition->getArgumentRequiredCount(), '->getArgumentRequiredCount() returns the number of required arguments');
145         $definition->addArgument($this->foo);
146         $this->assertEquals(1, $definition->getArgumentRequiredCount(), '->getArgumentRequiredCount() returns the number of required arguments');
147     }
148
149     public function testGetArgumentCount()
150     {
151         $this->initializeArguments();
152
153         $definition = new InputDefinition();
154         $definition->addArgument($this->foo2);
155         $this->assertEquals(1, $definition->getArgumentCount(), '->getArgumentCount() returns the number of arguments');
156         $definition->addArgument($this->foo);
157         $this->assertEquals(2, $definition->getArgumentCount(), '->getArgumentCount() returns the number of arguments');
158     }
159
160     public function testGetArgumentDefaults()
161     {
162         $definition = new InputDefinition(array(
163             new InputArgument('foo1', InputArgument::OPTIONAL),
164             new InputArgument('foo2', InputArgument::OPTIONAL, '', 'default'),
165             new InputArgument('foo3', InputArgument::OPTIONAL | InputArgument::IS_ARRAY),
166         //  new InputArgument('foo4', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, '', array(1, 2)),
167         ));
168         $this->assertEquals(array('foo1' => null, 'foo2' => 'default', 'foo3' => array()), $definition->getArgumentDefaults(), '->getArgumentDefaults() return the default values for each argument');
169
170         $definition = new InputDefinition(array(
171             new InputArgument('foo4', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, '', array(1, 2)),
172         ));
173         $this->assertEquals(array('foo4' => array(1, 2)), $definition->getArgumentDefaults(), '->getArgumentDefaults() return the default values for each argument');
174     }
175
176     public function testSetOptions()
177     {
178         $this->initializeOptions();
179
180         $definition = new InputDefinition(array($this->foo));
181         $this->assertEquals(array('foo' => $this->foo), $definition->getOptions(), '->setOptions() sets the array of InputOption objects');
182         $definition->setOptions(array($this->bar));
183         $this->assertEquals(array('bar' => $this->bar), $definition->getOptions(), '->setOptions() clears all InputOption objects');
184         try {
185             $definition->getOptionForShortcut('f');
186             $this->fail('->setOptions() clears all InputOption objects');
187         } catch (\Exception $e) {
188             $this->assertInstanceOf('\Exception', $e, '->setOptions() clears all InputOption objects');
189             $this->assertEquals('The "-f" option does not exist.', $e->getMessage());
190         }
191     }
192
193     public function testAddOptions()
194     {
195         $this->initializeOptions();
196
197         $definition = new InputDefinition(array($this->foo));
198         $this->assertEquals(array('foo' => $this->foo), $definition->getOptions(), '->addOptions() adds an array of InputOption objects');
199         $definition->addOptions(array($this->bar));
200         $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getOptions(), '->addOptions() does not clear existing InputOption objects');
201     }
202
203     public function testAddOption()
204     {
205         $this->initializeOptions();
206
207         $definition = new InputDefinition();
208         $definition->addOption($this->foo);
209         $this->assertEquals(array('foo' => $this->foo), $definition->getOptions(), '->addOption() adds a InputOption object');
210         $definition->addOption($this->bar);
211         $this->assertEquals(array('foo' => $this->foo, 'bar' => $this->bar), $definition->getOptions(), '->addOption() adds a InputOption object');
212         try {
213             $definition->addOption($this->foo2);
214             $this->fail('->addOption() throws a Exception if the another option is already registered with the same name');
215         } catch (\Exception $e) {
216             $this->assertInstanceOf('\Exception', $e, '->addOption() throws a Exception if the another option is already registered with the same name');
217             $this->assertEquals('An option named "foo" already exists.', $e->getMessage());
218         }
219         try {
220             $definition->addOption($this->foo1);
221             $this->fail('->addOption() throws a Exception if the another option is already registered with the same shortcut');
222         } catch (\Exception $e) {
223             $this->assertInstanceOf('\Exception', $e, '->addOption() throws a Exception if the another option is already registered with the same shortcut');
224             $this->assertEquals('An option with shortcut "f" already exists.', $e->getMessage());
225         }
226     }
227
228     public function testGetOption()
229     {
230         $this->initializeOptions();
231
232         $definition = new InputDefinition(array($this->foo));
233         $this->assertEquals($this->foo, $definition->getOption('foo'), '->getOption() returns a InputOption by its name');
234         try {
235             $definition->getOption('bar');
236             $this->fail('->getOption() throws an exception if the option name does not exist');
237         } catch (\Exception $e) {
238             $this->assertInstanceOf('\Exception', $e, '->getOption() throws an exception if the option name does not exist');
239             $this->assertEquals('The "--bar" option does not exist.', $e->getMessage());
240         }
241     }
242
243     public function testHasOption()
244     {
245         $this->initializeOptions();
246
247         $definition = new InputDefinition(array($this->foo));
248         $this->assertTrue($definition->hasOption('foo'), '->hasOption() returns true if a InputOption exists for the given name');
249         $this->assertFalse($definition->hasOption('bar'), '->hasOption() returns false if a InputOption exists for the given name');
250     }
251
252     public function testHasShortcut()
253     {
254         $this->initializeOptions();
255
256         $definition = new InputDefinition(array($this->foo));
257         $this->assertTrue($definition->hasShortcut('f'), '->hasShortcut() returns true if a InputOption exists for the given shortcut');
258         $this->assertFalse($definition->hasShortcut('b'), '->hasShortcut() returns false if a InputOption exists for the given shortcut');
259     }
260
261     public function testGetOptionForShortcut()
262     {
263         $this->initializeOptions();
264
265         $definition = new InputDefinition(array($this->foo));
266         $this->assertEquals($this->foo, $definition->getOptionForShortcut('f'), '->getOptionForShortcut() returns a InputOption by its shortcut');
267         try {
268             $definition->getOptionForShortcut('l');
269             $this->fail('->getOption() throws an exception if the shortcut does not exist');
270         } catch (\Exception $e) {
271             $this->assertInstanceOf('\Exception', $e, '->getOption() throws an exception if the shortcut does not exist');
272             $this->assertEquals('The "-l" option does not exist.', $e->getMessage());
273         }
274     }
275
276     public function testGetOptionDefaults()
277     {
278         $definition = new InputDefinition(array(
279             new InputOption('foo1', null, InputOption::VALUE_NONE),
280             new InputOption('foo2', null, InputOption::VALUE_REQUIRED),
281             new InputOption('foo3', null, InputOption::VALUE_REQUIRED, '', 'default'),
282             new InputOption('foo4', null, InputOption::VALUE_OPTIONAL),
283             new InputOption('foo5', null, InputOption::VALUE_OPTIONAL, '', 'default'),
284             new InputOption('foo6', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY),
285             new InputOption('foo7', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, '', array(1, 2)),
286         ));
287         $defaults = array(
288             'foo1' => null,
289             'foo2' => null,
290             'foo3' => 'default',
291             'foo4' => null,
292             'foo5' => 'default',
293             'foo6' => array(),
294             'foo7' => array(1, 2),
295         );
296         $this->assertEquals($defaults, $definition->getOptionDefaults(), '->getOptionDefaults() returns the default values for all options');
297     }
298
299     public function testGetSynopsis()
300     {
301         $definition = new InputDefinition(array(new InputOption('foo')));
302         $this->assertEquals('[--foo]', $definition->getSynopsis(), '->getSynopsis() returns a synopsis of arguments and options');
303         $definition = new InputDefinition(array(new InputOption('foo', 'f')));
304         $this->assertEquals('[-f|--foo]', $definition->getSynopsis(), '->getSynopsis() returns a synopsis of arguments and options');
305         $definition = new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)));
306         $this->assertEquals('[-f|--foo="..."]', $definition->getSynopsis(), '->getSynopsis() returns a synopsis of arguments and options');
307         $definition = new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)));
308         $this->assertEquals('[-f|--foo[="..."]]', $definition->getSynopsis(), '->getSynopsis() returns a synopsis of arguments and options');
309
310         $definition = new InputDefinition(array(new InputArgument('foo')));
311         $this->assertEquals('[foo]', $definition->getSynopsis(), '->getSynopsis() returns a synopsis of arguments and options');
312         $definition = new InputDefinition(array(new InputArgument('foo', InputArgument::REQUIRED)));
313         $this->assertEquals('foo', $definition->getSynopsis(), '->getSynopsis() returns a synopsis of arguments and options');
314         $definition = new InputDefinition(array(new InputArgument('foo', InputArgument::IS_ARRAY)));
315         $this->assertEquals('[foo1] ... [fooN]', $definition->getSynopsis(), '->getSynopsis() returns a synopsis of arguments and options');
316         $definition = new InputDefinition(array(new InputArgument('foo', InputArgument::REQUIRED | InputArgument::IS_ARRAY)));
317         $this->assertEquals('foo1 ... [fooN]', $definition->getSynopsis(), '->getSynopsis() returns a synopsis of arguments and options');
318     }
319
320     public function testAsText()
321     {
322         $definition = new InputDefinition(array(
323             new InputArgument('foo', InputArgument::OPTIONAL, 'The foo argument'),
324             new InputArgument('baz', InputArgument::OPTIONAL, 'The baz argument', true),
325             new InputArgument('bar', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The bar argument', array('bar')),
326             new InputOption('foo', 'f', InputOption::VALUE_REQUIRED, 'The foo option'),
327             new InputOption('baz', null, InputOption::VALUE_OPTIONAL, 'The baz option', false),
328             new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL, 'The bar option', 'bar'),
329             new InputOption('qux', '', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The qux option', array('foo', 'bar')),
330             new InputOption('qux2', '', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The qux2 option', array('foo' => 'bar')),
331         ));
332         $this->assertStringEqualsFile(self::$fixtures.'/definition_astext.txt', $definition->asText(), '->asText() returns a textual representation of the InputDefinition');
333     }
334
335     public function testAsXml()
336     {
337         $definition = new InputDefinition(array(
338             new InputArgument('foo', InputArgument::OPTIONAL, 'The foo argument'),
339             new InputArgument('baz', InputArgument::OPTIONAL, 'The baz argument', true),
340             new InputArgument('bar', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The bar argument', array('bar')),
341             new InputOption('foo', 'f', InputOption::VALUE_REQUIRED, 'The foo option'),
342             new InputOption('baz', null, InputOption::VALUE_OPTIONAL, 'The baz option', false),
343             new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL, 'The bar option', 'bar'),
344         ));
345         $this->assertXmlStringEqualsXmlFile(self::$fixtures.'/definition_asxml.txt', $definition->asXml(), '->asText() returns a textual representation of the InputDefinition');
346     }
347
348     protected function initializeArguments()
349     {
350         $this->foo = new InputArgument('foo');
351         $this->bar = new InputArgument('bar');
352         $this->foo1 = new InputArgument('foo');
353         $this->foo2 = new InputArgument('foo2', InputArgument::REQUIRED);
354     }
355
356     protected function initializeOptions()
357     {
358         $this->foo = new InputOption('foo', 'f');
359         $this->bar = new InputOption('bar', 'b');
360         $this->foo1 = new InputOption('fooBis', 'f');
361         $this->foo2 = new InputOption('foo', 'p');
362     }
363 }