ConsoleOptionParserTest.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license https://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. * @group deprecated
  30. * @return void
  31. */
  32. public function testDescriptionDeprecated()
  33. {
  34. $this->deprecated(function () {
  35. $parser = new ConsoleOptionParser('test', false);
  36. $result = $parser->description('A test');
  37. $this->assertEquals($parser, $result, 'Setting description is not chainable');
  38. $this->assertEquals('A test', $parser->description(), 'getting value is wrong.');
  39. });
  40. }
  41. /**
  42. * test setting the console description
  43. *
  44. * @return void
  45. */
  46. public function testDescription()
  47. {
  48. $parser = new ConsoleOptionParser('test', false);
  49. $result = $parser->setDescription('A test');
  50. $this->assertEquals($parser, $result, 'Setting description is not chainable');
  51. $this->assertEquals('A test', $parser->getDescription(), 'getting value is wrong.');
  52. $result = $parser->setDescription(['A test', 'something']);
  53. $this->assertEquals("A test\nsomething", $parser->getDescription(), 'getting value is wrong.');
  54. }
  55. /**
  56. * test setting the console description
  57. *
  58. * @group deprecated
  59. * @return void
  60. */
  61. public function testEplilogDeprecated()
  62. {
  63. $this->deprecated(function () {
  64. $parser = new ConsoleOptionParser('test', false);
  65. $result = $parser->epilog('A test');
  66. $this->assertEquals($parser, $result, 'Setting epilog is not chainable');
  67. $this->assertEquals('A test', $parser->epilog(), 'getting value is wrong.');
  68. });
  69. }
  70. /**
  71. * test setting the console epilog
  72. *
  73. * @return void
  74. */
  75. public function testEpilog()
  76. {
  77. $parser = new ConsoleOptionParser('test', false);
  78. $result = $parser->setEpilog('A test');
  79. $this->assertEquals($parser, $result, 'Setting epilog is not chainable');
  80. $this->assertEquals('A test', $parser->getEpilog(), 'getting value is wrong.');
  81. $result = $parser->setEpilog(['A test', 'something']);
  82. $this->assertEquals("A test\nsomething", $parser->getEpilog(), 'getting value is wrong.');
  83. }
  84. /**
  85. * test adding an option returns self.
  86. *
  87. * @return void
  88. */
  89. public function testAddOptionReturnSelf()
  90. {
  91. $parser = new ConsoleOptionParser('test', false);
  92. $result = $parser->addOption('test');
  93. $this->assertEquals($parser, $result, 'Did not return $this from addOption');
  94. }
  95. /**
  96. * test removing an option
  97. *
  98. * @return void
  99. */
  100. public function testRemoveOption()
  101. {
  102. $parser = new ConsoleOptionParser('test', false);
  103. $result = $parser->addOption('test')
  104. ->removeOption('test')
  105. ->removeOption('help');
  106. $this->assertSame($parser, $result, 'Did not return $this from removeOption');
  107. $this->assertEquals([], $result->options());
  108. }
  109. /**
  110. * test adding an option and using the long value for parsing.
  111. *
  112. * @return void
  113. */
  114. public function testAddOptionLong()
  115. {
  116. $parser = new ConsoleOptionParser('test', false);
  117. $parser->addOption('test', [
  118. 'short' => 't'
  119. ]);
  120. $result = $parser->parse(['--test', 'value']);
  121. $this->assertEquals(['test' => 'value', 'help' => false], $result[0], 'Long parameter did not parse out');
  122. }
  123. /**
  124. * test adding an option with a zero value
  125. *
  126. * @return void
  127. */
  128. public function testAddOptionZero()
  129. {
  130. $parser = new ConsoleOptionParser('test', false);
  131. $parser->addOption('count', []);
  132. $result = $parser->parse(['--count', '0']);
  133. $this->assertEquals(['count' => '0', 'help' => false], $result[0], 'Zero parameter did not parse out');
  134. }
  135. /**
  136. * test addOption with an object.
  137. *
  138. * @return void
  139. */
  140. public function testAddOptionObject()
  141. {
  142. $parser = new ConsoleOptionParser('test', false);
  143. $parser->addOption(new ConsoleInputOption('test', 't'));
  144. $result = $parser->parse(['--test=value']);
  145. $this->assertEquals(['test' => 'value', 'help' => false], $result[0], 'Long parameter did not parse out');
  146. }
  147. /**
  148. * test adding an option and using the long value for parsing.
  149. *
  150. * @return void
  151. */
  152. public function testAddOptionLongEquals()
  153. {
  154. $parser = new ConsoleOptionParser('test', false);
  155. $parser->addOption('test', [
  156. 'short' => 't'
  157. ]);
  158. $result = $parser->parse(['--test=value']);
  159. $this->assertEquals(['test' => 'value', 'help' => false], $result[0], 'Long parameter did not parse out');
  160. }
  161. /**
  162. * test adding an option and using the default.
  163. *
  164. * @return void
  165. */
  166. public function testAddOptionDefault()
  167. {
  168. $parser = new ConsoleOptionParser('test', false);
  169. $parser->addOption('test', [
  170. 'default' => 'default value',
  171. ]);
  172. $result = $parser->parse(['--test']);
  173. $this->assertEquals(['test' => 'default value', 'help' => false], $result[0], 'Default value did not parse out');
  174. $parser = new ConsoleOptionParser('test', false);
  175. $parser->addOption('test', [
  176. 'default' => 'default value',
  177. ]);
  178. $result = $parser->parse([]);
  179. $this->assertEquals(['test' => 'default value', 'help' => false], $result[0], 'Default value did not parse out');
  180. }
  181. /**
  182. * test adding an option and using the short value for parsing.
  183. *
  184. * @return void
  185. */
  186. public function testAddOptionShort()
  187. {
  188. $parser = new ConsoleOptionParser('test', false);
  189. $parser->addOption('test', [
  190. 'short' => 't'
  191. ]);
  192. $result = $parser->parse(['-t', 'value']);
  193. $this->assertEquals(['test' => 'value', 'help' => false], $result[0], 'Short parameter did not parse out');
  194. }
  195. /**
  196. * test adding an option and using the short value for parsing.
  197. *
  198. * @return void
  199. */
  200. public function testAddOptionWithMultipleShort()
  201. {
  202. $parser = new ConsoleOptionParser('test', false);
  203. $parser->addOption('source', [
  204. 'multiple' => true,
  205. 'short' => 's'
  206. ]);
  207. $result = $parser->parse(['-s', 'mysql', '-s', 'postgres']);
  208. $this->assertEquals(
  209. [
  210. 'source' => ['mysql', 'postgres'],
  211. 'help' => false
  212. ],
  213. $result[0],
  214. 'Short parameter did not parse out'
  215. );
  216. }
  217. /**
  218. * Test that adding an option using a two letter short value causes an exception.
  219. * As they will not parse correctly.
  220. *
  221. * @return void
  222. */
  223. public function testAddOptionShortOneLetter()
  224. {
  225. $this->expectException(\Cake\Console\Exception\ConsoleException::class);
  226. $parser = new ConsoleOptionParser('test', false);
  227. $parser->addOption('test', ['short' => 'te']);
  228. }
  229. /**
  230. * test adding and using boolean options.
  231. *
  232. * @return void
  233. */
  234. public function testAddOptionBoolean()
  235. {
  236. $parser = new ConsoleOptionParser('test', false);
  237. $parser->addOption('test', [
  238. 'boolean' => true,
  239. ]);
  240. $result = $parser->parse(['--test', 'value']);
  241. $expected = [['test' => true, 'help' => false], ['value']];
  242. $this->assertEquals($expected, $result);
  243. $result = $parser->parse(['value']);
  244. $expected = [['test' => false, 'help' => false], ['value']];
  245. $this->assertEquals($expected, $result);
  246. }
  247. /**
  248. * test adding an multiple shorts.
  249. *
  250. * @return void
  251. */
  252. public function testAddOptionMultipleShort()
  253. {
  254. $parser = new ConsoleOptionParser('test', false);
  255. $parser->addOption('test', ['short' => 't', 'boolean' => true])
  256. ->addOption('file', ['short' => 'f', 'boolean' => true])
  257. ->addOption('output', ['short' => 'o', 'boolean' => true]);
  258. $result = $parser->parse(['-o', '-t', '-f']);
  259. $expected = ['file' => true, 'test' => true, 'output' => true, 'help' => false];
  260. $this->assertEquals($expected, $result[0], 'Short parameter did not parse out');
  261. $result = $parser->parse(['-otf']);
  262. $this->assertEquals($expected, $result[0], 'Short parameter did not parse out');
  263. }
  264. /**
  265. * test multiple options at once.
  266. *
  267. * @return void
  268. */
  269. public function testMultipleOptions()
  270. {
  271. $parser = new ConsoleOptionParser('test', false);
  272. $parser->addOption('test')
  273. ->addOption('connection')
  274. ->addOption('table', ['short' => 't', 'default' => true]);
  275. $result = $parser->parse(['--test', 'value', '-t', '--connection', 'postgres']);
  276. $expected = ['test' => 'value', 'table' => true, 'connection' => 'postgres', 'help' => false];
  277. $this->assertEquals($expected, $result[0], 'multiple options did not parse');
  278. }
  279. /**
  280. * test adding an option that accepts multiple values.
  281. *
  282. * @return void
  283. */
  284. public function testAddOptionWithMultiple()
  285. {
  286. $parser = new ConsoleOptionParser('test', false);
  287. $parser->addOption('source', ['short' => 's', 'multiple' => true]);
  288. $result = $parser->parse(['--source', 'mysql', '-s', 'postgres']);
  289. $expected = [
  290. 'source' => [
  291. 'mysql',
  292. 'postgres'
  293. ],
  294. 'help' => false
  295. ];
  296. $this->assertEquals($expected, $result[0], 'options with multiple values did not parse');
  297. }
  298. /**
  299. * test adding multiple options, including one that accepts multiple values.
  300. *
  301. * @return void
  302. */
  303. public function testAddMultipleOptionsWithMultiple()
  304. {
  305. $parser = new ConsoleOptionParser('test', false);
  306. $parser
  307. ->addOption('source', ['multiple' => true])
  308. ->addOption('name')
  309. ->addOption('export', ['boolean' => true]);
  310. $result = $parser->parse(['--export', '--source', 'mysql', '--name', 'annual-report', '--source', 'postgres']);
  311. $expected = [
  312. 'export' => true,
  313. 'source' => [
  314. 'mysql',
  315. 'postgres'
  316. ],
  317. 'name' => 'annual-report',
  318. 'help' => false
  319. ];
  320. $this->assertEquals($expected, $result[0], 'options with multiple values did not parse');
  321. }
  322. /**
  323. * Test adding multiple options.
  324. *
  325. * @return void
  326. */
  327. public function testAddOptions()
  328. {
  329. $parser = new ConsoleOptionParser('something', false);
  330. $result = $parser->addOptions([
  331. 'name' => ['help' => 'The name'],
  332. 'other' => ['help' => 'The other arg']
  333. ]);
  334. $this->assertEquals($parser, $result, 'addOptions is not chainable.');
  335. $result = $parser->options();
  336. $this->assertCount(3, $result, 'Not enough options');
  337. }
  338. /**
  339. * test that boolean options work
  340. *
  341. * @return void
  342. */
  343. public function testOptionWithBooleanParam()
  344. {
  345. $parser = new ConsoleOptionParser('test', false);
  346. $parser->addOption('no-commit', ['boolean' => true])
  347. ->addOption('table', ['short' => 't']);
  348. $result = $parser->parse(['--table', 'posts', '--no-commit', 'arg1', 'arg2']);
  349. $expected = [['table' => 'posts', 'no-commit' => true, 'help' => false], ['arg1', 'arg2']];
  350. $this->assertEquals($expected, $result, 'Boolean option did not parse correctly.');
  351. }
  352. /**
  353. * test parsing options that do not exist.
  354. *
  355. * @expectedExceptionMessageRegexp /Unknown option `fail`.\n\nDid you mean `help` \?\n\nAvailable options are :\n\n
  356. * - help\n - no-commit/
  357. * @return void
  358. */
  359. public function testOptionThatDoesNotExist()
  360. {
  361. $this->expectException(\Cake\Console\Exception\ConsoleException::class);
  362. $parser = new ConsoleOptionParser('test', false);
  363. $parser->addOption('no-commit', ['boolean' => true]);
  364. $parser->parse(['--fail', 'other']);
  365. }
  366. /**
  367. * test parsing short options that do not exist.
  368. *
  369. * @expectedExceptionMessageRegexp /Unknown short option `f`.\n\nAvailable short options are :\n\n
  370. * - `n` (short for `--no-commit`)\n - `c` (short for `--clear`)/
  371. * @return void
  372. */
  373. public function testShortOptionThatDoesNotExist()
  374. {
  375. $this->expectException(\Cake\Console\Exception\ConsoleException::class);
  376. $parser = new ConsoleOptionParser('test', false);
  377. $parser->addOption('no-commit', ['boolean' => true, 'short' => 'n']);
  378. $parser->addOption('construct', ['boolean' => true]);
  379. $parser->addOption('clear', ['boolean' => true, 'short' => 'c']);
  380. $parser->parse(['-f']);
  381. }
  382. /**
  383. * test that options with choices enforce them.
  384. *
  385. * @return void
  386. */
  387. public function testOptionWithChoices()
  388. {
  389. $this->expectException(\Cake\Console\Exception\ConsoleException::class);
  390. $parser = new ConsoleOptionParser('test', false);
  391. $parser->addOption('name', ['choices' => ['mark', 'jose']]);
  392. $result = $parser->parse(['--name', 'mark']);
  393. $expected = ['name' => 'mark', 'help' => false];
  394. $this->assertEquals($expected, $result[0], 'Got the correct value.');
  395. $result = $parser->parse(['--name', 'jimmy']);
  396. }
  397. /**
  398. * Ensure that option values can start with -
  399. *
  400. * @return void
  401. */
  402. public function testOptionWithValueStartingWithMinus()
  403. {
  404. $parser = new ConsoleOptionParser('test', false);
  405. $parser->addOption('name')
  406. ->addOption('age');
  407. $result = $parser->parse(['--name', '-foo', '--age', 'old']);
  408. $expected = ['name' => '-foo', 'age' => 'old', 'help' => false];
  409. $this->assertEquals($expected, $result[0], 'Option values starting with "-" are broken.');
  410. }
  411. /**
  412. * test positional argument parsing.
  413. *
  414. * @return void
  415. */
  416. public function testPositionalArgument()
  417. {
  418. $parser = new ConsoleOptionParser('test', false);
  419. $result = $parser->addArgument('name', ['help' => 'An argument']);
  420. $this->assertEquals($parser, $result, 'Should return this');
  421. }
  422. /**
  423. * test addOption with an object.
  424. *
  425. * @return void
  426. */
  427. public function testAddArgumentObject()
  428. {
  429. $parser = new ConsoleOptionParser('test', false);
  430. $parser->addArgument(new ConsoleInputArgument('test'));
  431. $result = $parser->arguments();
  432. $this->assertCount(1, $result);
  433. $this->assertEquals('test', $result[0]->name());
  434. }
  435. /**
  436. * Test adding arguments out of order.
  437. *
  438. * @return void
  439. */
  440. public function testAddArgumentOutOfOrder()
  441. {
  442. $parser = new ConsoleOptionParser('test', false);
  443. $parser->addArgument('name', ['index' => 1, 'help' => 'first argument'])
  444. ->addArgument('bag', ['index' => 2, 'help' => 'second argument'])
  445. ->addArgument('other', ['index' => 0, 'help' => 'Zeroth argument']);
  446. $result = $parser->arguments();
  447. $this->assertCount(3, $result);
  448. $this->assertEquals('other', $result[0]->name());
  449. $this->assertEquals('name', $result[1]->name());
  450. $this->assertEquals('bag', $result[2]->name());
  451. $this->assertSame([0, 1, 2], array_keys($result));
  452. $this->assertEquals(
  453. ['other', 'name', 'bag'],
  454. $parser->argumentNames()
  455. );
  456. }
  457. /**
  458. * test overwriting positional arguments.
  459. *
  460. * @return void
  461. */
  462. public function testPositionalArgOverwrite()
  463. {
  464. $parser = new ConsoleOptionParser('test', false);
  465. $parser->addArgument('name', ['help' => 'An argument'])
  466. ->addArgument('other', ['index' => 0]);
  467. $result = $parser->arguments();
  468. $this->assertCount(1, $result, 'Overwrite did not occur');
  469. }
  470. /**
  471. * test parsing arguments.
  472. *
  473. * @return void
  474. */
  475. public function testParseArgumentTooMany()
  476. {
  477. $this->expectException(\Cake\Console\Exception\ConsoleException::class);
  478. $parser = new ConsoleOptionParser('test', false);
  479. $parser->addArgument('name', ['help' => 'An argument'])
  480. ->addArgument('other');
  481. $expected = ['one', 'two'];
  482. $result = $parser->parse($expected);
  483. $this->assertEquals($expected, $result[1], 'Arguments are not as expected');
  484. $result = $parser->parse(['one', 'two', 'three']);
  485. }
  486. /**
  487. * test parsing arguments with 0 value.
  488. *
  489. * @return void
  490. */
  491. public function testParseArgumentZero()
  492. {
  493. $parser = new ConsoleOptionParser('test', false);
  494. $expected = ['one', 'two', 0, 'after', 'zero'];
  495. $result = $parser->parse($expected);
  496. $this->assertEquals($expected, $result[1], 'Arguments are not as expected');
  497. }
  498. /**
  499. * test that when there are not enough arguments an exception is raised
  500. *
  501. * @return void
  502. */
  503. public function testPositionalArgNotEnough()
  504. {
  505. $this->expectException(\Cake\Console\Exception\ConsoleException::class);
  506. $parser = new ConsoleOptionParser('test', false);
  507. $parser->addArgument('name', ['required' => true])
  508. ->addArgument('other', ['required' => true]);
  509. $parser->parse(['one']);
  510. }
  511. /**
  512. * test that when there are required arguments after optional ones an exception is raised
  513. *
  514. * @return void
  515. */
  516. public function testPositionalArgRequiredAfterOptional()
  517. {
  518. $this->expectException(\LogicException::class);
  519. $parser = new ConsoleOptionParser('test');
  520. $parser->addArgument('name', ['required' => false])
  521. ->addArgument('other', ['required' => true]);
  522. }
  523. /**
  524. * test that arguments with choices enforce them.
  525. *
  526. * @return void
  527. */
  528. public function testPositionalArgWithChoices()
  529. {
  530. $this->expectException(\Cake\Console\Exception\ConsoleException::class);
  531. $parser = new ConsoleOptionParser('test', false);
  532. $parser->addArgument('name', ['choices' => ['mark', 'jose']])
  533. ->addArgument('alias', ['choices' => ['cowboy', 'samurai']])
  534. ->addArgument('weapon', ['choices' => ['gun', 'sword']]);
  535. $result = $parser->parse(['mark', 'samurai', 'sword']);
  536. $expected = ['mark', 'samurai', 'sword'];
  537. $this->assertEquals($expected, $result[1], 'Got the correct value.');
  538. $result = $parser->parse(['jose', 'coder']);
  539. }
  540. /**
  541. * Test adding multiple arguments.
  542. *
  543. * @return void
  544. */
  545. public function testAddArguments()
  546. {
  547. $parser = new ConsoleOptionParser('test', false);
  548. $result = $parser->addArguments([
  549. 'name' => ['help' => 'The name'],
  550. 'other' => ['help' => 'The other arg']
  551. ]);
  552. $this->assertEquals($parser, $result, 'addArguments is not chainable.');
  553. $result = $parser->arguments();
  554. $this->assertCount(2, $result, 'Not enough arguments');
  555. }
  556. /**
  557. * test setting a subcommand up.
  558. *
  559. * @return void
  560. */
  561. public function testSubcommand()
  562. {
  563. $parser = new ConsoleOptionParser('test', false);
  564. $result = $parser->addSubcommand('initdb', [
  565. 'help' => 'Initialize the database'
  566. ]);
  567. $this->assertEquals($parser, $result, 'Adding a subcommand is not chainable');
  568. }
  569. /**
  570. * Tests setting a subcommand up for a Shell method `initMyDb`.
  571. *
  572. * @return void
  573. */
  574. public function testSubcommandCamelBacked()
  575. {
  576. $parser = new ConsoleOptionParser('test', false);
  577. $result = $parser->addSubcommand('initMyDb', [
  578. 'help' => 'Initialize the database'
  579. ]);
  580. $subcommands = array_keys($result->subcommands());
  581. $this->assertEquals(['init_my_db'], $subcommands, 'Adding a subcommand does not work with camel backed method names.');
  582. }
  583. /**
  584. * Test addSubcommand inherits options as no
  585. * parser is created.
  586. *
  587. * @return void
  588. */
  589. public function testAddSubcommandInheritOptions()
  590. {
  591. $parser = new ConsoleOptionParser('test', false);
  592. $parser->addSubcommand('build', [
  593. 'help' => 'Build things'
  594. ])->addOption('connection', [
  595. 'short' => 'c',
  596. 'default' => 'default'
  597. ])->addArgument('name', ['required' => false]);
  598. $result = $parser->parse(['build']);
  599. $this->assertEquals('default', $result[0]['connection']);
  600. $result = $parser->subcommands();
  601. $this->assertArrayHasKey('build', $result);
  602. $this->assertFalse($result['build']->parser(), 'No parser should be created');
  603. }
  604. /**
  605. * test addSubcommand with an object.
  606. *
  607. * @return void
  608. */
  609. public function testAddSubcommandObject()
  610. {
  611. $parser = new ConsoleOptionParser('test', false);
  612. $parser->addSubcommand(new ConsoleInputSubcommand('test'));
  613. $result = $parser->subcommands();
  614. $this->assertCount(1, $result);
  615. $this->assertEquals('test', $result['test']->name());
  616. }
  617. /**
  618. * test addSubcommand without sorting applied.
  619. */
  620. public function testAddSubcommandSort()
  621. {
  622. $parser = new ConsoleOptionParser('test', false);
  623. $this->assertEquals(true, $parser->isSubcommandSortEnabled());
  624. $parser->enableSubcommandSort(false);
  625. $this->assertEquals(false, $parser->isSubcommandSortEnabled());
  626. $parser->addSubcommand(new ConsoleInputSubcommand('betaTest'), []);
  627. $parser->addSubcommand(new ConsoleInputSubcommand('alphaTest'), []);
  628. $result = $parser->subcommands();
  629. $this->assertCount(2, $result);
  630. $firstResult = key($result);
  631. $this->assertEquals('betaTest', $firstResult);
  632. }
  633. /**
  634. * test removeSubcommand with an object.
  635. *
  636. * @return void
  637. */
  638. public function testRemoveSubcommand()
  639. {
  640. $parser = new ConsoleOptionParser('test', false);
  641. $parser->addSubcommand(new ConsoleInputSubcommand('test'));
  642. $result = $parser->subcommands();
  643. $this->assertCount(1, $result);
  644. $parser->removeSubcommand('test');
  645. $result = $parser->subcommands();
  646. $this->assertCount(0, $result, 'Remove a subcommand does not work');
  647. }
  648. /**
  649. * test adding multiple subcommands
  650. *
  651. * @return void
  652. */
  653. public function testAddSubcommands()
  654. {
  655. $parser = new ConsoleOptionParser('test', false);
  656. $result = $parser->addSubcommands([
  657. 'initdb' => ['help' => 'Initialize the database'],
  658. 'create' => ['help' => 'Create something']
  659. ]);
  660. $this->assertEquals($parser, $result, 'Adding a subcommands is not chainable');
  661. $result = $parser->subcommands();
  662. $this->assertCount(2, $result, 'Not enough subcommands');
  663. }
  664. /**
  665. * test that no exception is triggered when help is being generated
  666. *
  667. * @return void
  668. */
  669. public function testHelpNoExceptionWhenGettingHelp()
  670. {
  671. $parser = new ConsoleOptionParser('mycommand', false);
  672. $parser->addOption('test', ['help' => 'A test option.'])
  673. ->addArgument('model', ['help' => 'The model to make.', 'required' => true]);
  674. $result = $parser->parse(['--help']);
  675. $this->assertTrue($result[0]['help']);
  676. }
  677. /**
  678. * test that help() with a command param shows the help for a subcommand
  679. *
  680. * @return void
  681. */
  682. public function testHelpSubcommandHelp()
  683. {
  684. $subParser = new ConsoleOptionParser('method', false);
  685. $subParser->addOption('connection', ['help' => 'Db connection.']);
  686. $subParser->addOption('zero', ['short' => '0', 'help' => 'Zero.']);
  687. $parser = new ConsoleOptionParser('mycommand', false);
  688. $parser->addSubcommand('method', [
  689. 'help' => 'This is another command',
  690. 'parser' => $subParser
  691. ])
  692. ->addOption('test', ['help' => 'A test option.']);
  693. $result = $parser->help('method');
  694. $expected = <<<TEXT
  695. This is another command
  696. <info>Usage:</info>
  697. cake mycommand method [--connection] [-h] [-0]
  698. <info>Options:</info>
  699. --connection Db connection.
  700. --help, -h Display this help.
  701. --zero, -0 Zero.
  702. TEXT;
  703. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  704. }
  705. /**
  706. * test that help() with a command param shows the help for a subcommand
  707. *
  708. * @return void
  709. */
  710. public function testHelpSubcommandHelpArray()
  711. {
  712. $subParser = [
  713. 'options' => [
  714. 'foo' => [
  715. 'short' => 'f',
  716. 'help' => 'Foo.',
  717. 'boolean' => true,
  718. ]
  719. ],
  720. ];
  721. $parser = new ConsoleOptionParser('mycommand', false);
  722. $parser->addSubcommand('method', [
  723. 'help' => 'This is a subcommand',
  724. 'parser' => $subParser
  725. ])
  726. ->setRootName('tool')
  727. ->addOption('test', ['help' => 'A test option.']);
  728. $result = $parser->help('method');
  729. $expected = <<<TEXT
  730. This is a subcommand
  731. <info>Usage:</info>
  732. tool mycommand method [-f] [-h] [-q] [-v]
  733. <info>Options:</info>
  734. --foo, -f Foo.
  735. --help, -h Display this help.
  736. --quiet, -q Enable quiet output.
  737. --verbose, -v Enable verbose output.
  738. TEXT;
  739. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  740. }
  741. /**
  742. * test that help() with a custom rootName
  743. *
  744. * @return void
  745. */
  746. public function testHelpWithRootName()
  747. {
  748. $parser = new ConsoleOptionParser('sample', false);
  749. $parser->setDescription('A command!')
  750. ->setRootName('tool')
  751. ->addOption('test', ['help' => 'A test option.']);
  752. $result = $parser->help();
  753. $expected = <<<TEXT
  754. A command!
  755. <info>Usage:</info>
  756. tool sample [-h] [--test]
  757. <info>Options:</info>
  758. --help, -h Display this help.
  759. --test A test option.
  760. TEXT;
  761. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  762. }
  763. /**
  764. * test that getCommandError() with an unknown subcommand param shows a helpful message
  765. *
  766. * @return void
  767. */
  768. public function testHelpUnknownSubcommand()
  769. {
  770. $subParser = [
  771. 'options' => [
  772. 'foo' => [
  773. 'short' => 'f',
  774. 'help' => 'Foo.',
  775. 'boolean' => true,
  776. ]
  777. ],
  778. ];
  779. $parser = new ConsoleOptionParser('mycommand', false);
  780. $parser
  781. ->addSubcommand('method', [
  782. 'help' => 'This is a subcommand',
  783. 'parser' => $subParser
  784. ])
  785. ->addOption('test', ['help' => 'A test option.'])
  786. ->addSubcommand('unstash');
  787. $result = $parser->help('unknown');
  788. $expected = <<<TEXT
  789. Unable to find the `mycommand unknown` subcommand. See `bin/cake mycommand --help`.
  790. Did you mean : `mycommand unstash` ?
  791. Available subcommands for the `mycommand` command are :
  792. - method
  793. - unstash
  794. TEXT;
  795. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  796. }
  797. /**
  798. * test building a parser from an array.
  799. *
  800. * @return void
  801. */
  802. public function testBuildFromArray()
  803. {
  804. $spec = [
  805. 'command' => 'test',
  806. 'arguments' => [
  807. 'name' => ['help' => 'The name'],
  808. 'other' => ['help' => 'The other arg']
  809. ],
  810. 'options' => [
  811. 'name' => ['help' => 'The name'],
  812. 'other' => ['help' => 'The other arg']
  813. ],
  814. 'subcommands' => [
  815. 'initdb' => ['help' => 'make database']
  816. ],
  817. 'description' => 'description text',
  818. 'epilog' => 'epilog text'
  819. ];
  820. $parser = ConsoleOptionParser::buildFromArray($spec);
  821. $this->assertEquals($spec['description'], $parser->getDescription());
  822. $this->assertEquals($spec['epilog'], $parser->getEpilog());
  823. $options = $parser->options();
  824. $this->assertArrayHasKey('name', $options);
  825. $this->assertArrayHasKey('other', $options);
  826. $args = $parser->arguments();
  827. $this->assertCount(2, $args);
  828. $commands = $parser->subcommands();
  829. $this->assertCount(1, $commands);
  830. }
  831. /**
  832. * test that create() returns instances
  833. *
  834. * @return void
  835. */
  836. public function testCreateFactory()
  837. {
  838. $parser = ConsoleOptionParser::create('factory', false);
  839. $this->assertInstanceOf('Cake\Console\ConsoleOptionParser', $parser);
  840. $this->assertEquals('factory', $parser->getCommand());
  841. }
  842. /**
  843. * test that command() inflects the command name.
  844. *
  845. * @return void
  846. */
  847. public function testCommandInflection()
  848. {
  849. $parser = new ConsoleOptionParser('CommandLine');
  850. $this->assertEquals('command_line', $parser->getCommand());
  851. }
  852. /**
  853. * test that parse() takes a subcommand argument, and that the subcommand parser
  854. * is used.
  855. *
  856. * @return void
  857. */
  858. public function testParsingWithSubParser()
  859. {
  860. $parser = new ConsoleOptionParser('test', false);
  861. $parser->addOption('primary')
  862. ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']])
  863. ->addArgument('two', ['required' => true])
  864. ->addSubcommand('sub', [
  865. 'parser' => [
  866. 'options' => [
  867. 'secondary' => ['boolean' => true],
  868. 'fourth' => ['help' => 'fourth option']
  869. ],
  870. 'arguments' => [
  871. 'sub_arg' => ['choices' => ['c', 'd']]
  872. ]
  873. ]
  874. ]);
  875. $result = $parser->parse(['sub', '--secondary', '--fourth', '4', 'c']);
  876. $expected = [[
  877. 'secondary' => true,
  878. 'fourth' => '4',
  879. 'help' => false,
  880. 'verbose' => false,
  881. 'quiet' => false], ['c']];
  882. $this->assertEquals($expected, $result, 'Sub parser did not parse request.');
  883. }
  884. /**
  885. * Tests toArray()
  886. *
  887. * @return void
  888. */
  889. public function testToArray()
  890. {
  891. $spec = [
  892. 'command' => 'test',
  893. 'arguments' => [
  894. 'name' => ['help' => 'The name'],
  895. 'other' => ['help' => 'The other arg']
  896. ],
  897. 'options' => [
  898. 'name' => ['help' => 'The name'],
  899. 'other' => ['help' => 'The other arg']
  900. ],
  901. 'subcommands' => [
  902. 'initdb' => ['help' => 'make database']
  903. ],
  904. 'description' => 'description text',
  905. 'epilog' => 'epilog text'
  906. ];
  907. $parser = ConsoleOptionParser::buildFromArray($spec);
  908. $result = $parser->toArray();
  909. $this->assertEquals($spec['description'], $result['description']);
  910. $this->assertEquals($spec['epilog'], $result['epilog']);
  911. $options = $result['options'];
  912. $this->assertArrayHasKey('name', $options);
  913. $this->assertArrayHasKey('other', $options);
  914. $this->assertCount(2, $result['arguments']);
  915. $this->assertCount(1, $result['subcommands']);
  916. }
  917. /**
  918. * Tests merge()
  919. *
  920. * @return void
  921. */
  922. public function testMerge()
  923. {
  924. $parser = new ConsoleOptionParser('test');
  925. $parser->addOption('test', ['short' => 't', 'boolean' => true])
  926. ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']])
  927. ->addArgument('two', ['required' => true]);
  928. $parserTwo = new ConsoleOptionParser('test');
  929. $parserTwo->addOption('file', ['short' => 'f', 'boolean' => true])
  930. ->addOption('output', ['short' => 'o', 'boolean' => true])
  931. ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']]);
  932. $parser->merge($parserTwo);
  933. $result = $parser->toArray();
  934. $options = $result['options'];
  935. $this->assertArrayHasKey('quiet', $options);
  936. $this->assertArrayHasKey('test', $options);
  937. $this->assertArrayHasKey('file', $options);
  938. $this->assertArrayHasKey('output', $options);
  939. $this->assertCount(2, $result['arguments']);
  940. $this->assertCount(6, $result['options']);
  941. }
  942. }