ShellTest.php 25 KB

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