InputOptionTest.php 6.8 KB

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