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 Symfony\Component\Console\Input\InputDefinition;
15 use Symfony\Component\Console\Input\InputArgument;
16 use Symfony\Component\Console\Input\InputOption;
18 class InputDefinitionTest extends \PHPUnit_Framework_TestCase
20 protected static $fixtures;
22 protected $foo, $bar, $foo1, $foo2;
24 public static function setUpBeforeClass()
26 self::$fixtures = __DIR__.'/../Fixtures/';
29 public function testConstructor()
31 $this->initializeArguments();
33 $definition = new InputDefinition();
34 $this->assertEquals(array(), $definition->getArguments(), '__construct() creates a new InputDefinition object');
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');
39 $this->initializeOptions();
41 $definition = new InputDefinition();
42 $this->assertEquals(array(), $definition->getOptions(), '__construct() creates a new InputDefinition object');
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');
48 public function testSetArguments()
50 $this->initializeArguments();
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));
57 $this->assertEquals(array('bar' => $this->bar), $definition->getArguments(), '->setArguments() clears all InputArgument objects');
60 public function testAddArguments()
62 $this->initializeArguments();
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');
71 public function testAddArgument()
73 $this->initializeArguments();
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');
81 // arguments must have different names
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());
90 // cannot add a parameter after an array parameter
91 $definition->addArgument(new InputArgument('fooarray', InputArgument::IS_ARRAY));
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());
100 // cannot add a required argument after an optional one
101 $definition = new InputDefinition();
102 $definition->addArgument($this->foo);
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());
112 public function testGetArgument()
114 $this->initializeArguments();
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');
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());
128 public function testHasArgument()
130 $this->initializeArguments();
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');
138 public function testGetArgumentRequiredCount()
140 $this->initializeArguments();
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');
149 public function testGetArgumentCount()
151 $this->initializeArguments();
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');
160 public function testGetArgumentDefaults()
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)),
168 $this->assertEquals(array('foo1' => null, 'foo2' => 'default', 'foo3' => array()), $definition->getArgumentDefaults(), '->getArgumentDefaults() return the default values for each argument');
170 $definition = new InputDefinition(array(
171 new InputArgument('foo4', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, '', array(1, 2)),
173 $this->assertEquals(array('foo4' => array(1, 2)), $definition->getArgumentDefaults(), '->getArgumentDefaults() return the default values for each argument');
176 public function testSetOptions()
178 $this->initializeOptions();
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');
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());
193 public function testAddOptions()
195 $this->initializeOptions();
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');
203 public function testAddOption()
205 $this->initializeOptions();
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');
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());
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());
228 public function testGetOption()
230 $this->initializeOptions();
232 $definition = new InputDefinition(array($this->foo));
233 $this->assertEquals($this->foo, $definition->getOption('foo'), '->getOption() returns a InputOption by its name');
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());
243 public function testHasOption()
245 $this->initializeOptions();
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');
252 public function testHasShortcut()
254 $this->initializeOptions();
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');
261 public function testGetOptionForShortcut()
263 $this->initializeOptions();
265 $definition = new InputDefinition(array($this->foo));
266 $this->assertEquals($this->foo, $definition->getOptionForShortcut('f'), '->getOptionForShortcut() returns a InputOption by its shortcut');
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());
276 public function testGetOptionDefaults()
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)),
294 'foo7' => array(1, 2),
296 $this->assertEquals($defaults, $definition->getOptionDefaults(), '->getOptionDefaults() returns the default values for all options');
299 public function testGetSynopsis()
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');
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');
320 public function testAsText()
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')),
332 $this->assertStringEqualsFile(self::$fixtures.'/definition_astext.txt', $definition->asText(), '->asText() returns a textual representation of the InputDefinition');
335 public function testAsXml()
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'),
345 $this->assertXmlStringEqualsXmlFile(self::$fixtures.'/definition_asxml.txt', $definition->asXml(), '->asText() returns a textual representation of the InputDefinition');
348 protected function initializeArguments()
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);
356 protected function initializeOptions()
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');