ArgvInputTest.php 21 KB

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