ArgvInputTest.php 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Input\ArgvInput;
  13. use Symfony\Component\Console\Input\InputDefinition;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputOption;
  16. class ArgvInputTest extends TestCase
  17. {
  18. public function testConstructor()
  19. {
  20. $_SERVER['argv'] = array('cli.php', 'foo');
  21. $input = new ArgvInput();
  22. $r = new \ReflectionObject($input);
  23. $p = $r->getProperty('tokens');
  24. $p->setAccessible(true);
  25. $this->assertEquals(array('foo'), $p->getValue($input), '__construct() automatically get its input from the argv server variable');
  26. }
  27. public function testParseArguments()
  28. {
  29. $input = new ArgvInput(array('cli.php', 'foo'));
  30. $input->bind(new InputDefinition(array(new InputArgument('name'))));
  31. $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() parses required arguments');
  32. $input->bind(new InputDefinition(array(new InputArgument('name'))));
  33. $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() is stateless');
  34. }
  35. /**
  36. * @dataProvider provideOptions
  37. */
  38. public function testParseOptions($input, $options, $expectedOptions, $message)
  39. {
  40. $input = new ArgvInput($input);
  41. $input->bind(new InputDefinition($options));
  42. $this->assertSame($expectedOptions, $input->getOptions(), $message);
  43. }
  44. public function provideOptions()
  45. {
  46. return array(
  47. array(
  48. array('cli.php', '--foo'),
  49. array(new InputOption('foo')),
  50. array('foo' => true),
  51. '->parse() parses long options without a value',
  52. ),
  53. array(
  54. array('cli.php', '--foo=bar'),
  55. array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)),
  56. array('foo' => 'bar'),
  57. '->parse() parses long options with a required value (with a = separator)',
  58. ),
  59. array(
  60. array('cli.php', '--foo', 'bar'),
  61. array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)),
  62. array('foo' => 'bar'),
  63. '->parse() parses long options with a required value (with a space separator)',
  64. ),
  65. array(
  66. array('cli.php', '--foo='),
  67. array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)),
  68. array('foo' => ''),
  69. '->parse() parses long options with optional value which is empty (with a = separator) as empty string',
  70. ),
  71. array(
  72. array('cli.php', '--foo=', 'bar'),
  73. array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED)),
  74. array('foo' => ''),
  75. '->parse() parses long options with optional value without value specified or an empty string (with a = separator) followed by an argument as empty string',
  76. ),
  77. array(
  78. array('cli.php', 'bar', '--foo'),
  79. array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED)),
  80. array('foo' => null),
  81. '->parse() parses long options with optional value which is empty (with a = separator) preceded by an argument',
  82. ),
  83. array(
  84. array('cli.php', '--foo', '', 'bar'),
  85. array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED)),
  86. array('foo' => ''),
  87. '->parse() parses long options with optional value which is empty as empty string even followed by an argument',
  88. ),
  89. array(
  90. array('cli.php', '--foo'),
  91. array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)),
  92. array('foo' => null),
  93. '->parse() parses long options with optional value specified with no separator and no value as null',
  94. ),
  95. array(
  96. array('cli.php', '-f'),
  97. array(new InputOption('foo', 'f')),
  98. array('foo' => true),
  99. '->parse() parses short options without a value',
  100. ),
  101. array(
  102. array('cli.php', '-fbar'),
  103. array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)),
  104. array('foo' => 'bar'),
  105. '->parse() parses short options with a required value (with no separator)',
  106. ),
  107. array(
  108. array('cli.php', '-f', 'bar'),
  109. array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)),
  110. array('foo' => 'bar'),
  111. '->parse() parses short options with a required value (with a space separator)',
  112. ),
  113. array(
  114. array('cli.php', '-f', ''),
  115. array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)),
  116. array('foo' => ''),
  117. '->parse() parses short options with an optional empty value',
  118. ),
  119. array(
  120. array('cli.php', '-f', '', 'foo'),
  121. array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)),
  122. array('foo' => ''),
  123. '->parse() parses short options with an optional empty value followed by an argument',
  124. ),
  125. array(
  126. array('cli.php', '-f', '', '-b'),
  127. array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b')),
  128. array('foo' => '', 'bar' => true),
  129. '->parse() parses short options with an optional empty value followed by an option',
  130. ),
  131. array(
  132. array('cli.php', '-f', '-b', 'foo'),
  133. array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b')),
  134. array('foo' => null, 'bar' => true),
  135. '->parse() parses short options with an optional value which is not present',
  136. ),
  137. array(
  138. array('cli.php', '-fb'),
  139. array(new InputOption('foo', 'f'), new InputOption('bar', 'b')),
  140. array('foo' => true, 'bar' => true),
  141. '->parse() parses short options when they are aggregated as a single one',
  142. ),
  143. array(
  144. array('cli.php', '-fb', 'bar'),
  145. array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_REQUIRED)),
  146. array('foo' => true, 'bar' => 'bar'),
  147. '->parse() parses short options when they are aggregated as a single one and the last one has a required value',
  148. ),
  149. array(
  150. array('cli.php', '-fb', 'bar'),
  151. array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL)),
  152. array('foo' => true, 'bar' => 'bar'),
  153. '->parse() parses short options when they are aggregated as a single one and the last one has an optional value',
  154. ),
  155. array(
  156. array('cli.php', '-fbbar'),
  157. array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL)),
  158. array('foo' => true, 'bar' => 'bar'),
  159. '->parse() parses short options when they are aggregated as a single one and the last one has an optional value with no separator',
  160. ),
  161. array(
  162. array('cli.php', '-fbbar'),
  163. array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL)),
  164. array('foo' => 'bbar', 'bar' => null),
  165. '->parse() parses short options when they are aggregated as a single one and one of them takes a value',
  166. ),
  167. );
  168. }
  169. /**
  170. * @dataProvider provideInvalidInput
  171. */
  172. public function testInvalidInput($argv, $definition, $expectedExceptionMessage)
  173. {
  174. if (method_exists($this, 'expectException')) {
  175. $this->expectException('RuntimeException');
  176. $this->expectExceptionMessage($expectedExceptionMessage);
  177. } else {
  178. $this->setExpectedException('RuntimeException', $expectedExceptionMessage);
  179. }
  180. $input = new ArgvInput($argv);
  181. $input->bind($definition);
  182. }
  183. public function provideInvalidInput()
  184. {
  185. return array(
  186. array(
  187. array('cli.php', '--foo'),
  188. new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))),
  189. 'The "--foo" option requires a value.',
  190. ),
  191. array(
  192. array('cli.php', '-f'),
  193. new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))),
  194. 'The "--foo" option requires a value.',
  195. ),
  196. array(
  197. array('cli.php', '-ffoo'),
  198. new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_NONE))),
  199. 'The "-o" option does not exist.',
  200. ),
  201. array(
  202. array('cli.php', '--foo=bar'),
  203. new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_NONE))),
  204. 'The "--foo" option does not accept a value.',
  205. ),
  206. array(
  207. array('cli.php', 'foo', 'bar'),
  208. new InputDefinition(),
  209. 'No arguments expected, got "foo".',
  210. ),
  211. array(
  212. array('cli.php', 'foo', 'bar'),
  213. new InputDefinition(array(new InputArgument('number'))),
  214. 'Too many arguments, expected arguments "number".',
  215. ),
  216. array(
  217. array('cli.php', 'foo', 'bar', 'zzz'),
  218. new InputDefinition(array(new InputArgument('number'), new InputArgument('county'))),
  219. 'Too many arguments, expected arguments "number" "county".',
  220. ),
  221. array(
  222. array('cli.php', '--foo'),
  223. new InputDefinition(),
  224. 'The "--foo" option does not exist.',
  225. ),
  226. array(
  227. array('cli.php', '-f'),
  228. new InputDefinition(),
  229. 'The "-f" option does not exist.',
  230. ),
  231. array(
  232. array('cli.php', '-1'),
  233. new InputDefinition(array(new InputArgument('number'))),
  234. 'The "-1" option does not exist.',
  235. ),
  236. );
  237. }
  238. public function testParseArrayArgument()
  239. {
  240. $input = new ArgvInput(array('cli.php', 'foo', 'bar', 'baz', 'bat'));
  241. $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::IS_ARRAY))));
  242. $this->assertEquals(array('name' => array('foo', 'bar', 'baz', 'bat')), $input->getArguments(), '->parse() parses array arguments');
  243. }
  244. public function testParseArrayOption()
  245. {
  246. $input = new ArgvInput(array('cli.php', '--name=foo', '--name=bar', '--name=baz'));
  247. $input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
  248. $this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions(), '->parse() parses array options ("--option=value" syntax)');
  249. $input = new ArgvInput(array('cli.php', '--name', 'foo', '--name', 'bar', '--name', 'baz'));
  250. $input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
  251. $this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions(), '->parse() parses array options ("--option value" syntax)');
  252. $input = new ArgvInput(array('cli.php', '--name=foo', '--name=bar', '--name='));
  253. $input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
  254. $this->assertSame(array('name' => array('foo', 'bar', '')), $input->getOptions(), '->parse() parses empty array options as null ("--option=value" syntax)');
  255. $input = new ArgvInput(array('cli.php', '--name', 'foo', '--name', 'bar', '--name', '--anotherOption'));
  256. $input->bind(new InputDefinition(array(
  257. new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY),
  258. new InputOption('anotherOption', null, InputOption::VALUE_NONE),
  259. )));
  260. $this->assertSame(array('name' => array('foo', 'bar', null), 'anotherOption' => true), $input->getOptions(), '->parse() parses empty array options ("--option value" syntax)');
  261. }
  262. public function testParseNegativeNumberAfterDoubleDash()
  263. {
  264. $input = new ArgvInput(array('cli.php', '--', '-1'));
  265. $input->bind(new InputDefinition(array(new InputArgument('number'))));
  266. $this->assertEquals(array('number' => '-1'), $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence');
  267. $input = new ArgvInput(array('cli.php', '-f', 'bar', '--', '-1'));
  268. $input->bind(new InputDefinition(array(new InputArgument('number'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
  269. $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses arguments with leading dashes as options before having encountered a double-dash sequence');
  270. $this->assertEquals(array('number' => '-1'), $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence');
  271. }
  272. public function testParseEmptyStringArgument()
  273. {
  274. $input = new ArgvInput(array('cli.php', '-f', 'bar', ''));
  275. $input->bind(new InputDefinition(array(new InputArgument('empty'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
  276. $this->assertEquals(array('empty' => ''), $input->getArguments(), '->parse() parses empty string arguments');
  277. }
  278. public function testGetFirstArgument()
  279. {
  280. $input = new ArgvInput(array('cli.php', '-fbbar'));
  281. $this->assertNull($input->getFirstArgument(), '->getFirstArgument() returns null when there is no arguments');
  282. $input = new ArgvInput(array('cli.php', '-fbbar', 'foo'));
  283. $this->assertEquals('foo', $input->getFirstArgument(), '->getFirstArgument() returns the first argument from the raw input');
  284. }
  285. public function testHasParameterOption()
  286. {
  287. $input = new ArgvInput(array('cli.php', '-f', 'foo'));
  288. $this->assertTrue($input->hasParameterOption('-f'), '->hasParameterOption() returns true if the given short option is in the raw input');
  289. $input = new ArgvInput(array('cli.php', '-etest'));
  290. $this->assertTrue($input->hasParameterOption('-e'), '->hasParameterOption() returns true if the given short option is in the raw input');
  291. $this->assertFalse($input->hasParameterOption('-s'), '->hasParameterOption() returns true if the given short option is in the raw input');
  292. $input = new ArgvInput(array('cli.php', '--foo', 'foo'));
  293. $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if the given short option is in the raw input');
  294. $input = new ArgvInput(array('cli.php', 'foo'));
  295. $this->assertFalse($input->hasParameterOption('--foo'), '->hasParameterOption() returns false if the given short option is not in the raw input');
  296. $input = new ArgvInput(array('cli.php', '--foo=bar'));
  297. $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if the given option with provided value is in the raw input');
  298. }
  299. public function testHasParameterOptionOnlyOptions()
  300. {
  301. $input = new ArgvInput(array('cli.php', '-f', 'foo'));
  302. $this->assertTrue($input->hasParameterOption('-f', true), '->hasParameterOption() returns true if the given short option is in the raw input');
  303. $input = new ArgvInput(array('cli.php', '--foo', '--', 'foo'));
  304. $this->assertTrue($input->hasParameterOption('--foo', true), '->hasParameterOption() returns true if the given long option is in the raw input');
  305. $input = new ArgvInput(array('cli.php', '--foo=bar', 'foo'));
  306. $this->assertTrue($input->hasParameterOption('--foo', true), '->hasParameterOption() returns true if the given long option with provided value is in the raw input');
  307. $input = new ArgvInput(array('cli.php', '--', '--foo'));
  308. $this->assertFalse($input->hasParameterOption('--foo', true), '->hasParameterOption() returns false if the given option is in the raw input but after an end of options signal');
  309. }
  310. public function testHasParameterOptionEdgeCasesAndLimitations()
  311. {
  312. $input = new ArgvInput(array('cli.php', '-fh'));
  313. // hasParameterOption does not know if the previous short option, -f,
  314. // takes a value or not. If -f takes a value, then -fh does NOT include
  315. // -h; Otherwise it does. Since we do not know which short options take
  316. // values, hasParameterOption does not support this use-case.
  317. $this->assertFalse($input->hasParameterOption('-h'), '->hasParameterOption() returns true if the given short option is in the raw input');
  318. // hasParameterOption does detect that `-fh` contains `-f`, since
  319. // `-f` is the first short option in the set.
  320. $this->assertTrue($input->hasParameterOption('-f'), '->hasParameterOption() returns true if the given short option is in the raw input');
  321. // The test below happens to pass, although it might make more sense
  322. // to disallow it, and require the use of
  323. // $input->hasParameterOption('-f') && $input->hasParameterOption('-h')
  324. // instead.
  325. $this->assertTrue($input->hasParameterOption('-fh'), '->hasParameterOption() returns true if the given short option is in the raw input');
  326. // In theory, if -fh is supported, then -hf should also work.
  327. // However, this is not supported.
  328. $this->assertFalse($input->hasParameterOption('-hf'), '->hasParameterOption() returns true if the given short option is in the raw input');
  329. $input = new ArgvInput(array('cli.php', '-f', '-h'));
  330. // If hasParameterOption('-fh') is supported for 'cli.php -fh', then
  331. // one might also expect that it should also be supported for
  332. // 'cli.php -f -h'. However, this is not supported.
  333. $this->assertFalse($input->hasParameterOption('-fh'), '->hasParameterOption() returns true if the given short option is in the raw input');
  334. }
  335. public function testNoWarningOnInvalidParameterOption()
  336. {
  337. $input = new ArgvInput(array('cli.php', '-edev'));
  338. $this->assertTrue($input->hasParameterOption(array('-e', '')));
  339. // No warning thrown
  340. $this->assertFalse($input->hasParameterOption(array('-m', '')));
  341. $this->assertEquals('dev', $input->getParameterOption(array('-e', '')));
  342. // No warning thrown
  343. $this->assertFalse($input->getParameterOption(array('-m', '')));
  344. }
  345. public function testToString()
  346. {
  347. $input = new ArgvInput(array('cli.php', '-f', 'foo'));
  348. $this->assertEquals('-f foo', (string) $input);
  349. $input = new ArgvInput(array('cli.php', '-f', '--bar=foo', 'a b c d', "A\nB'C"));
  350. $this->assertEquals('-f --bar=foo '.escapeshellarg('a b c d').' '.escapeshellarg("A\nB'C"), (string) $input);
  351. }
  352. /**
  353. * @dataProvider provideGetParameterOptionValues
  354. */
  355. public function testGetParameterOptionEqualSign($argv, $key, $onlyParams, $expected)
  356. {
  357. $input = new ArgvInput($argv);
  358. $this->assertEquals($expected, $input->getParameterOption($key, false, $onlyParams), '->getParameterOption() returns the expected value');
  359. }
  360. public function provideGetParameterOptionValues()
  361. {
  362. return array(
  363. array(array('app/console', 'foo:bar', '-e', 'dev'), '-e', false, 'dev'),
  364. array(array('app/console', 'foo:bar', '--env=dev'), '--env', false, 'dev'),
  365. array(array('app/console', 'foo:bar', '-e', 'dev'), array('-e', '--env'), false, 'dev'),
  366. array(array('app/console', 'foo:bar', '--env=dev'), array('-e', '--env'), false, 'dev'),
  367. array(array('app/console', 'foo:bar', '--env=dev', '--en=1'), array('--en'), false, '1'),
  368. array(array('app/console', 'foo:bar', '--env=dev', '', '--en=1'), array('--en'), false, '1'),
  369. array(array('app/console', 'foo:bar', '--env', 'val'), '--env', false, 'val'),
  370. array(array('app/console', 'foo:bar', '--env', 'val', '--dummy'), '--env', false, 'val'),
  371. array(array('app/console', 'foo:bar', '--', '--env=dev'), '--env', false, 'dev'),
  372. array(array('app/console', 'foo:bar', '--', '--env=dev'), '--env', true, false),
  373. );
  374. }
  375. public function testParseSingleDashAsArgument()
  376. {
  377. $input = new ArgvInput(array('cli.php', '-'));
  378. $input->bind(new InputDefinition(array(new InputArgument('file'))));
  379. $this->assertEquals(array('file' => '-'), $input->getArguments(), '->parse() parses single dash as an argument');
  380. }
  381. public function testParseOptionWithValueOptionalGivenEmptyAndRequiredArgument()
  382. {
  383. $input = new ArgvInput(array('cli.php', '--foo=', 'bar'));
  384. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED))));
  385. $this->assertEquals(array('foo' => null), $input->getOptions(), '->parse() parses optional options with empty value as null');
  386. $this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->parse() parses required arguments');
  387. $input = new ArgvInput(array('cli.php', '--foo=0', 'bar'));
  388. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED))));
  389. $this->assertEquals(array('foo' => '0'), $input->getOptions(), '->parse() parses optional options with empty value as null');
  390. $this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->parse() parses required arguments');
  391. }
  392. public function testParseOptionWithValueOptionalGivenEmptyAndOptionalArgument()
  393. {
  394. $input = new ArgvInput(array('cli.php', '--foo=', 'bar'));
  395. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::OPTIONAL))));
  396. $this->assertEquals(array('foo' => null), $input->getOptions(), '->parse() parses optional options with empty value as null');
  397. $this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->parse() parses optional arguments');
  398. $input = new ArgvInput(array('cli.php', '--foo=0', 'bar'));
  399. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::OPTIONAL))));
  400. $this->assertEquals(array('foo' => '0'), $input->getOptions(), '->parse() parses optional options with empty value as null');
  401. $this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->parse() parses optional arguments');
  402. }
  403. }