ShellTest.php 32 KB

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