ShellTest.php 40 KB

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