InputArgumentTest.php 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\InputArgument;
  11. use Symfony\Components\Console\Exception;
  12. $t = new LimeTest(16);
  13. // __construct()
  14. $t->diag('__construct()');
  15. $argument = new InputArgument('foo');
  16. $t->is($argument->getName(), 'foo', '__construct() takes a name as its first argument');
  17. // mode argument
  18. $argument = new InputArgument('foo');
  19. $t->is($argument->isRequired(), false, '__construct() gives a "Argument::OPTIONAL" mode by default');
  20. $argument = new InputArgument('foo', null);
  21. $t->is($argument->isRequired(), false, '__construct() can take "Argument::OPTIONAL" as its mode');
  22. $argument = new InputArgument('foo', InputArgument::OPTIONAL);
  23. $t->is($argument->isRequired(), false, '__construct() can take "Argument::PARAMETER_OPTIONAL" as its mode');
  24. $argument = new InputArgument('foo', InputArgument::REQUIRED);
  25. $t->is($argument->isRequired(), true, '__construct() can take "Argument::PARAMETER_REQUIRED" as its mode');
  26. try
  27. {
  28. $argument = new InputArgument('foo', 'ANOTHER_ONE');
  29. $t->fail('__construct() throws an Exception if the mode is not valid');
  30. }
  31. catch (\Exception $e)
  32. {
  33. $t->pass('__construct() throws an Exception if the mode is not valid');
  34. }
  35. // ->isArray()
  36. $t->diag('->isArray()');
  37. $argument = new InputArgument('foo', InputArgument::IS_ARRAY);
  38. $t->ok($argument->isArray(), '->isArray() returns true if the argument can be an array');
  39. $argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY);
  40. $t->ok($argument->isArray(), '->isArray() returns true if the argument can be an array');
  41. $argument = new InputArgument('foo', InputArgument::OPTIONAL);
  42. $t->ok(!$argument->isArray(), '->isArray() returns false if the argument can not be an array');
  43. // ->getDescription()
  44. $t->diag('->getDescription()');
  45. $argument = new InputArgument('foo', null, 'Some description');
  46. $t->is($argument->getDescription(), 'Some description', '->getDescription() return the message description');
  47. // ->getDefault()
  48. $t->diag('->getDefault()');
  49. $argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
  50. $t->is($argument->getDefault(), 'default', '->getDefault() return the default value');
  51. // ->setDefault()
  52. $t->diag('->setDefault()');
  53. $argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default');
  54. $argument->setDefault(null);
  55. $t->ok(is_null($argument->getDefault()), '->setDefault() can reset the default value by passing null');
  56. $argument->setDefault('another');
  57. $t->is($argument->getDefault(), 'another', '->setDefault() changes the default value');
  58. $argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY);
  59. $argument->setDefault(array(1, 2));
  60. $t->is($argument->getDefault(), array(1, 2), '->setDefault() changes the default value');
  61. try
  62. {
  63. $argument = new InputArgument('foo', InputArgument::REQUIRED);
  64. $argument->setDefault('default');
  65. $t->fail('->setDefault() throws an Exception if you give a default value for a required argument');
  66. }
  67. catch (\Exception $e)
  68. {
  69. $t->pass('->setDefault() throws an Exception if you give a default value for a required argument');
  70. }
  71. try
  72. {
  73. $argument = new InputArgument('foo', InputArgument::IS_ARRAY);
  74. $argument->setDefault('default');
  75. $t->fail('->setDefault() throws an Exception if you give a default value which is not an array for a IS_ARRAY option');
  76. }
  77. catch (\Exception $e)
  78. {
  79. $t->pass('->setDefault() throws an Exception if you give a default value which is not an array for a IS_ARRAY option');
  80. }