InputTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\Tests\Component\Console\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. try {
  33. $input->setOption('foo', 'bar');
  34. $this->fail('->setOption() throws a \InvalidArgumentException if the option does not exist');
  35. } catch (\Exception $e) {
  36. $this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist');
  37. $this->assertEquals('The "foo" option does not exist.', $e->getMessage());
  38. }
  39. try {
  40. $input->getOption('foo');
  41. $this->fail('->getOption() throws a \InvalidArgumentException if the option does not exist');
  42. } catch (\Exception $e) {
  43. $this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist');
  44. $this->assertEquals('The "foo" option does not exist.', $e->getMessage());
  45. }
  46. }
  47. public function testArguments()
  48. {
  49. $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
  50. $this->assertEquals('foo', $input->getArgument('name'), '->getArgument() returns the value for the given argument');
  51. $input->setArgument('name', 'bar');
  52. $this->assertEquals('bar', $input->getArgument('name'), '->setArgument() sets the value for a given argument');
  53. $this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->getArguments() returns all argument values');
  54. $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default'))));
  55. $this->assertEquals('default', $input->getArgument('bar'), '->getArgument() returns the default value for optional arguments');
  56. $this->assertEquals(array('name' => 'foo', 'bar' => 'default'), $input->getArguments(), '->getArguments() returns all argument values, even optional ones');
  57. try {
  58. $input->setArgument('foo', 'bar');
  59. $this->fail('->setArgument() throws a \InvalidArgumentException if the argument does not exist');
  60. } catch (\Exception $e) {
  61. $this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist');
  62. $this->assertEquals('The "foo" argument does not exist.', $e->getMessage());
  63. }
  64. try {
  65. $input->getArgument('foo');
  66. $this->fail('->getArgument() throws a \InvalidArgumentException if the argument does not exist');
  67. } catch (\Exception $e) {
  68. $this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist');
  69. $this->assertEquals('The "foo" argument does not exist.', $e->getMessage());
  70. }
  71. }
  72. public function testValidate()
  73. {
  74. $input = new ArrayInput(array());
  75. $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
  76. try {
  77. $input->validate();
  78. $this->fail('->validate() throws a \RuntimeException if not enough arguments are given');
  79. } catch (\Exception $e) {
  80. $this->assertInstanceOf('\RuntimeException', $e, '->validate() throws a \RuntimeException if not enough arguments are given');
  81. $this->assertEquals('Not enough arguments.', $e->getMessage());
  82. }
  83. $input = new ArrayInput(array('name' => 'foo'));
  84. $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
  85. try {
  86. $input->validate();
  87. } catch (\RuntimeException $e) {
  88. $this->fail('->validate() does not throw a \RuntimeException if enough arguments are given');
  89. }
  90. }
  91. public function testSetFetInteractive()
  92. {
  93. $input = new ArrayInput(array());
  94. $this->assertTrue($input->isInteractive(), '->isInteractive() returns whether the input should be interactive or not');
  95. $input->setInteractive(false);
  96. $this->assertFalse($input->isInteractive(), '->setInteractive() changes the interactive flag');
  97. }
  98. }