InputOptionTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. }
  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->assertEquals(false, $option->acceptParameter(), '__construct() gives a "Option::PARAMETER_NONE" mode by default');
  36. $this->assertEquals(false, $option->isParameterRequired(), '__construct() gives a "Option::PARAMETER_NONE" mode by default');
  37. $this->assertEquals(false, $option->isParameterOptional(), '__construct() gives a "Option::PARAMETER_NONE" mode by default');
  38. $option = new InputOption('foo', 'f', null);
  39. $this->assertEquals(false, $option->acceptParameter(), '__construct() can take "Option::PARAMETER_NONE" as its mode');
  40. $this->assertEquals(false, $option->isParameterRequired(), '__construct() can take "Option::PARAMETER_NONE" as its mode');
  41. $this->assertEquals(false, $option->isParameterOptional(), '__construct() can take "Option::PARAMETER_NONE" as its mode');
  42. $option = new InputOption('foo', 'f', InputOption::PARAMETER_NONE);
  43. $this->assertEquals(false, $option->acceptParameter(), '__construct() can take "Option::PARAMETER_NONE" as its mode');
  44. $this->assertEquals(false, $option->isParameterRequired(), '__construct() can take "Option::PARAMETER_NONE" as its mode');
  45. $this->assertEquals(false, $option->isParameterOptional(), '__construct() can take "Option::PARAMETER_NONE" as its mode');
  46. $option = new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED);
  47. $this->assertEquals(true, $option->acceptParameter(), '__construct() can take "Option::PARAMETER_REQUIRED" as its mode');
  48. $this->assertEquals(true, $option->isParameterRequired(), '__construct() can take "Option::PARAMETER_REQUIRED" as its mode');
  49. $this->assertEquals(false, $option->isParameterOptional(), '__construct() can take "Option::PARAMETER_REQUIRED" as its mode');
  50. $option = new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL);
  51. $this->assertEquals(true, $option->acceptParameter(), '__construct() can take "Option::PARAMETER_OPTIONAL" as its mode');
  52. $this->assertEquals(false, $option->isParameterRequired(), '__construct() can take "Option::PARAMETER_OPTIONAL" as its mode');
  53. $this->assertEquals(true, $option->isParameterOptional(), '__construct() can take "Option::PARAMETER_OPTIONAL" as its mode');
  54. try
  55. {
  56. $option = new InputOption('foo', 'f', 'ANOTHER_ONE');
  57. $this->fail('__construct() throws an Exception if the mode is not valid');
  58. }
  59. catch (\Exception $e)
  60. {
  61. }
  62. }
  63. public function testIsArray()
  64. {
  65. $option = new InputOption('foo', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY);
  66. $this->assertTrue($option->isArray(), '->isArray() returns true if the option can be an array');
  67. $option = new InputOption('foo', null, InputOption::PARAMETER_NONE);
  68. $this->assertTrue(!$option->isArray(), '->isArray() returns false if the option can not be an array');
  69. }
  70. public function testGetDescription()
  71. {
  72. $option = new InputOption('foo', 'f', null, 'Some description');
  73. $this->assertEquals('Some description', $option->getDescription(), '->getDescription() returns the description message');
  74. }
  75. public function testGetDefault()
  76. {
  77. $option = new InputOption('foo', null, InputOption::PARAMETER_OPTIONAL, '', 'default');
  78. $this->assertEquals('default', $option->getDefault(), '->getDefault() returns the default value');
  79. $option = new InputOption('foo', null, InputOption::PARAMETER_REQUIRED, '', 'default');
  80. $this->assertEquals('default', $option->getDefault(), '->getDefault() returns the default value');
  81. $option = new InputOption('foo', null, InputOption::PARAMETER_REQUIRED);
  82. $this->assertTrue(is_null($option->getDefault()), '->getDefault() returns null if no default value is configured');
  83. $option = new InputOption('foo', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY);
  84. $this->assertEquals(array(), $option->getDefault(), '->getDefault() returns an empty array if option is an array');
  85. $option = new InputOption('foo', null, InputOption::PARAMETER_NONE);
  86. $this->assertTrue($option->getDefault() === false, '->getDefault() returns false if the option does not take a parameter');
  87. }
  88. public function testSetDefault()
  89. {
  90. $option = new InputOption('foo', null, InputOption::PARAMETER_REQUIRED, '', 'default');
  91. $option->setDefault(null);
  92. $this->assertTrue(is_null($option->getDefault()), '->setDefault() can reset the default value by passing null');
  93. $option->setDefault('another');
  94. $this->assertEquals('another', $option->getDefault(), '->setDefault() changes the default value');
  95. $option = new InputOption('foo', null, InputOption::PARAMETER_REQUIRED | InputOption::PARAMETER_IS_ARRAY);
  96. $option->setDefault(array(1, 2));
  97. $this->assertEquals(array(1, 2), $option->getDefault(), '->setDefault() changes the default value');
  98. $option = new InputOption('foo', 'f', InputOption::PARAMETER_NONE);
  99. try
  100. {
  101. $option->setDefault('default');
  102. $this->fail('->setDefault() throws an Exception if you give a default value for a PARAMETER_NONE option');
  103. }
  104. catch (\Exception $e)
  105. {
  106. }
  107. $option = new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY);
  108. try
  109. {
  110. $option->setDefault('default');
  111. $this->fail('->setDefault() throws an Exception if you give a default value which is not an array for a PARAMETER_IS_ARRAY option');
  112. }
  113. catch (\Exception $e)
  114. {
  115. }
  116. }
  117. }