ShellTest.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  1. <?php
  2. /**
  3. * CakePHP : 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 Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Console;
  16. use Cake\Console\ConsoleIo;
  17. use Cake\Console\ConsoleOptionParser;
  18. use Cake\Console\Shell;
  19. use Cake\Core\Plugin;
  20. use Cake\Filesystem\Folder;
  21. use Cake\TestSuite\TestCase;
  22. use TestApp\Shell\TestingDispatchShell;
  23. /**
  24. * for testing merging vars
  25. */
  26. class MergeShell extends Shell
  27. {
  28. public $tasks = ['DbConfig', 'Fixture'];
  29. public $modelClass = 'Articles';
  30. }
  31. /**
  32. * ShellTestShell class
  33. */
  34. class ShellTestShell extends Shell
  35. {
  36. /**
  37. * name property
  38. *
  39. * @var string
  40. */
  41. public $name = 'ShellTestShell';
  42. /**
  43. * stopped property
  44. *
  45. * @var int
  46. */
  47. public $stopped;
  48. /**
  49. * testMessage property
  50. *
  51. * @var string
  52. */
  53. public $testMessage = 'all your base are belong to us';
  54. /**
  55. * stop method
  56. *
  57. * @param int $status
  58. * @return void
  59. */
  60. protected function _stop($status = Shell::CODE_SUCCESS)
  61. {
  62. $this->stopped = $status;
  63. }
  64. protected function _secret()
  65. {
  66. }
  67. //@codingStandardsIgnoreStart
  68. public function doSomething()
  69. {
  70. }
  71. protected function noAccess()
  72. {
  73. }
  74. public function logSomething()
  75. {
  76. $this->log($this->testMessage);
  77. }
  78. //@codingStandardsIgnoreEnd
  79. }
  80. /**
  81. * TestAppleTask class
  82. */
  83. class TestAppleTask extends Shell
  84. {
  85. }
  86. /**
  87. * TestBananaTask class
  88. */
  89. class TestBananaTask extends Shell
  90. {
  91. }
  92. class_alias(__NAMESPACE__ . '\TestAppleTask', 'Cake\Shell\Task\TestAppleTask');
  93. class_alias(__NAMESPACE__ . '\TestBananaTask', 'Cake\Shell\Task\TestBananaTask');
  94. /**
  95. * ShellTest class
  96. */
  97. class ShellTest extends TestCase
  98. {
  99. /**
  100. * Fixtures used in this test case
  101. *
  102. * @var array
  103. */
  104. public $fixtures = [
  105. 'core.articles',
  106. 'core.articles_tags',
  107. 'core.attachments',
  108. 'core.comments',
  109. 'core.posts',
  110. 'core.tags',
  111. 'core.users'
  112. ];
  113. /**
  114. * setUp method
  115. *
  116. * @return void
  117. */
  118. public function setUp()
  119. {
  120. parent::setUp();
  121. $this->io = $this->getMockBuilder('Cake\Console\ConsoleIo')
  122. ->disableOriginalConstructor()
  123. ->getMock();
  124. $this->Shell = new ShellTestShell($this->io);
  125. if (is_dir(TMP . 'shell_test')) {
  126. $Folder = new Folder(TMP . 'shell_test');
  127. $Folder->delete();
  128. }
  129. }
  130. /**
  131. * testConstruct method
  132. *
  133. * @return void
  134. */
  135. public function testConstruct()
  136. {
  137. $this->assertEquals('ShellTestShell', $this->Shell->name);
  138. $this->assertInstanceOf('Cake\Console\ConsoleIo', $this->Shell->io());
  139. }
  140. /**
  141. * testInitialize method
  142. *
  143. * @return void
  144. */
  145. public function testInitialize()
  146. {
  147. static::setAppNamespace();
  148. Plugin::load('TestPlugin');
  149. $this->Shell->tasks = ['DbConfig' => ['one', 'two']];
  150. $this->Shell->plugin = 'TestPlugin';
  151. $this->Shell->modelClass = 'TestPlugin.TestPluginComments';
  152. $this->Shell->initialize();
  153. $this->Shell->loadModel();
  154. $this->assertTrue(isset($this->Shell->TestPluginComments));
  155. $this->assertInstanceOf(
  156. 'TestPlugin\Model\Table\TestPluginCommentsTable',
  157. $this->Shell->TestPluginComments
  158. );
  159. }
  160. /**
  161. * test LoadModel method
  162. *
  163. * @return void
  164. */
  165. public function testLoadModel()
  166. {
  167. static::setAppNamespace();
  168. $Shell = new MergeShell();
  169. $this->assertInstanceOf(
  170. 'TestApp\Model\Table\ArticlesTable',
  171. $Shell->Articles
  172. );
  173. $this->assertEquals('Articles', $Shell->modelClass);
  174. Plugin::load('TestPlugin');
  175. $result = $this->Shell->loadModel('TestPlugin.TestPluginComments');
  176. $this->assertInstanceOf(
  177. 'TestPlugin\Model\Table\TestPluginCommentsTable',
  178. $result
  179. );
  180. $this->assertInstanceOf(
  181. 'TestPlugin\Model\Table\TestPluginCommentsTable',
  182. $this->Shell->TestPluginComments
  183. );
  184. }
  185. /**
  186. * testIn method
  187. *
  188. * @return void
  189. */
  190. public function testIn()
  191. {
  192. $this->io->expects($this->at(0))
  193. ->method('askChoice')
  194. ->with('Just a test?', ['y', 'n'], 'n')
  195. ->will($this->returnValue('n'));
  196. $this->io->expects($this->at(1))
  197. ->method('ask')
  198. ->with('Just a test?', 'n')
  199. ->will($this->returnValue('n'));
  200. $result = $this->Shell->in('Just a test?', ['y', 'n'], 'n');
  201. $this->assertEquals('n', $result);
  202. $result = $this->Shell->in('Just a test?', null, 'n');
  203. $this->assertEquals('n', $result);
  204. }
  205. /**
  206. * Test in() when not interactive.
  207. *
  208. * @return void
  209. */
  210. public function testInNonInteractive()
  211. {
  212. $this->io->expects($this->never())
  213. ->method('askChoice');
  214. $this->io->expects($this->never())
  215. ->method('ask');
  216. $this->Shell->interactive = false;
  217. $result = $this->Shell->in('Just a test?', 'y/n', 'n');
  218. $this->assertEquals('n', $result);
  219. }
  220. /**
  221. * testVerbose method
  222. *
  223. * @return void
  224. */
  225. public function testVerbose()
  226. {
  227. $this->io->expects($this->once())
  228. ->method('verbose')
  229. ->with('Just a test', 1);
  230. $this->Shell->verbose('Just a test');
  231. }
  232. /**
  233. * testQuiet method
  234. *
  235. * @return void
  236. */
  237. public function testQuiet()
  238. {
  239. $this->io->expects($this->once())
  240. ->method('quiet')
  241. ->with('Just a test', 1);
  242. $this->Shell->quiet('Just a test');
  243. }
  244. /**
  245. * testOut method
  246. *
  247. * @return void
  248. */
  249. public function testOut()
  250. {
  251. $this->io->expects($this->once())
  252. ->method('out')
  253. ->with('Just a test', 1);
  254. $this->Shell->out('Just a test');
  255. }
  256. /**
  257. * testErr method
  258. *
  259. * @return void
  260. */
  261. public function testErr()
  262. {
  263. $this->io->expects($this->once())
  264. ->method('err')
  265. ->with('<error>Just a test</error>', 1);
  266. $this->Shell->err('Just a test');
  267. }
  268. /**
  269. * testErr method with array
  270. *
  271. * @return void
  272. */
  273. public function testErrArray()
  274. {
  275. $this->io->expects($this->once())
  276. ->method('err')
  277. ->with(['<error>Just</error>', '<error>a</error>', '<error>test</error>'], 1);
  278. $this->Shell->err(['Just', 'a', 'test']);
  279. }
  280. /**
  281. * testInfo method
  282. *
  283. * @return void
  284. */
  285. public function testInfo()
  286. {
  287. $this->io->expects($this->once())
  288. ->method('out')
  289. ->with('<info>Just a test</info>', 1);
  290. $this->Shell->info('Just a test');
  291. }
  292. /**
  293. * testInfo method with array
  294. *
  295. * @return void
  296. */
  297. public function testInfoArray()
  298. {
  299. $this->io->expects($this->once())
  300. ->method('out')
  301. ->with(['<info>Just</info>', '<info>a</info>', '<info>test</info>'], 1);
  302. $this->Shell->info(['Just', 'a', 'test']);
  303. }
  304. /**
  305. * testWarn method
  306. *
  307. * @return void
  308. */
  309. public function testWarn()
  310. {
  311. $this->io->expects($this->once())
  312. ->method('err')
  313. ->with('<warning>Just a test</warning>', 1);
  314. $this->Shell->warn('Just a test');
  315. }
  316. /**
  317. * testWarn method with array
  318. *
  319. * @return void
  320. */
  321. public function testWarnArray()
  322. {
  323. $this->io->expects($this->once())
  324. ->method('err')
  325. ->with(['<warning>Just</warning>', '<warning>a</warning>', '<warning>test</warning>'], 1);
  326. $this->Shell->warn(['Just', 'a', 'test']);
  327. }
  328. /**
  329. * testSuccess method
  330. *
  331. * @return void
  332. */
  333. public function testSuccess()
  334. {
  335. $this->io->expects($this->once())
  336. ->method('out')
  337. ->with('<success>Just a test</success>', 1);
  338. $this->Shell->success('Just a test');
  339. }
  340. /**
  341. * testSuccess method with array
  342. *
  343. * @return void
  344. */
  345. public function testSuccessArray()
  346. {
  347. $this->io->expects($this->once())
  348. ->method('out')
  349. ->with(['<success>Just</success>', '<success>a</success>', '<success>test</success>'], 1);
  350. $this->Shell->success(['Just', 'a', 'test']);
  351. }
  352. /**
  353. * testNl
  354. *
  355. * @return void
  356. */
  357. public function testNl()
  358. {
  359. $this->io->expects($this->once())
  360. ->method('nl')
  361. ->with(2);
  362. $this->Shell->nl(2);
  363. }
  364. /**
  365. * testHr
  366. *
  367. * @return void
  368. */
  369. public function testHr()
  370. {
  371. $this->io->expects($this->once())
  372. ->method('hr')
  373. ->with(2);
  374. $this->Shell->hr(2);
  375. }
  376. /**
  377. * testError
  378. *
  379. * @return void
  380. */
  381. public function testError()
  382. {
  383. $this->io->expects($this->at(0))
  384. ->method('err')
  385. ->with('<error>Error:</error> Foo Not Found');
  386. $this->io->expects($this->at(1))
  387. ->method('err')
  388. ->with('Searched all...');
  389. $this->Shell->error('Foo Not Found', 'Searched all...');
  390. $this->assertSame($this->Shell->stopped, 1);
  391. }
  392. /**
  393. * testLoadTasks method
  394. *
  395. * @return void
  396. */
  397. public function testLoadTasks()
  398. {
  399. $this->assertTrue($this->Shell->loadTasks());
  400. $this->Shell->tasks = null;
  401. $this->assertTrue($this->Shell->loadTasks());
  402. $this->Shell->tasks = false;
  403. $this->assertTrue($this->Shell->loadTasks());
  404. $this->Shell->tasks = true;
  405. $this->assertTrue($this->Shell->loadTasks());
  406. $this->Shell->tasks = [];
  407. $this->assertTrue($this->Shell->loadTasks());
  408. $this->Shell->tasks = ['TestApple'];
  409. $this->assertTrue($this->Shell->loadTasks());
  410. $this->assertInstanceOf('Cake\Shell\Task\TestAppleTask', $this->Shell->TestApple);
  411. $this->Shell->tasks = 'TestBanana';
  412. $this->assertTrue($this->Shell->loadTasks());
  413. $this->assertInstanceOf('Cake\Shell\Task\TestAppleTask', $this->Shell->TestApple);
  414. $this->assertInstanceOf('Cake\Shell\Task\TestBananaTask', $this->Shell->TestBanana);
  415. unset($this->Shell->ShellTestApple, $this->Shell->TestBanana);
  416. $this->Shell->tasks = ['TestApple', 'TestBanana'];
  417. $this->assertTrue($this->Shell->loadTasks());
  418. $this->assertInstanceOf('Cake\Shell\Task\TestAppleTask', $this->Shell->TestApple);
  419. $this->assertInstanceOf('Cake\Shell\Task\TestBananaTask', $this->Shell->TestBanana);
  420. }
  421. /**
  422. * test that __get() makes args and params references
  423. *
  424. * @return void
  425. */
  426. public function testMagicGetArgAndParamReferences()
  427. {
  428. $this->Shell->tasks = ['TestApple'];
  429. $this->Shell->args = ['one'];
  430. $this->Shell->params = ['help' => false];
  431. $this->Shell->loadTasks();
  432. $result = $this->Shell->TestApple;
  433. $this->Shell->args = ['one', 'two'];
  434. $this->assertSame($this->Shell->args, $result->args);
  435. $this->assertSame($this->Shell->params, $result->params);
  436. }
  437. /**
  438. * testShortPath method
  439. *
  440. * @return void
  441. */
  442. public function testShortPath()
  443. {
  444. $path = $expected = DS . 'tmp/ab/cd';
  445. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  446. $path = $expected = DS . 'tmp/ab/cd/';
  447. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  448. $path = $expected = DS . 'tmp/ab/index.php';
  449. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  450. $path = DS . 'tmp/ab/' . DS . 'cd';
  451. $expected = DS . 'tmp/ab/cd';
  452. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  453. $path = 'tmp/ab';
  454. $expected = 'tmp/ab';
  455. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  456. $path = 'tmp/ab';
  457. $expected = 'tmp/ab';
  458. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  459. $path = APP;
  460. $result = $this->Shell->shortPath($path);
  461. $this->assertNotContains(ROOT, $result, 'Short paths should not contain ROOT');
  462. }
  463. /**
  464. * testCreateFile method
  465. *
  466. * @return void
  467. */
  468. public function testCreateFileNonInteractive()
  469. {
  470. $eol = PHP_EOL;
  471. $path = TMP . 'shell_test';
  472. $file = $path . DS . 'file1.php';
  473. new Folder($path, true);
  474. $contents = "<?php{$eol}echo 'test';${eol}\$te = 'st';{$eol}";
  475. $result = $this->Shell->createFile($file, $contents);
  476. $this->assertTrue($result);
  477. $this->assertFileExists($file);
  478. $this->assertEquals(file_get_contents($file), $contents);
  479. }
  480. /**
  481. * Test that files are not changed with a 'n' reply.
  482. *
  483. * @return void
  484. */
  485. public function testCreateFileNoReply()
  486. {
  487. $eol = PHP_EOL;
  488. $path = TMP . 'shell_test';
  489. $file = $path . DS . 'file1.php';
  490. new Folder($path, true);
  491. $this->io->expects($this->once())
  492. ->method('askChoice')
  493. ->will($this->returnValue('n'));
  494. touch($file);
  495. $this->assertFileExists($file);
  496. $contents = 'My content';
  497. $result = $this->Shell->createFile($file, $contents);
  498. $this->assertFileExists($file);
  499. $this->assertTextEquals('', file_get_contents($file));
  500. $this->assertFalse($result, 'Did not create file.');
  501. }
  502. /**
  503. * Test that files are changed with a 'y' reply.
  504. *
  505. * @return void
  506. */
  507. public function testCreateFileOverwrite()
  508. {
  509. $eol = PHP_EOL;
  510. $path = TMP . 'shell_test';
  511. $file = $path . DS . 'file1.php';
  512. new Folder($path, true);
  513. $this->io->expects($this->once())
  514. ->method('askChoice')
  515. ->will($this->returnValue('y'));
  516. touch($file);
  517. $this->assertFileExists($file);
  518. $contents = 'My content';
  519. $result = $this->Shell->createFile($file, $contents);
  520. $this->assertFileExists($file);
  521. $this->assertTextEquals($contents, file_get_contents($file));
  522. $this->assertTrue($result, 'Did create file.');
  523. }
  524. /**
  525. * Test that there is no user prompt in non-interactive mode while file already exists.
  526. *
  527. * @return void
  528. */
  529. public function testCreateFileOverwriteNonInteractive()
  530. {
  531. $path = TMP . 'shell_test';
  532. $file = $path . DS . 'file1.php';
  533. new Folder($path, true);
  534. touch($file);
  535. $this->assertFileExists($file);
  536. $this->io->expects($this->never())->method('askChoice');
  537. $this->Shell->interactive = false;
  538. $result = $this->Shell->createFile($file, 'My content');
  539. $this->assertTrue($result);
  540. $this->assertEquals(file_get_contents($file), 'My content');
  541. }
  542. /**
  543. * Test that all files are changed with a 'a' reply.
  544. *
  545. * @return void
  546. */
  547. public function testCreateFileOverwriteAll()
  548. {
  549. $eol = PHP_EOL;
  550. $path = TMP . 'shell_test';
  551. $files = [
  552. $path . DS . 'file1.php' => 'My first content',
  553. $path . DS . 'file2.php' => 'My second content',
  554. $path . DS . 'file3.php' => 'My third content'
  555. ];
  556. new Folder($path, true);
  557. $this->io->expects($this->once())
  558. ->method('askChoice')
  559. ->will($this->returnValue('a'));
  560. foreach ($files as $file => $contents) {
  561. touch($file);
  562. $this->assertFileExists($file);
  563. $result = $this->Shell->createFile($file, $contents);
  564. $this->assertFileExists($file);
  565. $this->assertTextEquals($contents, file_get_contents($file));
  566. $this->assertTrue($result, 'Did create file.');
  567. }
  568. }
  569. /**
  570. * Test that you can't create files that aren't writable.
  571. *
  572. * @return void
  573. */
  574. public function testCreateFileNoPermissions()
  575. {
  576. $this->skipIf(DS === '\\', 'Cant perform operations using permissions on windows.');
  577. $path = TMP . 'shell_test';
  578. $file = $path . DS . 'no_perms';
  579. if (!is_dir($path)) {
  580. mkdir($path);
  581. }
  582. chmod($path, 0444);
  583. $this->Shell->createFile($file, 'testing');
  584. $this->assertFileNotExists($file);
  585. chmod($path, 0744);
  586. rmdir($path);
  587. }
  588. /**
  589. * test hasTask method
  590. *
  591. * @return void
  592. */
  593. public function testHasTask()
  594. {
  595. $this->Shell->tasks = ['Extract', 'DbConfig'];
  596. $this->Shell->loadTasks();
  597. $this->assertTrue($this->Shell->hasTask('extract'));
  598. $this->assertTrue($this->Shell->hasTask('Extract'));
  599. $this->assertFalse($this->Shell->hasTask('random'));
  600. $this->assertTrue($this->Shell->hasTask('db_config'));
  601. $this->assertTrue($this->Shell->hasTask('DbConfig'));
  602. }
  603. /**
  604. * test the hasMethod
  605. *
  606. * @return void
  607. */
  608. public function testHasMethod()
  609. {
  610. $this->assertTrue($this->Shell->hasMethod('doSomething'));
  611. $this->assertFalse($this->Shell->hasMethod('hr'), 'hr is callable');
  612. $this->assertFalse($this->Shell->hasMethod('_secret'), '_secret is callable');
  613. $this->assertFalse($this->Shell->hasMethod('no_access'), 'no_access is callable');
  614. }
  615. /**
  616. * test run command calling main.
  617. *
  618. * @return void
  619. */
  620. public function testRunCommandMain()
  621. {
  622. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  623. $shell = $this->getMockBuilder('Cake\Console\Shell')
  624. ->setMethods(['main', 'startup'])
  625. ->setConstructorArgs([$io])
  626. ->getMock();
  627. $shell->expects($this->once())->method('startup');
  628. $shell->expects($this->once())->method('main')
  629. ->with('cakes')
  630. ->will($this->returnValue(true));
  631. $result = $shell->runCommand(['cakes', '--verbose']);
  632. $this->assertTrue($result);
  633. $this->assertEquals('main', $shell->command);
  634. }
  635. /**
  636. * test run command calling a real method with no subcommands defined.
  637. *
  638. * @return void
  639. */
  640. public function testRunCommandWithMethod()
  641. {
  642. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  643. $shell = $this->getMockBuilder('Cake\Console\Shell')
  644. ->setMethods(['hitMe', 'startup'])
  645. ->setConstructorArgs([$io])
  646. ->getMock();
  647. $shell->expects($this->once())->method('startup');
  648. $shell->expects($this->once())->method('hitMe')
  649. ->with('cakes')
  650. ->will($this->returnValue(true));
  651. $result = $shell->runCommand(['hit_me', 'cakes', '--verbose'], true);
  652. $this->assertTrue($result);
  653. $this->assertEquals('hit_me', $shell->command);
  654. }
  655. /**
  656. * test that a command called with an extra parameter passed merges the extra parameters
  657. * to the shell's one
  658. * Also tests that if an extra `requested` parameter prevents the welcome message from
  659. * being displayed
  660. *
  661. * @return void
  662. */
  663. public function testRunCommandWithExtra()
  664. {
  665. $Parser = $this->getMockBuilder('Cake\Console\ConsoleOptionParser')
  666. ->setMethods(['help'])
  667. ->setConstructorArgs(['knife'])
  668. ->getMock();
  669. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  670. $Shell = $this->getMockBuilder('Cake\Console\Shell')
  671. ->setMethods(['getOptionParser', 'slice', '_welcome', 'param'])
  672. ->setConstructorArgs([$io])
  673. ->getMock();
  674. $Parser->addSubCommand('slice');
  675. $Shell->expects($this->once())
  676. ->method('getOptionParser')
  677. ->will($this->returnValue($Parser));
  678. $Shell->expects($this->once())
  679. ->method('slice')
  680. ->with('cakes');
  681. $Shell->expects($this->never())->method('_welcome');
  682. $Shell->expects($this->once())->method('param')
  683. ->with('requested')
  684. ->will($this->returnValue(true));
  685. $Shell->runCommand(['slice', 'cakes'], false, ['requested' => true]);
  686. }
  687. /**
  688. * Test the dispatchShell() arguments parser
  689. *
  690. * @return void
  691. */
  692. public function testDispatchShellArgsParser()
  693. {
  694. $Shell = new Shell();
  695. $expected = [['schema', 'create', 'DbAcl'], []];
  696. // Shell::dispatchShell('schema create DbAcl');
  697. $result = $Shell->parseDispatchArguments(['schema create DbAcl']);
  698. $this->assertEquals($expected, $result);
  699. // Shell::dispatchShell('schema', 'create', 'DbAcl');
  700. $result = $Shell->parseDispatchArguments(['schema', 'create', 'DbAcl']);
  701. $this->assertEquals($expected, $result);
  702. // Shell::dispatchShell(['command' => 'schema create DbAcl']);
  703. $result = $Shell->parseDispatchArguments([[
  704. 'command' => 'schema create DbAcl'
  705. ]]);
  706. $this->assertEquals($expected, $result);
  707. // Shell::dispatchShell(['command' => ['schema', 'create', 'DbAcl']]);
  708. $result = $Shell->parseDispatchArguments([[
  709. 'command' => ['schema', 'create', 'DbAcl']
  710. ]]);
  711. $this->assertEquals($expected, $result);
  712. $expected[1] = ['param' => 'value'];
  713. // Shell::dispatchShell(['command' => 'schema create DbAcl', 'extra' => ['param' => 'value']]);
  714. $result = $Shell->parseDispatchArguments([[
  715. 'command' => 'schema create DbAcl',
  716. 'extra' => ['param' => 'value']
  717. ]]);
  718. $this->assertEquals($expected, $result);
  719. // Shell::dispatchShell(['command' => ['schema', 'create', 'DbAcl'], 'extra' => ['param' => 'value']]);
  720. $result = $Shell->parseDispatchArguments([[
  721. 'command' => ['schema', 'create', 'DbAcl'],
  722. 'extra' => ['param' => 'value']
  723. ]]);
  724. $this->assertEquals($expected, $result);
  725. }
  726. /**
  727. * test calling a shell that dispatch another one
  728. *
  729. * @return void
  730. */
  731. public function testDispatchShell()
  732. {
  733. $Shell = new TestingDispatchShell();
  734. ob_start();
  735. $Shell->runCommand(['test_task'], true);
  736. $result = ob_get_clean();
  737. $expected = <<<TEXT
  738. <info>Welcome to CakePHP Console</info>
  739. I am a test task, I dispatch another Shell
  740. I am a dispatched Shell
  741. TEXT;
  742. $this->assertEquals($expected, $result);
  743. ob_start();
  744. $Shell->runCommand(['test_task_dispatch_array'], true);
  745. $result = ob_get_clean();
  746. $this->assertEquals($expected, $result);
  747. ob_start();
  748. $Shell->runCommand(['test_task_dispatch_command_string'], true);
  749. $result = ob_get_clean();
  750. $this->assertEquals($expected, $result);
  751. ob_start();
  752. $Shell->runCommand(['test_task_dispatch_command_array'], true);
  753. $result = ob_get_clean();
  754. $this->assertEquals($expected, $result);
  755. $expected = <<<TEXT
  756. <info>Welcome to CakePHP Console</info>
  757. I am a test task, I dispatch another Shell
  758. I am a dispatched Shell. My param `foo` has the value `bar`
  759. TEXT;
  760. ob_start();
  761. $Shell->runCommand(['test_task_dispatch_with_param'], true);
  762. $result = ob_get_clean();
  763. $this->assertEquals($expected, $result);
  764. $expected = <<<TEXT
  765. <info>Welcome to CakePHP Console</info>
  766. I am a test task, I dispatch another Shell
  767. I am a dispatched Shell. My param `foo` has the value `bar`
  768. My param `fooz` has the value `baz`
  769. TEXT;
  770. ob_start();
  771. $Shell->runCommand(['test_task_dispatch_with_multiple_params'], true);
  772. $result = ob_get_clean();
  773. $this->assertEquals($expected, $result);
  774. $expected = <<<TEXT
  775. <info>Welcome to CakePHP Console</info>
  776. I am a test task, I dispatch another Shell
  777. <info>Welcome to CakePHP Console</info>
  778. I am a dispatched Shell
  779. TEXT;
  780. ob_start();
  781. $Shell->runCommand(['test_task_dispatch_with_requested_off'], true);
  782. $result = ob_get_clean();
  783. $this->assertEquals($expected, $result);
  784. }
  785. /**
  786. * Test that runCommand() doesn't call public methods when the second arg is false.
  787. *
  788. * @return void
  789. */
  790. public function testRunCommandAutoMethodOff()
  791. {
  792. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  793. $shell = $this->getMockBuilder('Cake\Console\Shell')
  794. ->setMethods(['hit_me', 'startup'])
  795. ->setConstructorArgs([$io])
  796. ->getMock();
  797. $shell->expects($this->never())->method('startup');
  798. $shell->expects($this->never())->method('hit_me');
  799. $result = $shell->runCommand(['hit_me', 'baseball'], false);
  800. $this->assertFalse($result);
  801. $result = $shell->runCommand(['hit_me', 'baseball']);
  802. $this->assertFalse($result, 'Default value of runCommand() should be false');
  803. }
  804. /**
  805. * test run command calling a real method with mismatching subcommands defined.
  806. *
  807. * @return void
  808. */
  809. public function testRunCommandWithMethodNotInSubcommands()
  810. {
  811. $parser = $this->getMockBuilder('Cake\Console\ConsoleOptionParser')
  812. ->setMethods(['help'])
  813. ->setConstructorArgs(['knife'])
  814. ->getMock();
  815. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  816. $shell = $this->getMockBuilder('Cake\Console\Shell')
  817. ->setMethods(['getOptionParser', 'roll', 'startup'])
  818. ->setConstructorArgs([$io])
  819. ->getMock();
  820. $parser->addSubCommand('slice');
  821. $shell->expects($this->any())
  822. ->method('getOptionParser')
  823. ->will($this->returnValue($parser));
  824. $parser->expects($this->once())
  825. ->method('help');
  826. $shell->expects($this->never())->method('startup');
  827. $shell->expects($this->never())->method('roll');
  828. $result = $shell->runCommand(['roll', 'cakes', '--verbose']);
  829. $this->assertFalse($result);
  830. }
  831. /**
  832. * test run command calling a real method with subcommands defined.
  833. *
  834. * @return void
  835. */
  836. public function testRunCommandWithMethodInSubcommands()
  837. {
  838. $parser = $this->getMockBuilder('Cake\Console\ConsoleOptionParser')
  839. ->setMethods(['help'])
  840. ->setConstructorArgs(['knife'])
  841. ->getMock();
  842. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  843. $shell = $this->getMockBuilder('Cake\Console\Shell')
  844. ->setMethods(['getOptionParser', 'slice', 'startup'])
  845. ->setConstructorArgs([$io])
  846. ->getMock();
  847. $parser->addSubCommand('slice');
  848. $shell->expects($this->any())
  849. ->method('getOptionParser')
  850. ->will($this->returnValue($parser));
  851. $shell->expects($this->once())->method('startup');
  852. $shell->expects($this->once())
  853. ->method('slice')
  854. ->with('cakes');
  855. $shell->runCommand(['slice', 'cakes', '--verbose']);
  856. }
  857. /**
  858. * test run command calling a missing method with subcommands defined.
  859. *
  860. * @return void
  861. */
  862. public function testRunCommandWithMissingMethodInSubcommands()
  863. {
  864. $parser = $this->getMockBuilder('Cake\Console\ConsoleOptionParser')
  865. ->setMethods(['help'])
  866. ->setConstructorArgs(['knife'])
  867. ->getMock();
  868. $parser->addSubCommand('slice');
  869. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  870. $shell = $this->getMockBuilder('Cake\Console\Shell')
  871. ->setMethods(['getOptionParser', 'startup'])
  872. ->setConstructorArgs([$io])
  873. ->getMock();
  874. $shell->expects($this->any())
  875. ->method('getOptionParser')
  876. ->will($this->returnValue($parser));
  877. $shell->expects($this->never())
  878. ->method('startup');
  879. $parser->expects($this->once())
  880. ->method('help');
  881. $shell->runCommand(['slice', 'cakes', '--verbose']);
  882. }
  883. /**
  884. * test run command causing exception on Shell method.
  885. *
  886. * @return void
  887. */
  888. public function testRunCommandBaseClassMethod()
  889. {
  890. $shell = $this->getMockBuilder('Cake\Console\Shell')
  891. ->setMethods(['startup', 'getOptionParser', 'hr'])
  892. ->disableOriginalConstructor()
  893. ->getMock();
  894. $shell->io(
  895. $this->getMockBuilder('Cake\Console\ConsoleIo')
  896. ->setMethods(['err'])
  897. ->getMock()
  898. );
  899. $parser = $this->getMockBuilder('Cake\Console\ConsoleOptionParser')
  900. ->disableOriginalConstructor()
  901. ->getMock();
  902. $parser->expects($this->once())->method('help');
  903. $shell->expects($this->once())->method('getOptionParser')
  904. ->will($this->returnValue($parser));
  905. $shell->expects($this->never())->method('hr');
  906. $shell->_io->expects($this->exactly(2))->method('err');
  907. $shell->runCommand(['hr']);
  908. }
  909. /**
  910. * test run command causing exception on Shell method.
  911. *
  912. * @return void
  913. */
  914. public function testRunCommandMissingMethod()
  915. {
  916. $shell = $this->getMockBuilder('Cake\Console\Shell')
  917. ->setMethods(['startup', 'getOptionParser', 'hr'])
  918. ->disableOriginalConstructor()
  919. ->getMock();
  920. $shell->io(
  921. $this->getMockBuilder('Cake\Console\ConsoleIo')
  922. ->setMethods(['err'])
  923. ->getMock()
  924. );
  925. $parser = $this->getMockBuilder('Cake\Console\ConsoleOptionParser')
  926. ->disableOriginalConstructor()
  927. ->getMock();
  928. $parser->expects($this->once())->method('help');
  929. $shell->expects($this->once())->method('getOptionParser')
  930. ->will($this->returnValue($parser));
  931. $shell->_io->expects($this->exactly(2))->method('err');
  932. $result = $shell->runCommand(['idontexist']);
  933. $this->assertFalse($result);
  934. }
  935. /**
  936. * test that a --help causes help to show.
  937. *
  938. * @return void
  939. */
  940. public function testRunCommandTriggeringHelp()
  941. {
  942. $parser = $this->getMockBuilder('Cake\Console\ConsoleOptionParser')
  943. ->disableOriginalConstructor()
  944. ->getMock();
  945. $parser->expects($this->once())->method('parse')
  946. ->with(['--help'])
  947. ->will($this->returnValue([['help' => true], []]));
  948. $parser->expects($this->once())->method('help');
  949. $shell = $this->getMockBuilder('Cake\Console\Shell')
  950. ->setMethods(['getOptionParser', 'out', 'startup', '_welcome'])
  951. ->disableOriginalConstructor()
  952. ->getMock();
  953. $shell->io($this->getMockBuilder('Cake\Console\ConsoleIo')->getMock());
  954. $shell->expects($this->once())->method('getOptionParser')
  955. ->will($this->returnValue($parser));
  956. $shell->expects($this->once())->method('out');
  957. $shell->runCommand(['--help']);
  958. }
  959. /**
  960. * test that runCommand will not call runCommand on tasks that are not subcommands.
  961. *
  962. * @return void
  963. */
  964. public function testRunCommandNotCallUnexposedTask()
  965. {
  966. $shell = $this->getMockBuilder('Cake\Console\Shell')
  967. ->setMethods(['startup', 'hasTask'])
  968. ->disableOriginalConstructor()
  969. ->getMock();
  970. $shell->io(
  971. $this->getMockBuilder('Cake\Console\ConsoleIo')
  972. ->setMethods(['err'])
  973. ->getMock()
  974. );
  975. $task = $this->getMockBuilder('Cake\Console\Shell')
  976. ->setMethods(['runCommand'])
  977. ->disableOriginalConstructor()
  978. ->getMock();
  979. $task->expects($this->never())
  980. ->method('runCommand');
  981. $shell->expects($this->any())
  982. ->method('hasTask')
  983. ->will($this->returnValue(true));
  984. $shell->expects($this->never())->method('startup');
  985. $shell->_io->expects($this->exactly(2))->method('err');
  986. $shell->RunCommand = $task;
  987. $result = $shell->runCommand(['run_command', 'one']);
  988. $this->assertFalse($result);
  989. }
  990. /**
  991. * test that runCommand will call runCommand on the task.
  992. *
  993. * @return void
  994. */
  995. public function testRunCommandHittingTaskInSubcommand()
  996. {
  997. $parser = new ConsoleOptionParser('knife');
  998. $parser->addSubcommand('slice');
  999. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  1000. $shell = $this->getMockBuilder('Cake\Console\Shell')
  1001. ->setMethods(['hasTask', 'startup', 'getOptionParser'])
  1002. ->disableOriginalConstructor()
  1003. ->getMock();
  1004. $shell->io($io);
  1005. $task = $this->getMockBuilder('Cake\Console\Shell')
  1006. ->setMethods(['main', 'runCommand'])
  1007. ->disableOriginalConstructor()
  1008. ->getMock();
  1009. $task->io($io);
  1010. $task->expects($this->once())
  1011. ->method('runCommand')
  1012. ->with(['one'], false, ['requested' => true]);
  1013. $shell->expects($this->once())->method('getOptionParser')
  1014. ->will($this->returnValue($parser));
  1015. $shell->expects($this->once())->method('startup');
  1016. $shell->expects($this->any())
  1017. ->method('hasTask')
  1018. ->will($this->returnValue(true));
  1019. $shell->Slice = $task;
  1020. $shell->runCommand(['slice', 'one']);
  1021. }
  1022. /**
  1023. * test that runCommand will invoke a task
  1024. *
  1025. * @return void
  1026. */
  1027. public function testRunCommandInvokeTask()
  1028. {
  1029. $parser = new ConsoleOptionParser('knife');
  1030. $parser->addSubcommand('slice');
  1031. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  1032. $shell = $this->getMockBuilder('Cake\Console\Shell')
  1033. ->setMethods(['hasTask', 'getOptionParser'])
  1034. ->setConstructorArgs([$io])
  1035. ->getMock();
  1036. $task = $this->getMockBuilder('Cake\Console\Shell')
  1037. ->setMethods(['main', '_welcome'])
  1038. ->setConstructorArgs([$io])
  1039. ->getMock();
  1040. $shell->expects($this->once())
  1041. ->method('getOptionParser')
  1042. ->will($this->returnValue($parser));
  1043. $shell->expects($this->any())
  1044. ->method('hasTask')
  1045. ->will($this->returnValue(true));
  1046. $task->expects($this->never())
  1047. ->method('_welcome');
  1048. $shell->Slice = $task;
  1049. $shell->runCommand(['slice', 'one']);
  1050. $this->assertTrue($task->params['requested'], 'Task is requested, no welcome.');
  1051. }
  1052. /**
  1053. * test run command missing parameters
  1054. *
  1055. * @return void
  1056. */
  1057. public function testRunCommandMainMissingArgument()
  1058. {
  1059. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  1060. $shell = $this->getMockBuilder('Cake\Console\Shell')
  1061. ->setMethods(['main', 'startup', 'getOptionParser'])
  1062. ->setConstructorArgs([$io])
  1063. ->getMock();
  1064. $parser = new ConsoleOptionParser('test');
  1065. $parser->addArgument('filename', [
  1066. 'required' => true,
  1067. 'help' => 'a file',
  1068. ]);
  1069. $shell->expects($this->once())
  1070. ->method('getOptionParser')
  1071. ->will($this->returnValue($parser));
  1072. $shell->expects($this->never())->method('main');
  1073. $io->expects($this->once())
  1074. ->method('err')
  1075. ->with('<error>Error: Missing required arguments. filename is required.</error>');
  1076. $result = $shell->runCommand([]);
  1077. $this->assertFalse($result, 'Shell should fail');
  1078. }
  1079. /**
  1080. * test wrapBlock wrapping text.
  1081. *
  1082. * @return void
  1083. */
  1084. public function testWrapText()
  1085. {
  1086. $text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.';
  1087. $result = $this->Shell->wrapText($text, ['width' => 33]);
  1088. $expected = <<<TEXT
  1089. This is the song that never ends.
  1090. This is the song that never ends.
  1091. This is the song that never ends.
  1092. TEXT;
  1093. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  1094. $result = $this->Shell->wrapText($text, ['indent' => ' ', 'width' => 33]);
  1095. $expected = <<<TEXT
  1096. This is the song that never ends.
  1097. This is the song that never ends.
  1098. This is the song that never ends.
  1099. TEXT;
  1100. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  1101. }
  1102. /**
  1103. * Testing camel cased naming of tasks
  1104. *
  1105. * @return void
  1106. */
  1107. public function testShellNaming()
  1108. {
  1109. $this->Shell->tasks = ['TestApple'];
  1110. $this->Shell->loadTasks();
  1111. $expected = 'TestApple';
  1112. $this->assertEquals($expected, $this->Shell->TestApple->name);
  1113. }
  1114. /**
  1115. * Test reading params
  1116. *
  1117. * @dataProvider paramReadingDataProvider
  1118. */
  1119. public function testParamReading($toRead, $expected)
  1120. {
  1121. $this->Shell->params = [
  1122. 'key' => 'value',
  1123. 'help' => false,
  1124. 'emptykey' => '',
  1125. 'truthy' => true
  1126. ];
  1127. $this->assertSame($expected, $this->Shell->param($toRead));
  1128. }
  1129. /**
  1130. * Data provider for testing reading values with Shell::param()
  1131. *
  1132. * @return array
  1133. */
  1134. public function paramReadingDataProvider()
  1135. {
  1136. return [
  1137. [
  1138. 'key',
  1139. 'value',
  1140. ],
  1141. [
  1142. 'help',
  1143. false,
  1144. ],
  1145. [
  1146. 'emptykey',
  1147. '',
  1148. ],
  1149. [
  1150. 'truthy',
  1151. true,
  1152. ],
  1153. [
  1154. 'does_not_exist',
  1155. null,
  1156. ]
  1157. ];
  1158. }
  1159. /**
  1160. * Test that option parsers are created with the correct name/command.
  1161. *
  1162. * @return void
  1163. */
  1164. public function testGetOptionParser()
  1165. {
  1166. $this->Shell->name = 'test';
  1167. $this->Shell->plugin = 'plugin';
  1168. $parser = $this->Shell->getOptionParser();
  1169. $this->assertEquals('plugin.test', $parser->command());
  1170. }
  1171. /**
  1172. * Test file and console and logging quiet output
  1173. *
  1174. * @return void
  1175. */
  1176. public function testQuietLog()
  1177. {
  1178. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')
  1179. ->disableOriginalConstructor()
  1180. ->getMock();
  1181. $io->expects($this->once())
  1182. ->method('level')
  1183. ->with(Shell::QUIET);
  1184. $io->expects($this->at(0))
  1185. ->method('setLoggers')
  1186. ->with(true);
  1187. $io->expects($this->at(2))
  1188. ->method('setLoggers')
  1189. ->with(ConsoleIo::QUIET);
  1190. $this->Shell = $this->getMockBuilder(__NAMESPACE__ . '\ShellTestShell')
  1191. ->setMethods(['welcome'])
  1192. ->setConstructorArgs([$io])
  1193. ->getMock();
  1194. $this->Shell->runCommand(['foo', '--quiet']);
  1195. }
  1196. /**
  1197. * Test getIo() and setIo() methods
  1198. *
  1199. * @return void
  1200. */
  1201. public function testGetSetIo()
  1202. {
  1203. $this->Shell->setIo($this->io);
  1204. $this->assertSame($this->Shell->getIo(), $this->io);
  1205. }
  1206. /**
  1207. * Test setRootName filters into the option parser help text.
  1208. *
  1209. * @return void
  1210. */
  1211. public function testSetRootNamePropagatesToHelpText()
  1212. {
  1213. $this->assertSame($this->Shell, $this->Shell->setRootName('tool'), 'is chainable');
  1214. $this->assertContains('tool shell_test_shell [-h]', $this->Shell->getOptionParser()->help());
  1215. }
  1216. /**
  1217. * Tests __debugInfo
  1218. *
  1219. * @return void
  1220. */
  1221. public function testDebugInfo()
  1222. {
  1223. $expected = [
  1224. 'name' => 'ShellTestShell',
  1225. 'plugin' => null,
  1226. 'command' => null,
  1227. 'tasks' => [],
  1228. 'params' => [],
  1229. 'args' => [],
  1230. 'interactive' => true
  1231. ];
  1232. $result = $this->Shell->__debugInfo();
  1233. $this->assertEquals($expected, $result);
  1234. }
  1235. }