InputOptionTest.php 8.0 KB

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