ArgvInputTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. /**
  45. * @dataProvider provideNegatableOptions
  46. */
  47. public function testParseOptionsNegatable($input, $options, $expectedOptions, $message)
  48. {
  49. $input = new ArgvInput($input);
  50. $input->bind(new InputDefinition($options));
  51. $this->assertEquals($expectedOptions, $input->getOptions(), $message);
  52. }
  53. public static function provideOptions()
  54. {
  55. return [
  56. [
  57. ['cli.php', '--foo'],
  58. [new InputOption('foo')],
  59. ['foo' => true],
  60. '->parse() parses long options without a value',
  61. ],
  62. [
  63. ['cli.php', '--foo=bar'],
  64. [new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)],
  65. ['foo' => 'bar'],
  66. '->parse() parses long options with a required value (with a = separator)',
  67. ],
  68. [
  69. ['cli.php', '--foo', 'bar'],
  70. [new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)],
  71. ['foo' => 'bar'],
  72. '->parse() parses long options with a required value (with a space separator)',
  73. ],
  74. [
  75. ['cli.php', '--foo='],
  76. [new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)],
  77. ['foo' => ''],
  78. '->parse() parses long options with optional value which is empty (with a = separator) as empty string',
  79. ],
  80. [
  81. ['cli.php', '--foo=', 'bar'],
  82. [new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED)],
  83. ['foo' => ''],
  84. '->parse() parses long options with optional value without value specified or an empty string (with a = separator) followed by an argument as empty string',
  85. ],
  86. [
  87. ['cli.php', 'bar', '--foo'],
  88. [new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED)],
  89. ['foo' => null],
  90. '->parse() parses long options with optional value which is empty (with a = separator) preceded by an argument',
  91. ],
  92. [
  93. ['cli.php', '--foo', '', 'bar'],
  94. [new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED)],
  95. ['foo' => ''],
  96. '->parse() parses long options with optional value which is empty as empty string even followed by an argument',
  97. ],
  98. [
  99. ['cli.php', '--foo'],
  100. [new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)],
  101. ['foo' => null],
  102. '->parse() parses long options with optional value specified with no separator and no value as null',
  103. ],
  104. [
  105. ['cli.php', '-f'],
  106. [new InputOption('foo', 'f')],
  107. ['foo' => true],
  108. '->parse() parses short options without a value',
  109. ],
  110. [
  111. ['cli.php', '-fbar'],
  112. [new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)],
  113. ['foo' => 'bar'],
  114. '->parse() parses short options with a required value (with no separator)',
  115. ],
  116. [
  117. ['cli.php', '-f', 'bar'],
  118. [new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)],
  119. ['foo' => 'bar'],
  120. '->parse() parses short options with a required value (with a space separator)',
  121. ],
  122. [
  123. ['cli.php', '-f', ''],
  124. [new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)],
  125. ['foo' => ''],
  126. '->parse() parses short options with an optional empty value',
  127. ],
  128. [
  129. ['cli.php', '-f', '', 'foo'],
  130. [new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)],
  131. ['foo' => ''],
  132. '->parse() parses short options with an optional empty value followed by an argument',
  133. ],
  134. [
  135. ['cli.php', '-f', '', '-b'],
  136. [new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b')],
  137. ['foo' => '', 'bar' => true],
  138. '->parse() parses short options with an optional empty value followed by an option',
  139. ],
  140. [
  141. ['cli.php', '-f', '-b', 'foo'],
  142. [new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b')],
  143. ['foo' => null, 'bar' => true],
  144. '->parse() parses short options with an optional value which is not present',
  145. ],
  146. [
  147. ['cli.php', '-fb'],
  148. [new InputOption('foo', 'f'), new InputOption('bar', 'b')],
  149. ['foo' => true, 'bar' => true],
  150. '->parse() parses short options when they are aggregated as a single one',
  151. ],
  152. [
  153. ['cli.php', '-fb', 'bar'],
  154. [new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_REQUIRED)],
  155. ['foo' => true, 'bar' => 'bar'],
  156. '->parse() parses short options when they are aggregated as a single one and the last one has a required value',
  157. ],
  158. [
  159. ['cli.php', '-fb', 'bar'],
  160. [new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL)],
  161. ['foo' => true, 'bar' => 'bar'],
  162. '->parse() parses short options when they are aggregated as a single one and the last one has an optional value',
  163. ],
  164. [
  165. ['cli.php', '-fbbar'],
  166. [new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL)],
  167. ['foo' => true, 'bar' => 'bar'],
  168. '->parse() parses short options when they are aggregated as a single one and the last one has an optional value with no separator',
  169. ],
  170. [
  171. ['cli.php', '-fbbar'],
  172. [new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL)],
  173. ['foo' => 'bbar', 'bar' => null],
  174. '->parse() parses short options when they are aggregated as a single one and one of them takes a value',
  175. ],
  176. ];
  177. }
  178. public static function provideNegatableOptions()
  179. {
  180. return [
  181. [
  182. ['cli.php', '--foo'],
  183. [new InputOption('foo', null, InputOption::VALUE_NEGATABLE)],
  184. ['foo' => true],
  185. '->parse() parses long options without a value',
  186. ],
  187. [
  188. ['cli.php', '--foo'],
  189. [new InputOption('foo', null, InputOption::VALUE_NONE | InputOption::VALUE_NEGATABLE)],
  190. ['foo' => true],
  191. '->parse() parses long options without a value',
  192. ],
  193. [
  194. ['cli.php', '--no-foo'],
  195. [new InputOption('foo', null, InputOption::VALUE_NEGATABLE)],
  196. ['foo' => false],
  197. '->parse() parses long options without a value',
  198. ],
  199. [
  200. ['cli.php', '--no-foo'],
  201. [new InputOption('foo', null, InputOption::VALUE_NONE | InputOption::VALUE_NEGATABLE)],
  202. ['foo' => false],
  203. '->parse() parses long options without a value',
  204. ],
  205. [
  206. ['cli.php'],
  207. [new InputOption('foo', null, InputOption::VALUE_NEGATABLE)],
  208. ['foo' => null],
  209. '->parse() parses long options without a value',
  210. ],
  211. [
  212. ['cli.php'],
  213. [new InputOption('foo', null, InputOption::VALUE_NONE | InputOption::VALUE_NEGATABLE)],
  214. ['foo' => null],
  215. '->parse() parses long options without a value',
  216. ],
  217. [
  218. ['cli.php'],
  219. [new InputOption('foo', null, InputOption::VALUE_NEGATABLE, '', false)],
  220. ['foo' => false],
  221. '->parse() parses long options without a value',
  222. ],
  223. ];
  224. }
  225. /**
  226. * @dataProvider provideInvalidInput
  227. */
  228. public function testInvalidInput($argv, $definition, $expectedExceptionMessage)
  229. {
  230. $this->expectException(\RuntimeException::class);
  231. $this->expectExceptionMessage($expectedExceptionMessage);
  232. $input = new ArgvInput($argv);
  233. $input->bind($definition);
  234. }
  235. /**
  236. * @dataProvider provideInvalidNegatableInput
  237. */
  238. public function testInvalidInputNegatable($argv, $definition, $expectedExceptionMessage)
  239. {
  240. $this->expectException(\RuntimeException::class);
  241. $this->expectExceptionMessage($expectedExceptionMessage);
  242. $input = new ArgvInput($argv);
  243. $input->bind($definition);
  244. }
  245. public static function provideInvalidInput()
  246. {
  247. return [
  248. [
  249. ['cli.php', '--foo'],
  250. new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)]),
  251. 'The "--foo" option requires a value.',
  252. ],
  253. [
  254. ['cli.php', '-f'],
  255. new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_REQUIRED)]),
  256. 'The "--foo" option requires a value.',
  257. ],
  258. [
  259. ['cli.php', '-ffoo'],
  260. new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_NONE)]),
  261. 'The "-o" option does not exist.',
  262. ],
  263. [
  264. ['cli.php', '--foo=bar'],
  265. new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_NONE)]),
  266. 'The "--foo" option does not accept a value.',
  267. ],
  268. [
  269. ['cli.php', 'foo', 'bar'],
  270. new InputDefinition(),
  271. 'No arguments expected, got "foo".',
  272. ],
  273. [
  274. ['cli.php', 'foo', 'bar'],
  275. new InputDefinition([new InputArgument('number')]),
  276. 'Too many arguments, expected arguments "number".',
  277. ],
  278. [
  279. ['cli.php', 'foo', 'bar', 'zzz'],
  280. new InputDefinition([new InputArgument('number'), new InputArgument('county')]),
  281. 'Too many arguments, expected arguments "number" "county".',
  282. ],
  283. [
  284. ['cli.php', '--foo'],
  285. new InputDefinition(),
  286. 'The "--foo" option does not exist.',
  287. ],
  288. [
  289. ['cli.php', '-f'],
  290. new InputDefinition(),
  291. 'The "-f" option does not exist.',
  292. ],
  293. [
  294. ['cli.php', '-1'],
  295. new InputDefinition([new InputArgument('number')]),
  296. 'The "-1" option does not exist.',
  297. ],
  298. [
  299. ['cli.php', '-fЩ'],
  300. new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_NONE)]),
  301. 'The "-Щ" option does not exist.',
  302. ],
  303. [
  304. ['cli.php', 'acme:foo', 'bar'],
  305. new InputDefinition([new InputArgument('command', InputArgument::REQUIRED)]),
  306. 'No arguments expected for "acme:foo" command, got "bar"',
  307. ],
  308. [
  309. ['cli.php', 'acme:foo', 'bar'],
  310. new InputDefinition([new InputArgument('name', InputArgument::REQUIRED)]),
  311. 'Too many arguments, expected arguments "name".',
  312. ],
  313. ];
  314. }
  315. public static function provideInvalidNegatableInput()
  316. {
  317. return [
  318. [
  319. ['cli.php', '--no-foo=bar'],
  320. new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_NEGATABLE)]),
  321. 'The "--no-foo" option does not accept a value.',
  322. ],
  323. [
  324. ['cli.php', '--no-foo='],
  325. new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_NEGATABLE)]),
  326. 'The "--no-foo" option does not accept a value.',
  327. ],
  328. [
  329. ['cli.php', '--no-foo=bar'],
  330. new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_NONE | InputOption::VALUE_NEGATABLE)]),
  331. 'The "--no-foo" option does not accept a value.',
  332. ],
  333. [
  334. ['cli.php', '--no-foo='],
  335. new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_NONE | InputOption::VALUE_NEGATABLE)]),
  336. 'The "--no-foo" option does not accept a value.',
  337. ],
  338. ];
  339. }
  340. public function testParseArrayArgument()
  341. {
  342. $input = new ArgvInput(['cli.php', 'foo', 'bar', 'baz', 'bat']);
  343. $input->bind(new InputDefinition([new InputArgument('name', InputArgument::IS_ARRAY)]));
  344. $this->assertEquals(['name' => ['foo', 'bar', 'baz', 'bat']], $input->getArguments(), '->parse() parses array arguments');
  345. }
  346. public function testParseArrayOption()
  347. {
  348. $input = new ArgvInput(['cli.php', '--name=foo', '--name=bar', '--name=baz']);
  349. $input->bind(new InputDefinition([new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY)]));
  350. $this->assertEquals(['name' => ['foo', 'bar', 'baz']], $input->getOptions(), '->parse() parses array options ("--option=value" syntax)');
  351. $input = new ArgvInput(['cli.php', '--name', 'foo', '--name', 'bar', '--name', 'baz']);
  352. $input->bind(new InputDefinition([new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY)]));
  353. $this->assertEquals(['name' => ['foo', 'bar', 'baz']], $input->getOptions(), '->parse() parses array options ("--option value" syntax)');
  354. $input = new ArgvInput(['cli.php', '--name=foo', '--name=bar', '--name=']);
  355. $input->bind(new InputDefinition([new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY)]));
  356. $this->assertSame(['name' => ['foo', 'bar', '']], $input->getOptions(), '->parse() parses empty array options as null ("--option=value" syntax)');
  357. $input = new ArgvInput(['cli.php', '--name', 'foo', '--name', 'bar', '--name', '--anotherOption']);
  358. $input->bind(new InputDefinition([
  359. new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY),
  360. new InputOption('anotherOption', null, InputOption::VALUE_NONE),
  361. ]));
  362. $this->assertSame(['name' => ['foo', 'bar', null], 'anotherOption' => true], $input->getOptions(), '->parse() parses empty array options ("--option value" syntax)');
  363. }
  364. public function testParseNegativeNumberAfterDoubleDash()
  365. {
  366. $input = new ArgvInput(['cli.php', '--', '-1']);
  367. $input->bind(new InputDefinition([new InputArgument('number')]));
  368. $this->assertEquals(['number' => '-1'], $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence');
  369. $input = new ArgvInput(['cli.php', '-f', 'bar', '--', '-1']);
  370. $input->bind(new InputDefinition([new InputArgument('number'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)]));
  371. $this->assertEquals(['foo' => 'bar'], $input->getOptions(), '->parse() parses arguments with leading dashes as options before having encountered a double-dash sequence');
  372. $this->assertEquals(['number' => '-1'], $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence');
  373. }
  374. public function testParseEmptyStringArgument()
  375. {
  376. $input = new ArgvInput(['cli.php', '-f', 'bar', '']);
  377. $input->bind(new InputDefinition([new InputArgument('empty'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL)]));
  378. $this->assertEquals(['empty' => ''], $input->getArguments(), '->parse() parses empty string arguments');
  379. }
  380. public function testGetFirstArgument()
  381. {
  382. $input = new ArgvInput(['cli.php', '-fbbar']);
  383. $this->assertNull($input->getFirstArgument(), '->getFirstArgument() returns null when there is no arguments');
  384. $input = new ArgvInput(['cli.php', '-fbbar', 'foo']);
  385. $this->assertEquals('foo', $input->getFirstArgument(), '->getFirstArgument() returns the first argument from the raw input');
  386. $input = new ArgvInput(['cli.php', '--foo', 'fooval', 'bar']);
  387. $input->bind(new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('arg')]));
  388. $this->assertSame('bar', $input->getFirstArgument());
  389. $input = new ArgvInput(['cli.php', '-bf', 'fooval', 'argval']);
  390. $input->bind(new InputDefinition([new InputOption('bar', 'b', InputOption::VALUE_NONE), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('arg')]));
  391. $this->assertSame('argval', $input->getFirstArgument());
  392. }
  393. public function testHasParameterOption()
  394. {
  395. $input = new ArgvInput(['cli.php', '-f', 'foo']);
  396. $this->assertTrue($input->hasParameterOption('-f'), '->hasParameterOption() returns true if the given short option is in the raw input');
  397. $input = new ArgvInput(['cli.php', '-etest']);
  398. $this->assertTrue($input->hasParameterOption('-e'), '->hasParameterOption() returns true if the given short option is in the raw input');
  399. $this->assertFalse($input->hasParameterOption('-s'), '->hasParameterOption() returns true if the given short option is in the raw input');
  400. $input = new ArgvInput(['cli.php', '--foo', 'foo']);
  401. $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if the given short option is in the raw input');
  402. $input = new ArgvInput(['cli.php', 'foo']);
  403. $this->assertFalse($input->hasParameterOption('--foo'), '->hasParameterOption() returns false if the given short option is not in the raw input');
  404. $input = new ArgvInput(['cli.php', '--foo=bar']);
  405. $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if the given option with provided value is in the raw input');
  406. }
  407. public function testHasParameterOptionOnlyOptions()
  408. {
  409. $input = new ArgvInput(['cli.php', '-f', 'foo']);
  410. $this->assertTrue($input->hasParameterOption('-f', true), '->hasParameterOption() returns true if the given short option is in the raw input');
  411. $input = new ArgvInput(['cli.php', '--foo', '--', 'foo']);
  412. $this->assertTrue($input->hasParameterOption('--foo', true), '->hasParameterOption() returns true if the given long option is in the raw input');
  413. $input = new ArgvInput(['cli.php', '--foo=bar', 'foo']);
  414. $this->assertTrue($input->hasParameterOption('--foo', true), '->hasParameterOption() returns true if the given long option with provided value is in the raw input');
  415. $input = new ArgvInput(['cli.php', '--', '--foo']);
  416. $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');
  417. }
  418. public function testHasParameterOptionEdgeCasesAndLimitations()
  419. {
  420. $input = new ArgvInput(['cli.php', '-fh']);
  421. // hasParameterOption does not know if the previous short option, -f,
  422. // takes a value or not. If -f takes a value, then -fh does NOT include
  423. // -h; Otherwise it does. Since we do not know which short options take
  424. // values, hasParameterOption does not support this use-case.
  425. $this->assertFalse($input->hasParameterOption('-h'), '->hasParameterOption() returns true if the given short option is in the raw input');
  426. // hasParameterOption does detect that `-fh` contains `-f`, since
  427. // `-f` is the first short option in the set.
  428. $this->assertTrue($input->hasParameterOption('-f'), '->hasParameterOption() returns true if the given short option is in the raw input');
  429. // The test below happens to pass, although it might make more sense
  430. // to disallow it, and require the use of
  431. // $input->hasParameterOption('-f') && $input->hasParameterOption('-h')
  432. // instead.
  433. $this->assertTrue($input->hasParameterOption('-fh'), '->hasParameterOption() returns true if the given short option is in the raw input');
  434. // In theory, if -fh is supported, then -hf should also work.
  435. // However, this is not supported.
  436. $this->assertFalse($input->hasParameterOption('-hf'), '->hasParameterOption() returns true if the given short option is in the raw input');
  437. $input = new ArgvInput(['cli.php', '-f', '-h']);
  438. // If hasParameterOption('-fh') is supported for 'cli.php -fh', then
  439. // one might also expect that it should also be supported for
  440. // 'cli.php -f -h'. However, this is not supported.
  441. $this->assertFalse($input->hasParameterOption('-fh'), '->hasParameterOption() returns true if the given short option is in the raw input');
  442. }
  443. public function testNoWarningOnInvalidParameterOption()
  444. {
  445. $input = new ArgvInput(['cli.php', '-edev']);
  446. $this->assertTrue($input->hasParameterOption(['-e', '']));
  447. // No warning thrown
  448. $this->assertFalse($input->hasParameterOption(['-m', '']));
  449. $this->assertEquals('dev', $input->getParameterOption(['-e', '']));
  450. // No warning thrown
  451. $this->assertFalse($input->getParameterOption(['-m', '']));
  452. }
  453. public function testToString()
  454. {
  455. $input = new ArgvInput(['cli.php', '-f', 'foo']);
  456. $this->assertEquals('-f foo', (string) $input);
  457. $input = new ArgvInput(['cli.php', '-f', '--bar=foo', 'a b c d', "A\nB'C"]);
  458. $this->assertEquals('-f --bar=foo '.escapeshellarg('a b c d').' '.escapeshellarg("A\nB'C"), (string) $input);
  459. }
  460. /**
  461. * @dataProvider provideGetParameterOptionValues
  462. */
  463. public function testGetParameterOptionEqualSign($argv, $key, $default, $onlyParams, $expected)
  464. {
  465. $input = new ArgvInput($argv);
  466. $this->assertEquals($expected, $input->getParameterOption($key, $default, $onlyParams), '->getParameterOption() returns the expected value');
  467. }
  468. public static function provideGetParameterOptionValues()
  469. {
  470. return [
  471. [['app/console', 'foo:bar'], '-e', 'default', false, 'default'],
  472. [['app/console', 'foo:bar', '-e', 'dev'], '-e', 'default', false, 'dev'],
  473. [['app/console', 'foo:bar', '--env=dev'], '--env', 'default', false, 'dev'],
  474. [['app/console', 'foo:bar', '-e', 'dev'], ['-e', '--env'], 'default', false, 'dev'],
  475. [['app/console', 'foo:bar', '--env=dev'], ['-e', '--env'], 'default', false, 'dev'],
  476. [['app/console', 'foo:bar', '--env=dev', '--en=1'], ['--en'], 'default', false, '1'],
  477. [['app/console', 'foo:bar', '--env=dev', '', '--en=1'], ['--en'], 'default', false, '1'],
  478. [['app/console', 'foo:bar', '--env', 'val'], '--env', 'default', false, 'val'],
  479. [['app/console', 'foo:bar', '--env', 'val', '--dummy'], '--env', 'default', false, 'val'],
  480. [['app/console', 'foo:bar', '--', '--env=dev'], '--env', 'default', false, 'dev'],
  481. [['app/console', 'foo:bar', '--', '--env=dev'], '--env', 'default', true, 'default'],
  482. ];
  483. }
  484. public function testParseSingleDashAsArgument()
  485. {
  486. $input = new ArgvInput(['cli.php', '-']);
  487. $input->bind(new InputDefinition([new InputArgument('file')]));
  488. $this->assertEquals(['file' => '-'], $input->getArguments(), '->parse() parses single dash as an argument');
  489. }
  490. public function testParseOptionWithValueOptionalGivenEmptyAndRequiredArgument()
  491. {
  492. $input = new ArgvInput(['cli.php', '--foo=', 'bar']);
  493. $input->bind(new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED)]));
  494. $this->assertEquals(['foo' => null], $input->getOptions(), '->parse() parses optional options with empty value as null');
  495. $this->assertEquals(['name' => 'bar'], $input->getArguments(), '->parse() parses required arguments');
  496. $input = new ArgvInput(['cli.php', '--foo=0', 'bar']);
  497. $input->bind(new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED)]));
  498. $this->assertEquals(['foo' => '0'], $input->getOptions(), '->parse() parses optional options with empty value as null');
  499. $this->assertEquals(['name' => 'bar'], $input->getArguments(), '->parse() parses required arguments');
  500. }
  501. public function testParseOptionWithValueOptionalGivenEmptyAndOptionalArgument()
  502. {
  503. $input = new ArgvInput(['cli.php', '--foo=', 'bar']);
  504. $input->bind(new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::OPTIONAL)]));
  505. $this->assertEquals(['foo' => null], $input->getOptions(), '->parse() parses optional options with empty value as null');
  506. $this->assertEquals(['name' => 'bar'], $input->getArguments(), '->parse() parses optional arguments');
  507. $input = new ArgvInput(['cli.php', '--foo=0', 'bar']);
  508. $input->bind(new InputDefinition([new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::OPTIONAL)]));
  509. $this->assertEquals(['foo' => '0'], $input->getOptions(), '->parse() parses optional options with empty value as null');
  510. $this->assertEquals(['name' => 'bar'], $input->getArguments(), '->parse() parses optional arguments');
  511. }
  512. }