ArgvInputTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\ArgvInput;
  12. use Symfony\Component\Console\Input\InputDefinition;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputOption;
  15. class ArgvInputTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function testConstructor()
  18. {
  19. $_SERVER['argv'] = array('cli.php', 'foo');
  20. $input = new TestInput();
  21. $this->assertEquals(array('foo'), $input->getTokens(), '__construct() automatically get its input from the argv server variable');
  22. }
  23. public function testParser()
  24. {
  25. $input = new TestInput(array('cli.php', 'foo'));
  26. $input->bind(new InputDefinition(array(new InputArgument('name'))));
  27. $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() parses required arguments');
  28. $input->bind(new InputDefinition(array(new InputArgument('name'))));
  29. $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() is stateless');
  30. $input = new TestInput(array('cli.php', '--foo'));
  31. $input->bind(new InputDefinition(array(new InputOption('foo'))));
  32. $this->assertEquals(array('foo' => true), $input->getOptions(), '->parse() parses long options without a value');
  33. $input = new TestInput(array('cli.php', '--foo=bar'));
  34. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
  35. $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options with a required value (with a = separator)');
  36. $input = new TestInput(array('cli.php', '--foo', 'bar'));
  37. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
  38. $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options with a required value (with a space separator)');
  39. try {
  40. $input = new TestInput(array('cli.php', '--foo'));
  41. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
  42. $this->fail('->parse() throws a \RuntimeException if no value is passed to an option when it is required');
  43. } catch (\Exception $e) {
  44. $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if no value is passed to an option when it is required');
  45. $this->assertEquals('The "--foo" option requires a value.', $e->getMessage(), '->parse() throws a \RuntimeException if no value is passed to an option when it is required');
  46. }
  47. $input = new TestInput(array('cli.php', '-f'));
  48. $input->bind(new InputDefinition(array(new InputOption('foo', 'f'))));
  49. $this->assertEquals(array('foo' => true), $input->getOptions(), '->parse() parses short options without a value');
  50. $input = new TestInput(array('cli.php', '-fbar'));
  51. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
  52. $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses short options with a required value (with no separator)');
  53. $input = new TestInput(array('cli.php', '-f', 'bar'));
  54. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
  55. $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses short options with a required value (with a space separator)');
  56. $input = new TestInput(array('cli.php', '-f', '-b', 'foo'));
  57. $input->bind(new InputDefinition(array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b'))));
  58. $this->assertEquals(array('foo' => null, 'bar' => true), $input->getOptions(), '->parse() parses short options with an optional value which is not present');
  59. try {
  60. $input = new TestInput(array('cli.php', '-f'));
  61. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
  62. $this->fail('->parse() throws a \RuntimeException if no value is passed to an option when it is required');
  63. } catch (\Exception $e) {
  64. $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if no value is passed to an option when it is required');
  65. $this->assertEquals('The "--foo" option requires a value.', $e->getMessage(), '->parse() throws a \RuntimeException if no value is passed to an option when it is required');
  66. }
  67. try {
  68. $input = new TestInput(array('cli.php', '-ffoo'));
  69. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_NONE))));
  70. $this->fail('->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
  71. } catch (\Exception $e) {
  72. $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if a value is passed to an option which does not take one');
  73. $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');
  74. }
  75. try {
  76. $input = new TestInput(array('cli.php', 'foo', 'bar'));
  77. $input->bind(new InputDefinition());
  78. $this->fail('->parse() throws a \RuntimeException if too many arguments are passed');
  79. } catch (\Exception $e) {
  80. $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if too many arguments are passed');
  81. $this->assertEquals('Too many arguments.', $e->getMessage(), '->parse() throws a \RuntimeException if too many arguments are passed');
  82. }
  83. try {
  84. $input = new TestInput(array('cli.php', '--foo'));
  85. $input->bind(new InputDefinition());
  86. $this->fail('->parse() throws a \RuntimeException if an unknown long option is passed');
  87. } catch (\Exception $e) {
  88. $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if an unknown long option is passed');
  89. $this->assertEquals('The "--foo" option does not exist.', $e->getMessage(), '->parse() throws a \RuntimeException if an unknown long option is passed');
  90. }
  91. try {
  92. $input = new TestInput(array('cli.php', '-f'));
  93. $input->bind(new InputDefinition());
  94. $this->fail('->parse() throws a \RuntimeException if an unknown short option is passed');
  95. } catch (\Exception $e) {
  96. $this->assertInstanceOf('\RuntimeException', $e, '->parse() throws a \RuntimeException if an unknown short option is passed');
  97. $this->assertEquals('The "-f" option does not exist.', $e->getMessage(), '->parse() throws a \RuntimeException if an unknown short option is passed');
  98. }
  99. $input = new TestInput(array('cli.php', '-fb'));
  100. $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b'))));
  101. $this->assertEquals(array('foo' => true, 'bar' => true), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one');
  102. $input = new TestInput(array('cli.php', '-fb', 'bar'));
  103. $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_REQUIRED))));
  104. $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 value');
  105. $input = new TestInput(array('cli.php', '-fb', 'bar'));
  106. $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL))));
  107. $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 value');
  108. $input = new TestInput(array('cli.php', '-fbbar'));
  109. $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL))));
  110. $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 value with no separator');
  111. $input = new TestInput(array('cli.php', '-fbbar'));
  112. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL))));
  113. $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 value');
  114. try {
  115. $input = new TestInput(array('cli.php', 'foo', 'bar', 'baz', 'bat'));
  116. $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::IS_ARRAY))));
  117. $this->assertEquals(array('name' => array('foo', 'bar', 'baz', 'bat')), $input->getArguments(), '->parse() parses array arguments');
  118. } catch (\RuntimeException $e) {
  119. $this->assertNotEquals('Too many arguments.', $e->getMessage(), '->parse() parses array arguments');
  120. }
  121. }
  122. public function testGetFirstArgument()
  123. {
  124. $input = new TestInput(array('cli.php', '-fbbar'));
  125. $this->assertEquals('', $input->getFirstArgument(), '->getFirstArgument() returns the first argument from the raw input');
  126. $input = new TestInput(array('cli.php', '-fbbar', 'foo'));
  127. $this->assertEquals('foo', $input->getFirstArgument(), '->getFirstArgument() returns the first argument from the raw input');
  128. }
  129. public function testHasParameterOption()
  130. {
  131. $input = new TestInput(array('cli.php', '-f', 'foo'));
  132. $this->assertTrue($input->hasParameterOption('-f'), '->hasParameterOption() returns true if the given short option is in the raw input');
  133. $input = new TestInput(array('cli.php', '--foo', 'foo'));
  134. $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if the given short option is in the raw input');
  135. $input = new TestInput(array('cli.php', 'foo'));
  136. $this->assertFalse($input->hasParameterOption('--foo'), '->hasParameterOption() returns false if the given short option is not in the raw input');
  137. }
  138. /**
  139. * @dataProvider provideGetParameterOptionValues
  140. */
  141. public function testGetParameterOptionEqualSign($argv, $key, $expected)
  142. {
  143. $input = new ArgvInput($argv);
  144. $this->assertEquals($expected, $input->getParameterOption($key), '->getParameterOption() returns the expected value');
  145. }
  146. public function provideGetParameterOptionValues()
  147. {
  148. return array(
  149. array(array('app/console', 'foo:bar', '-e', 'dev'), '-e', 'dev'),
  150. array(array('app/console', 'foo:bar', '--env=dev'), '--env', 'dev'),
  151. array(array('app/console', 'foo:bar', '-e', 'dev'), array('-e', '--env'), 'dev'),
  152. array(array('app/console', 'foo:bar', '--env=dev'), array('-e', '--env'), 'dev'),
  153. );
  154. }
  155. }
  156. class TestInput extends ArgvInput
  157. {
  158. public function getTokens()
  159. {
  160. return $this->tokens;
  161. }
  162. }