ShellTest.php 39 KB

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