Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / symfony / console / Symfony / Component / Console / Tests / Input / InputOptionTest.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\InputOption;
15
16 class InputOptionTest extends \PHPUnit_Framework_TestCase
17 {
18     public function testConstructor()
19     {
20         $option = new InputOption('foo');
21         $this->assertEquals('foo', $option->getName(), '__construct() takes a name as its first argument');
22         $option = new InputOption('--foo');
23         $this->assertEquals('foo', $option->getName(), '__construct() removes the leading -- of the option name');
24
25         try {
26             $option = new InputOption('foo', 'f', InputOption::VALUE_IS_ARRAY);
27             $this->fail('->setDefault() throws an Exception if VALUE_IS_ARRAY option is used when an option does not accept a value');
28         } catch (\Exception $e) {
29             $this->assertInstanceOf('\Exception', $e, '->setDefault() throws an Exception if VALUE_IS_ARRAY option is used when an option does not accept a value');
30             $this->assertEquals('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.', $e->getMessage());
31         }
32
33         // shortcut argument
34         $option = new InputOption('foo', 'f');
35         $this->assertEquals('f', $option->getShortcut(), '__construct() can take a shortcut as its second argument');
36         $option = new InputOption('foo', '-f');
37         $this->assertEquals('f', $option->getShortcut(), '__construct() removes the leading - of the shortcut');
38         $option = new InputOption('foo');
39         $this->assertNull($option->getShortcut(), '__construct() makes the shortcut null by default');
40
41         // mode argument
42         $option = new InputOption('foo', 'f');
43         $this->assertFalse($option->acceptValue(), '__construct() gives a "InputOption::VALUE_NONE" mode by default');
44         $this->assertFalse($option->isValueRequired(), '__construct() gives a "InputOption::VALUE_NONE" mode by default');
45         $this->assertFalse($option->isValueOptional(), '__construct() gives a "InputOption::VALUE_NONE" mode by default');
46
47         $option = new InputOption('foo', 'f', null);
48         $this->assertFalse($option->acceptValue(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
49         $this->assertFalse($option->isValueRequired(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
50         $this->assertFalse($option->isValueOptional(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
51
52         $option = new InputOption('foo', 'f', InputOption::VALUE_NONE);
53         $this->assertFalse($option->acceptValue(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
54         $this->assertFalse($option->isValueRequired(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
55         $this->assertFalse($option->isValueOptional(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
56
57         $option = new InputOption('foo', 'f', InputOption::VALUE_REQUIRED);
58         $this->assertTrue($option->acceptValue(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');
59         $this->assertTrue($option->isValueRequired(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');
60         $this->assertFalse($option->isValueOptional(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');
61
62         $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL);
63         $this->assertTrue($option->acceptValue(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
64         $this->assertFalse($option->isValueRequired(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
65         $this->assertTrue($option->isValueOptional(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
66
67         try {
68             $option = new InputOption('foo', 'f', 'ANOTHER_ONE');
69             $this->fail('__construct() throws an Exception if the mode is not valid');
70         } catch (\Exception $e) {
71             $this->assertInstanceOf('\Exception', $e, '__construct() throws an Exception if the mode is not valid');
72             $this->assertEquals('Option mode "ANOTHER_ONE" is not valid.', $e->getMessage());
73         }
74         try {
75             $option = new InputOption('foo', 'f', -1);
76             $this->fail('__construct() throws an Exception if the mode is not valid');
77         } catch (\Exception $e) {
78             $this->assertInstanceOf('\Exception', $e, '__construct() throws an Exception if the mode is not valid');
79             $this->assertEquals('Option mode "-1" is not valid.', $e->getMessage());
80         }
81     }
82
83     /**
84      * @expectedException \InvalidArgumentException
85      */
86     public function testEmptyNameIsInvalid()
87     {
88         new InputOption('');
89     }
90
91     /**
92      * @expectedException \InvalidArgumentException
93      */
94     public function testDoubleDashNameIsInvalid()
95     {
96         new InputOption('--');
97     }
98
99     /**
100      * @expectedException \InvalidArgumentException
101      */
102     public function testSingleDashOptionIsInvalid()
103     {
104         new InputOption('foo', '-');
105     }
106
107     public function testIsArray()
108     {
109         $option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);
110         $this->assertTrue($option->isArray(), '->isArray() returns true if the option can be an array');
111         $option = new InputOption('foo', null, InputOption::VALUE_NONE);
112         $this->assertFalse($option->isArray(), '->isArray() returns false if the option can not be an array');
113     }
114
115     public function testGetDescription()
116     {
117         $option = new InputOption('foo', 'f', null, 'Some description');
118         $this->assertEquals('Some description', $option->getDescription(), '->getDescription() returns the description message');
119     }
120
121     public function testGetDefault()
122     {
123         $option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', 'default');
124         $this->assertEquals('default', $option->getDefault(), '->getDefault() returns the default value');
125
126         $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED, '', 'default');
127         $this->assertEquals('default', $option->getDefault(), '->getDefault() returns the default value');
128
129         $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED);
130         $this->assertNull($option->getDefault(), '->getDefault() returns null if no default value is configured');
131
132         $option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);
133         $this->assertEquals(array(), $option->getDefault(), '->getDefault() returns an empty array if option is an array');
134
135         $option = new InputOption('foo', null, InputOption::VALUE_NONE);
136         $this->assertFalse($option->getDefault(), '->getDefault() returns false if the option does not take a value');
137     }
138
139     public function testSetDefault()
140     {
141         $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED, '', 'default');
142         $option->setDefault(null);
143         $this->assertNull($option->getDefault(), '->setDefault() can reset the default value by passing null');
144         $option->setDefault('another');
145         $this->assertEquals('another', $option->getDefault(), '->setDefault() changes the default value');
146
147         $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY);
148         $option->setDefault(array(1, 2));
149         $this->assertEquals(array(1, 2), $option->getDefault(), '->setDefault() changes the default value');
150
151         $option = new InputOption('foo', 'f', InputOption::VALUE_NONE);
152         try {
153             $option->setDefault('default');
154             $this->fail('->setDefault() throws an Exception if you give a default value for a VALUE_NONE option');
155         } catch (\Exception $e) {
156             $this->assertInstanceOf('\Exception', $e, '->setDefault() throws an Exception if you give a default value for a VALUE_NONE option');
157             $this->assertEquals('Cannot set a default value when using Option::VALUE_NONE mode.', $e->getMessage());
158         }
159
160         $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);
161         try {
162             $option->setDefault('default');
163             $this->fail('->setDefault() throws an Exception if you give a default value which is not an array for a VALUE_IS_ARRAY option');
164         } catch (\Exception $e) {
165             $this->assertInstanceOf('\Exception', $e, '->setDefault() throws an Exception if you give a default value which is not an array for a VALUE_IS_ARRAY option');
166             $this->assertEquals('A default value for an array option must be an array.', $e->getMessage());
167         }
168     }
169
170     public function testEquals()
171     {
172         $option = new InputOption('foo', 'f', null, 'Some description');
173         $option2 = new InputOption('foo', 'f', null, 'Alternative description');
174         $this->assertTrue($option->equals($option2));
175
176         $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description');
177         $option2 = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description', true);
178         $this->assertFalse($option->equals($option2));
179
180         $option = new InputOption('foo', 'f', null, 'Some description');
181         $option2 = new InputOption('bar', 'f', null, 'Some description');
182         $this->assertFalse($option->equals($option2));
183
184         $option = new InputOption('foo', 'f', null, 'Some description');
185         $option2 = new InputOption('foo', '', null, 'Some description');
186         $this->assertFalse($option->equals($option2));
187
188         $option = new InputOption('foo', 'f', null, 'Some description');
189         $option2 = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description');
190         $this->assertFalse($option->equals($option2));
191     }
192 }