InputTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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(19);
  15. // __construct()
  16. $t->diag('__construct()');
  17. $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
  18. $t->is($input->getArgument('name'), 'foo', '->__construct() takes a InputDefinition as an argument');
  19. // ->getOption() ->setOption() ->getOptions()
  20. $t->diag('->getOption() ->setOption() ->getOptions()');
  21. $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'))));
  22. $t->is($input->getOption('name'), 'foo', '->getOption() returns the value for the given option');
  23. $input->setOption('name', 'bar');
  24. $t->is($input->getOption('name'), 'bar', '->setOption() sets the value for a given option');
  25. $t->is($input->getOptions(), array('name' => 'bar'), '->getOptions() returns all option values');
  26. $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::PARAMETER_OPTIONAL, '', 'default'))));
  27. $t->is($input->getOption('bar'), 'default', '->getOption() returns the default value for optional options');
  28. $t->is($input->getOptions(), array('name' => 'foo', 'bar' => 'default'), '->getOptions() returns all option values, even optional ones');
  29. try
  30. {
  31. $input->setOption('foo', 'bar');
  32. $t->fail('->setOption() throws a \InvalidArgumentException if the option does not exist');
  33. }
  34. catch (\InvalidArgumentException $e)
  35. {
  36. $t->pass('->setOption() throws a \InvalidArgumentException if the option does not exist');
  37. }
  38. try
  39. {
  40. $input->getOption('foo');
  41. $t->fail('->getOption() throws a \InvalidArgumentException if the option does not exist');
  42. }
  43. catch (\InvalidArgumentException $e)
  44. {
  45. $t->pass('->getOption() throws a \InvalidArgumentException if the option does not exist');
  46. }
  47. // ->getArgument() ->setArgument() ->getArguments()
  48. $t->diag('->getArgument() ->setArgument() ->getArguments()');
  49. $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
  50. $t->is($input->getArgument('name'), 'foo', '->getArgument() returns the value for the given argument');
  51. $input->setArgument('name', 'bar');
  52. $t->is($input->getArgument('name'), 'bar', '->setArgument() sets the value for a given argument');
  53. $t->is($input->getArguments(), array('name' => 'bar'), '->getArguments() returns all argument values');
  54. $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default'))));
  55. $t->is($input->getArgument('bar'), 'default', '->getArgument() returns the default value for optional arguments');
  56. $t->is($input->getArguments(), array('name' => 'foo', 'bar' => 'default'), '->getArguments() returns all argument values, even optional ones');
  57. try
  58. {
  59. $input->setArgument('foo', 'bar');
  60. $t->fail('->setArgument() throws a \InvalidArgumentException if the argument does not exist');
  61. }
  62. catch (\InvalidArgumentException $e)
  63. {
  64. $t->pass('->setArgument() throws a \InvalidArgumentException if the argument does not exist');
  65. }
  66. try
  67. {
  68. $input->getArgument('foo');
  69. $t->fail('->getArgument() throws a \InvalidArgumentException if the argument does not exist');
  70. }
  71. catch (\InvalidArgumentException $e)
  72. {
  73. $t->pass('->getArgument() throws a \InvalidArgumentException if the argument does not exist');
  74. }
  75. // ->validate()
  76. $t->diag('->validate()');
  77. $input = new ArrayInput(array());
  78. $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
  79. try
  80. {
  81. $input->validate();
  82. $t->fail('->validate() throws a \RuntimeException if not enough arguments are given');
  83. }
  84. catch (\RuntimeException $e)
  85. {
  86. $t->pass('->validate() throws a \RuntimeException if not enough arguments are given');
  87. }
  88. $input = new ArrayInput(array('name' => 'foo'));
  89. $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
  90. try
  91. {
  92. $input->validate();
  93. $t->pass('->validate() does not throw a \RuntimeException if enough arguments are given');
  94. }
  95. catch (\RuntimeException $e)
  96. {
  97. $t->fail('->validate() does not throw a \RuntimeException if enough arguments are given');
  98. }
  99. // ->setInteractive() ->isInteractive()
  100. $t->diag('->setInteractive() ->isInteractive()');
  101. $input = new ArrayInput(array());
  102. $t->ok($input->isInteractive(), '->isInteractive() returns whether the input should be interactive or not');
  103. $input->setInteractive(false);
  104. $t->ok(!$input->isInteractive(), '->setInteractive() changes the interactive flag');