6b443e0b2abae0a8221ebbd1c8321561a2e8ecef
[yaffs-website] / vendor / symfony / console / Tests / Input / ArrayInputTest.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\ArrayInput;
16 use Symfony\Component\Console\Input\InputDefinition;
17 use Symfony\Component\Console\Input\InputArgument;
18 use Symfony\Component\Console\Input\InputOption;
19
20 class ArrayInputTest extends TestCase
21 {
22     public function testGetFirstArgument()
23     {
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');
30     }
31
32     public function testHasParameterOption()
33     {
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');
37
38         $input = new ArrayInput(array('--foo'));
39         $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters');
40
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');
44     }
45
46     public function testGetParameterOption()
47     {
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');
51
52         $input = new ArrayInput(array('Fabien', '--foo' => 'bar'));
53         $this->assertEquals('bar', $input->getParameterOption('--foo'), '->getParameterOption() returns the option of specified name');
54
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');
58     }
59
60     public function testParseArguments()
61     {
62         $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
63
64         $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() parses required arguments');
65     }
66
67     /**
68      * @dataProvider provideOptions
69      */
70     public function testParseOptions($input, $options, $expectedOptions, $message)
71     {
72         $input = new ArrayInput($input, new InputDefinition($options));
73
74         $this->assertEquals($expectedOptions, $input->getOptions(), $message);
75     }
76
77     public function provideOptions()
78     {
79         return array(
80             array(
81                 array('--foo' => 'bar'),
82                 array(new InputOption('foo')),
83                 array('foo' => 'bar'),
84                 '->parse() parses long options',
85             ),
86             array(
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',
91             ),
92             array(
93                 array(),
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',
97             ),
98             array(
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',
103             ),
104             array(
105                 array('-f' => 'bar'),
106                 array(new InputOption('foo', 'f')),
107                 array('foo' => 'bar'),
108                 '->parse() parses short options',
109             ),
110             array(
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',
115             ),
116             array(
117                 array('--' => null),
118                 array(),
119                 array(),
120                 '->parse() does not choke on end of options signal',
121             ),
122         );
123     }
124
125     /**
126      * @dataProvider provideInvalidInput
127      */
128     public function testParseInvalidInput($parameters, $definition, $expectedExceptionMessage)
129     {
130         if (method_exists($this, 'expectException')) {
131             $this->expectException('InvalidArgumentException');
132             $this->expectExceptionMessage($expectedExceptionMessage);
133         } else {
134             $this->setExpectedException('InvalidArgumentException', $expectedExceptionMessage);
135         }
136
137         new ArrayInput($parameters, $definition);
138     }
139
140     public function provideInvalidInput()
141     {
142         return array(
143             array(
144                 array('foo' => 'foo'),
145                 new InputDefinition(array(new InputArgument('name'))),
146                 'The "foo" argument does not exist.',
147             ),
148             array(
149                 array('--foo' => null),
150                 new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))),
151                 'The "--foo" option requires a value.',
152             ),
153             array(
154                 array('--foo' => 'foo'),
155                 new InputDefinition(),
156                 'The "--foo" option does not exist.',
157             ),
158             array(
159                 array('-o' => 'foo'),
160                 new InputDefinition(),
161                 'The "-o" option does not exist.',
162             ),
163         );
164     }
165
166     public function testToString()
167     {
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);
170
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);
173
174         $input = new ArrayInput(array('array_arg' => array('val_1', 'val_2')));
175         $this->assertSame('val_1 val_2', (string) $input);
176     }
177 }