ShellTest.php 28 KB

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