InputOptionTest.php 6.4 KB

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