InputTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\Input;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Input\ArrayInput;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputDefinition;
  15. use Symfony\Component\Console\Input\InputOption;
  16. class InputTest extends TestCase
  17. {
  18. public function testConstructor()
  19. {
  20. $input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name')]));
  21. $this->assertEquals('foo', $input->getArgument('name'), '->__construct() takes a InputDefinition as an argument');
  22. }
  23. public function testOptions()
  24. {
  25. $input = new ArrayInput(['--name' => 'foo'], new InputDefinition([new InputOption('name')]));
  26. $this->assertEquals('foo', $input->getOption('name'), '->getOption() returns the value for the given option');
  27. $input->setOption('name', 'bar');
  28. $this->assertEquals('bar', $input->getOption('name'), '->setOption() sets the value for a given option');
  29. $this->assertEquals(['name' => 'bar'], $input->getOptions(), '->getOptions() returns all option values');
  30. $input = new ArrayInput(['--name' => 'foo'], new InputDefinition([new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default')]));
  31. $this->assertEquals('default', $input->getOption('bar'), '->getOption() returns the default value for optional options');
  32. $this->assertEquals(['name' => 'foo', 'bar' => 'default'], $input->getOptions(), '->getOptions() returns all option values, even optional ones');
  33. $input = new ArrayInput(['--name' => 'foo', '--bar' => ''], new InputDefinition([new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default')]));
  34. $this->assertEquals('', $input->getOption('bar'), '->getOption() returns null for options explicitly passed without value (or an empty value)');
  35. $this->assertEquals(['name' => 'foo', 'bar' => ''], $input->getOptions(), '->getOptions() returns all option values.');
  36. $input = new ArrayInput(['--name' => 'foo', '--bar' => null], new InputDefinition([new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default')]));
  37. $this->assertNull($input->getOption('bar'), '->getOption() returns null for options explicitly passed without value (or an empty value)');
  38. $this->assertEquals(['name' => 'foo', 'bar' => null], $input->getOptions(), '->getOptions() returns all option values');
  39. }
  40. public function testSetInvalidOption()
  41. {
  42. $this->expectException('InvalidArgumentException');
  43. $this->expectExceptionMessage('The "foo" option does not exist.');
  44. $input = new ArrayInput(['--name' => 'foo'], new InputDefinition([new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default')]));
  45. $input->setOption('foo', 'bar');
  46. }
  47. public function testGetInvalidOption()
  48. {
  49. $this->expectException('InvalidArgumentException');
  50. $this->expectExceptionMessage('The "foo" option does not exist.');
  51. $input = new ArrayInput(['--name' => 'foo'], new InputDefinition([new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default')]));
  52. $input->getOption('foo');
  53. }
  54. public function testArguments()
  55. {
  56. $input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name')]));
  57. $this->assertEquals('foo', $input->getArgument('name'), '->getArgument() returns the value for the given argument');
  58. $input->setArgument('name', 'bar');
  59. $this->assertEquals('bar', $input->getArgument('name'), '->setArgument() sets the value for a given argument');
  60. $this->assertEquals(['name' => 'bar'], $input->getArguments(), '->getArguments() returns all argument values');
  61. $input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default')]));
  62. $this->assertEquals('default', $input->getArgument('bar'), '->getArgument() returns the default value for optional arguments');
  63. $this->assertEquals(['name' => 'foo', 'bar' => 'default'], $input->getArguments(), '->getArguments() returns all argument values, even optional ones');
  64. }
  65. public function testSetInvalidArgument()
  66. {
  67. $this->expectException('InvalidArgumentException');
  68. $this->expectExceptionMessage('The "foo" argument does not exist.');
  69. $input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default')]));
  70. $input->setArgument('foo', 'bar');
  71. }
  72. public function testGetInvalidArgument()
  73. {
  74. $this->expectException('InvalidArgumentException');
  75. $this->expectExceptionMessage('The "foo" argument does not exist.');
  76. $input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default')]));
  77. $input->getArgument('foo');
  78. }
  79. public function testValidateWithMissingArguments()
  80. {
  81. $this->expectException('RuntimeException');
  82. $this->expectExceptionMessage('Not enough arguments (missing: "name").');
  83. $input = new ArrayInput([]);
  84. $input->bind(new InputDefinition([new InputArgument('name', InputArgument::REQUIRED)]));
  85. $input->validate();
  86. }
  87. public function testValidateWithMissingRequiredArguments()
  88. {
  89. $this->expectException('RuntimeException');
  90. $this->expectExceptionMessage('Not enough arguments (missing: "name").');
  91. $input = new ArrayInput(['bar' => 'baz']);
  92. $input->bind(new InputDefinition([new InputArgument('name', InputArgument::REQUIRED), new InputArgument('bar', InputArgument::OPTIONAL)]));
  93. $input->validate();
  94. }
  95. public function testValidate()
  96. {
  97. $input = new ArrayInput(['name' => 'foo']);
  98. $input->bind(new InputDefinition([new InputArgument('name', InputArgument::REQUIRED)]));
  99. $this->assertNull($input->validate());
  100. }
  101. public function testSetGetInteractive()
  102. {
  103. $input = new ArrayInput([]);
  104. $this->assertTrue($input->isInteractive(), '->isInteractive() returns whether the input should be interactive or not');
  105. $input->setInteractive(false);
  106. $this->assertFalse($input->isInteractive(), '->setInteractive() changes the interactive flag');
  107. }
  108. public function testSetGetStream()
  109. {
  110. $input = new ArrayInput([]);
  111. $stream = fopen('php://memory', 'r+', false);
  112. $input->setStream($stream);
  113. $this->assertSame($stream, $input->getStream());
  114. }
  115. }