ArgvInputTest.php 12 KB

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