InputTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. $input = new ArrayInput(['--name' => null], new InputDefinition([new InputOption('name', null, InputOption::VALUE_NEGATABLE)]));
  40. $this->assertTrue($input->hasOption('name'));
  41. $this->assertTrue($input->hasOption('no-name'));
  42. $this->assertTrue($input->getOption('name'));
  43. $this->assertFalse($input->getOption('no-name'));
  44. $input = new ArrayInput(['--no-name' => null], new InputDefinition([new InputOption('name', null, InputOption::VALUE_NEGATABLE)]));
  45. $this->assertFalse($input->getOption('name'));
  46. $this->assertTrue($input->getOption('no-name'));
  47. $input = new ArrayInput([], new InputDefinition([new InputOption('name', null, InputOption::VALUE_NEGATABLE)]));
  48. $this->assertNull($input->getOption('name'));
  49. $this->assertNull($input->getOption('no-name'));
  50. }
  51. public function testSetInvalidOption()
  52. {
  53. $this->expectException(\InvalidArgumentException::class);
  54. $this->expectExceptionMessage('The "foo" option does not exist.');
  55. $input = new ArrayInput(['--name' => 'foo'], new InputDefinition([new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default')]));
  56. $input->setOption('foo', 'bar');
  57. }
  58. public function testGetInvalidOption()
  59. {
  60. $this->expectException(\InvalidArgumentException::class);
  61. $this->expectExceptionMessage('The "foo" option does not exist.');
  62. $input = new ArrayInput(['--name' => 'foo'], new InputDefinition([new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default')]));
  63. $input->getOption('foo');
  64. }
  65. public function testArguments()
  66. {
  67. $input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name')]));
  68. $this->assertEquals('foo', $input->getArgument('name'), '->getArgument() returns the value for the given argument');
  69. $input->setArgument('name', 'bar');
  70. $this->assertEquals('bar', $input->getArgument('name'), '->setArgument() sets the value for a given argument');
  71. $this->assertEquals(['name' => 'bar'], $input->getArguments(), '->getArguments() returns all argument values');
  72. $input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default')]));
  73. $this->assertEquals('default', $input->getArgument('bar'), '->getArgument() returns the default value for optional arguments');
  74. $this->assertEquals(['name' => 'foo', 'bar' => 'default'], $input->getArguments(), '->getArguments() returns all argument values, even optional ones');
  75. }
  76. public function testSetInvalidArgument()
  77. {
  78. $this->expectException(\InvalidArgumentException::class);
  79. $this->expectExceptionMessage('The "foo" argument does not exist.');
  80. $input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default')]));
  81. $input->setArgument('foo', 'bar');
  82. }
  83. public function testGetInvalidArgument()
  84. {
  85. $this->expectException(\InvalidArgumentException::class);
  86. $this->expectExceptionMessage('The "foo" argument does not exist.');
  87. $input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default')]));
  88. $input->getArgument('foo');
  89. }
  90. public function testValidateWithMissingArguments()
  91. {
  92. $this->expectException(\RuntimeException::class);
  93. $this->expectExceptionMessage('Not enough arguments (missing: "name").');
  94. $input = new ArrayInput([]);
  95. $input->bind(new InputDefinition([new InputArgument('name', InputArgument::REQUIRED)]));
  96. $input->validate();
  97. }
  98. public function testValidateWithMissingRequiredArguments()
  99. {
  100. $this->expectException(\RuntimeException::class);
  101. $this->expectExceptionMessage('Not enough arguments (missing: "name").');
  102. $input = new ArrayInput(['bar' => 'baz']);
  103. $input->bind(new InputDefinition([new InputArgument('name', InputArgument::REQUIRED), new InputArgument('bar', InputArgument::OPTIONAL)]));
  104. $input->validate();
  105. }
  106. public function testValidate()
  107. {
  108. $input = new ArrayInput(['name' => 'foo']);
  109. $input->bind(new InputDefinition([new InputArgument('name', InputArgument::REQUIRED)]));
  110. $this->assertNull($input->validate());
  111. }
  112. public function testSetGetInteractive()
  113. {
  114. $input = new ArrayInput([]);
  115. $this->assertTrue($input->isInteractive(), '->isInteractive() returns whether the input should be interactive or not');
  116. $input->setInteractive(false);
  117. $this->assertFalse($input->isInteractive(), '->setInteractive() changes the interactive flag');
  118. }
  119. public function testSetGetStream()
  120. {
  121. $input = new ArrayInput([]);
  122. $stream = fopen('php://memory', 'r+', false);
  123. $input->setStream($stream);
  124. $this->assertSame($stream, $input->getStream());
  125. }
  126. }