InputOptionTest.php 9.0 KB

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