ArrayInputTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\ArrayInput;
  11. use Symfony\Components\Console\Input\InputDefinition;
  12. use Symfony\Components\Console\Input\InputArgument;
  13. use Symfony\Components\Console\Input\InputOption;
  14. $t = new LimeTest(15);
  15. // ->getFirstArgument()
  16. $t->diag('->getFirstArgument()');
  17. $input = new ArrayInput(array());
  18. $t->is($input->getFirstArgument(), null, '->getFirstArgument() returns null if no argument were passed');
  19. $input = new ArrayInput(array('name' => 'Fabien'));
  20. $t->is($input->getFirstArgument(), 'Fabien', '->getFirstArgument() returns the first passed argument');
  21. $input = new ArrayInput(array('--foo' => 'bar', 'name' => 'Fabien'));
  22. $t->is($input->getFirstArgument(), 'Fabien', '->getFirstArgument() returns the first passed argument');
  23. // ->hasParameterOption()
  24. $t->diag('->hasParameterOption()');
  25. $input = new ArrayInput(array('name' => 'Fabien', '--foo' => 'bar'));
  26. $t->ok($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters');
  27. $t->ok(!$input->hasParameterOption('--bar'), '->hasParameterOption() returns false if an option is not present in the passed parameters');
  28. $input = new ArrayInput(array('--foo'));
  29. $t->ok($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters');
  30. // ->parse()
  31. $t->diag('->parse()');
  32. $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
  33. $t->is($input->getArguments(), array('name' => 'foo'), '->parse() parses required arguments');
  34. try
  35. {
  36. $input = new ArrayInput(array('foo' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
  37. $t->fail('->parse() throws an \InvalidArgumentException exception if an invalid argument is passed');
  38. }
  39. catch (\RuntimeException $e)
  40. {
  41. $t->pass('->parse() throws an \InvalidArgumentException exception if an invalid argument is passed');
  42. }
  43. $input = new ArrayInput(array('--foo' => 'bar'), new InputDefinition(array(new InputOption('foo'))));
  44. $t->is($input->getOptions(), array('foo' => 'bar'), '->parse() parses long options');
  45. $input = new ArrayInput(array('--foo' => 'bar'), new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL, '', 'default'))));
  46. $t->is($input->getOptions(), array('foo' => 'bar'), '->parse() parses long options with a default value');
  47. $input = new ArrayInput(array('--foo' => null), new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL, '', 'default'))));
  48. $t->is($input->getOptions(), array('foo' => 'default'), '->parse() parses long options with a default value');
  49. try
  50. {
  51. $input = new ArrayInput(array('--foo' => null), new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED))));
  52. $t->fail('->parse() throws an \InvalidArgumentException exception if a required option is passed without a value');
  53. }
  54. catch (\RuntimeException $e)
  55. {
  56. $t->pass('->parse() throws an \InvalidArgumentException exception if a required option is passed without a value');
  57. }
  58. try
  59. {
  60. $input = new ArrayInput(array('--foo' => 'foo'), new InputDefinition());
  61. $t->fail('->parse() throws an \InvalidArgumentException exception if an invalid option is passed');
  62. }
  63. catch (\RuntimeException $e)
  64. {
  65. $t->pass('->parse() throws an \InvalidArgumentException exception if an invalid option is passed');
  66. }
  67. $input = new ArrayInput(array('-f' => 'bar'), new InputDefinition(array(new InputOption('foo', 'f'))));
  68. $t->is($input->getOptions(), array('foo' => 'bar'), '->parse() parses short options');
  69. try
  70. {
  71. $input = new ArrayInput(array('-o' => 'foo'), new InputDefinition());
  72. $t->fail('->parse() throws an \InvalidArgumentException exception if an invalid option is passed');
  73. }
  74. catch (\RuntimeException $e)
  75. {
  76. $t->pass('->parse() throws an \InvalidArgumentException exception if an invalid option is passed');
  77. }