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\ArrayInput;
16 use Symfony\Component\Console\Input\InputDefinition;
17 use Symfony\Component\Console\Input\InputArgument;
18 use Symfony\Component\Console\Input\InputOption;
20 class ArrayInputTest extends TestCase
22 public function testGetFirstArgument()
24 $input = new ArrayInput(array());
25 $this->assertNull($input->getFirstArgument(), '->getFirstArgument() returns null if no argument were passed');
26 $input = new ArrayInput(array('name' => 'Fabien'));
27 $this->assertEquals('Fabien', $input->getFirstArgument(), '->getFirstArgument() returns the first passed argument');
28 $input = new ArrayInput(array('--foo' => 'bar', 'name' => 'Fabien'));
29 $this->assertEquals('Fabien', $input->getFirstArgument(), '->getFirstArgument() returns the first passed argument');
32 public function testHasParameterOption()
34 $input = new ArrayInput(array('name' => 'Fabien', '--foo' => 'bar'));
35 $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters');
36 $this->assertFalse($input->hasParameterOption('--bar'), '->hasParameterOption() returns false if an option is not present in the passed parameters');
38 $input = new ArrayInput(array('--foo'));
39 $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters');
41 $input = new ArrayInput(array('--foo', '--', '--bar'));
42 $this->assertTrue($input->hasParameterOption('--bar'), '->hasParameterOption() returns true if an option is present in the passed parameters');
43 $this->assertFalse($input->hasParameterOption('--bar', true), '->hasParameterOption() returns false if an option is present in the passed parameters after an end of options signal');
46 public function testGetParameterOption()
48 $input = new ArrayInput(array('name' => 'Fabien', '--foo' => 'bar'));
49 $this->assertEquals('bar', $input->getParameterOption('--foo'), '->getParameterOption() returns the option of specified name');
50 $this->assertFalse($input->getParameterOption('--bar'), '->getParameterOption() returns the default if an option is not present in the passed parameters');
52 $input = new ArrayInput(array('Fabien', '--foo' => 'bar'));
53 $this->assertEquals('bar', $input->getParameterOption('--foo'), '->getParameterOption() returns the option of specified name');
55 $input = new ArrayInput(array('--foo', '--', '--bar' => 'woop'));
56 $this->assertEquals('woop', $input->getParameterOption('--bar'), '->getParameterOption() returns the correct value if an option is present in the passed parameters');
57 $this->assertFalse($input->getParameterOption('--bar', false, true), '->getParameterOption() returns false if an option is present in the passed parameters after an end of options signal');
60 public function testParseArguments()
62 $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
64 $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() parses required arguments');
68 * @dataProvider provideOptions
70 public function testParseOptions($input, $options, $expectedOptions, $message)
72 $input = new ArrayInput($input, new InputDefinition($options));
74 $this->assertEquals($expectedOptions, $input->getOptions(), $message);
77 public function provideOptions()
81 array('--foo' => 'bar'),
82 array(new InputOption('foo')),
83 array('foo' => 'bar'),
84 '->parse() parses long options',
87 array('--foo' => 'bar'),
88 array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')),
89 array('foo' => 'bar'),
90 '->parse() parses long options with a default value',
94 array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')),
95 array('foo' => 'default'),
96 '->parse() uses the default value for long options with value optional which are not passed',
99 array('--foo' => null),
100 array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')),
101 array('foo' => null),
102 '->parse() parses long options with a default value',
105 array('-f' => 'bar'),
106 array(new InputOption('foo', 'f')),
107 array('foo' => 'bar'),
108 '->parse() parses short options',
111 array('--' => null, '-f' => 'bar'),
112 array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, '', 'default')),
113 array('foo' => 'default'),
114 '->parse() does not parse opts after an end of options signal',
120 '->parse() does not choke on end of options signal',
126 * @dataProvider provideInvalidInput
128 public function testParseInvalidInput($parameters, $definition, $expectedExceptionMessage)
130 if (method_exists($this, 'expectException')) {
131 $this->expectException('InvalidArgumentException');
132 $this->expectExceptionMessage($expectedExceptionMessage);
134 $this->setExpectedException('InvalidArgumentException', $expectedExceptionMessage);
137 new ArrayInput($parameters, $definition);
140 public function provideInvalidInput()
144 array('foo' => 'foo'),
145 new InputDefinition(array(new InputArgument('name'))),
146 'The "foo" argument does not exist.',
149 array('--foo' => null),
150 new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))),
151 'The "--foo" option requires a value.',
154 array('--foo' => 'foo'),
155 new InputDefinition(),
156 'The "--foo" option does not exist.',
159 array('-o' => 'foo'),
160 new InputDefinition(),
161 'The "-o" option does not exist.',
166 public function testToString()
168 $input = new ArrayInput(array('-f' => null, '-b' => 'bar', '--foo' => 'b a z', '--lala' => null, 'test' => 'Foo', 'test2' => "A\nB'C"));
169 $this->assertEquals('-f -b=bar --foo='.escapeshellarg('b a z').' --lala Foo '.escapeshellarg("A\nB'C"), (string) $input);
171 $input = new ArrayInput(array('-b' => array('bval_1', 'bval_2'), '--f' => array('fval_1', 'fval_2')));
172 $this->assertSame('-b=bval_1 -b=bval_2 --f=fval_1 --f=fval_2', (string) $input);
174 $input = new ArrayInput(array('array_arg' => array('val_1', 'val_2')));
175 $this->assertSame('val_1 val_2', (string) $input);