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