InputOptionTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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', ['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. * @expectedException \InvalidArgumentException
  66. * @expectedExceptionMessage Option mode "-1" is not valid.
  67. */
  68. public function testInvalidModes()
  69. {
  70. new InputOption('foo', 'f', '-1');
  71. }
  72. /**
  73. * @expectedException \InvalidArgumentException
  74. */
  75. public function testEmptyNameIsInvalid()
  76. {
  77. new InputOption('');
  78. }
  79. /**
  80. * @expectedException \InvalidArgumentException
  81. */
  82. public function testDoubleDashNameIsInvalid()
  83. {
  84. new InputOption('--');
  85. }
  86. /**
  87. * @expectedException \InvalidArgumentException
  88. */
  89. public function testSingleDashOptionIsInvalid()
  90. {
  91. new InputOption('foo', '-');
  92. }
  93. public function testIsArray()
  94. {
  95. $option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);
  96. $this->assertTrue($option->isArray(), '->isArray() returns true if the option can be an array');
  97. $option = new InputOption('foo', null, InputOption::VALUE_NONE);
  98. $this->assertFalse($option->isArray(), '->isArray() returns false if the option can not be an array');
  99. }
  100. public function testGetDescription()
  101. {
  102. $option = new InputOption('foo', 'f', null, 'Some description');
  103. $this->assertEquals('Some description', $option->getDescription(), '->getDescription() returns the description message');
  104. }
  105. public function testGetDefault()
  106. {
  107. $option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', 'default');
  108. $this->assertEquals('default', $option->getDefault(), '->getDefault() returns the default value');
  109. $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED, '', 'default');
  110. $this->assertEquals('default', $option->getDefault(), '->getDefault() returns the default value');
  111. $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED);
  112. $this->assertNull($option->getDefault(), '->getDefault() returns null if no default value is configured');
  113. $option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);
  114. $this->assertEquals([], $option->getDefault(), '->getDefault() returns an empty array if option is an array');
  115. $option = new InputOption('foo', null, InputOption::VALUE_NONE);
  116. $this->assertFalse($option->getDefault(), '->getDefault() returns false if the option does not take a value');
  117. }
  118. public function testSetDefault()
  119. {
  120. $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED, '', 'default');
  121. $option->setDefault(null);
  122. $this->assertNull($option->getDefault(), '->setDefault() can reset the default value by passing null');
  123. $option->setDefault('another');
  124. $this->assertEquals('another', $option->getDefault(), '->setDefault() changes the default value');
  125. $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY);
  126. $option->setDefault([1, 2]);
  127. $this->assertEquals([1, 2], $option->getDefault(), '->setDefault() changes the default value');
  128. }
  129. /**
  130. * @expectedException \LogicException
  131. * @expectedExceptionMessage Cannot set a default value when using InputOption::VALUE_NONE mode.
  132. */
  133. public function testDefaultValueWithValueNoneMode()
  134. {
  135. $option = new InputOption('foo', 'f', InputOption::VALUE_NONE);
  136. $option->setDefault('default');
  137. }
  138. /**
  139. * @expectedException \LogicException
  140. * @expectedExceptionMessage A default value for an array option must be an array.
  141. */
  142. public function testDefaultValueWithIsArrayMode()
  143. {
  144. $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);
  145. $option->setDefault('default');
  146. }
  147. public function testEquals()
  148. {
  149. $option = new InputOption('foo', 'f', null, 'Some description');
  150. $option2 = new InputOption('foo', 'f', null, 'Alternative description');
  151. $this->assertTrue($option->equals($option2));
  152. $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description');
  153. $option2 = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description', true);
  154. $this->assertFalse($option->equals($option2));
  155. $option = new InputOption('foo', 'f', null, 'Some description');
  156. $option2 = new InputOption('bar', 'f', null, 'Some description');
  157. $this->assertFalse($option->equals($option2));
  158. $option = new InputOption('foo', 'f', null, 'Some description');
  159. $option2 = new InputOption('foo', '', null, 'Some description');
  160. $this->assertFalse($option->equals($option2));
  161. $option = new InputOption('foo', 'f', null, 'Some description');
  162. $option2 = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description');
  163. $this->assertFalse($option->equals($option2));
  164. }
  165. }