ShellTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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 runCommand() doesn't call public methods when the second arg is false.
  522. *
  523. * @return void
  524. */
  525. public function testRunCommandAutoMethodOff()
  526. {
  527. $io = $this->getMock('Cake\Console\ConsoleIo');
  528. $shell = $this->getMock('Cake\Console\Shell', ['hit_me', 'startup'], [$io]);
  529. $shell->expects($this->never())->method('startup');
  530. $shell->expects($this->never())->method('hit_me');
  531. $result = $shell->runCommand(['hit_me', 'baseball'], false);
  532. $this->assertFalse($result);
  533. $result = $shell->runCommand(['hit_me', 'baseball']);
  534. $this->assertFalse($result, 'Default value of runCommand() should be false');
  535. }
  536. /**
  537. * test run command calling a real method with mismatching subcommands defined.
  538. *
  539. * @return void
  540. */
  541. public function testRunCommandWithMethodNotInSubcommands()
  542. {
  543. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  544. $io = $this->getMock('Cake\Console\ConsoleIo');
  545. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'roll', 'startup'], [$io]);
  546. $parser->addSubCommand('slice');
  547. $shell->expects($this->any())
  548. ->method('getOptionParser')
  549. ->will($this->returnValue($parser));
  550. $parser->expects($this->once())
  551. ->method('help');
  552. $shell->expects($this->never())->method('startup');
  553. $shell->expects($this->never())->method('roll');
  554. $result = $shell->runCommand(['roll', 'cakes', '--verbose']);
  555. $this->assertFalse($result);
  556. }
  557. /**
  558. * test run command calling a real method with subcommands defined.
  559. *
  560. * @return void
  561. */
  562. public function testRunCommandWithMethodInSubcommands()
  563. {
  564. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  565. $io = $this->getMock('Cake\Console\ConsoleIo');
  566. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'slice', 'startup'], [$io]);
  567. $parser->addSubCommand('slice');
  568. $shell->expects($this->any())
  569. ->method('getOptionParser')
  570. ->will($this->returnValue($parser));
  571. $shell->expects($this->once())->method('startup');
  572. $shell->expects($this->once())
  573. ->method('slice')
  574. ->with('cakes');
  575. $shell->runCommand(['slice', 'cakes', '--verbose']);
  576. }
  577. /**
  578. * test run command calling a missing method with subcommands defined.
  579. *
  580. * @return void
  581. */
  582. public function testRunCommandWithMissingMethodInSubcommands()
  583. {
  584. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  585. $parser->addSubCommand('slice');
  586. $io = $this->getMock('Cake\Console\ConsoleIo');
  587. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'startup'], [$io]);
  588. $shell->expects($this->any())
  589. ->method('getOptionParser')
  590. ->will($this->returnValue($parser));
  591. $shell->expects($this->never())
  592. ->method('startup');
  593. $parser->expects($this->once())
  594. ->method('help');
  595. $shell->runCommand(['slice', 'cakes', '--verbose']);
  596. }
  597. /**
  598. * test run command causing exception on Shell method.
  599. *
  600. * @return void
  601. */
  602. public function testRunCommandBaseclassMethod()
  603. {
  604. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'getOptionParser', 'out'], [], '', false);
  605. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [], '', false);
  606. $parser->expects($this->once())->method('help');
  607. $shell->expects($this->once())->method('getOptionParser')
  608. ->will($this->returnValue($parser));
  609. $shell->expects($this->never())->method('hr');
  610. $shell->expects($this->once())->method('out');
  611. $shell->runCommand(['hr']);
  612. }
  613. /**
  614. * test run command causing exception on Shell method.
  615. *
  616. * @return void
  617. */
  618. public function testRunCommandMissingMethod()
  619. {
  620. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'getOptionParser', 'out'], [], '', false);
  621. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [], '', false);
  622. $parser->expects($this->once())->method('help');
  623. $shell->expects($this->once())->method('getOptionParser')
  624. ->will($this->returnValue($parser));
  625. $shell->expects($this->once())->method('out');
  626. $result = $shell->runCommand(['idontexist']);
  627. $this->assertFalse($result);
  628. }
  629. /**
  630. * test that a --help causes help to show.
  631. *
  632. * @return void
  633. */
  634. public function testRunCommandTriggeringHelp()
  635. {
  636. $Parser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [], '', false);
  637. $Parser->expects($this->once())->method('parse')
  638. ->with(['--help'])
  639. ->will($this->returnValue([['help' => true], []]));
  640. $Parser->expects($this->once())->method('help');
  641. $Shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'out', 'startup', '_welcome'], [], '', false);
  642. $Shell->expects($this->once())->method('getOptionParser')
  643. ->will($this->returnValue($Parser));
  644. $Shell->expects($this->once())->method('out');
  645. $Shell->runCommand(['--help']);
  646. }
  647. /**
  648. * test that runCommand will not call runCommand on tasks that are not subcommands.
  649. *
  650. * @return void
  651. */
  652. public function testRunCommandNotCallUnexposedTask()
  653. {
  654. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'hasTask', 'out'], [], '', false);
  655. $task = $this->getMock('Cake\Console\Shell', ['runCommand'], [], '', false);
  656. $task->expects($this->never())
  657. ->method('runCommand');
  658. $shell->expects($this->any())
  659. ->method('hasTask')
  660. ->will($this->returnValue(true));
  661. $shell->expects($this->never())->method('startup');
  662. $shell->expects($this->once())->method('out');
  663. $shell->RunCommand = $task;
  664. $result = $shell->runCommand(['run_command', 'one']);
  665. $this->assertFalse($result);
  666. }
  667. /**
  668. * test that runCommand will call runCommand on the task.
  669. *
  670. * @return void
  671. */
  672. public function testRunCommandHittingTaskInSubcommand()
  673. {
  674. $parser = new ConsoleOptionParser('knife');
  675. $parser->addSubcommand('slice');
  676. $shell = $this->getMock('Cake\Console\Shell', ['hasTask', 'startup', 'getOptionParser'], [], '', false);
  677. $task = $this->getMock('Cake\Console\Shell', ['main', 'runCommand'], [], '', false);
  678. $task->expects($this->once())
  679. ->method('runCommand')
  680. ->with(['one'], false);
  681. $shell->expects($this->once())->method('getOptionParser')
  682. ->will($this->returnValue($parser));
  683. $shell->expects($this->once())->method('startup');
  684. $shell->expects($this->any())
  685. ->method('hasTask')
  686. ->will($this->returnValue(true));
  687. $shell->Slice = $task;
  688. $shell->runCommand(['slice', 'one']);
  689. }
  690. /**
  691. * test wrapBlock wrapping text.
  692. *
  693. * @return void
  694. */
  695. public function testWrapText()
  696. {
  697. $text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.';
  698. $result = $this->Shell->wrapText($text, ['width' => 33]);
  699. $expected = <<<TEXT
  700. This is the song that never ends.
  701. This is the song that never ends.
  702. This is the song that never ends.
  703. TEXT;
  704. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  705. $result = $this->Shell->wrapText($text, ['indent' => ' ', 'width' => 33]);
  706. $expected = <<<TEXT
  707. This is the song that never ends.
  708. This is the song that never ends.
  709. This is the song that never ends.
  710. TEXT;
  711. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  712. }
  713. /**
  714. * Testing camel cased naming of tasks
  715. *
  716. * @return void
  717. */
  718. public function testShellNaming()
  719. {
  720. $this->Shell->tasks = ['TestApple'];
  721. $this->Shell->loadTasks();
  722. $expected = 'TestApple';
  723. $this->assertEquals($expected, $this->Shell->TestApple->name);
  724. }
  725. /**
  726. * Test reading params
  727. *
  728. * @dataProvider paramReadingDataProvider
  729. */
  730. public function testParamReading($toRead, $expected)
  731. {
  732. $this->Shell->params = [
  733. 'key' => 'value',
  734. 'help' => false,
  735. 'emptykey' => '',
  736. 'truthy' => true
  737. ];
  738. $this->assertSame($expected, $this->Shell->param($toRead));
  739. }
  740. /**
  741. * Data provider for testing reading values with Shell::param()
  742. *
  743. * @return array
  744. */
  745. public function paramReadingDataProvider()
  746. {
  747. return [
  748. [
  749. 'key',
  750. 'value',
  751. ],
  752. [
  753. 'help',
  754. false,
  755. ],
  756. [
  757. 'emptykey',
  758. '',
  759. ],
  760. [
  761. 'truthy',
  762. true,
  763. ],
  764. [
  765. 'does_not_exist',
  766. null,
  767. ]
  768. ];
  769. }
  770. /**
  771. * Test that option parsers are created with the correct name/command.
  772. *
  773. * @return void
  774. */
  775. public function testGetOptionParser()
  776. {
  777. $this->Shell->name = 'test';
  778. $this->Shell->plugin = 'plugin';
  779. $parser = $this->Shell->getOptionParser();
  780. $this->assertEquals('plugin.test', $parser->command());
  781. }
  782. /**
  783. * Test file and console and logging quiet output
  784. *
  785. * @return void
  786. */
  787. public function testQuietLog()
  788. {
  789. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  790. $io->expects($this->once())
  791. ->method('level')
  792. ->with(Shell::QUIET);
  793. $io->expects($this->at(0))
  794. ->method('setLoggers')
  795. ->with(true);
  796. $io->expects($this->at(2))
  797. ->method('setLoggers')
  798. ->with(false);
  799. $this->Shell = $this->getMock(__NAMESPACE__ . '\ShellTestShell', ['_useLogger'], [$io]);
  800. $this->Shell->runCommand(['foo', '--quiet']);
  801. }
  802. }