ArgvInputTest.php 23 KB

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