InputOptionTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Components\Console\Input;
  10. use Symfony\Components\Console\Input\InputOption;
  11. use Symfony\Components\Console\Exception;
  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. try
  21. {
  22. $option = new InputOption('foo', 'f', InputOption::PARAMETER_IS_ARRAY);
  23. $this->fail('->setDefault() throws an Exception if PARAMETER_IS_ARRAY option is used when an option does not accept a value');
  24. }
  25. catch (\Exception $e)
  26. {
  27. $this->assertType('\Exception', $e, '->setDefault() throws an Exception if PARAMETER_IS_ARRAY option is used when an option does not accept a value');
  28. $this->assertEquals('Impossible to have an option mode PARAMETER_IS_ARRAY if the option does not accept a parameter.', $e->getMessage());
  29. }
  30. // shortcut argument
  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');
  34. $this->assertEquals('f', $option->getShortcut(), '__construct() removes the leading - of the shortcut');
  35. // mode argument
  36. $option = new InputOption('foo', 'f');
  37. $this->assertFalse($option->acceptParameter(), '__construct() gives a "Option::PARAMETER_NONE" mode by default');
  38. $this->assertFalse($option->isParameterRequired(), '__construct() gives a "Option::PARAMETER_NONE" mode by default');
  39. $this->assertFalse($option->isParameterOptional(), '__construct() gives a "Option::PARAMETER_NONE" mode by default');
  40. $option = new InputOption('foo', 'f', null);
  41. $this->assertFalse($option->acceptParameter(), '__construct() can take "Option::PARAMETER_NONE" as its mode');
  42. $this->assertFalse($option->isParameterRequired(), '__construct() can take "Option::PARAMETER_NONE" as its mode');
  43. $this->assertFalse($option->isParameterOptional(), '__construct() can take "Option::PARAMETER_NONE" as its mode');
  44. $option = new InputOption('foo', 'f', InputOption::PARAMETER_NONE);
  45. $this->assertFalse($option->acceptParameter(), '__construct() can take "Option::PARAMETER_NONE" as its mode');
  46. $this->assertFalse($option->isParameterRequired(), '__construct() can take "Option::PARAMETER_NONE" as its mode');
  47. $this->assertFalse($option->isParameterOptional(), '__construct() can take "Option::PARAMETER_NONE" as its mode');
  48. $option = new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED);
  49. $this->assertTrue($option->acceptParameter(), '__construct() can take "Option::PARAMETER_REQUIRED" as its mode');
  50. $this->assertTrue($option->isParameterRequired(), '__construct() can take "Option::PARAMETER_REQUIRED" as its mode');
  51. $this->assertFalse($option->isParameterOptional(), '__construct() can take "Option::PARAMETER_REQUIRED" as its mode');
  52. $option = new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL);
  53. $this->assertTrue($option->acceptParameter(), '__construct() can take "Option::PARAMETER_OPTIONAL" as its mode');
  54. $this->assertFalse($option->isParameterRequired(), '__construct() can take "Option::PARAMETER_OPTIONAL" as its mode');
  55. $this->assertTrue($option->isParameterOptional(), '__construct() can take "Option::PARAMETER_OPTIONAL" as its mode');
  56. try
  57. {
  58. $option = new InputOption('foo', 'f', 'ANOTHER_ONE');
  59. $this->fail('__construct() throws an Exception if the mode is not valid');
  60. }
  61. catch (\Exception $e)
  62. {
  63. $this->assertType('\Exception', $e, '__construct() throws an Exception if the mode is not valid');
  64. $this->assertEquals('Option mode "ANOTHER_ONE" is not valid.', $e->getMessage());
  65. }
  66. }
  67. public function testIsArray()
  68. {
  69. $option = new InputOption('foo', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY);
  70. $this->assertTrue($option->isArray(), '->isArray() returns true if the option can be an array');
  71. $option = new InputOption('foo', null, InputOption::PARAMETER_NONE);
  72. $this->assertFalse($option->isArray(), '->isArray() returns false if the option can not be an array');
  73. }
  74. public function testGetDescription()
  75. {
  76. $option = new InputOption('foo', 'f', null, 'Some description');
  77. $this->assertEquals('Some description', $option->getDescription(), '->getDescription() returns the description message');
  78. }
  79. public function testGetDefault()
  80. {
  81. $option = new InputOption('foo', null, InputOption::PARAMETER_OPTIONAL, '', 'default');
  82. $this->assertEquals('default', $option->getDefault(), '->getDefault() returns the default value');
  83. $option = new InputOption('foo', null, InputOption::PARAMETER_REQUIRED, '', 'default');
  84. $this->assertEquals('default', $option->getDefault(), '->getDefault() returns the default value');
  85. $option = new InputOption('foo', null, InputOption::PARAMETER_REQUIRED);
  86. $this->assertNull($option->getDefault(), '->getDefault() returns null if no default value is configured');
  87. $option = new InputOption('foo', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY);
  88. $this->assertEquals(array(), $option->getDefault(), '->getDefault() returns an empty array if option is an array');
  89. $option = new InputOption('foo', null, InputOption::PARAMETER_NONE);
  90. $this->assertFalse($option->getDefault(), '->getDefault() returns false if the option does not take a parameter');
  91. }
  92. public function testSetDefault()
  93. {
  94. $option = new InputOption('foo', null, InputOption::PARAMETER_REQUIRED, '', 'default');
  95. $option->setDefault(null);
  96. $this->assertNull($option->getDefault(), '->setDefault() can reset the default value by passing null');
  97. $option->setDefault('another');
  98. $this->assertEquals('another', $option->getDefault(), '->setDefault() changes the default value');
  99. $option = new InputOption('foo', null, InputOption::PARAMETER_REQUIRED | InputOption::PARAMETER_IS_ARRAY);
  100. $option->setDefault(array(1, 2));
  101. $this->assertEquals(array(1, 2), $option->getDefault(), '->setDefault() changes the default value');
  102. $option = new InputOption('foo', 'f', InputOption::PARAMETER_NONE);
  103. try
  104. {
  105. $option->setDefault('default');
  106. $this->fail('->setDefault() throws an Exception if you give a default value for a PARAMETER_NONE option');
  107. }
  108. catch (\Exception $e)
  109. {
  110. $this->assertType('\Exception', $e, '->setDefault() throws an Exception if you give a default value for a PARAMETER_NONE option');
  111. $this->assertEquals('Cannot set a default value when using Option::PARAMETER_NONE mode.', $e->getMessage());
  112. }
  113. $option = new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY);
  114. try
  115. {
  116. $option->setDefault('default');
  117. $this->fail('->setDefault() throws an Exception if you give a default value which is not an array for a PARAMETER_IS_ARRAY option');
  118. }
  119. catch (\Exception $e)
  120. {
  121. $this->assertType('\Exception', $e, '->setDefault() throws an Exception if you give a default value which is not an array for a PARAMETER_IS_ARRAY option');
  122. $this->assertEquals('A default value for an array option must be an array.', $e->getMessage());
  123. }
  124. }
  125. }