ShellTest.php 38 KB

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