ShellTest.php 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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. * testOut method
  231. *
  232. * @return void
  233. */
  234. public function testOut()
  235. {
  236. $this->io->expects($this->once())
  237. ->method('out')
  238. ->with('Just a test', 1);
  239. $this->Shell->out('Just a test');
  240. }
  241. /**
  242. * testErr method
  243. *
  244. * @return void
  245. */
  246. public function testErr()
  247. {
  248. $this->io->expects($this->once())
  249. ->method('err')
  250. ->with('Just a test', 1);
  251. $this->Shell->err('Just a test');
  252. }
  253. /**
  254. * testNl
  255. *
  256. * @return void
  257. */
  258. public function testNl()
  259. {
  260. $this->io->expects($this->once())
  261. ->method('nl')
  262. ->with(2);
  263. $this->Shell->nl(2);
  264. }
  265. /**
  266. * testHr
  267. *
  268. * @return void
  269. */
  270. public function testHr()
  271. {
  272. $this->io->expects($this->once())
  273. ->method('hr')
  274. ->with(2);
  275. $this->Shell->hr(2);
  276. }
  277. /**
  278. * testError
  279. *
  280. * @return void
  281. */
  282. public function testError()
  283. {
  284. $this->io->expects($this->at(0))
  285. ->method('err')
  286. ->with('<error>Error:</error> Foo Not Found');
  287. $this->io->expects($this->at(1))
  288. ->method('err')
  289. ->with("Searched all...");
  290. $this->Shell->error('Foo Not Found', 'Searched all...');
  291. $this->assertSame($this->Shell->stopped, 1);
  292. }
  293. /**
  294. * testLoadTasks method
  295. *
  296. * @return void
  297. */
  298. public function testLoadTasks()
  299. {
  300. $this->assertTrue($this->Shell->loadTasks());
  301. $this->Shell->tasks = null;
  302. $this->assertTrue($this->Shell->loadTasks());
  303. $this->Shell->tasks = false;
  304. $this->assertTrue($this->Shell->loadTasks());
  305. $this->Shell->tasks = true;
  306. $this->assertTrue($this->Shell->loadTasks());
  307. $this->Shell->tasks = [];
  308. $this->assertTrue($this->Shell->loadTasks());
  309. $this->Shell->tasks = ['TestApple'];
  310. $this->assertTrue($this->Shell->loadTasks());
  311. $this->assertInstanceOf('Cake\Shell\Task\TestAppleTask', $this->Shell->TestApple);
  312. $this->Shell->tasks = 'TestBanana';
  313. $this->assertTrue($this->Shell->loadTasks());
  314. $this->assertInstanceOf('Cake\Shell\Task\TestAppleTask', $this->Shell->TestApple);
  315. $this->assertInstanceOf('Cake\Shell\Task\TestBananaTask', $this->Shell->TestBanana);
  316. unset($this->Shell->ShellTestApple, $this->Shell->TestBanana);
  317. $this->Shell->tasks = ['TestApple', 'TestBanana'];
  318. $this->assertTrue($this->Shell->loadTasks());
  319. $this->assertInstanceOf('Cake\Shell\Task\TestAppleTask', $this->Shell->TestApple);
  320. $this->assertInstanceOf('Cake\Shell\Task\TestBananaTask', $this->Shell->TestBanana);
  321. }
  322. /**
  323. * test that __get() makes args and params references
  324. *
  325. * @return void
  326. */
  327. public function testMagicGetArgAndParamReferences()
  328. {
  329. $this->Shell->tasks = ['TestApple'];
  330. $this->Shell->args = ['one'];
  331. $this->Shell->params = ['help' => false];
  332. $this->Shell->loadTasks();
  333. $result = $this->Shell->TestApple;
  334. $this->Shell->args = ['one', 'two'];
  335. $this->assertSame($this->Shell->args, $result->args);
  336. $this->assertSame($this->Shell->params, $result->params);
  337. }
  338. /**
  339. * testShortPath method
  340. *
  341. * @return void
  342. */
  343. public function testShortPath()
  344. {
  345. $path = $expected = DS . 'tmp/ab/cd';
  346. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  347. $path = $expected = DS . 'tmp/ab/cd/';
  348. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  349. $path = $expected = DS . 'tmp/ab/index.php';
  350. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  351. $path = DS . 'tmp/ab/' . DS . 'cd';
  352. $expected = DS . 'tmp/ab/cd';
  353. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  354. $path = 'tmp/ab';
  355. $expected = 'tmp/ab';
  356. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  357. $path = 'tmp/ab';
  358. $expected = 'tmp/ab';
  359. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  360. $path = APP;
  361. $result = $this->Shell->shortPath($path);
  362. $this->assertNotContains(ROOT, $result, 'Short paths should not contain ROOT');
  363. }
  364. /**
  365. * testCreateFile method
  366. *
  367. * @return void
  368. */
  369. public function testCreateFileNonInteractive()
  370. {
  371. $eol = PHP_EOL;
  372. $path = TMP . 'shell_test';
  373. $file = $path . DS . 'file1.php';
  374. new Folder($path, true);
  375. $contents = "<?php{$eol}echo 'test';${eol}\$te = 'st';{$eol}";
  376. $result = $this->Shell->createFile($file, $contents);
  377. $this->assertTrue($result);
  378. $this->assertTrue(file_exists($file));
  379. $this->assertEquals(file_get_contents($file), $contents);
  380. }
  381. /**
  382. * Test that files are not changed with a 'n' reply.
  383. *
  384. * @return void
  385. */
  386. public function testCreateFileNoReply()
  387. {
  388. $eol = PHP_EOL;
  389. $path = TMP . 'shell_test';
  390. $file = $path . DS . 'file1.php';
  391. new Folder($path, true);
  392. $this->io->expects($this->once())
  393. ->method('askChoice')
  394. ->will($this->returnValue('n'));
  395. touch($file);
  396. $this->assertTrue(file_exists($file));
  397. $contents = "My content";
  398. $result = $this->Shell->createFile($file, $contents);
  399. $this->assertTrue(file_exists($file));
  400. $this->assertTextEquals('', file_get_contents($file));
  401. $this->assertFalse($result, 'Did not create file.');
  402. }
  403. /**
  404. * Test that files are changed with a 'y' reply.
  405. *
  406. * @return void
  407. */
  408. public function testCreateFileOverwrite()
  409. {
  410. $eol = PHP_EOL;
  411. $path = TMP . 'shell_test';
  412. $file = $path . DS . 'file1.php';
  413. new Folder($path, true);
  414. $this->io->expects($this->once())
  415. ->method('askChoice')
  416. ->will($this->returnValue('y'));
  417. touch($file);
  418. $this->assertTrue(file_exists($file));
  419. $contents = "My content";
  420. $result = $this->Shell->createFile($file, $contents);
  421. $this->assertTrue(file_exists($file));
  422. $this->assertTextEquals($contents, file_get_contents($file));
  423. $this->assertTrue($result, 'Did create file.');
  424. }
  425. /**
  426. * Test that there is no user prompt in non-interactive mode while file already exists.
  427. *
  428. * @return void
  429. */
  430. public function testCreateFileOverwriteNonInteractive()
  431. {
  432. $path = TMP . 'shell_test';
  433. $file = $path . DS . 'file1.php';
  434. new Folder($path, true);
  435. touch($file);
  436. $this->assertTrue(file_exists($file));
  437. $this->io->expects($this->never())->method('askChoice');
  438. $this->Shell->interactive = false;
  439. $result = $this->Shell->createFile($file, 'My content');
  440. $this->assertTrue($result);
  441. $this->assertEquals(file_get_contents($file), 'My content');
  442. }
  443. /**
  444. * Test that all files are changed with a 'a' reply.
  445. *
  446. * @return void
  447. */
  448. public function testCreateFileOverwriteAll()
  449. {
  450. $eol = PHP_EOL;
  451. $path = TMP . 'shell_test';
  452. $files = [
  453. $path . DS . 'file1.php' => 'My first content',
  454. $path . DS . 'file2.php' => 'My second content',
  455. $path . DS . 'file3.php' => 'My third content'
  456. ];
  457. new Folder($path, true);
  458. $this->io->expects($this->once())
  459. ->method('askChoice')
  460. ->will($this->returnValue('a'));
  461. foreach ($files as $file => $content) {
  462. touch($file);
  463. $this->assertTrue(file_exists($file));
  464. $contents = "My content";
  465. $result = $this->Shell->createFile($file, $contents);
  466. $this->assertTrue(file_exists($file));
  467. $this->assertTextEquals($contents, file_get_contents($file));
  468. $this->assertTrue($result, 'Did create file.');
  469. }
  470. }
  471. /**
  472. * Test that you can't create files that aren't writable.
  473. *
  474. * @return void
  475. */
  476. public function testCreateFileNoPermissions()
  477. {
  478. $this->skipIf(DS === '\\', 'Cant perform operations using permissions on windows.');
  479. $path = TMP . 'shell_test';
  480. $file = $path . DS . 'no_perms';
  481. if (!is_dir($path)) {
  482. mkdir($path);
  483. }
  484. chmod($path, 0444);
  485. $this->Shell->createFile($file, 'testing');
  486. $this->assertFalse(file_exists($file));
  487. chmod($path, 0744);
  488. rmdir($path);
  489. }
  490. /**
  491. * test hasTask method
  492. *
  493. * @return void
  494. */
  495. public function testHasTask()
  496. {
  497. $this->Shell->tasks = ['Extract', 'DbConfig'];
  498. $this->Shell->loadTasks();
  499. $this->assertTrue($this->Shell->hasTask('extract'));
  500. $this->assertTrue($this->Shell->hasTask('Extract'));
  501. $this->assertFalse($this->Shell->hasTask('random'));
  502. $this->assertTrue($this->Shell->hasTask('db_config'));
  503. $this->assertTrue($this->Shell->hasTask('DbConfig'));
  504. }
  505. /**
  506. * test the hasMethod
  507. *
  508. * @return void
  509. */
  510. public function testHasMethod()
  511. {
  512. $this->assertTrue($this->Shell->hasMethod('doSomething'));
  513. $this->assertFalse($this->Shell->hasMethod('hr'), 'hr is callable');
  514. $this->assertFalse($this->Shell->hasMethod('_secret'), '_secret is callable');
  515. $this->assertFalse($this->Shell->hasMethod('no_access'), 'no_access is callable');
  516. }
  517. /**
  518. * test run command calling main.
  519. *
  520. * @return void
  521. */
  522. public function testRunCommandMain()
  523. {
  524. $io = $this->getMock('Cake\Console\ConsoleIo');
  525. $shell = $this->getMock('Cake\Console\Shell', ['main', 'startup'], [$io]);
  526. $shell->expects($this->once())->method('startup');
  527. $shell->expects($this->once())->method('main')
  528. ->with('cakes')
  529. ->will($this->returnValue(true));
  530. $result = $shell->runCommand(['cakes', '--verbose']);
  531. $this->assertTrue($result);
  532. }
  533. /**
  534. * test run command calling a real method with no subcommands defined.
  535. *
  536. * @return void
  537. */
  538. public function testRunCommandWithMethod()
  539. {
  540. $io = $this->getMock('Cake\Console\ConsoleIo');
  541. $shell = $this->getMock('Cake\Console\Shell', ['hitMe', 'startup'], [$io]);
  542. $shell->expects($this->once())->method('startup');
  543. $shell->expects($this->once())->method('hitMe')
  544. ->with('cakes')
  545. ->will($this->returnValue(true));
  546. $result = $shell->runCommand(['hit_me', 'cakes', '--verbose'], true);
  547. $this->assertTrue($result);
  548. }
  549. /**
  550. * Test that runCommand() doesn't call public methods when the second arg is false.
  551. *
  552. * @return void
  553. */
  554. public function testRunCommandAutoMethodOff()
  555. {
  556. $io = $this->getMock('Cake\Console\ConsoleIo');
  557. $shell = $this->getMock('Cake\Console\Shell', ['hit_me', 'startup'], [$io]);
  558. $shell->expects($this->never())->method('startup');
  559. $shell->expects($this->never())->method('hit_me');
  560. $result = $shell->runCommand(['hit_me', 'baseball'], false);
  561. $this->assertFalse($result);
  562. $result = $shell->runCommand(['hit_me', 'baseball']);
  563. $this->assertFalse($result, 'Default value of runCommand() should be false');
  564. }
  565. /**
  566. * test run command calling a real method with mismatching subcommands defined.
  567. *
  568. * @return void
  569. */
  570. public function testRunCommandWithMethodNotInSubcommands()
  571. {
  572. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  573. $io = $this->getMock('Cake\Console\ConsoleIo');
  574. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'roll', 'startup'], [$io]);
  575. $parser->addSubCommand('slice');
  576. $shell->expects($this->any())
  577. ->method('getOptionParser')
  578. ->will($this->returnValue($parser));
  579. $parser->expects($this->once())
  580. ->method('help');
  581. $shell->expects($this->never())->method('startup');
  582. $shell->expects($this->never())->method('roll');
  583. $result = $shell->runCommand(['roll', 'cakes', '--verbose']);
  584. $this->assertFalse($result);
  585. }
  586. /**
  587. * test run command calling a real method with subcommands defined.
  588. *
  589. * @return void
  590. */
  591. public function testRunCommandWithMethodInSubcommands()
  592. {
  593. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  594. $io = $this->getMock('Cake\Console\ConsoleIo');
  595. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'slice', 'startup'], [$io]);
  596. $parser->addSubCommand('slice');
  597. $shell->expects($this->any())
  598. ->method('getOptionParser')
  599. ->will($this->returnValue($parser));
  600. $shell->expects($this->once())->method('startup');
  601. $shell->expects($this->once())
  602. ->method('slice')
  603. ->with('cakes');
  604. $shell->runCommand(['slice', 'cakes', '--verbose']);
  605. }
  606. /**
  607. * test run command calling a missing method with subcommands defined.
  608. *
  609. * @return void
  610. */
  611. public function testRunCommandWithMissingMethodInSubcommands()
  612. {
  613. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  614. $parser->addSubCommand('slice');
  615. $io = $this->getMock('Cake\Console\ConsoleIo');
  616. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'startup'], [$io]);
  617. $shell->expects($this->any())
  618. ->method('getOptionParser')
  619. ->will($this->returnValue($parser));
  620. $shell->expects($this->never())
  621. ->method('startup');
  622. $parser->expects($this->once())
  623. ->method('help');
  624. $shell->runCommand(['slice', 'cakes', '--verbose']);
  625. }
  626. /**
  627. * test run command causing exception on Shell method.
  628. *
  629. * @return void
  630. */
  631. public function testRunCommandBaseclassMethod()
  632. {
  633. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'getOptionParser', 'out'], [], '', false);
  634. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [], '', false);
  635. $parser->expects($this->once())->method('help');
  636. $shell->expects($this->once())->method('getOptionParser')
  637. ->will($this->returnValue($parser));
  638. $shell->expects($this->never())->method('hr');
  639. $shell->expects($this->once())->method('out');
  640. $shell->runCommand(['hr']);
  641. }
  642. /**
  643. * test run command causing exception on Shell method.
  644. *
  645. * @return void
  646. */
  647. public function testRunCommandMissingMethod()
  648. {
  649. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'getOptionParser', 'out'], [], '', false);
  650. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [], '', false);
  651. $parser->expects($this->once())->method('help');
  652. $shell->expects($this->once())->method('getOptionParser')
  653. ->will($this->returnValue($parser));
  654. $shell->expects($this->once())->method('out');
  655. $result = $shell->runCommand(['idontexist']);
  656. $this->assertFalse($result);
  657. }
  658. /**
  659. * test that a --help causes help to show.
  660. *
  661. * @return void
  662. */
  663. public function testRunCommandTriggeringHelp()
  664. {
  665. $Parser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [], '', false);
  666. $Parser->expects($this->once())->method('parse')
  667. ->with(['--help'])
  668. ->will($this->returnValue([['help' => true], []]));
  669. $Parser->expects($this->once())->method('help');
  670. $Shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'out', 'startup', '_welcome'], [], '', false);
  671. $Shell->expects($this->once())->method('getOptionParser')
  672. ->will($this->returnValue($Parser));
  673. $Shell->expects($this->once())->method('out');
  674. $Shell->runCommand(['--help']);
  675. }
  676. /**
  677. * test that runCommand will not call runCommand on tasks that are not subcommands.
  678. *
  679. * @return void
  680. */
  681. public function testRunCommandNotCallUnexposedTask()
  682. {
  683. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'hasTask', 'out'], [], '', false);
  684. $task = $this->getMock('Cake\Console\Shell', ['runCommand'], [], '', false);
  685. $task->expects($this->never())
  686. ->method('runCommand');
  687. $shell->expects($this->any())
  688. ->method('hasTask')
  689. ->will($this->returnValue(true));
  690. $shell->expects($this->never())->method('startup');
  691. $shell->expects($this->once())->method('out');
  692. $shell->RunCommand = $task;
  693. $result = $shell->runCommand(['run_command', 'one']);
  694. $this->assertFalse($result);
  695. }
  696. /**
  697. * test that runCommand will call runCommand on the task.
  698. *
  699. * @return void
  700. */
  701. public function testRunCommandHittingTaskInSubcommand()
  702. {
  703. $parser = new ConsoleOptionParser('knife');
  704. $parser->addSubcommand('slice');
  705. $shell = $this->getMock('Cake\Console\Shell', ['hasTask', 'startup', 'getOptionParser'], [], '', false);
  706. $task = $this->getMock('Cake\Console\Shell', ['main', 'runCommand'], [], '', false);
  707. $task->expects($this->once())
  708. ->method('runCommand')
  709. ->with(['one'], false);
  710. $shell->expects($this->once())->method('getOptionParser')
  711. ->will($this->returnValue($parser));
  712. $shell->expects($this->once())->method('startup');
  713. $shell->expects($this->any())
  714. ->method('hasTask')
  715. ->will($this->returnValue(true));
  716. $shell->Slice = $task;
  717. $shell->runCommand(['slice', 'one']);
  718. }
  719. /**
  720. * test calling a shell that dispatch another one
  721. *
  722. * @return void
  723. */
  724. public function testDispatchShell()
  725. {
  726. $Shell = new TestingDispatchShell();
  727. ob_start();
  728. $Shell->runCommand(['test_task'], true);
  729. $result = ob_get_clean();
  730. $expected = <<<TEXT
  731. <info>Welcome to CakePHP Console</info>
  732. I am a test task, I dispatch another Shell
  733. <info>Welcome to CakePHP Console</info>
  734. I am a dispatched Shell
  735. TEXT;
  736. $this->assertEquals($expected, $result);
  737. }
  738. /**
  739. * test wrapBlock wrapping text.
  740. *
  741. * @return void
  742. */
  743. public function testWrapText()
  744. {
  745. $text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.';
  746. $result = $this->Shell->wrapText($text, ['width' => 33]);
  747. $expected = <<<TEXT
  748. This is the song that never ends.
  749. This is the song that never ends.
  750. This is the song that never ends.
  751. TEXT;
  752. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  753. $result = $this->Shell->wrapText($text, ['indent' => ' ', 'width' => 33]);
  754. $expected = <<<TEXT
  755. This is the song that never ends.
  756. This is the song that never ends.
  757. This is the song that never ends.
  758. TEXT;
  759. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  760. }
  761. /**
  762. * Testing camel cased naming of tasks
  763. *
  764. * @return void
  765. */
  766. public function testShellNaming()
  767. {
  768. $this->Shell->tasks = ['TestApple'];
  769. $this->Shell->loadTasks();
  770. $expected = 'TestApple';
  771. $this->assertEquals($expected, $this->Shell->TestApple->name);
  772. }
  773. /**
  774. * Test reading params
  775. *
  776. * @dataProvider paramReadingDataProvider
  777. */
  778. public function testParamReading($toRead, $expected)
  779. {
  780. $this->Shell->params = [
  781. 'key' => 'value',
  782. 'help' => false,
  783. 'emptykey' => '',
  784. 'truthy' => true
  785. ];
  786. $this->assertSame($expected, $this->Shell->param($toRead));
  787. }
  788. /**
  789. * Data provider for testing reading values with Shell::param()
  790. *
  791. * @return array
  792. */
  793. public function paramReadingDataProvider()
  794. {
  795. return [
  796. [
  797. 'key',
  798. 'value',
  799. ],
  800. [
  801. 'help',
  802. false,
  803. ],
  804. [
  805. 'emptykey',
  806. '',
  807. ],
  808. [
  809. 'truthy',
  810. true,
  811. ],
  812. [
  813. 'does_not_exist',
  814. null,
  815. ]
  816. ];
  817. }
  818. /**
  819. * Test that option parsers are created with the correct name/command.
  820. *
  821. * @return void
  822. */
  823. public function testGetOptionParser()
  824. {
  825. $this->Shell->name = 'test';
  826. $this->Shell->plugin = 'plugin';
  827. $parser = $this->Shell->getOptionParser();
  828. $this->assertEquals('plugin.test', $parser->command());
  829. }
  830. /**
  831. * Test file and console and logging quiet output
  832. *
  833. * @return void
  834. */
  835. public function testQuietLog()
  836. {
  837. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  838. $io->expects($this->once())
  839. ->method('level')
  840. ->with(Shell::QUIET);
  841. $io->expects($this->at(0))
  842. ->method('setLoggers')
  843. ->with(true);
  844. $io->expects($this->at(2))
  845. ->method('setLoggers')
  846. ->with(false);
  847. $this->Shell = $this->getMock(__NAMESPACE__ . '\ShellTestShell', ['_useLogger'], [$io]);
  848. $this->Shell->runCommand(['foo', '--quiet']);
  849. }
  850. /**
  851. * Tests __debugInfo
  852. *
  853. * @return void
  854. */
  855. public function testDebugInfo()
  856. {
  857. $expected = [
  858. 'name' => 'ShellTestShell',
  859. 'plugin' => null,
  860. 'command' => null,
  861. 'tasks' => [],
  862. 'params' => [],
  863. 'args' => [],
  864. 'interactive' => true
  865. ];
  866. $result = $this->Shell->__debugInfo();
  867. $this->assertEquals($expected, $result);
  868. }
  869. }