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