Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / symfony / console / Symfony / Component / Console / Tests / Input / ArgvInputTest.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\ArgvInput;
15 use Symfony\Component\Console\Input\InputDefinition;
16 use Symfony\Component\Console\Input\InputArgument;
17 use Symfony\Component\Console\Input\InputOption;
18
19 class ArgvInputTest extends \PHPUnit_Framework_TestCase
20 {
21     public function testConstructor()
22     {
23         $_SERVER['argv'] = array('cli.php', 'foo');
24         $input = new ArgvInput();
25         $r = new \ReflectionObject($input);
26         $p = $r->getProperty('tokens');
27         $p->setAccessible(true);
28
29         $this->assertEquals(array('foo'), $p->getValue($input), '__construct() automatically get its input from the argv server variable');
30     }
31
32     public function testParser()
33     {
34         $input = new ArgvInput(array('cli.php', 'foo'));
35         $input->bind(new InputDefinition(array(new InputArgument('name'))));
36         $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() parses required arguments');
37
38         $input->bind(new InputDefinition(array(new InputArgument('name'))));
39         $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() is stateless');
40
41         $input = new ArgvInput(array('cli.php', '--foo'));
42         $input->bind(new InputDefinition(array(new InputOption('foo'))));
43         $this->assertEquals(array('foo' => true), $input->getOptions(), '->parse() parses long options without a value');
44
45         $input = new ArgvInput(array('cli.php', '--foo=bar'));
46         $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
47         $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options with a required value (with a = separator)');
48
49         $input = new ArgvInput(array('cli.php', '--foo', 'bar'));
50         $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
51         $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options with a required value (with a space separator)');
52
53         try {
54             $input = new ArgvInput(array('cli.php', '--foo'));
55             $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
56             $this->fail('->parse() throws a \RuntimeException if no value is passed to an option when it is required');
57         } catch (\Exception $e) {
58             $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if no value is passed to an option when it is required');
59             $this->assertEquals('The "--foo" option requires a value.', $e->getMessage(), '->parse() throws a \RuntimeException if no value is passed to an option when it is required');
60         }
61
62         $input = new ArgvInput(array('cli.php', '-f'));
63         $input->bind(new InputDefinition(array(new InputOption('foo', 'f'))));
64         $this->assertEquals(array('foo' => true), $input->getOptions(), '->parse() parses short options without a value');
65
66         $input = new ArgvInput(array('cli.php', '-fbar'));
67         $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
68         $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses short options with a required value (with no separator)');
69
70         $input = new ArgvInput(array('cli.php', '-f', 'bar'));
71         $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
72         $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses short options with a required value (with a space separator)');
73
74         $input = new ArgvInput(array('cli.php', '-f', '-b', 'foo'));
75         $input->bind(new InputDefinition(array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b'))));
76         $this->assertEquals(array('foo' => null, 'bar' => true), $input->getOptions(), '->parse() parses short options with an optional value which is not present');
77
78         try {
79             $input = new ArgvInput(array('cli.php', '-f'));
80             $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
81             $this->fail('->parse() throws a \RuntimeException if no value is passed to an option when it is required');
82         } catch (\Exception $e) {
83             $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if no value is passed to an option when it is required');
84             $this->assertEquals('The "--foo" option requires a value.', $e->getMessage(), '->parse() throws a \RuntimeException if no value is passed to an option when it is required');
85         }
86
87         try {
88             $input = new ArgvInput(array('cli.php', '-ffoo'));
89             $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_NONE))));
90             $this->fail('->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
91         } catch (\Exception $e) {
92             $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
93             $this->assertEquals('The "-o" option does not exist.', $e->getMessage(), '->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
94         }
95
96         try {
97             $input = new ArgvInput(array('cli.php', 'foo', 'bar'));
98             $input->bind(new InputDefinition());
99             $this->fail('->parse() throws a \RuntimeException if too many arguments are passed');
100         } catch (\Exception $e) {
101             $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if too many arguments are passed');
102             $this->assertEquals('Too many arguments.', $e->getMessage(), '->parse() throws a \RuntimeException if too many arguments are passed');
103         }
104
105         try {
106             $input = new ArgvInput(array('cli.php', '--foo'));
107             $input->bind(new InputDefinition());
108             $this->fail('->parse() throws a \RuntimeException if an unknown long option is passed');
109         } catch (\Exception $e) {
110             $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if an unknown long option is passed');
111             $this->assertEquals('The "--foo" option does not exist.', $e->getMessage(), '->parse() throws a \RuntimeException if an unknown long option is passed');
112         }
113
114         try {
115             $input = new ArgvInput(array('cli.php', '-f'));
116             $input->bind(new InputDefinition());
117             $this->fail('->parse() throws a \RuntimeException if an unknown short option is passed');
118         } catch (\Exception $e) {
119             $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if an unknown short option is passed');
120             $this->assertEquals('The "-f" option does not exist.', $e->getMessage(), '->parse() throws a \RuntimeException if an unknown short option is passed');
121         }
122
123         $input = new ArgvInput(array('cli.php', '-fb'));
124         $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b'))));
125         $this->assertEquals(array('foo' => true, 'bar' => true), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one');
126
127         $input = new ArgvInput(array('cli.php', '-fb', 'bar'));
128         $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_REQUIRED))));
129         $this->assertEquals(array('foo' => true, 'bar' => 'bar'), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and the last one has a required value');
130
131         $input = new ArgvInput(array('cli.php', '-fb', 'bar'));
132         $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL))));
133         $this->assertEquals(array('foo' => true, 'bar' => 'bar'), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and the last one has an optional value');
134
135         $input = new ArgvInput(array('cli.php', '-fbbar'));
136         $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL))));
137         $this->assertEquals(array('foo' => true, 'bar' => 'bar'), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and the last one has an optional value with no separator');
138
139         $input = new ArgvInput(array('cli.php', '-fbbar'));
140         $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL))));
141         $this->assertEquals(array('foo' => 'bbar', 'bar' => null), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and one of them takes a value');
142
143         try {
144             $input = new ArgvInput(array('cli.php', 'foo', 'bar', 'baz', 'bat'));
145             $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::IS_ARRAY))));
146             $this->assertEquals(array('name' => array('foo', 'bar', 'baz', 'bat')), $input->getArguments(), '->parse() parses array arguments');
147         } catch (\RuntimeException $e) {
148             $this->assertNotEquals('Too many arguments.', $e->getMessage(), '->parse() parses array arguments');
149         }
150
151         $input = new ArgvInput(array('cli.php', '--name=foo', '--name=bar', '--name=baz'));
152         $input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
153         $this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions());
154
155         try {
156             $input = new ArgvInput(array('cli.php', '-1'));
157             $input->bind(new InputDefinition(array(new InputArgument('number'))));
158             $this->fail('->parse() throws a \RuntimeException if an unknown option is passed');
159         } catch (\Exception $e) {
160             $this->assertInstanceOf('\RuntimeException', $e, '->parse() parses arguments with leading dashes as options without having encountered a double-dash sequence');
161             $this->assertEquals('The "-1" option does not exist.', $e->getMessage(), '->parse() parses arguments with leading dashes as options without having encountered a double-dash sequence');
162         }
163
164         $input = new ArgvInput(array('cli.php', '--', '-1'));
165         $input->bind(new InputDefinition(array(new InputArgument('number'))));
166         $this->assertEquals(array('number' => '-1'), $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence');
167
168         $input = new ArgvInput(array('cli.php', '-f', 'bar', '--', '-1'));
169         $input->bind(new InputDefinition(array(new InputArgument('number'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
170         $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses arguments with leading dashes as options before having encountered a double-dash sequence');
171         $this->assertEquals(array('number' => '-1'), $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence');
172
173         $input = new ArgvInput(array('cli.php', '-f', 'bar', ''));
174         $input->bind(new InputDefinition(array(new InputArgument('empty'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
175         $this->assertEquals(array('empty' => ''), $input->getArguments(), '->parse() parses empty string arguments');
176     }
177
178     public function testGetFirstArgument()
179     {
180         $input = new ArgvInput(array('cli.php', '-fbbar'));
181         $this->assertEquals('', $input->getFirstArgument(), '->getFirstArgument() returns the first argument from the raw input');
182
183         $input = new ArgvInput(array('cli.php', '-fbbar', 'foo'));
184         $this->assertEquals('foo', $input->getFirstArgument(), '->getFirstArgument() returns the first argument from the raw input');
185     }
186
187     public function testHasParameterOption()
188     {
189         $input = new ArgvInput(array('cli.php', '-f', 'foo'));
190         $this->assertTrue($input->hasParameterOption('-f'), '->hasParameterOption() returns true if the given short option is in the raw input');
191
192         $input = new ArgvInput(array('cli.php', '--foo', 'foo'));
193         $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if the given short option is in the raw input');
194
195         $input = new ArgvInput(array('cli.php', 'foo'));
196         $this->assertFalse($input->hasParameterOption('--foo'), '->hasParameterOption() returns false if the given short option is not in the raw input');
197     }
198
199     /**
200      * @dataProvider provideGetParameterOptionValues
201      */
202     public function testGetParameterOptionEqualSign($argv, $key, $expected)
203     {
204         $input = new ArgvInput($argv);
205         $this->assertEquals($expected, $input->getParameterOption($key), '->getParameterOption() returns the expected value');
206     }
207
208     public function provideGetParameterOptionValues()
209     {
210         return array(
211             array(array('app/console', 'foo:bar', '-e', 'dev'), '-e', 'dev'),
212             array(array('app/console', 'foo:bar', '--env=dev'), '--env', 'dev'),
213             array(array('app/console', 'foo:bar', '-e', 'dev'), array('-e', '--env'), 'dev'),
214             array(array('app/console', 'foo:bar', '--env=dev'), array('-e', '--env'), 'dev'),
215         );
216     }
217 }