InputTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\Components\Console\Input;
  10. use Symfony\Components\Console\Input\ArrayInput;
  11. use Symfony\Components\Console\Input\InputDefinition;
  12. use Symfony\Components\Console\Input\InputArgument;
  13. use Symfony\Components\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. {
  33. $input->setOption('foo', 'bar');
  34. $this->fail('->setOption() throws a \InvalidArgumentException if the option does not exist');
  35. }
  36. catch (\Exception $e)
  37. {
  38. $this->assertType('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist');
  39. $this->assertEquals('The "foo" option does not exist.', $e->getMessage());
  40. }
  41. try
  42. {
  43. $input->getOption('foo');
  44. $this->fail('->getOption() throws a \InvalidArgumentException if the option does not exist');
  45. }
  46. catch (\Exception $e)
  47. {
  48. $this->assertType('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist');
  49. $this->assertEquals('The "foo" option does not exist.', $e->getMessage());
  50. }
  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. try
  63. {
  64. $input->setArgument('foo', 'bar');
  65. $this->fail('->setArgument() throws a \InvalidArgumentException if the argument does not exist');
  66. }
  67. catch (\Exception $e)
  68. {
  69. $this->assertType('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist');
  70. $this->assertEquals('The "foo" argument does not exist.', $e->getMessage());
  71. }
  72. try
  73. {
  74. $input->getArgument('foo');
  75. $this->fail('->getArgument() throws a \InvalidArgumentException if the argument does not exist');
  76. }
  77. catch (\Exception $e)
  78. {
  79. $this->assertType('\InvalidArgumentException', $e, '->setOption() throws a \InvalidArgumentException if the option does not exist');
  80. $this->assertEquals('The "foo" argument does not exist.', $e->getMessage());
  81. }
  82. }
  83. public function testValidate()
  84. {
  85. $input = new ArrayInput(array());
  86. $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
  87. try
  88. {
  89. $input->validate();
  90. $this->fail('->validate() throws a \RuntimeException if not enough arguments are given');
  91. }
  92. catch (\Exception $e)
  93. {
  94. $this->assertType('\RuntimeException', $e, '->validate() throws a \RuntimeException if not enough arguments are given');
  95. $this->assertEquals('Not enough arguments.', $e->getMessage());
  96. }
  97. $input = new ArrayInput(array('name' => 'foo'));
  98. $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED))));
  99. try
  100. {
  101. $input->validate();
  102. }
  103. catch (\RuntimeException $e)
  104. {
  105. $this->fail('->validate() does not throw a \RuntimeException if enough arguments are given');
  106. }
  107. }
  108. public function testSetFetInteractive()
  109. {
  110. $input = new ArrayInput(array());
  111. $this->assertTrue($input->isInteractive(), '->isInteractive() returns whether the input should be interactive or not');
  112. $input->setInteractive(false);
  113. $this->assertFalse($input->isInteractive(), '->setInteractive() changes the interactive flag');
  114. }
  115. }