ShellTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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 you can't create files that aren't writable.
  445. *
  446. * @return void
  447. */
  448. public function testCreateFileNoPermissions()
  449. {
  450. $this->skipIf(DS === '\\', 'Cant perform operations using permissions on windows.');
  451. $path = TMP . 'shell_test';
  452. $file = $path . DS . 'no_perms';
  453. if (!is_dir($path)) {
  454. mkdir($path);
  455. }
  456. chmod($path, 0444);
  457. $this->Shell->createFile($file, 'testing');
  458. $this->assertFalse(file_exists($file));
  459. chmod($path, 0744);
  460. rmdir($path);
  461. }
  462. /**
  463. * test hasTask method
  464. *
  465. * @return void
  466. */
  467. public function testHasTask()
  468. {
  469. $this->Shell->tasks = ['Extract', 'DbConfig'];
  470. $this->Shell->loadTasks();
  471. $this->assertTrue($this->Shell->hasTask('extract'));
  472. $this->assertTrue($this->Shell->hasTask('Extract'));
  473. $this->assertFalse($this->Shell->hasTask('random'));
  474. $this->assertTrue($this->Shell->hasTask('db_config'));
  475. $this->assertTrue($this->Shell->hasTask('DbConfig'));
  476. }
  477. /**
  478. * test the hasMethod
  479. *
  480. * @return void
  481. */
  482. public function testHasMethod()
  483. {
  484. $this->assertTrue($this->Shell->hasMethod('doSomething'));
  485. $this->assertFalse($this->Shell->hasMethod('hr'), 'hr is callable');
  486. $this->assertFalse($this->Shell->hasMethod('_secret'), '_secret is callable');
  487. $this->assertFalse($this->Shell->hasMethod('no_access'), 'no_access is callable');
  488. }
  489. /**
  490. * test run command calling main.
  491. *
  492. * @return void
  493. */
  494. public function testRunCommandMain()
  495. {
  496. $io = $this->getMock('Cake\Console\ConsoleIo');
  497. $shell = $this->getMock('Cake\Console\Shell', ['main', 'startup'], [$io]);
  498. $shell->expects($this->once())->method('startup');
  499. $shell->expects($this->once())->method('main')
  500. ->with('cakes')
  501. ->will($this->returnValue(true));
  502. $result = $shell->runCommand(['cakes', '--verbose']);
  503. $this->assertTrue($result);
  504. }
  505. /**
  506. * test run command calling a real method with no subcommands defined.
  507. *
  508. * @return void
  509. */
  510. public function testRunCommandWithMethod()
  511. {
  512. $io = $this->getMock('Cake\Console\ConsoleIo');
  513. $shell = $this->getMock('Cake\Console\Shell', ['hitMe', 'startup'], [$io]);
  514. $shell->expects($this->once())->method('startup');
  515. $shell->expects($this->once())->method('hitMe')
  516. ->with('cakes')
  517. ->will($this->returnValue(true));
  518. $result = $shell->runCommand(['hit_me', 'cakes', '--verbose'], true);
  519. $this->assertTrue($result);
  520. }
  521. /**
  522. * Test that runCommand() doesn't call public methods when the second arg is false.
  523. *
  524. * @return void
  525. */
  526. public function testRunCommandAutoMethodOff()
  527. {
  528. $io = $this->getMock('Cake\Console\ConsoleIo');
  529. $shell = $this->getMock('Cake\Console\Shell', ['hit_me', 'startup'], [$io]);
  530. $shell->expects($this->never())->method('startup');
  531. $shell->expects($this->never())->method('hit_me');
  532. $result = $shell->runCommand(['hit_me', 'baseball'], false);
  533. $this->assertFalse($result);
  534. $result = $shell->runCommand(['hit_me', 'baseball']);
  535. $this->assertFalse($result, 'Default value of runCommand() should be false');
  536. }
  537. /**
  538. * test run command calling a real method with mismatching subcommands defined.
  539. *
  540. * @return void
  541. */
  542. public function testRunCommandWithMethodNotInSubcommands()
  543. {
  544. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  545. $io = $this->getMock('Cake\Console\ConsoleIo');
  546. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'roll', 'startup'], [$io]);
  547. $parser->addSubCommand('slice');
  548. $shell->expects($this->any())
  549. ->method('getOptionParser')
  550. ->will($this->returnValue($parser));
  551. $parser->expects($this->once())
  552. ->method('help');
  553. $shell->expects($this->never())->method('startup');
  554. $shell->expects($this->never())->method('roll');
  555. $result = $shell->runCommand(['roll', 'cakes', '--verbose']);
  556. $this->assertFalse($result);
  557. }
  558. /**
  559. * test run command calling a real method with subcommands defined.
  560. *
  561. * @return void
  562. */
  563. public function testRunCommandWithMethodInSubcommands()
  564. {
  565. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  566. $io = $this->getMock('Cake\Console\ConsoleIo');
  567. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'slice', 'startup'], [$io]);
  568. $parser->addSubCommand('slice');
  569. $shell->expects($this->any())
  570. ->method('getOptionParser')
  571. ->will($this->returnValue($parser));
  572. $shell->expects($this->once())->method('startup');
  573. $shell->expects($this->once())
  574. ->method('slice')
  575. ->with('cakes');
  576. $shell->runCommand(['slice', 'cakes', '--verbose']);
  577. }
  578. /**
  579. * test run command calling a missing method with subcommands defined.
  580. *
  581. * @return void
  582. */
  583. public function testRunCommandWithMissingMethodInSubcommands()
  584. {
  585. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  586. $parser->addSubCommand('slice');
  587. $io = $this->getMock('Cake\Console\ConsoleIo');
  588. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'startup'], [$io]);
  589. $shell->expects($this->any())
  590. ->method('getOptionParser')
  591. ->will($this->returnValue($parser));
  592. $shell->expects($this->never())
  593. ->method('startup');
  594. $parser->expects($this->once())
  595. ->method('help');
  596. $shell->runCommand(['slice', 'cakes', '--verbose']);
  597. }
  598. /**
  599. * test run command causing exception on Shell method.
  600. *
  601. * @return void
  602. */
  603. public function testRunCommandBaseclassMethod()
  604. {
  605. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'getOptionParser', 'out'], [], '', false);
  606. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [], '', false);
  607. $parser->expects($this->once())->method('help');
  608. $shell->expects($this->once())->method('getOptionParser')
  609. ->will($this->returnValue($parser));
  610. $shell->expects($this->never())->method('hr');
  611. $shell->expects($this->once())->method('out');
  612. $shell->runCommand(['hr']);
  613. }
  614. /**
  615. * test run command causing exception on Shell method.
  616. *
  617. * @return void
  618. */
  619. public function testRunCommandMissingMethod()
  620. {
  621. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'getOptionParser', 'out'], [], '', false);
  622. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [], '', false);
  623. $parser->expects($this->once())->method('help');
  624. $shell->expects($this->once())->method('getOptionParser')
  625. ->will($this->returnValue($parser));
  626. $shell->expects($this->once())->method('out');
  627. $result = $shell->runCommand(['idontexist']);
  628. $this->assertFalse($result);
  629. }
  630. /**
  631. * test that a --help causes help to show.
  632. *
  633. * @return void
  634. */
  635. public function testRunCommandTriggeringHelp()
  636. {
  637. $Parser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [], '', false);
  638. $Parser->expects($this->once())->method('parse')
  639. ->with(['--help'])
  640. ->will($this->returnValue([['help' => true], []]));
  641. $Parser->expects($this->once())->method('help');
  642. $Shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'out', 'startup', '_welcome'], [], '', false);
  643. $Shell->expects($this->once())->method('getOptionParser')
  644. ->will($this->returnValue($Parser));
  645. $Shell->expects($this->once())->method('out');
  646. $Shell->runCommand(['--help']);
  647. }
  648. /**
  649. * test that runCommand will not call runCommand on tasks that are not subcommands.
  650. *
  651. * @return void
  652. */
  653. public function testRunCommandNotCallUnexposedTask()
  654. {
  655. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'hasTask', 'out'], [], '', false);
  656. $task = $this->getMock('Cake\Console\Shell', ['runCommand'], [], '', false);
  657. $task->expects($this->never())
  658. ->method('runCommand');
  659. $shell->expects($this->any())
  660. ->method('hasTask')
  661. ->will($this->returnValue(true));
  662. $shell->expects($this->never())->method('startup');
  663. $shell->expects($this->once())->method('out');
  664. $shell->RunCommand = $task;
  665. $result = $shell->runCommand(['run_command', 'one']);
  666. $this->assertFalse($result);
  667. }
  668. /**
  669. * test that runCommand will call runCommand on the task.
  670. *
  671. * @return void
  672. */
  673. public function testRunCommandHittingTaskInSubcommand()
  674. {
  675. $parser = new ConsoleOptionParser('knife');
  676. $parser->addSubcommand('slice');
  677. $shell = $this->getMock('Cake\Console\Shell', ['hasTask', 'startup', 'getOptionParser'], [], '', false);
  678. $task = $this->getMock('Cake\Console\Shell', ['main', 'runCommand'], [], '', false);
  679. $task->expects($this->once())
  680. ->method('runCommand')
  681. ->with(['one'], false);
  682. $shell->expects($this->once())->method('getOptionParser')
  683. ->will($this->returnValue($parser));
  684. $shell->expects($this->once())->method('startup');
  685. $shell->expects($this->any())
  686. ->method('hasTask')
  687. ->will($this->returnValue(true));
  688. $shell->Slice = $task;
  689. $shell->runCommand(['slice', 'one']);
  690. }
  691. /**
  692. * test calling a shell that dispatch another one
  693. *
  694. * @return void
  695. */
  696. public function testDispatchShell()
  697. {
  698. $Shell = new TestingDispatchShell();
  699. ob_start();
  700. $Shell->runCommand(['test_task'], true);
  701. $result = ob_get_clean();
  702. $expected = <<<TEXT
  703. <info>Welcome to CakePHP v3.0.1 Console</info>
  704. I am a test task, I dispatch another Shell
  705. <info>Welcome to CakePHP v3.0.1 Console</info>
  706. I am a dispatched Shell
  707. TEXT;
  708. $this->assertEquals($expected, $result);
  709. }
  710. /**
  711. * test wrapBlock wrapping text.
  712. *
  713. * @return void
  714. */
  715. public function testWrapText()
  716. {
  717. $text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.';
  718. $result = $this->Shell->wrapText($text, ['width' => 33]);
  719. $expected = <<<TEXT
  720. This is the song that never ends.
  721. This is the song that never ends.
  722. This is the song that never ends.
  723. TEXT;
  724. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  725. $result = $this->Shell->wrapText($text, ['indent' => ' ', 'width' => 33]);
  726. $expected = <<<TEXT
  727. This is the song that never ends.
  728. This is the song that never ends.
  729. This is the song that never ends.
  730. TEXT;
  731. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  732. }
  733. /**
  734. * Testing camel cased naming of tasks
  735. *
  736. * @return void
  737. */
  738. public function testShellNaming()
  739. {
  740. $this->Shell->tasks = ['TestApple'];
  741. $this->Shell->loadTasks();
  742. $expected = 'TestApple';
  743. $this->assertEquals($expected, $this->Shell->TestApple->name);
  744. }
  745. /**
  746. * Test reading params
  747. *
  748. * @dataProvider paramReadingDataProvider
  749. */
  750. public function testParamReading($toRead, $expected)
  751. {
  752. $this->Shell->params = [
  753. 'key' => 'value',
  754. 'help' => false,
  755. 'emptykey' => '',
  756. 'truthy' => true
  757. ];
  758. $this->assertSame($expected, $this->Shell->param($toRead));
  759. }
  760. /**
  761. * Data provider for testing reading values with Shell::param()
  762. *
  763. * @return array
  764. */
  765. public function paramReadingDataProvider()
  766. {
  767. return [
  768. [
  769. 'key',
  770. 'value',
  771. ],
  772. [
  773. 'help',
  774. false,
  775. ],
  776. [
  777. 'emptykey',
  778. '',
  779. ],
  780. [
  781. 'truthy',
  782. true,
  783. ],
  784. [
  785. 'does_not_exist',
  786. null,
  787. ]
  788. ];
  789. }
  790. /**
  791. * Test that option parsers are created with the correct name/command.
  792. *
  793. * @return void
  794. */
  795. public function testGetOptionParser()
  796. {
  797. $this->Shell->name = 'test';
  798. $this->Shell->plugin = 'plugin';
  799. $parser = $this->Shell->getOptionParser();
  800. $this->assertEquals('plugin.test', $parser->command());
  801. }
  802. /**
  803. * Test file and console and logging quiet output
  804. *
  805. * @return void
  806. */
  807. public function testQuietLog()
  808. {
  809. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  810. $io->expects($this->once())
  811. ->method('level')
  812. ->with(Shell::QUIET);
  813. $io->expects($this->at(0))
  814. ->method('setLoggers')
  815. ->with(true);
  816. $io->expects($this->at(2))
  817. ->method('setLoggers')
  818. ->with(false);
  819. $this->Shell = $this->getMock(__NAMESPACE__ . '\ShellTestShell', ['_useLogger'], [$io]);
  820. $this->Shell->runCommand(['foo', '--quiet']);
  821. }
  822. }