InputOptionTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. public function testArrayModeWithoutValue()
  23. {
  24. $this->expectException('InvalidArgumentException');
  25. $this->expectExceptionMessage('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.');
  26. new InputOption('foo', 'f', InputOption::VALUE_IS_ARRAY);
  27. }
  28. public function testShortcut()
  29. {
  30. $option = new InputOption('foo', 'f');
  31. $this->assertEquals('f', $option->getShortcut(), '__construct() can take a shortcut as its second argument');
  32. $option = new InputOption('foo', '-f|-ff|fff');
  33. $this->assertEquals('f|ff|fff', $option->getShortcut(), '__construct() removes the leading - of the shortcuts');
  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');
  37. $this->assertNull($option->getShortcut(), '__construct() makes the shortcut null by default');
  38. }
  39. public function testModes()
  40. {
  41. $option = new InputOption('foo', 'f');
  42. $this->assertFalse($option->acceptValue(), '__construct() gives a "InputOption::VALUE_NONE" mode by default');
  43. $this->assertFalse($option->isValueRequired(), '__construct() gives a "InputOption::VALUE_NONE" mode by default');
  44. $this->assertFalse($option->isValueOptional(), '__construct() gives a "InputOption::VALUE_NONE" mode by default');
  45. $option = new InputOption('foo', 'f', null);
  46. $this->assertFalse($option->acceptValue(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
  47. $this->assertFalse($option->isValueRequired(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
  48. $this->assertFalse($option->isValueOptional(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
  49. $option = new InputOption('foo', 'f', InputOption::VALUE_NONE);
  50. $this->assertFalse($option->acceptValue(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
  51. $this->assertFalse($option->isValueRequired(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
  52. $this->assertFalse($option->isValueOptional(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
  53. $option = new InputOption('foo', 'f', InputOption::VALUE_REQUIRED);
  54. $this->assertTrue($option->acceptValue(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');
  55. $this->assertTrue($option->isValueRequired(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');
  56. $this->assertFalse($option->isValueOptional(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');
  57. $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL);
  58. $this->assertTrue($option->acceptValue(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
  59. $this->assertFalse($option->isValueRequired(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
  60. $this->assertTrue($option->isValueOptional(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
  61. }
  62. /**
  63. * @dataProvider provideInvalidModes
  64. */
  65. public function testInvalidModes($mode)
  66. {
  67. $this->expectException('InvalidArgumentException');
  68. $this->expectExceptionMessage(sprintf('Option mode "%s" is not valid.', $mode));
  69. new InputOption('foo', 'f', $mode);
  70. }
  71. public function provideInvalidModes()
  72. {
  73. return [
  74. ['ANOTHER_ONE'],
  75. [-1],
  76. ];
  77. }
  78. public function testEmptyNameIsInvalid()
  79. {
  80. $this->expectException('InvalidArgumentException');
  81. new InputOption('');
  82. }
  83. public function testDoubleDashNameIsInvalid()
  84. {
  85. $this->expectException('InvalidArgumentException');
  86. new InputOption('--');
  87. }
  88. public function testSingleDashOptionIsInvalid()
  89. {
  90. $this->expectException('InvalidArgumentException');
  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. public function testDefaultValueWithValueNoneMode()
  130. {
  131. $this->expectException('LogicException');
  132. $this->expectExceptionMessage('Cannot set a default value when using InputOption::VALUE_NONE mode.');
  133. $option = new InputOption('foo', 'f', InputOption::VALUE_NONE);
  134. $option->setDefault('default');
  135. }
  136. public function testDefaultValueWithIsArrayMode()
  137. {
  138. $this->expectException('LogicException');
  139. $this->expectExceptionMessage('A default value for an array option must be an array.');
  140. $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);
  141. $option->setDefault('default');
  142. }
  143. public function testEquals()
  144. {
  145. $option = new InputOption('foo', 'f', null, 'Some description');
  146. $option2 = new InputOption('foo', 'f', null, 'Alternative description');
  147. $this->assertTrue($option->equals($option2));
  148. $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description');
  149. $option2 = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description', true);
  150. $this->assertFalse($option->equals($option2));
  151. $option = new InputOption('foo', 'f', null, 'Some description');
  152. $option2 = new InputOption('bar', 'f', null, 'Some description');
  153. $this->assertFalse($option->equals($option2));
  154. $option = new InputOption('foo', 'f', null, 'Some description');
  155. $option2 = new InputOption('foo', '', null, 'Some description');
  156. $this->assertFalse($option->equals($option2));
  157. $option = new InputOption('foo', 'f', null, 'Some description');
  158. $option2 = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL, 'Some description');
  159. $this->assertFalse($option->equals($option2));
  160. }
  161. }