ShellTest.php 33 KB

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