ArgvInputTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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', '--foo', 'foo'));
  290. $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if the given short option is in the raw input');
  291. $input = new ArgvInput(array('cli.php', 'foo'));
  292. $this->assertFalse($input->hasParameterOption('--foo'), '->hasParameterOption() returns false if the given short option is not in the raw input');
  293. $input = new ArgvInput(array('cli.php', '--foo=bar'));
  294. $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if the given option with provided value is in the raw input');
  295. }
  296. public function testHasParameterOptionOnlyOptions()
  297. {
  298. $input = new ArgvInput(array('cli.php', '-f', 'foo'));
  299. $this->assertTrue($input->hasParameterOption('-f', true), '->hasParameterOption() returns true if the given short option is in the raw input');
  300. $input = new ArgvInput(array('cli.php', '--foo', '--', 'foo'));
  301. $this->assertTrue($input->hasParameterOption('--foo', true), '->hasParameterOption() returns true if the given long option is in the raw input');
  302. $input = new ArgvInput(array('cli.php', '--foo=bar', 'foo'));
  303. $this->assertTrue($input->hasParameterOption('--foo', true), '->hasParameterOption() returns true if the given long option with provided value is in the raw input');
  304. $input = new ArgvInput(array('cli.php', '--', '--foo'));
  305. $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');
  306. }
  307. public function testToString()
  308. {
  309. $input = new ArgvInput(array('cli.php', '-f', 'foo'));
  310. $this->assertEquals('-f foo', (string) $input);
  311. $input = new ArgvInput(array('cli.php', '-f', '--bar=foo', 'a b c d', "A\nB'C"));
  312. $this->assertEquals('-f --bar=foo '.escapeshellarg('a b c d').' '.escapeshellarg("A\nB'C"), (string) $input);
  313. }
  314. /**
  315. * @dataProvider provideGetParameterOptionValues
  316. */
  317. public function testGetParameterOptionEqualSign($argv, $key, $onlyParams, $expected)
  318. {
  319. $input = new ArgvInput($argv);
  320. $this->assertEquals($expected, $input->getParameterOption($key, false, $onlyParams), '->getParameterOption() returns the expected value');
  321. }
  322. public function provideGetParameterOptionValues()
  323. {
  324. return array(
  325. array(array('app/console', 'foo:bar', '-e', 'dev'), '-e', false, 'dev'),
  326. array(array('app/console', 'foo:bar', '--env=dev'), '--env', false, 'dev'),
  327. array(array('app/console', 'foo:bar', '-e', 'dev'), array('-e', '--env'), false, 'dev'),
  328. array(array('app/console', 'foo:bar', '--env=dev'), array('-e', '--env'), false, 'dev'),
  329. array(array('app/console', 'foo:bar', '--env=dev', '--en=1'), array('--en'), false, '1'),
  330. array(array('app/console', 'foo:bar', '--env=dev', '', '--en=1'), array('--en'), false, '1'),
  331. array(array('app/console', 'foo:bar', '--env', 'val'), '--env', false, 'val'),
  332. array(array('app/console', 'foo:bar', '--env', 'val', '--dummy'), '--env', false, 'val'),
  333. array(array('app/console', 'foo:bar', '--', '--env=dev'), '--env', false, 'dev'),
  334. array(array('app/console', 'foo:bar', '--', '--env=dev'), '--env', true, false),
  335. );
  336. }
  337. public function testParseSingleDashAsArgument()
  338. {
  339. $input = new ArgvInput(array('cli.php', '-'));
  340. $input->bind(new InputDefinition(array(new InputArgument('file'))));
  341. $this->assertEquals(array('file' => '-'), $input->getArguments(), '->parse() parses single dash as an argument');
  342. }
  343. public function testParseOptionWithValueOptionalGivenEmptyAndRequiredArgument()
  344. {
  345. $input = new ArgvInput(array('cli.php', '--foo=', 'bar'));
  346. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED))));
  347. $this->assertEquals(array('foo' => null), $input->getOptions(), '->parse() parses optional options with empty value as null');
  348. $this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->parse() parses required arguments');
  349. $input = new ArgvInput(array('cli.php', '--foo=0', 'bar'));
  350. $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::REQUIRED))));
  351. $this->assertEquals(array('foo' => '0'), $input->getOptions(), '->parse() parses optional options with empty value as null');
  352. $this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->parse() parses required arguments');
  353. }
  354. public function testParseOptionWithValueOptionalGivenEmptyAndOptionalArgument()
  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::OPTIONAL))));
  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 optional 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::OPTIONAL))));
  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 optional arguments');
  364. }
  365. }