ShellTest.php 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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 runCommand() doesn't call public methods when the second arg is false.
  574. *
  575. * @return void
  576. */
  577. public function testRunCommandAutoMethodOff()
  578. {
  579. $io = $this->getMock('Cake\Console\ConsoleIo');
  580. $shell = $this->getMock('Cake\Console\Shell', ['hit_me', 'startup'], [$io]);
  581. $shell->expects($this->never())->method('startup');
  582. $shell->expects($this->never())->method('hit_me');
  583. $result = $shell->runCommand(['hit_me', 'baseball'], false);
  584. $this->assertFalse($result);
  585. $result = $shell->runCommand(['hit_me', 'baseball']);
  586. $this->assertFalse($result, 'Default value of runCommand() should be false');
  587. }
  588. /**
  589. * test run command calling a real method with mismatching subcommands defined.
  590. *
  591. * @return void
  592. */
  593. public function testRunCommandWithMethodNotInSubcommands()
  594. {
  595. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  596. $io = $this->getMock('Cake\Console\ConsoleIo');
  597. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'roll', 'startup'], [$io]);
  598. $parser->addSubCommand('slice');
  599. $shell->expects($this->any())
  600. ->method('getOptionParser')
  601. ->will($this->returnValue($parser));
  602. $parser->expects($this->once())
  603. ->method('help');
  604. $shell->expects($this->never())->method('startup');
  605. $shell->expects($this->never())->method('roll');
  606. $result = $shell->runCommand(['roll', 'cakes', '--verbose']);
  607. $this->assertFalse($result);
  608. }
  609. /**
  610. * test run command calling a real method with subcommands defined.
  611. *
  612. * @return void
  613. */
  614. public function testRunCommandWithMethodInSubcommands()
  615. {
  616. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  617. $io = $this->getMock('Cake\Console\ConsoleIo');
  618. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'slice', 'startup'], [$io]);
  619. $parser->addSubCommand('slice');
  620. $shell->expects($this->any())
  621. ->method('getOptionParser')
  622. ->will($this->returnValue($parser));
  623. $shell->expects($this->once())->method('startup');
  624. $shell->expects($this->once())
  625. ->method('slice')
  626. ->with('cakes');
  627. $shell->runCommand(['slice', 'cakes', '--verbose']);
  628. }
  629. /**
  630. * test run command calling a missing method with subcommands defined.
  631. *
  632. * @return void
  633. */
  634. public function testRunCommandWithMissingMethodInSubcommands()
  635. {
  636. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  637. $parser->addSubCommand('slice');
  638. $io = $this->getMock('Cake\Console\ConsoleIo');
  639. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'startup'], [$io]);
  640. $shell->expects($this->any())
  641. ->method('getOptionParser')
  642. ->will($this->returnValue($parser));
  643. $shell->expects($this->never())
  644. ->method('startup');
  645. $parser->expects($this->once())
  646. ->method('help');
  647. $shell->runCommand(['slice', 'cakes', '--verbose']);
  648. }
  649. /**
  650. * test run command causing exception on Shell method.
  651. *
  652. * @return void
  653. */
  654. public function testRunCommandBaseclassMethod()
  655. {
  656. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'getOptionParser', 'out'], [], '', false);
  657. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [], '', false);
  658. $parser->expects($this->once())->method('help');
  659. $shell->expects($this->once())->method('getOptionParser')
  660. ->will($this->returnValue($parser));
  661. $shell->expects($this->never())->method('hr');
  662. $shell->expects($this->once())->method('out');
  663. $shell->runCommand(['hr']);
  664. }
  665. /**
  666. * test run command causing exception on Shell method.
  667. *
  668. * @return void
  669. */
  670. public function testRunCommandMissingMethod()
  671. {
  672. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'getOptionParser', 'out'], [], '', false);
  673. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [], '', false);
  674. $parser->expects($this->once())->method('help');
  675. $shell->expects($this->once())->method('getOptionParser')
  676. ->will($this->returnValue($parser));
  677. $shell->expects($this->once())->method('out');
  678. $result = $shell->runCommand(['idontexist']);
  679. $this->assertFalse($result);
  680. }
  681. /**
  682. * test that a --help causes help to show.
  683. *
  684. * @return void
  685. */
  686. public function testRunCommandTriggeringHelp()
  687. {
  688. $Parser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [], '', false);
  689. $Parser->expects($this->once())->method('parse')
  690. ->with(['--help'])
  691. ->will($this->returnValue([['help' => true], []]));
  692. $Parser->expects($this->once())->method('help');
  693. $Shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'out', 'startup', '_welcome'], [], '', false);
  694. $Shell->expects($this->once())->method('getOptionParser')
  695. ->will($this->returnValue($Parser));
  696. $Shell->expects($this->once())->method('out');
  697. $Shell->runCommand(['--help']);
  698. }
  699. /**
  700. * test that runCommand will not call runCommand on tasks that are not subcommands.
  701. *
  702. * @return void
  703. */
  704. public function testRunCommandNotCallUnexposedTask()
  705. {
  706. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'hasTask', 'out'], [], '', false);
  707. $task = $this->getMock('Cake\Console\Shell', ['runCommand'], [], '', false);
  708. $task->expects($this->never())
  709. ->method('runCommand');
  710. $shell->expects($this->any())
  711. ->method('hasTask')
  712. ->will($this->returnValue(true));
  713. $shell->expects($this->never())->method('startup');
  714. $shell->expects($this->once())->method('out');
  715. $shell->RunCommand = $task;
  716. $result = $shell->runCommand(['run_command', 'one']);
  717. $this->assertFalse($result);
  718. }
  719. /**
  720. * test that runCommand will call runCommand on the task.
  721. *
  722. * @return void
  723. */
  724. public function testRunCommandHittingTaskInSubcommand()
  725. {
  726. $parser = new ConsoleOptionParser('knife');
  727. $parser->addSubcommand('slice');
  728. $shell = $this->getMock('Cake\Console\Shell', ['hasTask', 'startup', 'getOptionParser'], [], '', false);
  729. $task = $this->getMock('Cake\Console\Shell', ['main', 'runCommand'], [], '', false);
  730. $task->expects($this->once())
  731. ->method('runCommand')
  732. ->with(['one'], false);
  733. $shell->expects($this->once())->method('getOptionParser')
  734. ->will($this->returnValue($parser));
  735. $shell->expects($this->once())->method('startup');
  736. $shell->expects($this->any())
  737. ->method('hasTask')
  738. ->will($this->returnValue(true));
  739. $shell->Slice = $task;
  740. $shell->runCommand(['slice', 'one']);
  741. }
  742. /**
  743. * test calling a shell that dispatch another one
  744. *
  745. * @return void
  746. */
  747. public function testDispatchShell()
  748. {
  749. $Shell = new TestingDispatchShell();
  750. ob_start();
  751. $Shell->runCommand(['test_task'], true);
  752. $result = ob_get_clean();
  753. $expected = <<<TEXT
  754. <info>Welcome to CakePHP Console</info>
  755. I am a test task, I dispatch another Shell
  756. <info>Welcome to CakePHP Console</info>
  757. I am a dispatched Shell
  758. TEXT;
  759. $this->assertEquals($expected, $result);
  760. }
  761. /**
  762. * test wrapBlock wrapping text.
  763. *
  764. * @return void
  765. */
  766. public function testWrapText()
  767. {
  768. $text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.';
  769. $result = $this->Shell->wrapText($text, ['width' => 33]);
  770. $expected = <<<TEXT
  771. This is the song that never ends.
  772. This is the song that never ends.
  773. This is the song that never ends.
  774. TEXT;
  775. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  776. $result = $this->Shell->wrapText($text, ['indent' => ' ', 'width' => 33]);
  777. $expected = <<<TEXT
  778. This is the song that never ends.
  779. This is the song that never ends.
  780. This is the song that never ends.
  781. TEXT;
  782. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  783. }
  784. /**
  785. * Testing camel cased naming of tasks
  786. *
  787. * @return void
  788. */
  789. public function testShellNaming()
  790. {
  791. $this->Shell->tasks = ['TestApple'];
  792. $this->Shell->loadTasks();
  793. $expected = 'TestApple';
  794. $this->assertEquals($expected, $this->Shell->TestApple->name);
  795. }
  796. /**
  797. * Test reading params
  798. *
  799. * @dataProvider paramReadingDataProvider
  800. */
  801. public function testParamReading($toRead, $expected)
  802. {
  803. $this->Shell->params = [
  804. 'key' => 'value',
  805. 'help' => false,
  806. 'emptykey' => '',
  807. 'truthy' => true
  808. ];
  809. $this->assertSame($expected, $this->Shell->param($toRead));
  810. }
  811. /**
  812. * Data provider for testing reading values with Shell::param()
  813. *
  814. * @return array
  815. */
  816. public function paramReadingDataProvider()
  817. {
  818. return [
  819. [
  820. 'key',
  821. 'value',
  822. ],
  823. [
  824. 'help',
  825. false,
  826. ],
  827. [
  828. 'emptykey',
  829. '',
  830. ],
  831. [
  832. 'truthy',
  833. true,
  834. ],
  835. [
  836. 'does_not_exist',
  837. null,
  838. ]
  839. ];
  840. }
  841. /**
  842. * Test that option parsers are created with the correct name/command.
  843. *
  844. * @return void
  845. */
  846. public function testGetOptionParser()
  847. {
  848. $this->Shell->name = 'test';
  849. $this->Shell->plugin = 'plugin';
  850. $parser = $this->Shell->getOptionParser();
  851. $this->assertEquals('plugin.test', $parser->command());
  852. }
  853. /**
  854. * Test file and console and logging quiet output
  855. *
  856. * @return void
  857. */
  858. public function testQuietLog()
  859. {
  860. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  861. $io->expects($this->once())
  862. ->method('level')
  863. ->with(Shell::QUIET);
  864. $io->expects($this->at(0))
  865. ->method('setLoggers')
  866. ->with(true);
  867. $io->expects($this->at(2))
  868. ->method('setLoggers')
  869. ->with(false);
  870. $this->Shell = $this->getMock(__NAMESPACE__ . '\ShellTestShell', ['_useLogger'], [$io]);
  871. $this->Shell->runCommand(['foo', '--quiet']);
  872. }
  873. /**
  874. * Tests __debugInfo
  875. *
  876. * @return void
  877. */
  878. public function testDebugInfo()
  879. {
  880. $expected = [
  881. 'name' => 'ShellTestShell',
  882. 'plugin' => null,
  883. 'command' => null,
  884. 'tasks' => [],
  885. 'params' => [],
  886. 'args' => [],
  887. 'interactive' => true
  888. ];
  889. $result = $this->Shell->__debugInfo();
  890. $this->assertEquals($expected, $result);
  891. }
  892. }