ConsoleOptionParserTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Console;
  16. use Cake\Console\ConsoleInputArgument;
  17. use Cake\Console\ConsoleInputOption;
  18. use Cake\Console\ConsoleInputSubcommand;
  19. use Cake\Console\ConsoleOptionParser;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * ConsoleOptionParserTest
  23. */
  24. class ConsoleOptionParserTest extends TestCase
  25. {
  26. /**
  27. * test setting the console description
  28. *
  29. * @return void
  30. */
  31. public function testDescription()
  32. {
  33. $parser = new ConsoleOptionParser('test', false);
  34. $result = $parser->description('A test');
  35. $this->assertEquals($parser, $result, 'Setting description is not chainable');
  36. $this->assertEquals('A test', $parser->description(), 'getting value is wrong.');
  37. $result = $parser->description(['A test', 'something']);
  38. $this->assertEquals("A test\nsomething", $parser->description(), 'getting value is wrong.');
  39. }
  40. /**
  41. * test setting the console epilog
  42. *
  43. * @return void
  44. */
  45. public function testEpilog()
  46. {
  47. $parser = new ConsoleOptionParser('test', false);
  48. $result = $parser->epilog('A test');
  49. $this->assertEquals($parser, $result, 'Setting epilog is not chainable');
  50. $this->assertEquals('A test', $parser->epilog(), 'getting value is wrong.');
  51. $result = $parser->epilog(['A test', 'something']);
  52. $this->assertEquals("A test\nsomething", $parser->epilog(), 'getting value is wrong.');
  53. }
  54. /**
  55. * test adding an option returns self.
  56. *
  57. * @return void
  58. */
  59. public function testAddOptionReturnSelf()
  60. {
  61. $parser = new ConsoleOptionParser('test', false);
  62. $result = $parser->addOption('test');
  63. $this->assertEquals($parser, $result, 'Did not return $this from addOption');
  64. }
  65. /**
  66. * test removing an option
  67. *
  68. * @return void
  69. */
  70. public function testRemoveOption()
  71. {
  72. $parser = new ConsoleOptionParser('test', false);
  73. $result = $parser->addOption('test')
  74. ->removeOption('test')
  75. ->removeOption('help');
  76. $this->assertSame($parser, $result, 'Did not return $this from removeOption');
  77. $this->assertEquals([], $result->options());
  78. }
  79. /**
  80. * test adding an option and using the long value for parsing.
  81. *
  82. * @return void
  83. */
  84. public function testAddOptionLong()
  85. {
  86. $parser = new ConsoleOptionParser('test', false);
  87. $parser->addOption('test', [
  88. 'short' => 't'
  89. ]);
  90. $result = $parser->parse(['--test', 'value']);
  91. $this->assertEquals(['test' => 'value', 'help' => false], $result[0], 'Long parameter did not parse out');
  92. }
  93. /**
  94. * test adding an option with a zero value
  95. *
  96. * @return void
  97. */
  98. public function testAddOptionZero()
  99. {
  100. $parser = new ConsoleOptionParser('test', false);
  101. $parser->addOption('count', []);
  102. $result = $parser->parse(['--count', '0']);
  103. $this->assertEquals(['count' => '0', 'help' => false], $result[0], 'Zero parameter did not parse out');
  104. }
  105. /**
  106. * test addOption with an object.
  107. *
  108. * @return void
  109. */
  110. public function testAddOptionObject()
  111. {
  112. $parser = new ConsoleOptionParser('test', false);
  113. $parser->addOption(new ConsoleInputOption('test', 't'));
  114. $result = $parser->parse(['--test=value']);
  115. $this->assertEquals(['test' => 'value', 'help' => false], $result[0], 'Long parameter did not parse out');
  116. }
  117. /**
  118. * test adding an option and using the long value for parsing.
  119. *
  120. * @return void
  121. */
  122. public function testAddOptionLongEquals()
  123. {
  124. $parser = new ConsoleOptionParser('test', false);
  125. $parser->addOption('test', [
  126. 'short' => 't'
  127. ]);
  128. $result = $parser->parse(['--test=value']);
  129. $this->assertEquals(['test' => 'value', 'help' => false], $result[0], 'Long parameter did not parse out');
  130. }
  131. /**
  132. * test adding an option and using the default.
  133. *
  134. * @return void
  135. */
  136. public function testAddOptionDefault()
  137. {
  138. $parser = new ConsoleOptionParser('test', false);
  139. $parser->addOption('test', [
  140. 'default' => 'default value',
  141. ]);
  142. $result = $parser->parse(['--test']);
  143. $this->assertEquals(['test' => 'default value', 'help' => false], $result[0], 'Default value did not parse out');
  144. $parser = new ConsoleOptionParser('test', false);
  145. $parser->addOption('test', [
  146. 'default' => 'default value',
  147. ]);
  148. $result = $parser->parse([]);
  149. $this->assertEquals(['test' => 'default value', 'help' => false], $result[0], 'Default value did not parse out');
  150. }
  151. /**
  152. * test adding an option and using the short value for parsing.
  153. *
  154. * @return void
  155. */
  156. public function testAddOptionShort()
  157. {
  158. $parser = new ConsoleOptionParser('test', false);
  159. $parser->addOption('test', [
  160. 'short' => 't'
  161. ]);
  162. $result = $parser->parse(['-t', 'value']);
  163. $this->assertEquals(['test' => 'value', 'help' => false], $result[0], 'Short parameter did not parse out');
  164. }
  165. /**
  166. * Test that adding an option using a two letter short value causes an exception.
  167. * As they will not parse correctly.
  168. *
  169. * @expectedException \Cake\Console\Exception\ConsoleException
  170. * @return void
  171. */
  172. public function testAddOptionShortOneLetter()
  173. {
  174. $parser = new ConsoleOptionParser('test', false);
  175. $parser->addOption('test', ['short' => 'te']);
  176. }
  177. /**
  178. * test adding and using boolean options.
  179. *
  180. * @return void
  181. */
  182. public function testAddOptionBoolean()
  183. {
  184. $parser = new ConsoleOptionParser('test', false);
  185. $parser->addOption('test', [
  186. 'boolean' => true,
  187. ]);
  188. $result = $parser->parse(['--test', 'value']);
  189. $expected = [['test' => true, 'help' => false], ['value']];
  190. $this->assertEquals($expected, $result);
  191. $result = $parser->parse(['value']);
  192. $expected = [['test' => false, 'help' => false], ['value']];
  193. $this->assertEquals($expected, $result);
  194. }
  195. /**
  196. * test adding an multiple shorts.
  197. *
  198. * @return void
  199. */
  200. public function testAddOptionMultipleShort()
  201. {
  202. $parser = new ConsoleOptionParser('test', false);
  203. $parser->addOption('test', ['short' => 't', 'boolean' => true])
  204. ->addOption('file', ['short' => 'f', 'boolean' => true])
  205. ->addOption('output', ['short' => 'o', 'boolean' => true]);
  206. $result = $parser->parse(['-o', '-t', '-f']);
  207. $expected = ['file' => true, 'test' => true, 'output' => true, 'help' => false];
  208. $this->assertEquals($expected, $result[0], 'Short parameter did not parse out');
  209. $result = $parser->parse(['-otf']);
  210. $this->assertEquals($expected, $result[0], 'Short parameter did not parse out');
  211. }
  212. /**
  213. * test multiple options at once.
  214. *
  215. * @return void
  216. */
  217. public function testMultipleOptions()
  218. {
  219. $parser = new ConsoleOptionParser('test', false);
  220. $parser->addOption('test')
  221. ->addOption('connection')
  222. ->addOption('table', ['short' => 't', 'default' => true]);
  223. $result = $parser->parse(['--test', 'value', '-t', '--connection', 'postgres']);
  224. $expected = ['test' => 'value', 'table' => true, 'connection' => 'postgres', 'help' => false];
  225. $this->assertEquals($expected, $result[0], 'multiple options did not parse');
  226. }
  227. /**
  228. * Test adding multiple options.
  229. *
  230. * @return void
  231. */
  232. public function testAddOptions()
  233. {
  234. $parser = new ConsoleOptionParser('something', false);
  235. $result = $parser->addOptions([
  236. 'name' => ['help' => 'The name'],
  237. 'other' => ['help' => 'The other arg']
  238. ]);
  239. $this->assertEquals($parser, $result, 'addOptions is not chainable.');
  240. $result = $parser->options();
  241. $this->assertEquals(3, count($result), 'Not enough options');
  242. }
  243. /**
  244. * test that boolean options work
  245. *
  246. * @return void
  247. */
  248. public function testOptionWithBooleanParam()
  249. {
  250. $parser = new ConsoleOptionParser('test', false);
  251. $parser->addOption('no-commit', ['boolean' => true])
  252. ->addOption('table', ['short' => 't']);
  253. $result = $parser->parse(['--table', 'posts', '--no-commit', 'arg1', 'arg2']);
  254. $expected = [['table' => 'posts', 'no-commit' => true, 'help' => false], ['arg1', 'arg2']];
  255. $this->assertEquals($expected, $result, 'Boolean option did not parse correctly.');
  256. }
  257. /**
  258. * test parsing options that do not exist.
  259. *
  260. * @expectedException \Cake\Console\Exception\ConsoleException
  261. * @return void
  262. */
  263. public function testOptionThatDoesNotExist()
  264. {
  265. $parser = new ConsoleOptionParser('test', false);
  266. $parser->addOption('no-commit', ['boolean' => true]);
  267. $parser->parse(['--fail', 'other']);
  268. }
  269. /**
  270. * test parsing short options that do not exist.
  271. *
  272. * @expectedException \Cake\Console\Exception\ConsoleException
  273. * @return void
  274. */
  275. public function testShortOptionThatDoesNotExist()
  276. {
  277. $parser = new ConsoleOptionParser('test', false);
  278. $parser->addOption('no-commit', ['boolean' => true]);
  279. $parser->parse(['-f']);
  280. }
  281. /**
  282. * test that options with choices enforce them.
  283. *
  284. * @expectedException \Cake\Console\Exception\ConsoleException
  285. * @return void
  286. */
  287. public function testOptionWithChoices()
  288. {
  289. $parser = new ConsoleOptionParser('test', false);
  290. $parser->addOption('name', ['choices' => ['mark', 'jose']]);
  291. $result = $parser->parse(['--name', 'mark']);
  292. $expected = ['name' => 'mark', 'help' => false];
  293. $this->assertEquals($expected, $result[0], 'Got the correct value.');
  294. $result = $parser->parse(['--name', 'jimmy']);
  295. }
  296. /**
  297. * Ensure that option values can start with -
  298. *
  299. * @return void
  300. */
  301. public function testOptionWithValueStartingWithMinus()
  302. {
  303. $parser = new ConsoleOptionParser('test', false);
  304. $parser->addOption('name')
  305. ->addOption('age');
  306. $result = $parser->parse(['--name', '-foo', '--age', 'old']);
  307. $expected = ['name' => '-foo', 'age' => 'old', 'help' => false];
  308. $this->assertEquals($expected, $result[0], 'Option values starting with "-" are broken.');
  309. }
  310. /**
  311. * test positional argument parsing.
  312. *
  313. * @return void
  314. */
  315. public function testPositionalArgument()
  316. {
  317. $parser = new ConsoleOptionParser('test', false);
  318. $result = $parser->addArgument('name', ['help' => 'An argument']);
  319. $this->assertEquals($parser, $result, 'Should return this');
  320. }
  321. /**
  322. * test addOption with an object.
  323. *
  324. * @return void
  325. */
  326. public function testAddArgumentObject()
  327. {
  328. $parser = new ConsoleOptionParser('test', false);
  329. $parser->addArgument(new ConsoleInputArgument('test'));
  330. $result = $parser->arguments();
  331. $this->assertCount(1, $result);
  332. $this->assertEquals('test', $result[0]->name());
  333. }
  334. /**
  335. * Test adding arguments out of order.
  336. *
  337. * @return void
  338. */
  339. public function testAddArgumentOutOfOrder()
  340. {
  341. $parser = new ConsoleOptionParser('test', false);
  342. $parser->addArgument('name', ['index' => 1, 'help' => 'first argument'])
  343. ->addArgument('bag', ['index' => 2, 'help' => 'second argument'])
  344. ->addArgument('other', ['index' => 0, 'help' => 'Zeroth argument']);
  345. $result = $parser->arguments();
  346. $this->assertCount(3, $result);
  347. $this->assertEquals('other', $result[0]->name());
  348. $this->assertEquals('name', $result[1]->name());
  349. $this->assertEquals('bag', $result[2]->name());
  350. $this->assertSame([0, 1, 2], array_keys($result));
  351. }
  352. /**
  353. * test overwriting positional arguments.
  354. *
  355. * @return void
  356. */
  357. public function testPositionalArgOverwrite()
  358. {
  359. $parser = new ConsoleOptionParser('test', false);
  360. $parser->addArgument('name', ['help' => 'An argument'])
  361. ->addArgument('other', ['index' => 0]);
  362. $result = $parser->arguments();
  363. $this->assertEquals(1, count($result), 'Overwrite did not occur');
  364. }
  365. /**
  366. * test parsing arguments.
  367. *
  368. * @expectedException \Cake\Console\Exception\ConsoleException
  369. * @return void
  370. */
  371. public function testParseArgumentTooMany()
  372. {
  373. $parser = new ConsoleOptionParser('test', false);
  374. $parser->addArgument('name', ['help' => 'An argument'])
  375. ->addArgument('other');
  376. $expected = ['one', 'two'];
  377. $result = $parser->parse($expected);
  378. $this->assertEquals($expected, $result[1], 'Arguments are not as expected');
  379. $result = $parser->parse(['one', 'two', 'three']);
  380. }
  381. /**
  382. * test parsing arguments with 0 value.
  383. *
  384. * @return void
  385. */
  386. public function testParseArgumentZero()
  387. {
  388. $parser = new ConsoleOptionParser('test', false);
  389. $expected = ['one', 'two', 0, 'after', 'zero'];
  390. $result = $parser->parse($expected);
  391. $this->assertEquals($expected, $result[1], 'Arguments are not as expected');
  392. }
  393. /**
  394. * test that when there are not enough arguments an exception is raised
  395. *
  396. * @expectedException \Cake\Console\Exception\ConsoleException
  397. * @return void
  398. */
  399. public function testPositionalArgNotEnough()
  400. {
  401. $parser = new ConsoleOptionParser('test', false);
  402. $parser->addArgument('name', ['required' => true])
  403. ->addArgument('other', ['required' => true]);
  404. $parser->parse(['one']);
  405. }
  406. /**
  407. * test that when there are required arguments after optional ones an exception is raised
  408. *
  409. * @expectedException \LogicException
  410. * @return void
  411. */
  412. public function testPositionalArgRequiredAfterOptional()
  413. {
  414. $parser = new ConsoleOptionParser('test');
  415. $parser->addArgument('name', ['required' => false])
  416. ->addArgument('other', ['required' => true]);
  417. }
  418. /**
  419. * test that arguments with choices enforce them.
  420. *
  421. * @expectedException \Cake\Console\Exception\ConsoleException
  422. * @return void
  423. */
  424. public function testPositionalArgWithChoices()
  425. {
  426. $parser = new ConsoleOptionParser('test', false);
  427. $parser->addArgument('name', ['choices' => ['mark', 'jose']])
  428. ->addArgument('alias', ['choices' => ['cowboy', 'samurai']])
  429. ->addArgument('weapon', ['choices' => ['gun', 'sword']]);
  430. $result = $parser->parse(['mark', 'samurai', 'sword']);
  431. $expected = ['mark', 'samurai', 'sword'];
  432. $this->assertEquals($expected, $result[1], 'Got the correct value.');
  433. $result = $parser->parse(['jose', 'coder']);
  434. }
  435. /**
  436. * Test adding multiple arguments.
  437. *
  438. * @return void
  439. */
  440. public function testAddArguments()
  441. {
  442. $parser = new ConsoleOptionParser('test', false);
  443. $result = $parser->addArguments([
  444. 'name' => ['help' => 'The name'],
  445. 'other' => ['help' => 'The other arg']
  446. ]);
  447. $this->assertEquals($parser, $result, 'addArguments is not chainable.');
  448. $result = $parser->arguments();
  449. $this->assertEquals(2, count($result), 'Not enough arguments');
  450. }
  451. /**
  452. * test setting a subcommand up.
  453. *
  454. * @return void
  455. */
  456. public function testSubcommand()
  457. {
  458. $parser = new ConsoleOptionParser('test', false);
  459. $result = $parser->addSubcommand('initdb', [
  460. 'help' => 'Initialize the database'
  461. ]);
  462. $this->assertEquals($parser, $result, 'Adding a subcommand is not chainable');
  463. }
  464. /**
  465. * test addSubcommand with an object.
  466. *
  467. * @return void
  468. */
  469. public function testAddSubcommandObject()
  470. {
  471. $parser = new ConsoleOptionParser('test', false);
  472. $parser->addSubcommand(new ConsoleInputSubcommand('test'));
  473. $result = $parser->subcommands();
  474. $this->assertEquals(1, count($result));
  475. $this->assertEquals('test', $result['test']->name());
  476. }
  477. /**
  478. * test removeSubcommand with an object.
  479. *
  480. * @return void
  481. */
  482. public function testRemoveSubcommand()
  483. {
  484. $parser = new ConsoleOptionParser('test', false);
  485. $parser->addSubcommand(new ConsoleInputSubcommand('test'));
  486. $result = $parser->subcommands();
  487. $this->assertEquals(1, count($result));
  488. $parser->removeSubcommand('test');
  489. $result = $parser->subcommands();
  490. $this->assertEquals(0, count($result), 'Remove a subcommand does not work');
  491. }
  492. /**
  493. * test adding multiple subcommands
  494. *
  495. * @return void
  496. */
  497. public function testAddSubcommands()
  498. {
  499. $parser = new ConsoleOptionParser('test', false);
  500. $result = $parser->addSubcommands([
  501. 'initdb' => ['help' => 'Initialize the database'],
  502. 'create' => ['help' => 'Create something']
  503. ]);
  504. $this->assertEquals($parser, $result, 'Adding a subcommands is not chainable');
  505. $result = $parser->subcommands();
  506. $this->assertEquals(2, count($result), 'Not enough subcommands');
  507. }
  508. /**
  509. * test that no exception is triggered when help is being generated
  510. *
  511. * @return void
  512. */
  513. public function testHelpNoExceptionWhenGettingHelp()
  514. {
  515. $parser = new ConsoleOptionParser('mycommand', false);
  516. $parser->addOption('test', ['help' => 'A test option.'])
  517. ->addArgument('model', ['help' => 'The model to make.', 'required' => true]);
  518. $result = $parser->parse(['--help']);
  519. $this->assertTrue($result[0]['help']);
  520. }
  521. /**
  522. * test that help() with a command param shows the help for a subcommand
  523. *
  524. * @return void
  525. */
  526. public function testHelpSubcommandHelp()
  527. {
  528. $subParser = new ConsoleOptionParser('method', false);
  529. $subParser->addOption('connection', ['help' => 'Db connection.']);
  530. $subParser->addOption('zero', ['short' => '0', 'help' => 'Zero.']);
  531. $parser = new ConsoleOptionParser('mycommand', false);
  532. $parser->addSubcommand('method', [
  533. 'help' => 'This is another command',
  534. 'parser' => $subParser
  535. ])
  536. ->addOption('test', ['help' => 'A test option.']);
  537. $result = $parser->help('method');
  538. $expected = <<<TEXT
  539. <info>Usage:</info>
  540. cake mycommand method [--connection] [-h] [-0]
  541. <info>Options:</info>
  542. --connection Db connection.
  543. --help, -h Display this help.
  544. --zero, -0 Zero.
  545. TEXT;
  546. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  547. }
  548. /**
  549. * test building a parser from an array.
  550. *
  551. * @return void
  552. */
  553. public function testBuildFromArray()
  554. {
  555. $spec = [
  556. 'command' => 'test',
  557. 'arguments' => [
  558. 'name' => ['help' => 'The name'],
  559. 'other' => ['help' => 'The other arg']
  560. ],
  561. 'options' => [
  562. 'name' => ['help' => 'The name'],
  563. 'other' => ['help' => 'The other arg']
  564. ],
  565. 'subcommands' => [
  566. 'initdb' => ['help' => 'make database']
  567. ],
  568. 'description' => 'description text',
  569. 'epilog' => 'epilog text'
  570. ];
  571. $parser = ConsoleOptionParser::buildFromArray($spec);
  572. $this->assertEquals($spec['description'], $parser->description());
  573. $this->assertEquals($spec['epilog'], $parser->epilog());
  574. $options = $parser->options();
  575. $this->assertTrue(isset($options['name']));
  576. $this->assertTrue(isset($options['other']));
  577. $args = $parser->arguments();
  578. $this->assertEquals(2, count($args));
  579. $commands = $parser->subcommands();
  580. $this->assertEquals(1, count($commands));
  581. }
  582. /**
  583. * test that create() returns instances
  584. *
  585. * @return void
  586. */
  587. public function testCreateFactory()
  588. {
  589. $parser = ConsoleOptionParser::create('factory', false);
  590. $this->assertInstanceOf('Cake\Console\ConsoleOptionParser', $parser);
  591. $this->assertEquals('factory', $parser->command());
  592. }
  593. /**
  594. * test that command() inflects the command name.
  595. *
  596. * @return void
  597. */
  598. public function testCommandInflection()
  599. {
  600. $parser = new ConsoleOptionParser('CommandLine');
  601. $this->assertEquals('command_line', $parser->command());
  602. }
  603. /**
  604. * test that parse() takes a subcommand argument, and that the subcommand parser
  605. * is used.
  606. *
  607. * @return void
  608. */
  609. public function testParsingWithSubParser()
  610. {
  611. $parser = new ConsoleOptionParser('test', false);
  612. $parser->addOption('primary')
  613. ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']])
  614. ->addArgument('two', ['required' => true])
  615. ->addSubcommand('sub', [
  616. 'parser' => [
  617. 'options' => [
  618. 'secondary' => ['boolean' => true],
  619. 'fourth' => ['help' => 'fourth option']
  620. ],
  621. 'arguments' => [
  622. 'sub_arg' => ['choices' => ['c', 'd']]
  623. ]
  624. ]
  625. ]);
  626. $result = $parser->parse(['sub', '--secondary', '--fourth', '4', 'c']);
  627. $expected = [[
  628. 'secondary' => true,
  629. 'fourth' => '4',
  630. 'help' => false,
  631. 'verbose' => false,
  632. 'quiet' => false], ['c']];
  633. $this->assertEquals($expected, $result, 'Sub parser did not parse request.');
  634. }
  635. /**
  636. * Tests toArray()
  637. *
  638. * @return void
  639. */
  640. public function testToArray()
  641. {
  642. $spec = [
  643. 'command' => 'test',
  644. 'arguments' => [
  645. 'name' => ['help' => 'The name'],
  646. 'other' => ['help' => 'The other arg']
  647. ],
  648. 'options' => [
  649. 'name' => ['help' => 'The name'],
  650. 'other' => ['help' => 'The other arg']
  651. ],
  652. 'subcommands' => [
  653. 'initdb' => ['help' => 'make database']
  654. ],
  655. 'description' => 'description text',
  656. 'epilog' => 'epilog text'
  657. ];
  658. $parser = ConsoleOptionParser::buildFromArray($spec);
  659. $result = $parser->toArray();
  660. $this->assertEquals($spec['description'], $result['description']);
  661. $this->assertEquals($spec['epilog'], $result['epilog']);
  662. $options = $result['options'];
  663. $this->assertTrue(isset($options['name']));
  664. $this->assertTrue(isset($options['other']));
  665. $this->assertEquals(2, count($result['arguments']));
  666. $this->assertEquals(1, count($result['subcommands']));
  667. }
  668. /**
  669. * Tests merge()
  670. *
  671. * @return void
  672. */
  673. public function testMerge()
  674. {
  675. $parser = new ConsoleOptionParser('test');
  676. $parser->addOption('test', ['short' => 't', 'boolean' => true])
  677. ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']])
  678. ->addArgument('two', ['required' => true]);
  679. $parserTwo = new ConsoleOptionParser('test');
  680. $parserTwo->addOption('file', ['short' => 'f', 'boolean' => true])
  681. ->addOption('output', ['short' => 'o', 'boolean' => true])
  682. ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']]);
  683. $parser->merge($parserTwo);
  684. $result = $parser->toArray();
  685. $options = $result['options'];
  686. $this->assertTrue(isset($options['quiet']));
  687. $this->assertTrue(isset($options['test']));
  688. $this->assertTrue(isset($options['file']));
  689. $this->assertTrue(isset($options['output']));
  690. $this->assertEquals(2, count($result['arguments']));
  691. $this->assertEquals(6, count($result['options']));
  692. }
  693. }