ShellTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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\Log\Log;
  22. use Cake\TestSuite\TestCase;
  23. use Cake\Utility\Folder;
  24. use Cake\Utility\Hash;
  25. /**
  26. * Class for testing merging vars
  27. */
  28. class MergeShell extends Shell {
  29. public $tasks = array('DbConfig', 'Fixture');
  30. public $modelClass = 'Articles';
  31. }
  32. /**
  33. * ShellTestShell class
  34. *
  35. */
  36. class ShellTestShell extends Shell {
  37. /**
  38. * name property
  39. *
  40. * @var name
  41. */
  42. public $name = 'ShellTestShell';
  43. /**
  44. * stopped property
  45. *
  46. * @var int
  47. */
  48. public $stopped;
  49. /**
  50. * testMessage property
  51. *
  52. * @var string
  53. */
  54. public $testMessage = 'all your base are belong to us';
  55. /**
  56. * stop method
  57. *
  58. * @param int $status
  59. * @return void
  60. */
  61. protected function _stop($status = 0) {
  62. $this->stopped = $status;
  63. }
  64. protected function _secret() {
  65. }
  66. //@codingStandardsIgnoreStart
  67. public function do_something() {
  68. }
  69. protected function no_access() {
  70. }
  71. public function log_something() {
  72. $this->log($this->testMessage);
  73. }
  74. //@codingStandardsIgnoreEnd
  75. public function useLogger($enable = true) {
  76. $this->_useLogger($enable);
  77. }
  78. }
  79. /**
  80. * TestAppleTask class
  81. *
  82. */
  83. class TestAppleTask extends Shell {
  84. }
  85. /**
  86. * TestBananaTask class
  87. *
  88. */
  89. class TestBananaTask extends Shell {
  90. }
  91. class_alias(__NAMESPACE__ . '\TestAppleTask', 'Cake\Console\Command\Task\TestAppleTask');
  92. class_alias(__NAMESPACE__ . '\TestBananaTask', 'Cake\Console\Command\Task\TestBananaTask');
  93. /**
  94. * ShellTest class
  95. *
  96. */
  97. class ShellTest extends TestCase {
  98. /**
  99. * Fixtures used in this test case
  100. *
  101. * @var array
  102. */
  103. public $fixtures = array(
  104. 'core.post', 'core.comment', 'core.article', 'core.user',
  105. 'core.tag', 'core.articles_tag', 'core.attachment'
  106. );
  107. /**
  108. * setUp method
  109. *
  110. * @return void
  111. */
  112. public function setUp() {
  113. parent::setUp();
  114. $this->io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  115. $this->Shell = new ShellTestShell($this->io);
  116. if (is_dir(TMP . 'shell_test')) {
  117. $Folder = new Folder(TMP . 'shell_test');
  118. $Folder->delete();
  119. }
  120. }
  121. /**
  122. * testConstruct method
  123. *
  124. * @return void
  125. */
  126. public function testConstruct() {
  127. $this->assertEquals('ShellTestShell', $this->Shell->name);
  128. $this->assertInstanceOf('Cake\Console\ConsoleIo', $this->Shell->io());
  129. }
  130. /**
  131. * testInitialize method
  132. *
  133. * @return void
  134. */
  135. public function testInitialize() {
  136. Configure::write('App.namespace', 'TestApp');
  137. Plugin::load('TestPlugin');
  138. $this->Shell->tasks = array('DbConfig' => array('one', 'two'));
  139. $this->Shell->plugin = 'TestPlugin';
  140. $this->Shell->modelClass = 'TestPluginComments';
  141. $this->Shell->initialize();
  142. $this->assertTrue(isset($this->Shell->TestPluginComments));
  143. $this->assertInstanceOf(
  144. 'TestPlugin\Model\Table\TestPluginCommentsTable',
  145. $this->Shell->TestPluginComments
  146. );
  147. }
  148. /**
  149. * test LoadModel method
  150. *
  151. * @return void
  152. */
  153. public function testLoadModel() {
  154. Configure::write('App.namespace', 'TestApp');
  155. $Shell = new MergeShell();
  156. $this->assertInstanceOf(
  157. 'TestApp\Model\Table\ArticlesTable',
  158. $Shell->Articles
  159. );
  160. $this->assertEquals('Articles', $Shell->modelClass);
  161. Plugin::load('TestPlugin');
  162. $this->Shell->loadModel('TestPlugin.TestPluginComments');
  163. $this->assertTrue(isset($this->Shell->TestPluginComments));
  164. $this->assertInstanceOf(
  165. 'TestPlugin\Model\Table\TestPluginCommentsTable',
  166. $this->Shell->TestPluginComments
  167. );
  168. }
  169. /**
  170. * testIn method
  171. *
  172. * @return void
  173. */
  174. public function testIn() {
  175. $this->io->expects($this->at(0))
  176. ->method('askChoice')
  177. ->with('Just a test?', ['y', 'n'], 'n')
  178. ->will($this->returnValue('n'));
  179. $this->io->expects($this->at(1))
  180. ->method('ask')
  181. ->with('Just a test?', 'n')
  182. ->will($this->returnValue('n'));
  183. $result = $this->Shell->in('Just a test?', array('y', 'n'), 'n');
  184. $this->assertEquals('n', $result);
  185. $result = $this->Shell->in('Just a test?', null, 'n');
  186. $this->assertEquals('n', $result);
  187. }
  188. /**
  189. * Test in() when not interactive.
  190. *
  191. * @return void
  192. */
  193. public function testInNonInteractive() {
  194. $this->io->expects($this->never())
  195. ->method('askChoice');
  196. $this->io->expects($this->never())
  197. ->method('ask');
  198. $this->Shell->interactive = false;
  199. $result = $this->Shell->in('Just a test?', 'y/n', 'n');
  200. $this->assertEquals('n', $result);
  201. }
  202. /**
  203. * testOut method
  204. *
  205. * @return void
  206. */
  207. public function testOut() {
  208. $this->io->expects($this->once())
  209. ->method('out')
  210. ->with('Just a test', 1);
  211. $this->Shell->out('Just a test');
  212. }
  213. /**
  214. * testErr method
  215. *
  216. * @return void
  217. */
  218. public function testErr() {
  219. $this->io->expects($this->once())
  220. ->method('err')
  221. ->with('Just a test', 1);
  222. $this->Shell->err('Just a test');
  223. }
  224. /**
  225. * testNl
  226. *
  227. * @return void
  228. */
  229. public function testNl() {
  230. $this->io->expects($this->once())
  231. ->method('nl')
  232. ->with(2);
  233. $this->Shell->nl(2);
  234. }
  235. /**
  236. * testHr
  237. *
  238. * @return void
  239. */
  240. public function testHr() {
  241. $this->io->expects($this->once())
  242. ->method('hr')
  243. ->with(2);
  244. $this->Shell->hr(2);
  245. }
  246. /**
  247. * testError
  248. *
  249. * @return void
  250. */
  251. public function testError() {
  252. $this->io->expects($this->at(0))
  253. ->method('err')
  254. ->with('<error>Error:</error> Foo Not Found');
  255. $this->io->expects($this->at(1))
  256. ->method('err')
  257. ->with("Searched all...");
  258. $this->Shell->error('Foo Not Found', 'Searched all...');
  259. $this->assertSame($this->Shell->stopped, 1);
  260. }
  261. /**
  262. * testLoadTasks method
  263. *
  264. * @return void
  265. */
  266. public function testLoadTasks() {
  267. $this->assertTrue($this->Shell->loadTasks());
  268. $this->Shell->tasks = null;
  269. $this->assertTrue($this->Shell->loadTasks());
  270. $this->Shell->tasks = false;
  271. $this->assertTrue($this->Shell->loadTasks());
  272. $this->Shell->tasks = true;
  273. $this->assertTrue($this->Shell->loadTasks());
  274. $this->Shell->tasks = array();
  275. $this->assertTrue($this->Shell->loadTasks());
  276. $this->Shell->tasks = array('TestApple');
  277. $this->assertTrue($this->Shell->loadTasks());
  278. $this->assertInstanceOf('Cake\Console\Command\Task\TestAppleTask', $this->Shell->TestApple);
  279. $this->Shell->tasks = 'TestBanana';
  280. $this->assertTrue($this->Shell->loadTasks());
  281. $this->assertInstanceOf('Cake\Console\Command\Task\TestAppleTask', $this->Shell->TestApple);
  282. $this->assertInstanceOf('Cake\Console\Command\Task\TestBananaTask', $this->Shell->TestBanana);
  283. unset($this->Shell->ShellTestApple, $this->Shell->TestBanana);
  284. $this->Shell->tasks = array('TestApple', 'TestBanana');
  285. $this->assertTrue($this->Shell->loadTasks());
  286. $this->assertInstanceOf('Cake\Console\Command\Task\TestAppleTask', $this->Shell->TestApple);
  287. $this->assertInstanceOf('Cake\Console\Command\Task\TestBananaTask', $this->Shell->TestBanana);
  288. }
  289. /**
  290. * test that __get() makes args and params references
  291. *
  292. * @return void
  293. */
  294. public function testMagicGetArgAndParamReferences() {
  295. $this->Shell->tasks = array('TestApple');
  296. $this->Shell->args = array('one');
  297. $this->Shell->params = array('help' => false);
  298. $this->Shell->loadTasks();
  299. $result = $this->Shell->TestApple;
  300. $this->Shell->args = array('one', 'two');
  301. $this->assertSame($this->Shell->args, $result->args);
  302. $this->assertSame($this->Shell->params, $result->params);
  303. }
  304. /**
  305. * testShortPath method
  306. *
  307. * @return void
  308. */
  309. public function testShortPath() {
  310. $path = $expected = DS . 'tmp/ab/cd';
  311. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  312. $path = $expected = DS . 'tmp/ab/cd/';
  313. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  314. $path = $expected = DS . 'tmp/ab/index.php';
  315. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  316. $path = DS . 'tmp/ab/' . DS . 'cd';
  317. $expected = DS . 'tmp/ab/cd';
  318. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  319. $path = 'tmp/ab';
  320. $expected = 'tmp/ab';
  321. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  322. $path = 'tmp/ab';
  323. $expected = 'tmp/ab';
  324. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  325. $path = APP;
  326. $result = $this->Shell->shortPath($path);
  327. $this->assertNotContains(ROOT, $result, 'Short paths should not contain ROOT');
  328. }
  329. /**
  330. * testCreateFile method
  331. *
  332. * @return void
  333. */
  334. public function testCreateFileNonInteractive() {
  335. $eol = PHP_EOL;
  336. $path = TMP . 'shell_test';
  337. $file = $path . DS . 'file1.php';
  338. new Folder($path, true);
  339. $contents = "<?php{$eol}echo 'test';${eol}\$te = 'st';{$eol}";
  340. $result = $this->Shell->createFile($file, $contents);
  341. $this->assertTrue($result);
  342. $this->assertTrue(file_exists($file));
  343. $this->assertEquals(file_get_contents($file), $contents);
  344. }
  345. /**
  346. * Test that files are not changed with a 'n' reply.
  347. *
  348. * @return void
  349. */
  350. public function testCreateFileNoReply() {
  351. $eol = PHP_EOL;
  352. $path = TMP . 'shell_test';
  353. $file = $path . DS . 'file1.php';
  354. new Folder($path, true);
  355. $this->io->expects($this->once())
  356. ->method('askChoice')
  357. ->will($this->returnValue('n'));
  358. touch($file);
  359. $this->assertTrue(file_exists($file));
  360. $contents = "My content";
  361. $result = $this->Shell->createFile($file, $contents);
  362. $this->assertTrue(file_exists($file));
  363. $this->assertTextEquals('', file_get_contents($file));
  364. $this->assertFalse($result, 'Did not create file.');
  365. }
  366. /**
  367. * Test that files are changed with a 'y' reply.
  368. *
  369. * @return void
  370. */
  371. public function testCreateFileOverwrite() {
  372. $eol = PHP_EOL;
  373. $path = TMP . 'shell_test';
  374. $file = $path . DS . 'file1.php';
  375. new Folder($path, true);
  376. $this->io->expects($this->once())
  377. ->method('askChoice')
  378. ->will($this->returnValue('y'));
  379. touch($file);
  380. $this->assertTrue(file_exists($file));
  381. $contents = "My content";
  382. $result = $this->Shell->createFile($file, $contents);
  383. $this->assertTrue(file_exists($file));
  384. $this->assertTextEquals($contents, file_get_contents($file));
  385. $this->assertTrue($result, 'Did create file.');
  386. }
  387. /**
  388. * Test that you can't create files that aren't writable.
  389. *
  390. * @return void
  391. */
  392. public function testCreateFileNoPermissions() {
  393. $this->skipIf(DIRECTORY_SEPARATOR === '\\', 'Cant perform operations using permissions on windows.');
  394. $path = TMP . 'shell_test';
  395. $file = $path . DS . 'no_perms';
  396. if (!is_dir($path)) {
  397. mkdir($path);
  398. }
  399. chmod($path, 0444);
  400. $this->Shell->createFile($file, 'testing');
  401. $this->assertFalse(file_exists($file));
  402. chmod($path, 0744);
  403. rmdir($path);
  404. }
  405. /**
  406. * test hasTask method
  407. *
  408. * @return void
  409. */
  410. public function testHasTask() {
  411. $this->Shell->tasks = array('Extract', 'DbConfig');
  412. $this->Shell->loadTasks();
  413. $this->assertTrue($this->Shell->hasTask('extract'));
  414. $this->assertTrue($this->Shell->hasTask('Extract'));
  415. $this->assertFalse($this->Shell->hasTask('random'));
  416. $this->assertTrue($this->Shell->hasTask('db_config'));
  417. $this->assertTrue($this->Shell->hasTask('DbConfig'));
  418. }
  419. /**
  420. * test the hasMethod
  421. *
  422. * @return void
  423. */
  424. public function testHasMethod() {
  425. $this->assertTrue($this->Shell->hasMethod('do_something'));
  426. $this->assertFalse($this->Shell->hasMethod('hr'), 'hr is callable');
  427. $this->assertFalse($this->Shell->hasMethod('_secret'), '_secret is callable');
  428. $this->assertFalse($this->Shell->hasMethod('no_access'), 'no_access is callable');
  429. }
  430. /**
  431. * test run command calling main.
  432. *
  433. * @return void
  434. */
  435. public function testRunCommandMain() {
  436. $io = $this->getMock('Cake\Console\ConsoleIo');
  437. $shell = $this->getMock('Cake\Console\Shell', ['main', 'startup'], [$io]);
  438. $shell->expects($this->once())->method('startup');
  439. $shell->expects($this->once())->method('main')
  440. ->with('cakes')
  441. ->will($this->returnValue(true));
  442. $result = $shell->runCommand(['cakes', '--verbose']);
  443. $this->assertTrue($result);
  444. }
  445. /**
  446. * test run command calling a real method with no subcommands defined.
  447. *
  448. * @return void
  449. */
  450. public function testRunCommandWithMethod() {
  451. $io = $this->getMock('Cake\Console\ConsoleIo');
  452. $shell = $this->getMock('Cake\Console\Shell', ['hit_me', 'startup'], [$io]);
  453. $shell->expects($this->once())->method('startup');
  454. $shell->expects($this->once())->method('hit_me')
  455. ->with('cakes')
  456. ->will($this->returnValue(true));
  457. $result = $shell->runCommand(['hit_me', 'cakes', '--verbose'], true);
  458. $this->assertTrue($result);
  459. }
  460. /**
  461. * Test that runCommand() doesn't call public methods when the second arg is false.
  462. *
  463. * @return void
  464. */
  465. public function testRunCommandAutoMethodOff() {
  466. $io = $this->getMock('Cake\Console\ConsoleIo');
  467. $shell = $this->getMock('Cake\Console\Shell', ['hit_me', 'startup'], [$io]);
  468. $shell->expects($this->never())->method('startup');
  469. $shell->expects($this->never())->method('hit_me');
  470. $result = $shell->runCommand(['hit_me', 'baseball'], false);
  471. $this->assertFalse($result);
  472. $result = $shell->runCommand(['hit_me', 'baseball']);
  473. $this->assertFalse($result, 'Default value of runCommand() should be false');
  474. }
  475. /**
  476. * test run command calling a real method with mismatching subcommands defined.
  477. *
  478. * @return void
  479. */
  480. public function testRunCommandWithMethodNotInSubcommands() {
  481. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  482. $io = $this->getMock('Cake\Console\ConsoleIo');
  483. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'roll', 'startup'], [$io]);
  484. $parser->addSubCommand('slice');
  485. $shell->expects($this->any())
  486. ->method('getOptionParser')
  487. ->will($this->returnValue($parser));
  488. $parser->expects($this->once())
  489. ->method('help');
  490. $shell->expects($this->never())->method('startup');
  491. $shell->expects($this->never())->method('roll');
  492. $result = $shell->runCommand(['roll', 'cakes', '--verbose']);
  493. $this->assertFalse($result);
  494. }
  495. /**
  496. * test run command calling a real method with subcommands defined.
  497. *
  498. * @return void
  499. */
  500. public function testRunCommandWithMethodInSubcommands() {
  501. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  502. $io = $this->getMock('Cake\Console\ConsoleIo');
  503. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'slice', 'startup'], [$io]);
  504. $parser->addSubCommand('slice');
  505. $shell->expects($this->any())
  506. ->method('getOptionParser')
  507. ->will($this->returnValue($parser));
  508. $shell->expects($this->once())->method('startup');
  509. $shell->expects($this->once())
  510. ->method('slice')
  511. ->with('cakes');
  512. $shell->runCommand(['slice', 'cakes', '--verbose']);
  513. }
  514. /**
  515. * test run command calling a missing method with subcommands defined.
  516. *
  517. * @return void
  518. */
  519. public function testRunCommandWithMissingMethodInSubcommands() {
  520. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  521. $parser->addSubCommand('slice');
  522. $io = $this->getMock('Cake\Console\ConsoleIo');
  523. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'startup'], [$io]);
  524. $shell->expects($this->any())
  525. ->method('getOptionParser')
  526. ->will($this->returnValue($parser));
  527. $shell->expects($this->never())
  528. ->method('startup');
  529. $parser->expects($this->once())
  530. ->method('help');
  531. $shell->runCommand(['slice', 'cakes', '--verbose']);
  532. }
  533. /**
  534. * test run command causing exception on Shell method.
  535. *
  536. * @return void
  537. */
  538. public function testRunCommandBaseclassMethod() {
  539. $shell = $this->getMock('Cake\Console\Shell', array('startup', 'getOptionParser', 'out'), array(), '', false);
  540. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', array(), array(), '', false);
  541. $parser->expects($this->once())->method('help');
  542. $shell->expects($this->once())->method('getOptionParser')
  543. ->will($this->returnValue($parser));
  544. $shell->expects($this->never())->method('hr');
  545. $shell->expects($this->once())->method('out');
  546. $shell->runCommand(['hr']);
  547. }
  548. /**
  549. * test run command causing exception on Shell method.
  550. *
  551. * @return void
  552. */
  553. public function testRunCommandMissingMethod() {
  554. $shell = $this->getMock('Cake\Console\Shell', array('startup', 'getOptionParser', 'out'), array(), '', false);
  555. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', array(), array(), '', false);
  556. $parser->expects($this->once())->method('help');
  557. $shell->expects($this->once())->method('getOptionParser')
  558. ->will($this->returnValue($parser));
  559. $shell->expects($this->once())->method('out');
  560. $result = $shell->runCommand(['idontexist']);
  561. $this->assertFalse($result);
  562. }
  563. /**
  564. * test that a --help causes help to show.
  565. *
  566. * @return void
  567. */
  568. public function testRunCommandTriggeringHelp() {
  569. $Parser = $this->getMock('Cake\Console\ConsoleOptionParser', array(), array(), '', false);
  570. $Parser->expects($this->once())->method('parse')
  571. ->with(array('--help'))
  572. ->will($this->returnValue(array(array('help' => true), array())));
  573. $Parser->expects($this->once())->method('help');
  574. $Shell = $this->getMock('Cake\Console\Shell', array('getOptionParser', 'out', 'startup', '_welcome'), array(), '', false);
  575. $Shell->expects($this->once())->method('getOptionParser')
  576. ->will($this->returnValue($Parser));
  577. $Shell->expects($this->once())->method('out');
  578. $Shell->runCommand(['--help']);
  579. }
  580. /**
  581. * test that runCommand will not call runCommand on tasks that are not subcommands.
  582. *
  583. * @return void
  584. */
  585. public function testRunCommandNotCallUnexposedTask() {
  586. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'hasTask', 'out'], [], '', false);
  587. $task = $this->getMock('Cake\Console\Shell', ['runCommand'], [], '', false);
  588. $task->expects($this->never())
  589. ->method('runCommand');
  590. $shell->expects($this->any())
  591. ->method('hasTask')
  592. ->will($this->returnValue(true));
  593. $shell->expects($this->never())->method('startup');
  594. $shell->expects($this->once())->method('out');
  595. $shell->RunCommand = $task;
  596. $result = $shell->runCommand(['run_command', 'one']);
  597. $this->assertFalse($result);
  598. }
  599. /**
  600. * test that runCommand will call runCommand on the task.
  601. *
  602. * @return void
  603. */
  604. public function testRunCommandHittingTaskInSubcommand() {
  605. $parser = new ConsoleOptionParser('knife');
  606. $parser->addSubcommand('slice');
  607. $shell = $this->getMock('Cake\Console\Shell', ['hasTask', 'startup', 'getOptionParser'], [], '', false);
  608. $task = $this->getMock('Cake\Console\Shell', ['main', 'runCommand'], [], '', false);
  609. $task->expects($this->once())
  610. ->method('runCommand')
  611. ->with(['one'], false);
  612. $shell->expects($this->once())->method('getOptionParser')
  613. ->will($this->returnValue($parser));
  614. $shell->expects($this->once())->method('startup');
  615. $shell->expects($this->any())
  616. ->method('hasTask')
  617. ->will($this->returnValue(true));
  618. $shell->Slice = $task;
  619. $shell->runCommand(['slice', 'one']);
  620. }
  621. /**
  622. * test wrapBlock wrapping text.
  623. *
  624. * @return void
  625. */
  626. public function testWrapText() {
  627. $text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.';
  628. $result = $this->Shell->wrapText($text, array('width' => 33));
  629. $expected = <<<TEXT
  630. This is the song that never ends.
  631. This is the song that never ends.
  632. This is the song that never ends.
  633. TEXT;
  634. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  635. $result = $this->Shell->wrapText($text, array('indent' => ' ', 'width' => 33));
  636. $expected = <<<TEXT
  637. This is the song that never ends.
  638. This is the song that never ends.
  639. This is the song that never ends.
  640. TEXT;
  641. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  642. }
  643. /**
  644. * Testing camel cased naming of tasks
  645. *
  646. * @return void
  647. */
  648. public function testShellNaming() {
  649. $this->Shell->tasks = array('TestApple');
  650. $this->Shell->loadTasks();
  651. $expected = 'TestApple';
  652. $this->assertEquals($expected, $this->Shell->TestApple->name);
  653. }
  654. /**
  655. * Test that option parsers are created with the correct name/command.
  656. *
  657. * @return void
  658. */
  659. public function testGetOptionParser() {
  660. $this->Shell->name = 'test';
  661. $this->Shell->plugin = 'plugin';
  662. $parser = $this->Shell->getOptionParser();
  663. $this->assertEquals('plugin.test', $parser->command());
  664. }
  665. /**
  666. * Test file and console and logging quiet output
  667. *
  668. * @return void
  669. */
  670. public function testQuietLog() {
  671. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  672. $io->expects($this->once())
  673. ->method('level')
  674. ->with(Shell::QUIET);
  675. $io->expects($this->at(0))
  676. ->method('setLoggers')
  677. ->with(true);
  678. $io->expects($this->at(2))
  679. ->method('setLoggers')
  680. ->with(false);
  681. $this->Shell = $this->getMock(__NAMESPACE__ . '\ShellTestShell', array('_useLogger'), array($io));
  682. $this->Shell->runCommand(['foo', '--quiet']);
  683. }
  684. }