TYPO3  7.6
InputOptionTest.php
Go to the documentation of this file.
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 
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 
30  public function testArrayModeWithoutValue()
31  {
33  }
34 
35  public function testShortcut()
36  {
37  $option = new InputOption('foo', 'f');
38  $this->assertEquals('f', $option->getShortcut(), '__construct() can take a shortcut as its second argument');
39  $option = new InputOption('foo', '-f|-ff|fff');
40  $this->assertEquals('f|ff|fff', $option->getShortcut(), '__construct() removes the leading - of the shortcuts');
41  $option = new InputOption('foo', array('f', 'ff', '-fff'));
42  $this->assertEquals('f|ff|fff', $option->getShortcut(), '__construct() removes the leading - of the shortcuts');
43  $option = new InputOption('foo');
44  $this->assertNull($option->getShortcut(), '__construct() makes the shortcut null by default');
45  }
46 
47  public function testModes()
48  {
49  $option = new InputOption('foo', 'f');
50  $this->assertFalse($option->acceptValue(), '__construct() gives a "InputOption::VALUE_NONE" mode by default');
51  $this->assertFalse($option->isValueRequired(), '__construct() gives a "InputOption::VALUE_NONE" mode by default');
52  $this->assertFalse($option->isValueOptional(), '__construct() gives a "InputOption::VALUE_NONE" mode by default');
53 
54  $option = new InputOption('foo', 'f', null);
55  $this->assertFalse($option->acceptValue(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
56  $this->assertFalse($option->isValueRequired(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
57  $this->assertFalse($option->isValueOptional(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
58 
59  $option = new InputOption('foo', 'f', InputOption::VALUE_NONE);
60  $this->assertFalse($option->acceptValue(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
61  $this->assertFalse($option->isValueRequired(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
62  $this->assertFalse($option->isValueOptional(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
63 
64  $option = new InputOption('foo', 'f', InputOption::VALUE_REQUIRED);
65  $this->assertTrue($option->acceptValue(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');
66  $this->assertTrue($option->isValueRequired(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');
67  $this->assertFalse($option->isValueOptional(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');
68 
69  $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL);
70  $this->assertTrue($option->acceptValue(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
71  $this->assertFalse($option->isValueRequired(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
72  $this->assertTrue($option->isValueOptional(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
73  }
74 
78  public function testInvalidModes($mode)
79  {
80  $this->setExpectedException('InvalidArgumentException', sprintf('Option mode "%s" is not valid.', $mode));
81 
82  new InputOption('foo', 'f', $mode);
83  }
84 
85  public function provideInvalidModes()
86  {
87  return array(
88  array('ANOTHER_ONE'),
89  array(-1),
90  );
91  }
92 
96  public function testEmptyNameIsInvalid()
97  {
98  new InputOption('');
99  }
100 
104  public function testDoubleDashNameIsInvalid()
105  {
106  new InputOption('--');
107  }
108 
113  {
114  new InputOption('foo', '-');
115  }
116 
117  public function testIsArray()
118  {
120  $this->assertTrue($option->isArray(), '->isArray() returns true if the option can be an array');
121  $option = new InputOption('foo', null, InputOption::VALUE_NONE);
122  $this->assertFalse($option->isArray(), '->isArray() returns false if the option can not be an array');
123  }
124 
125  public function testGetDescription()
126  {
127  $option = new InputOption('foo', 'f', null, 'Some description');
128  $this->assertEquals('Some description', $option->getDescription(), '->getDescription() returns the description message');
129  }
130 
131  public function testGetDefault()
132  {
133  $option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', 'default');
134  $this->assertEquals('default', $option->getDefault(), '->getDefault() returns the default value');
135 
136  $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED, '', 'default');
137  $this->assertEquals('default', $option->getDefault(), '->getDefault() returns the default value');
138 
139  $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED);
140  $this->assertNull($option->getDefault(), '->getDefault() returns null if no default value is configured');
141 
143  $this->assertEquals(array(), $option->getDefault(), '->getDefault() returns an empty array if option is an array');
144 
145  $option = new InputOption('foo', null, InputOption::VALUE_NONE);
146  $this->assertFalse($option->getDefault(), '->getDefault() returns false if the option does not take a value');
147  }
148 
149  public function testSetDefault()
150  {
151  $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED, '', 'default');
152  $option->setDefault(null);
153  $this->assertNull($option->getDefault(), '->setDefault() can reset the default value by passing null');
154  $option->setDefault('another');
155  $this->assertEquals('another', $option->getDefault(), '->setDefault() changes the default value');
156 
158  $option->setDefault(array(1, 2));
159  $this->assertEquals(array(1, 2), $option->getDefault(), '->setDefault() changes the default value');
160  }
161 
167  {
168  $option = new InputOption('foo', 'f', InputOption::VALUE_NONE);
169  $option->setDefault('default');
170  }
171 
177  {
179  $option->setDefault('default');
180  }
181 
182  public function testEquals()
183  {
184  $option = new InputOption('foo', 'f', null, 'Some description');
185  $option2 = new InputOption('foo', 'f', null, 'Alternative description');
186  $this->assertTrue($option->equals($option2));
187 
188  $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description');
189  $option2 = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description', true);
190  $this->assertFalse($option->equals($option2));
191 
192  $option = new InputOption('foo', 'f', null, 'Some description');
193  $option2 = new InputOption('bar', 'f', null, 'Some description');
194  $this->assertFalse($option->equals($option2));
195 
196  $option = new InputOption('foo', 'f', null, 'Some description');
197  $option2 = new InputOption('foo', '', null, 'Some description');
198  $this->assertFalse($option->equals($option2));
199 
200  $option = new InputOption('foo', 'f', null, 'Some description');
201  $option2 = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description');
202  $this->assertFalse($option->equals($option2));
203  }
204 }