InputTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Component\Console\Input;
  10. use Symfony\Component\Console\Input\ArrayInput;
  11. use Symfony\Component\Console\Input\InputDefinition;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputOption;
  14. class InputTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testConstructor()
  17. {
  18. $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
  19. $this->assertEquals('foo', $input->getArgument('name'), '->__construct() takes a InputDefinition as an argument');
  20. }
  21. public function testOptions()
  22. {
  23. $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'))));
  24. $this->assertEquals('foo', $input->getOption('name'), '->getOption() returns the value for the given option');
  25. $input->setOption('name', 'bar');
  26. $this->assertEquals('bar', $input->getOption('name'), '->setOption() sets the value for a given option');
  27. $this->assertEquals(array('name' => 'bar'), $input->getOptions(), '->getOptions() returns all option values');
  28. $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::PARAMETER_OPTIONAL, '', 'default'))));
  29. $this->assertEquals('default', $input->getOption('bar'), '->getOption() returns the default value for optional options');
  30. $this->assertEquals(array('name' => 'foo', 'bar' => 'default'), $input->getOptions(), '->getOptions() returns all option values, even optional ones');
  31. try {
  32. $input->setOption('foo', 'bar');
  33. $this->fail('->setOption() throws a \InvalidArgumentException if the option does not exist');
  34. } catch (\Exception $e) {
  35. $this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist');
  36. $this->assertEquals('The "foo" option does not exist.', $e->getMessage());
  37. }
  38. try {
  39. $input->getOption('foo');
  40. $this->fail('->getOption() throws a \InvalidArgumentException if the option does not exist');
  41. } catch (\Exception $e) {
  42. $this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist');
  43. $this->assertEquals('The "foo" option does not exist.', $e->getMessage());
  44. }
  45. }
  46. public function testArguments()
  47. {
  48. $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'))));
  49. $this->assertEquals('foo', $input->getArgument('name'), '->getArgument() returns the value for the given argument');
  50. $input->setArgument('name', 'bar');
  51. $this->assertEquals('bar', $input->getArgument('name'), '->setArgument() sets the value for a given argument');
  52. $this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->getArguments() returns all argument values');
  53. $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default'))));
  54. $this->assertEquals('default', $input->getArgument('bar'), '->getArgument() returns the default value for optional arguments');
  55. $this->assertEquals(array('name' => 'foo', 'bar' => 'default'), $input->getArguments(), '->getArguments() returns all argument values, even optional ones');
  56. try {
  57. $input->setArgument('foo', 'bar');
  58. $this->fail('->setArgument() throws a \InvalidArgumentException if the argument does not exist');
  59. } catch (\Exception $e) {
  60. $this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist');
  61. $this->assertEquals('The "foo" argument does not exist.', $e->getMessage());
  62. }
  63. try {
  64. $input->getArgument('foo');
  65. $this->fail('->getArgument() throws a \InvalidArgumentException if the argument does not exist');
  66. } catch (\Exception $e) {
  67. $this->assertInstanceOf('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist');
  68. $this->assertEquals('The "foo" argument does not exist.', $e->getMessage());
  69. }
  70. }
  71. public function testValidate()
  72. {
  73. $input = new ArrayInput(array());
  74. $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
  75. try {
  76. $input->validate();
  77. $this->fail('->validate() throws a \RuntimeException if not enough arguments are given');
  78. } catch (\Exception $e) {
  79. $this->assertInstanceOf('\RuntimeException', $e, '->validate() throws a \RuntimeException if not enough arguments are given');
  80. $this->assertEquals('Not enough arguments.', $e->getMessage());
  81. }
  82. $input = new ArrayInput(array('name' => 'foo'));
  83. $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
  84. try {
  85. $input->validate();
  86. } catch (\RuntimeException $e) {
  87. $this->fail('->validate() does not throw a \RuntimeException if enough arguments are given');
  88. }
  89. }
  90. public function testSetFetInteractive()
  91. {
  92. $input = new ArrayInput(array());
  93. $this->assertTrue($input->isInteractive(), '->isInteractive() returns whether the input should be interactive or not');
  94. $input->setInteractive(false);
  95. $this->assertFalse($input->isInteractive(), '->setInteractive() changes the interactive flag');
  96. }
  97. }