ArgvInputTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\ArgvInput;
  11. use Symfony\Component\Console\Input\InputDefinition;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputOption;
  14. class ArgvInputTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testConstructor()
  17. {
  18. $_SERVER['argv'] = array('cli.php', 'foo');
  19. $input = new TestInput();
  20. $this->assertEquals(array('foo'), $input->getTokens(), '__construct() automatically get its input from the argv server variable');
  21. }
  22. public function testParser()
  23. {
  24. $input = new TestInput(array('cli.php', 'foo'));
  25. $input->bind(new InputDefinition(array(new InputArgument('name'))));
  26. $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() parses required arguments');
  27. $input->bind(new InputDefinition(array(new InputArgument('name'))));
  28. $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() is stateless');
  29. $input = new TestInput(array('cli.php', '--foo'));
  30. $input->bind(new InputDefinition(array(new InputOption('foo'))));
  31. $this->assertEquals(array('foo' => true), $input->getOptions(), '->parse() parses long options without parameter');
  32. $input = new TestInput(array('cli.php', '--foo=bar'));
  33. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED))));
  34. $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options with a required parameter (with a = separator)');
  35. $input = new TestInput(array('cli.php', '--foo', 'bar'));
  36. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED))));
  37. $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options with a required parameter (with a space separator)');
  38. try {
  39. $input = new TestInput(array('cli.php', '--foo'));
  40. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED))));
  41. $this->fail('->parse() throws a \RuntimeException if no parameter is passed to an option when it is required');
  42. } catch (\Exception $e) {
  43. $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if no parameter is passed to an option when it is required');
  44. $this->assertEquals('The "--foo" option requires a value.', $e->getMessage(), '->parse() throws a \RuntimeException if no parameter is passed to an option when it is required');
  45. }
  46. $input = new TestInput(array('cli.php', '-f'));
  47. $input->bind(new InputDefinition(array(new InputOption('foo', 'f'))));
  48. $this->assertEquals(array('foo' => true), $input->getOptions(), '->parse() parses short options without parameter');
  49. $input = new TestInput(array('cli.php', '-fbar'));
  50. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED))));
  51. $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses short options with a required parameter (with no separator)');
  52. $input = new TestInput(array('cli.php', '-f', 'bar'));
  53. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED))));
  54. $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses short options with a required parameter (with a space separator)');
  55. $input = new TestInput(array('cli.php', '-f', '-b', 'foo'));
  56. $input->bind(new InputDefinition(array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL), new InputOption('bar', 'b'))));
  57. $this->assertEquals(array('foo' => null, 'bar' => true), $input->getOptions(), '->parse() parses short options with an optional parameter which is not present');
  58. try {
  59. $input = new TestInput(array('cli.php', '-f'));
  60. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED))));
  61. $this->fail('->parse() throws a \RuntimeException if no parameter is passed to an option when it is required');
  62. } catch (\Exception $e) {
  63. $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if no parameter is passed to an option when it is required');
  64. $this->assertEquals('The "--foo" option requires a value.', $e->getMessage(), '->parse() throws a \RuntimeException if no parameter is passed to an option when it is required');
  65. }
  66. try {
  67. $input = new TestInput(array('cli.php', '-ffoo'));
  68. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_NONE))));
  69. $this->fail('->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
  70. } catch (\Exception $e) {
  71. $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
  72. $this->assertEquals('The "-o" option does not exist.', $e->getMessage(), '->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
  73. }
  74. try {
  75. $input = new TestInput(array('cli.php', 'foo', 'bar'));
  76. $input->bind(new InputDefinition());
  77. $this->fail('->parse() throws a \RuntimeException if too many arguments are passed');
  78. } catch (\Exception $e) {
  79. $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if too many arguments are passed');
  80. $this->assertEquals('Too many arguments.', $e->getMessage(), '->parse() throws a \RuntimeException if too many arguments are passed');
  81. }
  82. try {
  83. $input = new TestInput(array('cli.php', '--foo'));
  84. $input->bind(new InputDefinition());
  85. $this->fail('->parse() throws a \RuntimeException if an unknown long option is passed');
  86. } catch (\Exception $e) {
  87. $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if an unknown long option is passed');
  88. $this->assertEquals('The "--foo" option does not exist.', $e->getMessage(), '->parse() throws a \RuntimeException if an unknown long option is passed');
  89. }
  90. try {
  91. $input = new TestInput(array('cli.php', '-f'));
  92. $input->bind(new InputDefinition());
  93. $this->fail('->parse() throws a \RuntimeException if an unknown short option is passed');
  94. } catch (\Exception $e) {
  95. $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if an unknown short option is passed');
  96. $this->assertEquals('The "-f" option does not exist.', $e->getMessage(), '->parse() throws a \RuntimeException if an unknown short option is passed');
  97. }
  98. $input = new TestInput(array('cli.php', '-fb'));
  99. $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b'))));
  100. $this->assertEquals(array('foo' => true, 'bar' => true), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one');
  101. $input = new TestInput(array('cli.php', '-fb', 'bar'));
  102. $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::PARAMETER_REQUIRED))));
  103. $this->assertEquals(array('foo' => true, 'bar' => 'bar'), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and the last one has a required parameter');
  104. $input = new TestInput(array('cli.php', '-fb', 'bar'));
  105. $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::PARAMETER_OPTIONAL))));
  106. $this->assertEquals(array('foo' => true, 'bar' => 'bar'), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and the last one has an optional parameter');
  107. $input = new TestInput(array('cli.php', '-fbbar'));
  108. $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::PARAMETER_OPTIONAL))));
  109. $this->assertEquals(array('foo' => true, 'bar' => 'bar'), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and the last one has an optional parameter with no separator');
  110. $input = new TestInput(array('cli.php', '-fbbar'));
  111. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL), new InputOption('bar', 'b', InputOption::PARAMETER_OPTIONAL))));
  112. $this->assertEquals(array('foo' => 'bbar', 'bar' => null), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and one of them takes a parameter');
  113. }
  114. public function testGetFirstArgument()
  115. {
  116. $input = new TestInput(array('cli.php', '-fbbar'));
  117. $this->assertEquals('', $input->getFirstArgument(), '->getFirstArgument() returns the first argument from the raw input');
  118. $input = new TestInput(array('cli.php', '-fbbar', 'foo'));
  119. $this->assertEquals('foo', $input->getFirstArgument(), '->getFirstArgument() returns the first argument from the raw input');
  120. }
  121. public function testHasParameterOption()
  122. {
  123. $input = new TestInput(array('cli.php', '-f', 'foo'));
  124. $this->assertTrue($input->hasParameterOption('-f'), '->hasParameterOption() returns true if the given short option is in the raw input');
  125. $input = new TestInput(array('cli.php', '--foo', 'foo'));
  126. $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if the given short option is in the raw input');
  127. $input = new TestInput(array('cli.php', 'foo'));
  128. $this->assertFalse($input->hasParameterOption('--foo'), '->hasParameterOption() returns false if the given short option is not in the raw input');
  129. }
  130. }
  131. class TestInput extends ArgvInput
  132. {
  133. public function getTokens()
  134. {
  135. return $this->tokens;
  136. }
  137. }