ConsoleOptionParserTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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 arguments with choices enforce them.
  408. *
  409. * @expectedException \Cake\Console\Exception\ConsoleException
  410. * @return void
  411. */
  412. public function testPositionalArgWithChoices()
  413. {
  414. $parser = new ConsoleOptionParser('test', false);
  415. $parser->addArgument('name', ['choices' => ['mark', 'jose']])
  416. ->addArgument('alias', ['choices' => ['cowboy', 'samurai']])
  417. ->addArgument('weapon', ['choices' => ['gun', 'sword']]);
  418. $result = $parser->parse(['mark', 'samurai', 'sword']);
  419. $expected = ['mark', 'samurai', 'sword'];
  420. $this->assertEquals($expected, $result[1], 'Got the correct value.');
  421. $result = $parser->parse(['jose', 'coder']);
  422. }
  423. /**
  424. * Test adding multiple arguments.
  425. *
  426. * @return void
  427. */
  428. public function testAddArguments()
  429. {
  430. $parser = new ConsoleOptionParser('test', false);
  431. $result = $parser->addArguments([
  432. 'name' => ['help' => 'The name'],
  433. 'other' => ['help' => 'The other arg']
  434. ]);
  435. $this->assertEquals($parser, $result, 'addArguments is not chainable.');
  436. $result = $parser->arguments();
  437. $this->assertEquals(2, count($result), 'Not enough arguments');
  438. }
  439. /**
  440. * test setting a subcommand up.
  441. *
  442. * @return void
  443. */
  444. public function testSubcommand()
  445. {
  446. $parser = new ConsoleOptionParser('test', false);
  447. $result = $parser->addSubcommand('initdb', [
  448. 'help' => 'Initialize the database'
  449. ]);
  450. $this->assertEquals($parser, $result, 'Adding a subcommand is not chainable');
  451. }
  452. /**
  453. * test addSubcommand with an object.
  454. *
  455. * @return void
  456. */
  457. public function testAddSubcommandObject()
  458. {
  459. $parser = new ConsoleOptionParser('test', false);
  460. $parser->addSubcommand(new ConsoleInputSubcommand('test'));
  461. $result = $parser->subcommands();
  462. $this->assertEquals(1, count($result));
  463. $this->assertEquals('test', $result['test']->name());
  464. }
  465. /**
  466. * test removeSubcommand with an object.
  467. *
  468. * @return void
  469. */
  470. public function testRemoveSubcommand()
  471. {
  472. $parser = new ConsoleOptionParser('test', false);
  473. $parser->addSubcommand(new ConsoleInputSubcommand('test'));
  474. $result = $parser->subcommands();
  475. $this->assertEquals(1, count($result));
  476. $parser->removeSubcommand('test');
  477. $result = $parser->subcommands();
  478. $this->assertEquals(0, count($result), 'Remove a subcommand does not work');
  479. }
  480. /**
  481. * test adding multiple subcommands
  482. *
  483. * @return void
  484. */
  485. public function testAddSubcommands()
  486. {
  487. $parser = new ConsoleOptionParser('test', false);
  488. $result = $parser->addSubcommands([
  489. 'initdb' => ['help' => 'Initialize the database'],
  490. 'create' => ['help' => 'Create something']
  491. ]);
  492. $this->assertEquals($parser, $result, 'Adding a subcommands is not chainable');
  493. $result = $parser->subcommands();
  494. $this->assertEquals(2, count($result), 'Not enough subcommands');
  495. }
  496. /**
  497. * test that no exception is triggered when help is being generated
  498. *
  499. * @return void
  500. */
  501. public function testHelpNoExceptionWhenGettingHelp()
  502. {
  503. $parser = new ConsoleOptionParser('mycommand', false);
  504. $parser->addOption('test', ['help' => 'A test option.'])
  505. ->addArgument('model', ['help' => 'The model to make.', 'required' => true]);
  506. $result = $parser->parse(['--help']);
  507. $this->assertTrue($result[0]['help']);
  508. }
  509. /**
  510. * test that help() with a command param shows the help for a subcommand
  511. *
  512. * @return void
  513. */
  514. public function testHelpSubcommandHelp()
  515. {
  516. $subParser = new ConsoleOptionParser('method', false);
  517. $subParser->addOption('connection', ['help' => 'Db connection.']);
  518. $parser = new ConsoleOptionParser('mycommand', false);
  519. $parser->addSubcommand('method', [
  520. 'help' => 'This is another command',
  521. 'parser' => $subParser
  522. ])
  523. ->addOption('test', ['help' => 'A test option.']);
  524. $result = $parser->help('method');
  525. $expected = <<<TEXT
  526. <info>Usage:</info>
  527. cake mycommand method [--connection] [-h]
  528. <info>Options:</info>
  529. --connection Db connection.
  530. --help, -h Display this help.
  531. TEXT;
  532. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  533. }
  534. /**
  535. * test building a parser from an array.
  536. *
  537. * @return void
  538. */
  539. public function testBuildFromArray()
  540. {
  541. $spec = [
  542. 'command' => 'test',
  543. 'arguments' => [
  544. 'name' => ['help' => 'The name'],
  545. 'other' => ['help' => 'The other arg']
  546. ],
  547. 'options' => [
  548. 'name' => ['help' => 'The name'],
  549. 'other' => ['help' => 'The other arg']
  550. ],
  551. 'subcommands' => [
  552. 'initdb' => ['help' => 'make database']
  553. ],
  554. 'description' => 'description text',
  555. 'epilog' => 'epilog text'
  556. ];
  557. $parser = ConsoleOptionParser::buildFromArray($spec);
  558. $this->assertEquals($spec['description'], $parser->description());
  559. $this->assertEquals($spec['epilog'], $parser->epilog());
  560. $options = $parser->options();
  561. $this->assertTrue(isset($options['name']));
  562. $this->assertTrue(isset($options['other']));
  563. $args = $parser->arguments();
  564. $this->assertEquals(2, count($args));
  565. $commands = $parser->subcommands();
  566. $this->assertEquals(1, count($commands));
  567. }
  568. /**
  569. * test that create() returns instances
  570. *
  571. * @return void
  572. */
  573. public function testCreateFactory()
  574. {
  575. $parser = ConsoleOptionParser::create('factory', false);
  576. $this->assertInstanceOf('Cake\Console\ConsoleOptionParser', $parser);
  577. $this->assertEquals('factory', $parser->command());
  578. }
  579. /**
  580. * test that command() inflects the command name.
  581. *
  582. * @return void
  583. */
  584. public function testCommandInflection()
  585. {
  586. $parser = new ConsoleOptionParser('CommandLine');
  587. $this->assertEquals('command_line', $parser->command());
  588. }
  589. /**
  590. * test that parse() takes a subcommand argument, and that the subcommand parser
  591. * is used.
  592. *
  593. * @return void
  594. */
  595. public function testParsingWithSubParser()
  596. {
  597. $parser = new ConsoleOptionParser('test', false);
  598. $parser->addOption('primary')
  599. ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']])
  600. ->addArgument('two', ['required' => true])
  601. ->addSubcommand('sub', [
  602. 'parser' => [
  603. 'options' => [
  604. 'secondary' => ['boolean' => true],
  605. 'fourth' => ['help' => 'fourth option']
  606. ],
  607. 'arguments' => [
  608. 'sub_arg' => ['choices' => ['c', 'd']]
  609. ]
  610. ]
  611. ]);
  612. $result = $parser->parse(['sub', '--secondary', '--fourth', '4', 'c']);
  613. $expected = [[
  614. 'secondary' => true,
  615. 'fourth' => '4',
  616. 'help' => false,
  617. 'verbose' => false,
  618. 'quiet' => false], ['c']];
  619. $this->assertEquals($expected, $result, 'Sub parser did not parse request.');
  620. }
  621. /**
  622. * Tests toArray()
  623. *
  624. * @return void
  625. */
  626. public function testToArray()
  627. {
  628. $spec = [
  629. 'command' => 'test',
  630. 'arguments' => [
  631. 'name' => ['help' => 'The name'],
  632. 'other' => ['help' => 'The other arg']
  633. ],
  634. 'options' => [
  635. 'name' => ['help' => 'The name'],
  636. 'other' => ['help' => 'The other arg']
  637. ],
  638. 'subcommands' => [
  639. 'initdb' => ['help' => 'make database']
  640. ],
  641. 'description' => 'description text',
  642. 'epilog' => 'epilog text'
  643. ];
  644. $parser = ConsoleOptionParser::buildFromArray($spec);
  645. $result = $parser->toArray();
  646. $this->assertEquals($spec['description'], $result['description']);
  647. $this->assertEquals($spec['epilog'], $result['epilog']);
  648. $options = $result['options'];
  649. $this->assertTrue(isset($options['name']));
  650. $this->assertTrue(isset($options['other']));
  651. $this->assertEquals(2, count($result['arguments']));
  652. $this->assertEquals(1, count($result['subcommands']));
  653. }
  654. /**
  655. * Tests merge()
  656. *
  657. * @return void
  658. */
  659. public function testMerge()
  660. {
  661. $parser = new ConsoleOptionParser('test');
  662. $parser->addOption('test', ['short' => 't', 'boolean' => true])
  663. ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']])
  664. ->addArgument('two', ['required' => true]);
  665. $parserTwo = new ConsoleOptionParser('test');
  666. $parserTwo->addOption('file', ['short' => 'f', 'boolean' => true])
  667. ->addOption('output', ['short' => 'o', 'boolean' => true])
  668. ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']]);
  669. $parser->merge($parserTwo);
  670. $result = $parser->toArray();
  671. $options = $result['options'];
  672. $this->assertTrue(isset($options['quiet']));
  673. $this->assertTrue(isset($options['test']));
  674. $this->assertTrue(isset($options['file']));
  675. $this->assertTrue(isset($options['output']));
  676. $this->assertEquals(2, count($result['arguments']));
  677. $this->assertEquals(6, count($result['options']));
  678. }
  679. }