InputTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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(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. }
  34. /**
  35. * @expectedException \InvalidArgumentException
  36. * @expectedExceptionMessage The "foo" option does not exist.
  37. */
  38. public function testSetInvalidOption()
  39. {
  40. $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default'))));
  41. $input->setOption('foo', 'bar');
  42. }
  43. /**
  44. * @expectedException \InvalidArgumentException
  45. * @expectedExceptionMessage The "foo" option does not exist.
  46. */
  47. public function testGetInvalidOption()
  48. {
  49. $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::VALUE_OPTIONAL, '', 'default'))));
  50. $input->getOption('foo');
  51. }
  52. public function testArguments()
  53. {
  54. $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
  55. $this->assertEquals('foo', $input->getArgument('name'), '->getArgument() returns the value for the given argument');
  56. $input->setArgument('name', 'bar');
  57. $this->assertEquals('bar', $input->getArgument('name'), '->setArgument() sets the value for a given argument');
  58. $this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->getArguments() returns all argument values');
  59. $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default'))));
  60. $this->assertEquals('default', $input->getArgument('bar'), '->getArgument() returns the default value for optional arguments');
  61. $this->assertEquals(array('name' => 'foo', 'bar' => 'default'), $input->getArguments(), '->getArguments() returns all argument values, even optional ones');
  62. }
  63. /**
  64. * @expectedException \InvalidArgumentException
  65. * @expectedExceptionMessage The "foo" argument does not exist.
  66. */
  67. public function testSetInvalidArgument()
  68. {
  69. $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default'))));
  70. $input->setArgument('foo', 'bar');
  71. }
  72. /**
  73. * @expectedException \InvalidArgumentException
  74. * @expectedExceptionMessage The "foo" argument does not exist.
  75. */
  76. public function testGetInvalidArgument()
  77. {
  78. $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default'))));
  79. $input->getArgument('foo');
  80. }
  81. /**
  82. * @expectedException \RuntimeException
  83. * @expectedExceptionMessage Not enough arguments (missing: "name").
  84. */
  85. public function testValidateWithMissingArguments()
  86. {
  87. $input = new ArrayInput(array());
  88. $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
  89. $input->validate();
  90. }
  91. /**
  92. * @expectedException \RuntimeException
  93. * @expectedExceptionMessage Not enough arguments (missing: "name").
  94. */
  95. public function testValidateWithMissingRequiredArguments()
  96. {
  97. $input = new ArrayInput(array('bar' => 'baz'));
  98. $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED), new InputArgument('bar', InputArgument::OPTIONAL))));
  99. $input->validate();
  100. }
  101. public function testValidate()
  102. {
  103. $input = new ArrayInput(array('name' => 'foo'));
  104. $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
  105. $this->assertNull($input->validate());
  106. }
  107. public function testSetGetInteractive()
  108. {
  109. $input = new ArrayInput(array());
  110. $this->assertTrue($input->isInteractive(), '->isInteractive() returns whether the input should be interactive or not');
  111. $input->setInteractive(false);
  112. $this->assertFalse($input->isInteractive(), '->setInteractive() changes the interactive flag');
  113. }
  114. }