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 Symfony\Component\Console\Input\InputOption;
16 class InputOptionTest extends \PHPUnit_Framework_TestCase
18 public function testConstructor()
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');
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());
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');
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');
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');
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');
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');
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');
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());
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());
84 * @expectedException \InvalidArgumentException
86 public function testEmptyNameIsInvalid()
92 * @expectedException \InvalidArgumentException
94 public function testDoubleDashNameIsInvalid()
96 new InputOption('--');
100 * @expectedException \InvalidArgumentException
102 public function testSingleDashOptionIsInvalid()
104 new InputOption('foo', '-');
107 public function testIsArray()
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');
115 public function testGetDescription()
117 $option = new InputOption('foo', 'f', null, 'Some description');
118 $this->assertEquals('Some description', $option->getDescription(), '->getDescription() returns the description message');
121 public function testGetDefault()
123 $option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', 'default');
124 $this->assertEquals('default', $option->getDefault(), '->getDefault() returns the default value');
126 $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED, '', 'default');
127 $this->assertEquals('default', $option->getDefault(), '->getDefault() returns the default value');
129 $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED);
130 $this->assertNull($option->getDefault(), '->getDefault() returns null if no default value is configured');
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');
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');
139 public function testSetDefault()
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');
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');
151 $option = new InputOption('foo', 'f', InputOption::VALUE_NONE);
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());
160 $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);
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());
170 public function testEquals()
172 $option = new InputOption('foo', 'f', null, 'Some description');
173 $option2 = new InputOption('foo', 'f', null, 'Alternative description');
174 $this->assertTrue($option->equals($option2));
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));
180 $option = new InputOption('foo', 'f', null, 'Some description');
181 $option2 = new InputOption('bar', 'f', null, 'Some description');
182 $this->assertFalse($option->equals($option2));
184 $option = new InputOption('foo', 'f', null, 'Some description');
185 $option2 = new InputOption('foo', '', null, 'Some description');
186 $this->assertFalse($option->equals($option2));
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));