InputTest.php 5.4 KB

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