ConsoleOptionParserTest.php 24 KB

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