ArgvInputTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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\Component\Console\Tests\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 testParseArguments()
  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. }
  34. /**
  35. * @dataProvider provideOptions
  36. */
  37. public function testParseOptions($input, $options, $expectedOptions, $message)
  38. {
  39. $input = new ArgvInput($input);
  40. $input->bind(new InputDefinition($options));
  41. $this->assertEquals($expectedOptions, $input->getOptions(), $message);
  42. }
  43. public function provideOptions()
  44. {
  45. return array(
  46. array(
  47. array('cli.php', '--foo'),
  48. array(new InputOption('foo')),
  49. array('foo' => true),
  50. '->parse() parses long options without a value',
  51. ),
  52. array(
  53. array('cli.php', '--foo=bar'),
  54. array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)),
  55. array('foo' => 'bar'),
  56. '->parse() parses long options with a required value (with a = separator)',
  57. ),
  58. array(
  59. array('cli.php', '--foo', 'bar'),
  60. array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)),
  61. array('foo' => 'bar'),
  62. '->parse() parses long options with a required value (with a space separator)',
  63. ),
  64. array(
  65. array('cli.php', '-f'),
  66. array(new InputOption('foo', 'f')),
  67. array('foo' => true),
  68. '->parse() parses short options without a value',
  69. ),
  70. array(
  71. array('cli.php', '-fbar'),
  72. array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)),
  73. array('foo' => 'bar'),
  74. '->parse() parses short options with a required value (with no separator)',
  75. ),
  76. array(
  77. array('cli.php', '-f', 'bar'),
  78. array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)),
  79. array('foo' => 'bar'),
  80. '->parse() parses short options with a required value (with a space separator)',
  81. ),
  82. array(
  83. array('cli.php', '-f', ''),
  84. array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)),
  85. array('foo' => ''),
  86. '->parse() parses short options with an optional empty value',
  87. ),
  88. array(
  89. array('cli.php', '-f', '', 'foo'),
  90. array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)),
  91. array('foo' => ''),
  92. '->parse() parses short options with an optional empty value followed by an argument',
  93. ),
  94. array(
  95. array('cli.php', '-f', '', '-b'),
  96. array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b')),
  97. array('foo' => '', 'bar' => true),
  98. '->parse() parses short options with an optional empty value followed by an option',
  99. ),
  100. array(
  101. array('cli.php', '-f', '-b', 'foo'),
  102. array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b')),
  103. array('foo' => null, 'bar' => true),
  104. '->parse() parses short options with an optional value which is not present',
  105. ),
  106. array(
  107. array('cli.php', '-fb'),
  108. array(new InputOption('foo', 'f'), new InputOption('bar', 'b')),
  109. array('foo' => true, 'bar' => true),
  110. '->parse() parses short options when they are aggregated as a single one',
  111. ),
  112. array(
  113. array('cli.php', '-fb', 'bar'),
  114. array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_REQUIRED)),
  115. array('foo' => true, 'bar' => 'bar'),
  116. '->parse() parses short options when they are aggregated as a single one and the last one has a required value',
  117. ),
  118. array(
  119. array('cli.php', '-fb', 'bar'),
  120. array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL)),
  121. array('foo' => true, 'bar' => 'bar'),
  122. '->parse() parses short options when they are aggregated as a single one and the last one has an optional value',
  123. ),
  124. array(
  125. array('cli.php', '-fbbar'),
  126. array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL)),
  127. array('foo' => true, 'bar' => 'bar'),
  128. '->parse() parses short options when they are aggregated as a single one and the last one has an optional value with no separator',
  129. ),
  130. array(
  131. array('cli.php', '-fbbar'),
  132. array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL)),
  133. array('foo' => 'bbar', 'bar' => null),
  134. '->parse() parses short options when they are aggregated as a single one and one of them takes a value',
  135. ),
  136. );
  137. }
  138. /**
  139. * @dataProvider provideInvalidInput
  140. */
  141. public function testInvalidInput($argv, $definition, $expectedExceptionMessage)
  142. {
  143. $this->setExpectedException('RuntimeException', $expectedExceptionMessage);
  144. $input = new ArgvInput($argv);
  145. $input->bind($definition);
  146. }
  147. public function provideInvalidInput()
  148. {
  149. return array(
  150. array(
  151. array('cli.php', '--foo'),
  152. new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))),
  153. 'The "--foo" option requires a value.',
  154. ),
  155. array(
  156. array('cli.php', '-f'),
  157. new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))),
  158. 'The "--foo" option requires a value.',
  159. ),
  160. array(
  161. array('cli.php', '-ffoo'),
  162. new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_NONE))),
  163. 'The "-o" option does not exist.',
  164. ),
  165. array(
  166. array('cli.php', '--foo=bar'),
  167. new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_NONE))),
  168. 'The "--foo" option does not accept a value.',
  169. ),
  170. array(
  171. array('cli.php', 'foo', 'bar'),
  172. new InputDefinition(),
  173. 'Too many arguments.',
  174. ),
  175. array(
  176. array('cli.php', '--foo'),
  177. new InputDefinition(),
  178. 'The "--foo" option does not exist.',
  179. ),
  180. array(
  181. array('cli.php', '-f'),
  182. new InputDefinition(),
  183. 'The "-f" option does not exist.',
  184. ),
  185. array(
  186. array('cli.php', '-1'),
  187. new InputDefinition(array(new InputArgument('number'))),
  188. 'The "-1" option does not exist.',
  189. ),
  190. );
  191. }
  192. public function testParseArrayArgument()
  193. {
  194. $input = new ArgvInput(array('cli.php', 'foo', 'bar', 'baz', 'bat'));
  195. $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::IS_ARRAY))));
  196. $this->assertEquals(array('name' => array('foo', 'bar', 'baz', 'bat')), $input->getArguments(), '->parse() parses array arguments');
  197. }
  198. public function testParseArrayOption()
  199. {
  200. $input = new ArgvInput(array('cli.php', '--name=foo', '--name=bar', '--name=baz'));
  201. $input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
  202. $this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions(), '->parse() parses array options ("--option=value" syntax)');
  203. $input = new ArgvInput(array('cli.php', '--name', 'foo', '--name', 'bar', '--name', 'baz'));
  204. $input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
  205. $this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions(), '->parse() parses array options ("--option value" syntax)');
  206. $input = new ArgvInput(array('cli.php', '--name=foo', '--name=bar', '--name='));
  207. $input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
  208. $this->assertSame(array('name' => array('foo', 'bar', null)), $input->getOptions(), '->parse() parses empty array options as null ("--option=value" syntax)');
  209. $input = new ArgvInput(array('cli.php', '--name', 'foo', '--name', 'bar', '--name', '--anotherOption'));
  210. $input->bind(new InputDefinition(array(
  211. new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY),
  212. new InputOption('anotherOption', null, InputOption::VALUE_NONE),
  213. )));
  214. $this->assertSame(array('name' => array('foo', 'bar', null), 'anotherOption' => true), $input->getOptions(), '->parse() parses empty array options as null ("--option value" syntax)');
  215. }
  216. public function testParseNegativeNumberAfterDoubleDash()
  217. {
  218. $input = new ArgvInput(array('cli.php', '--', '-1'));
  219. $input->bind(new InputDefinition(array(new InputArgument('number'))));
  220. $this->assertEquals(array('number' => '-1'), $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence');
  221. $input = new ArgvInput(array('cli.php', '-f', 'bar', '--', '-1'));
  222. $input->bind(new InputDefinition(array(new InputArgument('number'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
  223. $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses arguments with leading dashes as options before having encountered a double-dash sequence');
  224. $this->assertEquals(array('number' => '-1'), $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence');
  225. }
  226. public function testParseEmptyStringArgument()
  227. {
  228. $input = new ArgvInput(array('cli.php', '-f', 'bar', ''));
  229. $input->bind(new InputDefinition(array(new InputArgument('empty'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
  230. $this->assertEquals(array('empty' => ''), $input->getArguments(), '->parse() parses empty string arguments');
  231. }
  232. public function testGetFirstArgument()
  233. {
  234. $input = new ArgvInput(array('cli.php', '-fbbar'));
  235. $this->assertNull($input->getFirstArgument(), '->getFirstArgument() returns null when there is no arguments');
  236. $input = new ArgvInput(array('cli.php', '-fbbar', 'foo'));
  237. $this->assertEquals('foo', $input->getFirstArgument(), '->getFirstArgument() returns the first argument from the raw input');
  238. }
  239. public function testHasParameterOption()
  240. {
  241. $input = new ArgvInput(array('cli.php', '-f', 'foo'));
  242. $this->assertTrue($input->hasParameterOption('-f'), '->hasParameterOption() returns true if the given short option is in the raw input');
  243. $input = new ArgvInput(array('cli.php', '--foo', 'foo'));
  244. $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if the given short option is in the raw input');
  245. $input = new ArgvInput(array('cli.php', 'foo'));
  246. $this->assertFalse($input->hasParameterOption('--foo'), '->hasParameterOption() returns false if the given short option is not in the raw input');
  247. $input = new ArgvInput(array('cli.php', '--foo=bar'));
  248. $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if the given option with provided value is in the raw input');
  249. }
  250. public function testToString()
  251. {
  252. $input = new ArgvInput(array('cli.php', '-f', 'foo'));
  253. $this->assertEquals('-f foo', (string) $input);
  254. $input = new ArgvInput(array('cli.php', '-f', '--bar=foo', 'a b c d', "A\nB'C"));
  255. $this->assertEquals('-f --bar=foo '.escapeshellarg('a b c d').' '.escapeshellarg("A\nB'C"), (string) $input);
  256. }
  257. /**
  258. * @dataProvider provideGetParameterOptionValues
  259. */
  260. public function testGetParameterOptionEqualSign($argv, $key, $expected)
  261. {
  262. $input = new ArgvInput($argv);
  263. $this->assertEquals($expected, $input->getParameterOption($key), '->getParameterOption() returns the expected value');
  264. }
  265. public function provideGetParameterOptionValues()
  266. {
  267. return array(
  268. array(array('app/console', 'foo:bar', '-e', 'dev'), '-e', 'dev'),
  269. array(array('app/console', 'foo:bar', '--env=dev'), '--env', 'dev'),
  270. array(array('app/console', 'foo:bar', '-e', 'dev'), array('-e', '--env'), 'dev'),
  271. array(array('app/console', 'foo:bar', '--env=dev'), array('-e', '--env'), 'dev'),
  272. array(array('app/console', 'foo:bar', '--env=dev', '--en=1'), array('--en'), '1'),
  273. array(array('app/console', 'foo:bar', '--env=dev', '', '--en=1'), array('--en'), '1'),
  274. );
  275. }
  276. public function testParseSingleDashAsArgument()
  277. {
  278. $input = new ArgvInput(array('cli.php', '-'));
  279. $input->bind(new InputDefinition(array(new InputArgument('file'))));
  280. $this->assertEquals(array('file' => '-'), $input->getArguments(), '->parse() parses single dash as an argument');
  281. }
  282. }