ShellTest.php 40 KB

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