ConsoleOptionParserTest.php 31 KB

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