InputOptionTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Tests\Component\Console\Input;
  11. use Symfony\Component\Console\Input\InputOption;
  12. use Symfony\Component\Console\Exception;
  13. class InputOptionTest extends \PHPUnit_Framework_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. try {
  22. $option = new InputOption('foo', 'f', InputOption::VALUE_IS_ARRAY);
  23. $this->fail('->setDefault() throws an Exception if VALUE_IS_ARRAY option is used when an option does not accept a value');
  24. } catch (\Exception $e) {
  25. $this->assertInstanceOf('\Exception', $e, '->setDefault() throws an Exception if VALUE_IS_ARRAY option is used when an option does not accept a value');
  26. $this->assertEquals('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.', $e->getMessage());
  27. }
  28. // shortcut argument
  29. $option = new InputOption('foo', 'f');
  30. $this->assertEquals('f', $option->getShortcut(), '__construct() can take a shortcut as its second argument');
  31. $option = new InputOption('foo', '-f');
  32. $this->assertEquals('f', $option->getShortcut(), '__construct() removes the leading - of the shortcut');
  33. // mode argument
  34. $option = new InputOption('foo', 'f');
  35. $this->assertFalse($option->acceptValue(), '__construct() gives a "InputOption::VALUE_NONE" mode by default');
  36. $this->assertFalse($option->isValueRequired(), '__construct() gives a "InputOption::VALUE_NONE" mode by default');
  37. $this->assertFalse($option->isValueOptional(), '__construct() gives a "InputOption::VALUE_NONE" mode by default');
  38. $option = new InputOption('foo', 'f', null);
  39. $this->assertFalse($option->acceptValue(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
  40. $this->assertFalse($option->isValueRequired(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
  41. $this->assertFalse($option->isValueOptional(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
  42. $option = new InputOption('foo', 'f', InputOption::VALUE_NONE);
  43. $this->assertFalse($option->acceptValue(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
  44. $this->assertFalse($option->isValueRequired(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
  45. $this->assertFalse($option->isValueOptional(), '__construct() can take "InputOption::VALUE_NONE" as its mode');
  46. $option = new InputOption('foo', 'f', InputOption::VALUE_REQUIRED);
  47. $this->assertTrue($option->acceptValue(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');
  48. $this->assertTrue($option->isValueRequired(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');
  49. $this->assertFalse($option->isValueOptional(), '__construct() can take "InputOption::VALUE_REQUIRED" as its mode');
  50. $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL);
  51. $this->assertTrue($option->acceptValue(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
  52. $this->assertFalse($option->isValueRequired(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
  53. $this->assertTrue($option->isValueOptional(), '__construct() can take "InputOption::VALUE_OPTIONAL" as its mode');
  54. try {
  55. $option = new InputOption('foo', 'f', 'ANOTHER_ONE');
  56. $this->fail('__construct() throws an Exception if the mode is not valid');
  57. } catch (\Exception $e) {
  58. $this->assertInstanceOf('\Exception', $e, '__construct() throws an Exception if the mode is not valid');
  59. $this->assertEquals('Option mode "ANOTHER_ONE" is not valid.', $e->getMessage());
  60. }
  61. }
  62. public function testIsArray()
  63. {
  64. $option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);
  65. $this->assertTrue($option->isArray(), '->isArray() returns true if the option can be an array');
  66. $option = new InputOption('foo', null, InputOption::VALUE_NONE);
  67. $this->assertFalse($option->isArray(), '->isArray() returns false if the option can not be an array');
  68. }
  69. public function testGetDescription()
  70. {
  71. $option = new InputOption('foo', 'f', null, 'Some description');
  72. $this->assertEquals('Some description', $option->getDescription(), '->getDescription() returns the description message');
  73. }
  74. public function testGetDefault()
  75. {
  76. $option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', 'default');
  77. $this->assertEquals('default', $option->getDefault(), '->getDefault() returns the default value');
  78. $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED, '', 'default');
  79. $this->assertEquals('default', $option->getDefault(), '->getDefault() returns the default value');
  80. $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED);
  81. $this->assertNull($option->getDefault(), '->getDefault() returns null if no default value is configured');
  82. $option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);
  83. $this->assertEquals(array(), $option->getDefault(), '->getDefault() returns an empty array if option is an array');
  84. $option = new InputOption('foo', null, InputOption::VALUE_NONE);
  85. $this->assertFalse($option->getDefault(), '->getDefault() returns false if the option does not take a value');
  86. }
  87. public function testSetDefault()
  88. {
  89. $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED, '', 'default');
  90. $option->setDefault(null);
  91. $this->assertNull($option->getDefault(), '->setDefault() can reset the default value by passing null');
  92. $option->setDefault('another');
  93. $this->assertEquals('another', $option->getDefault(), '->setDefault() changes the default value');
  94. $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY);
  95. $option->setDefault(array(1, 2));
  96. $this->assertEquals(array(1, 2), $option->getDefault(), '->setDefault() changes the default value');
  97. $option = new InputOption('foo', 'f', InputOption::VALUE_NONE);
  98. try {
  99. $option->setDefault('default');
  100. $this->fail('->setDefault() throws an Exception if you give a default value for a VALUE_NONE option');
  101. } catch (\Exception $e) {
  102. $this->assertInstanceOf('\Exception', $e, '->setDefault() throws an Exception if you give a default value for a VALUE_NONE option');
  103. $this->assertEquals('Cannot set a default value when using Option::VALUE_NONE mode.', $e->getMessage());
  104. }
  105. $option = new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY);
  106. try {
  107. $option->setDefault('default');
  108. $this->fail('->setDefault() throws an Exception if you give a default value which is not an array for a VALUE_IS_ARRAY option');
  109. } catch (\Exception $e) {
  110. $this->assertInstanceOf('\Exception', $e, '->setDefault() throws an Exception if you give a default value which is not an array for a VALUE_IS_ARRAY option');
  111. $this->assertEquals('A default value for an array option must be an array.', $e->getMessage());
  112. }
  113. }
  114. }