ShellTest.php 38 KB

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