InputTest.php 4.7 KB

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