InputTest.php 7.1KB

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