ShellTest.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  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 = ['Command', 'Extract'];
  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 = ['Extract' => ['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('error')
  265. ->with('Just a test', 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('error')
  277. ->with(['Just', 'a', 'test'], 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('info')
  289. ->with('Just a test', 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('info')
  301. ->with(['Just', 'a', 'test'], 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('warning')
  313. ->with('Just a test', 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('warning')
  325. ->with(['Just', 'a', 'test'], 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('success')
  337. ->with('Just a test', 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('success')
  349. ->with(['Just', 'a', 'test'], 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', 'Assets'];
  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('assets'));
  601. $this->assertTrue($this->Shell->hasTask('Assets'));
  602. }
  603. /**
  604. * test task loading exception
  605. *
  606. * @expectedException \RuntimeException
  607. * @expectedExceptionMessage Task `DoesNotExist` not found. Maybe you made a typo or a plugin is missing or not loaded?
  608. * @return void
  609. */
  610. public function testMissingTaskException()
  611. {
  612. $this->Shell->tasks = ['DoesNotExist'];
  613. $this->Shell->loadTasks();
  614. }
  615. /**
  616. * test the hasMethod
  617. *
  618. * @return void
  619. */
  620. public function testHasMethod()
  621. {
  622. $this->assertTrue($this->Shell->hasMethod('doSomething'));
  623. $this->assertFalse($this->Shell->hasMethod('hr'), 'hr is callable');
  624. $this->assertFalse($this->Shell->hasMethod('_secret'), '_secret is callable');
  625. $this->assertFalse($this->Shell->hasMethod('no_access'), 'no_access is callable');
  626. }
  627. /**
  628. * test run command calling main.
  629. *
  630. * @return void
  631. */
  632. public function testRunCommandMain()
  633. {
  634. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  635. $shell = $this->getMockBuilder('Cake\Console\Shell')
  636. ->setMethods(['main', 'startup'])
  637. ->setConstructorArgs([$io])
  638. ->getMock();
  639. $shell->expects($this->once())->method('startup');
  640. $shell->expects($this->once())->method('main')
  641. ->with('cakes')
  642. ->will($this->returnValue(true));
  643. $result = $shell->runCommand(['cakes', '--verbose']);
  644. $this->assertTrue($result);
  645. $this->assertEquals('main', $shell->command);
  646. }
  647. /**
  648. * test run command calling a real method with no subcommands defined.
  649. *
  650. * @return void
  651. */
  652. public function testRunCommandWithMethod()
  653. {
  654. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  655. $shell = $this->getMockBuilder('Cake\Console\Shell')
  656. ->setMethods(['hitMe', 'startup'])
  657. ->setConstructorArgs([$io])
  658. ->getMock();
  659. $shell->expects($this->once())->method('startup');
  660. $shell->expects($this->once())->method('hitMe')
  661. ->with('cakes')
  662. ->will($this->returnValue(true));
  663. $result = $shell->runCommand(['hit_me', 'cakes', '--verbose'], true);
  664. $this->assertTrue($result);
  665. $this->assertEquals('hit_me', $shell->command);
  666. }
  667. /**
  668. * test that a command called with an extra parameter passed merges the extra parameters
  669. * to the shell's one
  670. * Also tests that if an extra `requested` parameter prevents the welcome message from
  671. * being displayed
  672. *
  673. * @return void
  674. */
  675. public function testRunCommandWithExtra()
  676. {
  677. $Parser = $this->getMockBuilder('Cake\Console\ConsoleOptionParser')
  678. ->setMethods(['help'])
  679. ->setConstructorArgs(['knife'])
  680. ->getMock();
  681. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  682. $Shell = $this->getMockBuilder('Cake\Console\Shell')
  683. ->setMethods(['getOptionParser', 'slice', '_welcome', 'param'])
  684. ->setConstructorArgs([$io])
  685. ->getMock();
  686. $Parser->addSubCommand('slice');
  687. $Shell->expects($this->once())
  688. ->method('getOptionParser')
  689. ->will($this->returnValue($Parser));
  690. $Shell->expects($this->once())
  691. ->method('slice')
  692. ->with('cakes');
  693. $Shell->expects($this->never())->method('_welcome');
  694. $Shell->expects($this->once())->method('param')
  695. ->with('requested')
  696. ->will($this->returnValue(true));
  697. $Shell->runCommand(['slice', 'cakes'], false, ['requested' => true]);
  698. }
  699. /**
  700. * Test the dispatchShell() arguments parser
  701. *
  702. * @return void
  703. */
  704. public function testDispatchShellArgsParser()
  705. {
  706. $Shell = new Shell();
  707. $expected = [['schema', 'create', 'DbAcl'], []];
  708. // Shell::dispatchShell('schema create DbAcl');
  709. $result = $Shell->parseDispatchArguments(['schema create DbAcl']);
  710. $this->assertEquals($expected, $result);
  711. // Shell::dispatchShell('schema', 'create', 'DbAcl');
  712. $result = $Shell->parseDispatchArguments(['schema', 'create', 'DbAcl']);
  713. $this->assertEquals($expected, $result);
  714. // Shell::dispatchShell(['command' => 'schema create DbAcl']);
  715. $result = $Shell->parseDispatchArguments([[
  716. 'command' => 'schema create DbAcl'
  717. ]]);
  718. $this->assertEquals($expected, $result);
  719. // Shell::dispatchShell(['command' => ['schema', 'create', 'DbAcl']]);
  720. $result = $Shell->parseDispatchArguments([[
  721. 'command' => ['schema', 'create', 'DbAcl']
  722. ]]);
  723. $this->assertEquals($expected, $result);
  724. $expected[1] = ['param' => 'value'];
  725. // Shell::dispatchShell(['command' => 'schema create DbAcl', 'extra' => ['param' => 'value']]);
  726. $result = $Shell->parseDispatchArguments([[
  727. 'command' => 'schema create DbAcl',
  728. 'extra' => ['param' => 'value']
  729. ]]);
  730. $this->assertEquals($expected, $result);
  731. // Shell::dispatchShell(['command' => ['schema', 'create', 'DbAcl'], 'extra' => ['param' => 'value']]);
  732. $result = $Shell->parseDispatchArguments([[
  733. 'command' => ['schema', 'create', 'DbAcl'],
  734. 'extra' => ['param' => 'value']
  735. ]]);
  736. $this->assertEquals($expected, $result);
  737. }
  738. /**
  739. * test calling a shell that dispatch another one
  740. *
  741. * @return void
  742. */
  743. public function testDispatchShell()
  744. {
  745. $Shell = new TestingDispatchShell();
  746. ob_start();
  747. $Shell->runCommand(['test_task'], true);
  748. $result = ob_get_clean();
  749. $expected = <<<TEXT
  750. <info>Welcome to CakePHP Console</info>
  751. I am a test task, I dispatch another Shell
  752. I am a dispatched Shell
  753. TEXT;
  754. $this->assertEquals($expected, $result);
  755. ob_start();
  756. $Shell->runCommand(['test_task_dispatch_array'], true);
  757. $result = ob_get_clean();
  758. $this->assertEquals($expected, $result);
  759. ob_start();
  760. $Shell->runCommand(['test_task_dispatch_command_string'], true);
  761. $result = ob_get_clean();
  762. $this->assertEquals($expected, $result);
  763. ob_start();
  764. $Shell->runCommand(['test_task_dispatch_command_array'], true);
  765. $result = ob_get_clean();
  766. $this->assertEquals($expected, $result);
  767. $expected = <<<TEXT
  768. <info>Welcome to CakePHP Console</info>
  769. I am a test task, I dispatch another Shell
  770. I am a dispatched Shell. My param `foo` has the value `bar`
  771. TEXT;
  772. ob_start();
  773. $Shell->runCommand(['test_task_dispatch_with_param'], true);
  774. $result = ob_get_clean();
  775. $this->assertEquals($expected, $result);
  776. $expected = <<<TEXT
  777. <info>Welcome to CakePHP Console</info>
  778. I am a test task, I dispatch another Shell
  779. I am a dispatched Shell. My param `foo` has the value `bar`
  780. My param `fooz` has the value `baz`
  781. TEXT;
  782. ob_start();
  783. $Shell->runCommand(['test_task_dispatch_with_multiple_params'], true);
  784. $result = ob_get_clean();
  785. $this->assertEquals($expected, $result);
  786. $expected = <<<TEXT
  787. <info>Welcome to CakePHP Console</info>
  788. I am a test task, I dispatch another Shell
  789. <info>Welcome to CakePHP Console</info>
  790. I am a dispatched Shell
  791. TEXT;
  792. ob_start();
  793. $Shell->runCommand(['test_task_dispatch_with_requested_off'], true);
  794. $result = ob_get_clean();
  795. $this->assertEquals($expected, $result);
  796. }
  797. /**
  798. * Test that runCommand() doesn't call public methods when the second arg is false.
  799. *
  800. * @return void
  801. */
  802. public function testRunCommandAutoMethodOff()
  803. {
  804. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  805. $shell = $this->getMockBuilder('Cake\Console\Shell')
  806. ->setMethods(['hit_me', 'startup'])
  807. ->setConstructorArgs([$io])
  808. ->getMock();
  809. $shell->expects($this->never())->method('startup');
  810. $shell->expects($this->never())->method('hit_me');
  811. $result = $shell->runCommand(['hit_me', 'baseball'], false);
  812. $this->assertFalse($result);
  813. $result = $shell->runCommand(['hit_me', 'baseball']);
  814. $this->assertFalse($result, 'Default value of runCommand() should be false');
  815. }
  816. /**
  817. * test run command calling a real method with mismatching subcommands defined.
  818. *
  819. * @return void
  820. */
  821. public function testRunCommandWithMethodNotInSubcommands()
  822. {
  823. $parser = $this->getMockBuilder('Cake\Console\ConsoleOptionParser')
  824. ->setMethods(['help'])
  825. ->setConstructorArgs(['knife'])
  826. ->getMock();
  827. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  828. $shell = $this->getMockBuilder('Cake\Console\Shell')
  829. ->setMethods(['getOptionParser', 'roll', 'startup'])
  830. ->setConstructorArgs([$io])
  831. ->getMock();
  832. $parser->addSubCommand('slice');
  833. $shell->expects($this->any())
  834. ->method('getOptionParser')
  835. ->will($this->returnValue($parser));
  836. $parser->expects($this->once())
  837. ->method('help');
  838. $shell->expects($this->never())->method('startup');
  839. $shell->expects($this->never())->method('roll');
  840. $result = $shell->runCommand(['roll', 'cakes', '--verbose']);
  841. $this->assertFalse($result);
  842. }
  843. /**
  844. * test run command calling a real method with subcommands defined.
  845. *
  846. * @return void
  847. */
  848. public function testRunCommandWithMethodInSubcommands()
  849. {
  850. $parser = $this->getMockBuilder('Cake\Console\ConsoleOptionParser')
  851. ->setMethods(['help'])
  852. ->setConstructorArgs(['knife'])
  853. ->getMock();
  854. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  855. $shell = $this->getMockBuilder('Cake\Console\Shell')
  856. ->setMethods(['getOptionParser', 'slice', 'startup'])
  857. ->setConstructorArgs([$io])
  858. ->getMock();
  859. $parser->addSubCommand('slice');
  860. $shell->expects($this->any())
  861. ->method('getOptionParser')
  862. ->will($this->returnValue($parser));
  863. $shell->expects($this->once())->method('startup');
  864. $shell->expects($this->once())
  865. ->method('slice')
  866. ->with('cakes');
  867. $shell->runCommand(['slice', 'cakes', '--verbose']);
  868. }
  869. /**
  870. * test run command calling a missing method with subcommands defined.
  871. *
  872. * @return void
  873. */
  874. public function testRunCommandWithMissingMethodInSubcommands()
  875. {
  876. $parser = $this->getMockBuilder('Cake\Console\ConsoleOptionParser')
  877. ->setMethods(['help'])
  878. ->setConstructorArgs(['knife'])
  879. ->getMock();
  880. $parser->addSubCommand('slice');
  881. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  882. $shell = $this->getMockBuilder('Cake\Console\Shell')
  883. ->setMethods(['getOptionParser', 'startup'])
  884. ->setConstructorArgs([$io])
  885. ->getMock();
  886. $shell->expects($this->any())
  887. ->method('getOptionParser')
  888. ->will($this->returnValue($parser));
  889. $shell->expects($this->never())
  890. ->method('startup');
  891. $parser->expects($this->once())
  892. ->method('help');
  893. $shell->runCommand(['slice', 'cakes', '--verbose']);
  894. }
  895. /**
  896. * test run command causing exception on Shell method.
  897. *
  898. * @return void
  899. */
  900. public function testRunCommandBaseClassMethod()
  901. {
  902. $shell = $this->getMockBuilder('Cake\Console\Shell')
  903. ->setMethods(['startup', 'getOptionParser', 'hr'])
  904. ->disableOriginalConstructor()
  905. ->getMock();
  906. $shell->io(
  907. $this->getMockBuilder('Cake\Console\ConsoleIo')
  908. ->setMethods(['err'])
  909. ->getMock()
  910. );
  911. $parser = $this->getMockBuilder('Cake\Console\ConsoleOptionParser')
  912. ->disableOriginalConstructor()
  913. ->getMock();
  914. $parser->expects($this->once())->method('help');
  915. $shell->expects($this->once())->method('getOptionParser')
  916. ->will($this->returnValue($parser));
  917. $shell->expects($this->never())->method('hr');
  918. $shell->_io->expects($this->exactly(2))->method('err');
  919. $shell->runCommand(['hr']);
  920. }
  921. /**
  922. * test run command causing exception on Shell method.
  923. *
  924. * @return void
  925. */
  926. public function testRunCommandMissingMethod()
  927. {
  928. $shell = $this->getMockBuilder('Cake\Console\Shell')
  929. ->setMethods(['startup', 'getOptionParser', 'hr'])
  930. ->disableOriginalConstructor()
  931. ->getMock();
  932. $shell->io(
  933. $this->getMockBuilder('Cake\Console\ConsoleIo')
  934. ->setMethods(['err'])
  935. ->getMock()
  936. );
  937. $parser = $this->getMockBuilder('Cake\Console\ConsoleOptionParser')
  938. ->disableOriginalConstructor()
  939. ->getMock();
  940. $parser->expects($this->once())->method('help');
  941. $shell->expects($this->once())->method('getOptionParser')
  942. ->will($this->returnValue($parser));
  943. $shell->_io->expects($this->exactly(2))->method('err');
  944. $result = $shell->runCommand(['idontexist']);
  945. $this->assertFalse($result);
  946. }
  947. /**
  948. * test that a --help causes help to show.
  949. *
  950. * @return void
  951. */
  952. public function testRunCommandTriggeringHelp()
  953. {
  954. $parser = $this->getMockBuilder('Cake\Console\ConsoleOptionParser')
  955. ->disableOriginalConstructor()
  956. ->getMock();
  957. $parser->expects($this->once())->method('parse')
  958. ->with(['--help'])
  959. ->will($this->returnValue([['help' => true], []]));
  960. $parser->expects($this->once())->method('help');
  961. $shell = $this->getMockBuilder('Cake\Console\Shell')
  962. ->setMethods(['getOptionParser', 'out', 'startup', '_welcome'])
  963. ->disableOriginalConstructor()
  964. ->getMock();
  965. $shell->io($this->getMockBuilder('Cake\Console\ConsoleIo')->getMock());
  966. $shell->expects($this->once())->method('getOptionParser')
  967. ->will($this->returnValue($parser));
  968. $shell->expects($this->once())->method('out');
  969. $shell->runCommand(['--help']);
  970. }
  971. /**
  972. * test that runCommand will not call runCommand on tasks that are not subcommands.
  973. *
  974. * @return void
  975. */
  976. public function testRunCommandNotCallUnexposedTask()
  977. {
  978. $shell = $this->getMockBuilder('Cake\Console\Shell')
  979. ->setMethods(['startup', 'hasTask'])
  980. ->disableOriginalConstructor()
  981. ->getMock();
  982. $shell->io(
  983. $this->getMockBuilder('Cake\Console\ConsoleIo')
  984. ->setMethods(['err'])
  985. ->getMock()
  986. );
  987. $task = $this->getMockBuilder('Cake\Console\Shell')
  988. ->setMethods(['runCommand'])
  989. ->disableOriginalConstructor()
  990. ->getMock();
  991. $task->expects($this->never())
  992. ->method('runCommand');
  993. $shell->expects($this->any())
  994. ->method('hasTask')
  995. ->will($this->returnValue(true));
  996. $shell->expects($this->never())->method('startup');
  997. $shell->_io->expects($this->exactly(2))->method('err');
  998. $shell->RunCommand = $task;
  999. $result = $shell->runCommand(['run_command', 'one']);
  1000. $this->assertFalse($result);
  1001. }
  1002. /**
  1003. * test that runCommand will call runCommand on the task.
  1004. *
  1005. * @return void
  1006. */
  1007. public function testRunCommandHittingTaskInSubcommand()
  1008. {
  1009. $parser = new ConsoleOptionParser('knife');
  1010. $parser->addSubcommand('slice');
  1011. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  1012. $shell = $this->getMockBuilder('Cake\Console\Shell')
  1013. ->setMethods(['hasTask', 'startup', 'getOptionParser'])
  1014. ->disableOriginalConstructor()
  1015. ->getMock();
  1016. $shell->io($io);
  1017. $task = $this->getMockBuilder('Cake\Console\Shell')
  1018. ->setMethods(['main', 'runCommand'])
  1019. ->disableOriginalConstructor()
  1020. ->getMock();
  1021. $task->io($io);
  1022. $task->expects($this->once())
  1023. ->method('runCommand')
  1024. ->with(['one'], false, ['requested' => true]);
  1025. $shell->expects($this->once())->method('getOptionParser')
  1026. ->will($this->returnValue($parser));
  1027. $shell->expects($this->once())->method('startup');
  1028. $shell->expects($this->any())
  1029. ->method('hasTask')
  1030. ->will($this->returnValue(true));
  1031. $shell->Slice = $task;
  1032. $shell->runCommand(['slice', 'one']);
  1033. }
  1034. /**
  1035. * test that runCommand will invoke a task
  1036. *
  1037. * @return void
  1038. */
  1039. public function testRunCommandInvokeTask()
  1040. {
  1041. $parser = new ConsoleOptionParser('knife');
  1042. $parser->addSubcommand('slice');
  1043. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  1044. $shell = $this->getMockBuilder('Cake\Console\Shell')
  1045. ->setMethods(['hasTask', 'getOptionParser'])
  1046. ->setConstructorArgs([$io])
  1047. ->getMock();
  1048. $task = $this->getMockBuilder('Cake\Console\Shell')
  1049. ->setMethods(['main', '_welcome'])
  1050. ->setConstructorArgs([$io])
  1051. ->getMock();
  1052. $shell->expects($this->once())
  1053. ->method('getOptionParser')
  1054. ->will($this->returnValue($parser));
  1055. $shell->expects($this->any())
  1056. ->method('hasTask')
  1057. ->will($this->returnValue(true));
  1058. $task->expects($this->never())
  1059. ->method('_welcome');
  1060. $shell->Slice = $task;
  1061. $shell->runCommand(['slice', 'one']);
  1062. $this->assertTrue($task->params['requested'], 'Task is requested, no welcome.');
  1063. }
  1064. /**
  1065. * test run command missing parameters
  1066. *
  1067. * @return void
  1068. */
  1069. public function testRunCommandMainMissingArgument()
  1070. {
  1071. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  1072. $shell = $this->getMockBuilder('Cake\Console\Shell')
  1073. ->setMethods(['main', 'startup', 'getOptionParser'])
  1074. ->setConstructorArgs([$io])
  1075. ->getMock();
  1076. $parser = new ConsoleOptionParser('test');
  1077. $parser->addArgument('filename', [
  1078. 'required' => true,
  1079. 'help' => 'a file',
  1080. ]);
  1081. $shell->expects($this->once())
  1082. ->method('getOptionParser')
  1083. ->will($this->returnValue($parser));
  1084. $shell->expects($this->never())->method('main');
  1085. $io->expects($this->once())
  1086. ->method('error')
  1087. ->with('Error: Missing required arguments. filename is required.');
  1088. $result = $shell->runCommand([]);
  1089. $this->assertFalse($result, 'Shell should fail');
  1090. }
  1091. /**
  1092. * test wrapBlock wrapping text.
  1093. *
  1094. * @return void
  1095. */
  1096. public function testWrapText()
  1097. {
  1098. $text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.';
  1099. $result = $this->Shell->wrapText($text, ['width' => 33]);
  1100. $expected = <<<TEXT
  1101. This is the song that never ends.
  1102. This is the song that never ends.
  1103. This is the song that never ends.
  1104. TEXT;
  1105. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  1106. $result = $this->Shell->wrapText($text, ['indent' => ' ', 'width' => 33]);
  1107. $expected = <<<TEXT
  1108. This is the song that never ends.
  1109. This is the song that never ends.
  1110. This is the song that never ends.
  1111. TEXT;
  1112. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  1113. }
  1114. /**
  1115. * Testing camel cased naming of tasks
  1116. *
  1117. * @return void
  1118. */
  1119. public function testShellNaming()
  1120. {
  1121. $this->Shell->tasks = ['TestApple'];
  1122. $this->Shell->loadTasks();
  1123. $expected = 'TestApple';
  1124. $this->assertEquals($expected, $this->Shell->TestApple->name);
  1125. }
  1126. /**
  1127. * Test reading params
  1128. *
  1129. * @dataProvider paramReadingDataProvider
  1130. */
  1131. public function testParamReading($toRead, $expected)
  1132. {
  1133. $this->Shell->params = [
  1134. 'key' => 'value',
  1135. 'help' => false,
  1136. 'emptykey' => '',
  1137. 'truthy' => true
  1138. ];
  1139. $this->assertSame($expected, $this->Shell->param($toRead));
  1140. }
  1141. /**
  1142. * Data provider for testing reading values with Shell::param()
  1143. *
  1144. * @return array
  1145. */
  1146. public function paramReadingDataProvider()
  1147. {
  1148. return [
  1149. [
  1150. 'key',
  1151. 'value',
  1152. ],
  1153. [
  1154. 'help',
  1155. false,
  1156. ],
  1157. [
  1158. 'emptykey',
  1159. '',
  1160. ],
  1161. [
  1162. 'truthy',
  1163. true,
  1164. ],
  1165. [
  1166. 'does_not_exist',
  1167. null,
  1168. ]
  1169. ];
  1170. }
  1171. /**
  1172. * Test that option parsers are created with the correct name/command.
  1173. *
  1174. * @return void
  1175. */
  1176. public function testGetOptionParser()
  1177. {
  1178. $this->Shell->name = 'test';
  1179. $this->Shell->plugin = 'plugin';
  1180. $parser = $this->Shell->getOptionParser();
  1181. $this->assertEquals('plugin.test', $parser->command());
  1182. }
  1183. /**
  1184. * Test file and console and logging quiet output
  1185. *
  1186. * @return void
  1187. */
  1188. public function testQuietLog()
  1189. {
  1190. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')
  1191. ->disableOriginalConstructor()
  1192. ->getMock();
  1193. $io->expects($this->once())
  1194. ->method('level')
  1195. ->with(Shell::QUIET);
  1196. $io->expects($this->at(0))
  1197. ->method('setLoggers')
  1198. ->with(true);
  1199. $io->expects($this->at(2))
  1200. ->method('setLoggers')
  1201. ->with(ConsoleIo::QUIET);
  1202. $this->Shell = $this->getMockBuilder(__NAMESPACE__ . '\ShellTestShell')
  1203. ->setMethods(['welcome'])
  1204. ->setConstructorArgs([$io])
  1205. ->getMock();
  1206. $this->Shell->runCommand(['foo', '--quiet']);
  1207. }
  1208. /**
  1209. * Test getIo() and setIo() methods
  1210. *
  1211. * @return void
  1212. */
  1213. public function testGetSetIo()
  1214. {
  1215. $this->Shell->setIo($this->io);
  1216. $this->assertSame($this->Shell->getIo(), $this->io);
  1217. }
  1218. /**
  1219. * Test setRootName filters into the option parser help text.
  1220. *
  1221. * @return void
  1222. */
  1223. public function testSetRootNamePropagatesToHelpText()
  1224. {
  1225. $this->assertSame($this->Shell, $this->Shell->setRootName('tool'), 'is chainable');
  1226. $this->assertContains('tool shell_test_shell [-h]', $this->Shell->getOptionParser()->help());
  1227. }
  1228. /**
  1229. * Tests __debugInfo
  1230. *
  1231. * @return void
  1232. */
  1233. public function testDebugInfo()
  1234. {
  1235. $expected = [
  1236. 'name' => 'ShellTestShell',
  1237. 'plugin' => null,
  1238. 'command' => null,
  1239. 'tasks' => [],
  1240. 'params' => [],
  1241. 'args' => [],
  1242. 'interactive' => true
  1243. ];
  1244. $result = $this->Shell->__debugInfo();
  1245. $this->assertEquals($expected, $result);
  1246. }
  1247. }