InputOptionTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\Input;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Input\InputOption;
  13. class InputOptionTest extends TestCase
  14. {
  15. public function testConstructor()
  16. {
  17. $option = new InputOption('foo');
  18. $this->assertEquals('foo', $option->getName(), '__construct() takes a name as its first argument');
  19. $option = new InputOption('--foo');
  20. $this->assertEquals('foo', $option->getName(), '__construct() removes the leading -- of the option name');
  21. }
  22. /**
  23. * @expectedException \InvalidArgumentException
  24. * @expectedExceptionMessage Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.
  25. */
  26. public function testArrayModeWithoutValue()
  27. {
  28. new InputOption('foo', 'f', InputOption::VALUE_IS_ARRAY);
  29. }
  30. public function testShortcut()
  31. {
  32. $option = new InputOption('foo', 'f');
  33. $this->assertEquals('f', $option->getShortcut(), '__construct() can take a shortcut as its second argument');
  34. $option = new InputOption('foo', '-f|-ff|fff');
  35. $this->assertEquals('f|ff|fff', $option->getShortcut(), '__construct() removes the leading - of the shortcuts');
  36. $option = new InputOption('foo', array('f', 'ff', '-fff'));
  37. $this->assertEquals('f|ff|fff', $option->getShortcut(), '__construct() removes the leading - of the shortcuts');
  38. $option = new InputOption('foo');
  39. $this->assertNull($option->getShortcut(), '__construct() makes the shortcut null by default');
  40. }
  41. public function testModes()
  42. {
  43. $option = new InputOption('foo', 'f');
  44. $this->assertFalse($option->acceptValue(), '__construct() gives a "InputOption::VALUE_NONE" mode by default');
  45. $this->assertFalse($option->isValueRequired(), '__construct() gives a "InputOption::VALUE_NONE" mode by default');
  46. $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');
  51. $option = new InputOption('foo', 'f', InputOption::VALUE_NONE);
  52. $this->assertFalse($option->acceptValue(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
  53. $this->assertFalse($option->isValueRequired(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
  54. $this->assertFalse($option->isValueOptional(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
  55. $option = new InputOption('foo', 'f', InputOption::VALUE_REQUIRED);
  56. $this->assertTrue($option->acceptValue(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');
  57. $this->assertTrue($option->isValueRequired(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');
  58. $this->assertFalse($option->isValueOptional(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');
  59. $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL);
  60. $this->assertTrue($option->acceptValue(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
  61. $this->assertFalse($option->isValueRequired(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
  62. $this->assertTrue($option->isValueOptional(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
  63. }
  64. /**
  65. * @dataProvider provideInvalidModes
  66. */
  67. public function testInvalidModes($mode)
  68. {
  69. if (method_exists($this, 'expectException')) {
  70. $this->expectException('InvalidArgumentException');
  71. $this->expectExceptionMessage(sprintf('Option mode "%s" is not valid.', $mode));
  72. } else {
  73. $this->setExpectedException('InvalidArgumentException', sprintf('Option mode "%s" is not valid.', $mode));
  74. }
  75. new InputOption('foo', 'f', $mode);
  76. }
  77. public function provideInvalidModes()
  78. {
  79. return array(
  80. array('ANOTHER_ONE'),
  81. array(-1),
  82. );
  83. }
  84. /**
  85. * @expectedException \InvalidArgumentException
  86. */
  87. public function testEmptyNameIsInvalid()
  88. {
  89. new InputOption('');
  90. }
  91. /**
  92. * @expectedException \InvalidArgumentException
  93. */
  94. public function testDoubleDashNameIsInvalid()
  95. {
  96. new InputOption('--');
  97. }
  98. /**
  99. * @expectedException \InvalidArgumentException
  100. */
  101. public function testSingleDashOptionIsInvalid()
  102. {
  103. new InputOption('foo', '-');
  104. }
  105. public function testIsArray()
  106. {
  107. $option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);
  108. $this->assertTrue($option->isArray(), '->isArray() returns true if the option can be an array');
  109. $option = new InputOption('foo', null, InputOption::VALUE_NONE);
  110. $this->assertFalse($option->isArray(), '->isArray() returns false if the option can not be an array');
  111. }
  112. public function testGetDescription()
  113. {
  114. $option = new InputOption('foo', 'f', null, 'Some description');
  115. $this->assertEquals('Some description', $option->getDescription(), '->getDescription() returns the description message');
  116. }
  117. public function testGetDefault()
  118. {
  119. $option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', 'default');
  120. $this->assertEquals('default', $option->getDefault(), '->getDefault() returns the default value');
  121. $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED, '', 'default');
  122. $this->assertEquals('default', $option->getDefault(), '->getDefault() returns the default value');
  123. $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED);
  124. $this->assertNull($option->getDefault(), '->getDefault() returns null if no default value is configured');
  125. $option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);
  126. $this->assertEquals(array(), $option->getDefault(), '->getDefault() returns an empty array if option is an array');
  127. $option = new InputOption('foo', null, InputOption::VALUE_NONE);
  128. $this->assertFalse($option->getDefault(), '->getDefault() returns false if the option does not take a value');
  129. }
  130. public function testSetDefault()
  131. {
  132. $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED, '', 'default');
  133. $option->setDefault(null);
  134. $this->assertNull($option->getDefault(), '->setDefault() can reset the default value by passing null');
  135. $option->setDefault('another');
  136. $this->assertEquals('another', $option->getDefault(), '->setDefault() changes the default value');
  137. $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY);
  138. $option->setDefault(array(1, 2));
  139. $this->assertEquals(array(1, 2), $option->getDefault(), '->setDefault() changes the default value');
  140. }
  141. /**
  142. * @expectedException \LogicException
  143. * @expectedExceptionMessage Cannot set a default value when using InputOption::VALUE_NONE mode.
  144. */
  145. public function testDefaultValueWithValueNoneMode()
  146. {
  147. $option = new InputOption('foo', 'f', InputOption::VALUE_NONE);
  148. $option->setDefault('default');
  149. }
  150. /**
  151. * @expectedException \LogicException
  152. * @expectedExceptionMessage A default value for an array option must be an array.
  153. */
  154. public function testDefaultValueWithIsArrayMode()
  155. {
  156. $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);
  157. $option->setDefault('default');
  158. }
  159. public function testEquals()
  160. {
  161. $option = new InputOption('foo', 'f', null, 'Some description');
  162. $option2 = new InputOption('foo', 'f', null, 'Alternative description');
  163. $this->assertTrue($option->equals($option2));
  164. $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description');
  165. $option2 = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description', true);
  166. $this->assertFalse($option->equals($option2));
  167. $option = new InputOption('foo', 'f', null, 'Some description');
  168. $option2 = new InputOption('bar', 'f', null, 'Some description');
  169. $this->assertFalse($option->equals($option2));
  170. $option = new InputOption('foo', 'f', null, 'Some description');
  171. $option2 = new InputOption('foo', '', null, 'Some description');
  172. $this->assertFalse($option->equals($option2));
  173. $option = new InputOption('foo', 'f', null, 'Some description');
  174. $option2 = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description');
  175. $this->assertFalse($option->equals($option2));
  176. }
  177. }