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 PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Input\InputOption;
17 class InputOptionTest extends TestCase
19 public function testConstructor()
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');
28 * @expectedException \InvalidArgumentException
29 * @expectedExceptionMessage Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.
31 public function testArrayModeWithoutValue()
33 new InputOption('foo', 'f', InputOption::VALUE_IS_ARRAY);
36 public function testShortcut()
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');
48 public function testModes()
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');
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');
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');
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');
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');
77 * @dataProvider provideInvalidModes
79 public function testInvalidModes($mode)
81 if (method_exists($this, 'expectException')) {
82 $this->expectException('InvalidArgumentException');
83 $this->expectExceptionMessage(sprintf('Option mode "%s" is not valid.', $mode));
85 $this->setExpectedException('InvalidArgumentException', sprintf('Option mode "%s" is not valid.', $mode));
88 new InputOption('foo', 'f', $mode);
91 public function provideInvalidModes()
100 * @expectedException \InvalidArgumentException
102 public function testEmptyNameIsInvalid()
108 * @expectedException \InvalidArgumentException
110 public function testDoubleDashNameIsInvalid()
112 new InputOption('--');
116 * @expectedException \InvalidArgumentException
118 public function testSingleDashOptionIsInvalid()
120 new InputOption('foo', '-');
123 public function testIsArray()
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');
131 public function testGetDescription()
133 $option = new InputOption('foo', 'f', null, 'Some description');
134 $this->assertEquals('Some description', $option->getDescription(), '->getDescription() returns the description message');
137 public function testGetDefault()
139 $option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', 'default');
140 $this->assertEquals('default', $option->getDefault(), '->getDefault() returns the default value');
142 $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED, '', 'default');
143 $this->assertEquals('default', $option->getDefault(), '->getDefault() returns the default value');
145 $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED);
146 $this->assertNull($option->getDefault(), '->getDefault() returns null if no default value is configured');
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');
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');
155 public function testSetDefault()
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');
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');
169 * @expectedException \LogicException
170 * @expectedExceptionMessage Cannot set a default value when using InputOption::VALUE_NONE mode.
172 public function testDefaultValueWithValueNoneMode()
174 $option = new InputOption('foo', 'f', InputOption::VALUE_NONE);
175 $option->setDefault('default');
179 * @expectedException \LogicException
180 * @expectedExceptionMessage A default value for an array option must be an array.
182 public function testDefaultValueWithIsArrayMode()
184 $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);
185 $option->setDefault('default');
188 public function testEquals()
190 $option = new InputOption('foo', 'f', null, 'Some description');
191 $option2 = new InputOption('foo', 'f', null, 'Alternative description');
192 $this->assertTrue($option->equals($option2));
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));
198 $option = new InputOption('foo', 'f', null, 'Some description');
199 $option2 = new InputOption('bar', 'f', null, 'Some description');
200 $this->assertFalse($option->equals($option2));
202 $option = new InputOption('foo', 'f', null, 'Some description');
203 $option2 = new InputOption('foo', '', null, 'Some description');
204 $this->assertFalse($option->equals($option2));
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));