ConsoleOptionParserTest.php 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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 removeSubcommand with an object.
  619. *
  620. * @return void
  621. */
  622. public function testRemoveSubcommand()
  623. {
  624. $parser = new ConsoleOptionParser('test', false);
  625. $parser->addSubcommand(new ConsoleInputSubcommand('test'));
  626. $result = $parser->subcommands();
  627. $this->assertCount(1, $result);
  628. $parser->removeSubcommand('test');
  629. $result = $parser->subcommands();
  630. $this->assertCount(0, $result, 'Remove a subcommand does not work');
  631. }
  632. /**
  633. * test adding multiple subcommands
  634. *
  635. * @return void
  636. */
  637. public function testAddSubcommands()
  638. {
  639. $parser = new ConsoleOptionParser('test', false);
  640. $result = $parser->addSubcommands([
  641. 'initdb' => ['help' => 'Initialize the database'],
  642. 'create' => ['help' => 'Create something']
  643. ]);
  644. $this->assertEquals($parser, $result, 'Adding a subcommands is not chainable');
  645. $result = $parser->subcommands();
  646. $this->assertCount(2, $result, 'Not enough subcommands');
  647. }
  648. /**
  649. * test that no exception is triggered when help is being generated
  650. *
  651. * @return void
  652. */
  653. public function testHelpNoExceptionWhenGettingHelp()
  654. {
  655. $parser = new ConsoleOptionParser('mycommand', false);
  656. $parser->addOption('test', ['help' => 'A test option.'])
  657. ->addArgument('model', ['help' => 'The model to make.', 'required' => true]);
  658. $result = $parser->parse(['--help']);
  659. $this->assertTrue($result[0]['help']);
  660. }
  661. /**
  662. * test that help() with a command param shows the help for a subcommand
  663. *
  664. * @return void
  665. */
  666. public function testHelpSubcommandHelp()
  667. {
  668. $subParser = new ConsoleOptionParser('method', false);
  669. $subParser->addOption('connection', ['help' => 'Db connection.']);
  670. $subParser->addOption('zero', ['short' => '0', 'help' => 'Zero.']);
  671. $parser = new ConsoleOptionParser('mycommand', false);
  672. $parser->addSubcommand('method', [
  673. 'help' => 'This is another command',
  674. 'parser' => $subParser
  675. ])
  676. ->addOption('test', ['help' => 'A test option.']);
  677. $result = $parser->help('method');
  678. $expected = <<<TEXT
  679. This is another command
  680. <info>Usage:</info>
  681. cake mycommand method [--connection] [-h] [-0]
  682. <info>Options:</info>
  683. --connection Db connection.
  684. --help, -h Display this help.
  685. --zero, -0 Zero.
  686. TEXT;
  687. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  688. }
  689. /**
  690. * test that help() with a command param shows the help for a subcommand
  691. *
  692. * @return void
  693. */
  694. public function testHelpSubcommandHelpArray()
  695. {
  696. $subParser = [
  697. 'options' => [
  698. 'foo' => [
  699. 'short' => 'f',
  700. 'help' => 'Foo.',
  701. 'boolean' => true,
  702. ]
  703. ],
  704. ];
  705. $parser = new ConsoleOptionParser('mycommand', false);
  706. $parser->addSubcommand('method', [
  707. 'help' => 'This is a subcommand',
  708. 'parser' => $subParser
  709. ])
  710. ->setRootName('tool')
  711. ->addOption('test', ['help' => 'A test option.']);
  712. $result = $parser->help('method');
  713. $expected = <<<TEXT
  714. This is a subcommand
  715. <info>Usage:</info>
  716. tool mycommand method [-f] [-h] [-q] [-v]
  717. <info>Options:</info>
  718. --foo, -f Foo.
  719. --help, -h Display this help.
  720. --quiet, -q Enable quiet output.
  721. --verbose, -v Enable verbose output.
  722. TEXT;
  723. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  724. }
  725. /**
  726. * test that help() with a custom rootName
  727. *
  728. * @return void
  729. */
  730. public function testHelpWithRootName()
  731. {
  732. $parser = new ConsoleOptionParser('sample', false);
  733. $parser->setDescription('A command!')
  734. ->setRootName('tool')
  735. ->addOption('test', ['help' => 'A test option.']);
  736. $result = $parser->help();
  737. $expected = <<<TEXT
  738. A command!
  739. <info>Usage:</info>
  740. tool sample [-h] [--test]
  741. <info>Options:</info>
  742. --help, -h Display this help.
  743. --test A test option.
  744. TEXT;
  745. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  746. }
  747. /**
  748. * test that getCommandError() with an unknown subcommand param shows a helpful message
  749. *
  750. * @return void
  751. */
  752. public function testHelpUnknownSubcommand()
  753. {
  754. $subParser = [
  755. 'options' => [
  756. 'foo' => [
  757. 'short' => 'f',
  758. 'help' => 'Foo.',
  759. 'boolean' => true,
  760. ]
  761. ],
  762. ];
  763. $parser = new ConsoleOptionParser('mycommand', false);
  764. $parser
  765. ->addSubcommand('method', [
  766. 'help' => 'This is a subcommand',
  767. 'parser' => $subParser
  768. ])
  769. ->addOption('test', ['help' => 'A test option.'])
  770. ->addSubcommand('unstash');
  771. $result = $parser->help('unknown');
  772. $expected = <<<TEXT
  773. Unable to find the `mycommand unknown` subcommand. See `bin/cake mycommand --help`.
  774. Did you mean : `mycommand unstash` ?
  775. Available subcommands for the `mycommand` command are :
  776. - method
  777. - unstash
  778. TEXT;
  779. $this->assertTextEquals($expected, $result, 'Help is not correct.');
  780. }
  781. /**
  782. * test building a parser from an array.
  783. *
  784. * @return void
  785. */
  786. public function testBuildFromArray()
  787. {
  788. $spec = [
  789. 'command' => 'test',
  790. 'arguments' => [
  791. 'name' => ['help' => 'The name'],
  792. 'other' => ['help' => 'The other arg']
  793. ],
  794. 'options' => [
  795. 'name' => ['help' => 'The name'],
  796. 'other' => ['help' => 'The other arg']
  797. ],
  798. 'subcommands' => [
  799. 'initdb' => ['help' => 'make database']
  800. ],
  801. 'description' => 'description text',
  802. 'epilog' => 'epilog text'
  803. ];
  804. $parser = ConsoleOptionParser::buildFromArray($spec);
  805. $this->assertEquals($spec['description'], $parser->getDescription());
  806. $this->assertEquals($spec['epilog'], $parser->getEpilog());
  807. $options = $parser->options();
  808. $this->assertArrayHasKey('name', $options);
  809. $this->assertArrayHasKey('other', $options);
  810. $args = $parser->arguments();
  811. $this->assertCount(2, $args);
  812. $commands = $parser->subcommands();
  813. $this->assertCount(1, $commands);
  814. }
  815. /**
  816. * test that create() returns instances
  817. *
  818. * @return void
  819. */
  820. public function testCreateFactory()
  821. {
  822. $parser = ConsoleOptionParser::create('factory', false);
  823. $this->assertInstanceOf('Cake\Console\ConsoleOptionParser', $parser);
  824. $this->assertEquals('factory', $parser->getCommand());
  825. }
  826. /**
  827. * test that command() inflects the command name.
  828. *
  829. * @return void
  830. */
  831. public function testCommandInflection()
  832. {
  833. $parser = new ConsoleOptionParser('CommandLine');
  834. $this->assertEquals('command_line', $parser->getCommand());
  835. }
  836. /**
  837. * test that parse() takes a subcommand argument, and that the subcommand parser
  838. * is used.
  839. *
  840. * @return void
  841. */
  842. public function testParsingWithSubParser()
  843. {
  844. $parser = new ConsoleOptionParser('test', false);
  845. $parser->addOption('primary')
  846. ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']])
  847. ->addArgument('two', ['required' => true])
  848. ->addSubcommand('sub', [
  849. 'parser' => [
  850. 'options' => [
  851. 'secondary' => ['boolean' => true],
  852. 'fourth' => ['help' => 'fourth option']
  853. ],
  854. 'arguments' => [
  855. 'sub_arg' => ['choices' => ['c', 'd']]
  856. ]
  857. ]
  858. ]);
  859. $result = $parser->parse(['sub', '--secondary', '--fourth', '4', 'c']);
  860. $expected = [[
  861. 'secondary' => true,
  862. 'fourth' => '4',
  863. 'help' => false,
  864. 'verbose' => false,
  865. 'quiet' => false], ['c']];
  866. $this->assertEquals($expected, $result, 'Sub parser did not parse request.');
  867. }
  868. /**
  869. * Tests toArray()
  870. *
  871. * @return void
  872. */
  873. public function testToArray()
  874. {
  875. $spec = [
  876. 'command' => 'test',
  877. 'arguments' => [
  878. 'name' => ['help' => 'The name'],
  879. 'other' => ['help' => 'The other arg']
  880. ],
  881. 'options' => [
  882. 'name' => ['help' => 'The name'],
  883. 'other' => ['help' => 'The other arg']
  884. ],
  885. 'subcommands' => [
  886. 'initdb' => ['help' => 'make database']
  887. ],
  888. 'description' => 'description text',
  889. 'epilog' => 'epilog text'
  890. ];
  891. $parser = ConsoleOptionParser::buildFromArray($spec);
  892. $result = $parser->toArray();
  893. $this->assertEquals($spec['description'], $result['description']);
  894. $this->assertEquals($spec['epilog'], $result['epilog']);
  895. $options = $result['options'];
  896. $this->assertArrayHasKey('name', $options);
  897. $this->assertArrayHasKey('other', $options);
  898. $this->assertCount(2, $result['arguments']);
  899. $this->assertCount(1, $result['subcommands']);
  900. }
  901. /**
  902. * Tests merge()
  903. *
  904. * @return void
  905. */
  906. public function testMerge()
  907. {
  908. $parser = new ConsoleOptionParser('test');
  909. $parser->addOption('test', ['short' => 't', 'boolean' => true])
  910. ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']])
  911. ->addArgument('two', ['required' => true]);
  912. $parserTwo = new ConsoleOptionParser('test');
  913. $parserTwo->addOption('file', ['short' => 'f', 'boolean' => true])
  914. ->addOption('output', ['short' => 'o', 'boolean' => true])
  915. ->addArgument('one', ['required' => true, 'choices' => ['a', 'b']]);
  916. $parser->merge($parserTwo);
  917. $result = $parser->toArray();
  918. $options = $result['options'];
  919. $this->assertArrayHasKey('quiet', $options);
  920. $this->assertArrayHasKey('test', $options);
  921. $this->assertArrayHasKey('file', $options);
  922. $this->assertArrayHasKey('output', $options);
  923. $this->assertCount(2, $result['arguments']);
  924. $this->assertCount(6, $result['options']);
  925. }
  926. }