ShellTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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. 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 doSomething() {
  68. }
  69. protected function noAccess() {
  70. }
  71. public function logSomething() {
  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\Shell\Task\TestAppleTask');
  92. class_alias(__NAMESPACE__ . '\TestBananaTask', 'Cake\Shell\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 = 'TestPlugin.TestPluginComments';
  141. $this->Shell->initialize();
  142. $this->Shell->loadModel();
  143. $this->assertTrue(isset($this->Shell->TestPluginComments));
  144. $this->assertInstanceOf(
  145. 'TestPlugin\Model\Table\TestPluginCommentsTable',
  146. $this->Shell->TestPluginComments
  147. );
  148. }
  149. /**
  150. * test LoadModel method
  151. *
  152. * @return void
  153. */
  154. public function testLoadModel() {
  155. Configure::write('App.namespace', 'TestApp');
  156. $Shell = new MergeShell();
  157. $this->assertInstanceOf(
  158. 'TestApp\Model\Table\ArticlesTable',
  159. $Shell->Articles
  160. );
  161. $this->assertEquals('Articles', $Shell->modelClass);
  162. Plugin::load('TestPlugin');
  163. $result = $this->Shell->loadModel('TestPlugin.TestPluginComments');
  164. $this->assertInstanceOf(
  165. 'TestPlugin\Model\Table\TestPluginCommentsTable',
  166. $result
  167. );
  168. $this->assertInstanceOf(
  169. 'TestPlugin\Model\Table\TestPluginCommentsTable',
  170. $this->Shell->TestPluginComments
  171. );
  172. }
  173. /**
  174. * testIn method
  175. *
  176. * @return void
  177. */
  178. public function testIn() {
  179. $this->io->expects($this->at(0))
  180. ->method('askChoice')
  181. ->with('Just a test?', ['y', 'n'], 'n')
  182. ->will($this->returnValue('n'));
  183. $this->io->expects($this->at(1))
  184. ->method('ask')
  185. ->with('Just a test?', 'n')
  186. ->will($this->returnValue('n'));
  187. $result = $this->Shell->in('Just a test?', array('y', 'n'), 'n');
  188. $this->assertEquals('n', $result);
  189. $result = $this->Shell->in('Just a test?', null, 'n');
  190. $this->assertEquals('n', $result);
  191. }
  192. /**
  193. * Test in() when not interactive.
  194. *
  195. * @return void
  196. */
  197. public function testInNonInteractive() {
  198. $this->io->expects($this->never())
  199. ->method('askChoice');
  200. $this->io->expects($this->never())
  201. ->method('ask');
  202. $this->Shell->interactive = false;
  203. $result = $this->Shell->in('Just a test?', 'y/n', 'n');
  204. $this->assertEquals('n', $result);
  205. }
  206. /**
  207. * testOut method
  208. *
  209. * @return void
  210. */
  211. public function testOut() {
  212. $this->io->expects($this->once())
  213. ->method('out')
  214. ->with('Just a test', 1);
  215. $this->Shell->out('Just a test');
  216. }
  217. /**
  218. * testErr method
  219. *
  220. * @return void
  221. */
  222. public function testErr() {
  223. $this->io->expects($this->once())
  224. ->method('err')
  225. ->with('Just a test', 1);
  226. $this->Shell->err('Just a test');
  227. }
  228. /**
  229. * testNl
  230. *
  231. * @return void
  232. */
  233. public function testNl() {
  234. $this->io->expects($this->once())
  235. ->method('nl')
  236. ->with(2);
  237. $this->Shell->nl(2);
  238. }
  239. /**
  240. * testHr
  241. *
  242. * @return void
  243. */
  244. public function testHr() {
  245. $this->io->expects($this->once())
  246. ->method('hr')
  247. ->with(2);
  248. $this->Shell->hr(2);
  249. }
  250. /**
  251. * testError
  252. *
  253. * @return void
  254. */
  255. public function testError() {
  256. $this->io->expects($this->at(0))
  257. ->method('err')
  258. ->with('<error>Error:</error> Foo Not Found');
  259. $this->io->expects($this->at(1))
  260. ->method('err')
  261. ->with("Searched all...");
  262. $this->Shell->error('Foo Not Found', 'Searched all...');
  263. $this->assertSame($this->Shell->stopped, 1);
  264. }
  265. /**
  266. * testLoadTasks method
  267. *
  268. * @return void
  269. */
  270. public function testLoadTasks() {
  271. $this->assertTrue($this->Shell->loadTasks());
  272. $this->Shell->tasks = null;
  273. $this->assertTrue($this->Shell->loadTasks());
  274. $this->Shell->tasks = false;
  275. $this->assertTrue($this->Shell->loadTasks());
  276. $this->Shell->tasks = true;
  277. $this->assertTrue($this->Shell->loadTasks());
  278. $this->Shell->tasks = array();
  279. $this->assertTrue($this->Shell->loadTasks());
  280. $this->Shell->tasks = array('TestApple');
  281. $this->assertTrue($this->Shell->loadTasks());
  282. $this->assertInstanceOf('Cake\Shell\Task\TestAppleTask', $this->Shell->TestApple);
  283. $this->Shell->tasks = 'TestBanana';
  284. $this->assertTrue($this->Shell->loadTasks());
  285. $this->assertInstanceOf('Cake\Shell\Task\TestAppleTask', $this->Shell->TestApple);
  286. $this->assertInstanceOf('Cake\Shell\Task\TestBananaTask', $this->Shell->TestBanana);
  287. unset($this->Shell->ShellTestApple, $this->Shell->TestBanana);
  288. $this->Shell->tasks = array('TestApple', 'TestBanana');
  289. $this->assertTrue($this->Shell->loadTasks());
  290. $this->assertInstanceOf('Cake\Shell\Task\TestAppleTask', $this->Shell->TestApple);
  291. $this->assertInstanceOf('Cake\Shell\Task\TestBananaTask', $this->Shell->TestBanana);
  292. }
  293. /**
  294. * test that __get() makes args and params references
  295. *
  296. * @return void
  297. */
  298. public function testMagicGetArgAndParamReferences() {
  299. $this->Shell->tasks = array('TestApple');
  300. $this->Shell->args = array('one');
  301. $this->Shell->params = array('help' => false);
  302. $this->Shell->loadTasks();
  303. $result = $this->Shell->TestApple;
  304. $this->Shell->args = array('one', 'two');
  305. $this->assertSame($this->Shell->args, $result->args);
  306. $this->assertSame($this->Shell->params, $result->params);
  307. }
  308. /**
  309. * testShortPath method
  310. *
  311. * @return void
  312. */
  313. public function testShortPath() {
  314. $path = $expected = DS . 'tmp/ab/cd';
  315. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  316. $path = $expected = DS . 'tmp/ab/cd/';
  317. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  318. $path = $expected = DS . 'tmp/ab/index.php';
  319. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  320. $path = DS . 'tmp/ab/' . DS . 'cd';
  321. $expected = DS . 'tmp/ab/cd';
  322. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  323. $path = 'tmp/ab';
  324. $expected = 'tmp/ab';
  325. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  326. $path = 'tmp/ab';
  327. $expected = 'tmp/ab';
  328. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  329. $path = APP;
  330. $result = $this->Shell->shortPath($path);
  331. $this->assertNotContains(ROOT, $result, 'Short paths should not contain ROOT');
  332. }
  333. /**
  334. * testCreateFile method
  335. *
  336. * @return void
  337. */
  338. public function testCreateFileNonInteractive() {
  339. $eol = PHP_EOL;
  340. $path = TMP . 'shell_test';
  341. $file = $path . DS . 'file1.php';
  342. new Folder($path, true);
  343. $contents = "<?php{$eol}echo 'test';${eol}\$te = 'st';{$eol}";
  344. $result = $this->Shell->createFile($file, $contents);
  345. $this->assertTrue($result);
  346. $this->assertTrue(file_exists($file));
  347. $this->assertEquals(file_get_contents($file), $contents);
  348. }
  349. /**
  350. * Test that files are not changed with a 'n' reply.
  351. *
  352. * @return void
  353. */
  354. public function testCreateFileNoReply() {
  355. $eol = PHP_EOL;
  356. $path = TMP . 'shell_test';
  357. $file = $path . DS . 'file1.php';
  358. new Folder($path, true);
  359. $this->io->expects($this->once())
  360. ->method('askChoice')
  361. ->will($this->returnValue('n'));
  362. touch($file);
  363. $this->assertTrue(file_exists($file));
  364. $contents = "My content";
  365. $result = $this->Shell->createFile($file, $contents);
  366. $this->assertTrue(file_exists($file));
  367. $this->assertTextEquals('', file_get_contents($file));
  368. $this->assertFalse($result, 'Did not create file.');
  369. }
  370. /**
  371. * Test that files are changed with a 'y' reply.
  372. *
  373. * @return void
  374. */
  375. public function testCreateFileOverwrite() {
  376. $eol = PHP_EOL;
  377. $path = TMP . 'shell_test';
  378. $file = $path . DS . 'file1.php';
  379. new Folder($path, true);
  380. $this->io->expects($this->once())
  381. ->method('askChoice')
  382. ->will($this->returnValue('y'));
  383. touch($file);
  384. $this->assertTrue(file_exists($file));
  385. $contents = "My content";
  386. $result = $this->Shell->createFile($file, $contents);
  387. $this->assertTrue(file_exists($file));
  388. $this->assertTextEquals($contents, file_get_contents($file));
  389. $this->assertTrue($result, 'Did create file.');
  390. }
  391. /**
  392. * Test that you can't create files that aren't writable.
  393. *
  394. * @return void
  395. */
  396. public function testCreateFileNoPermissions() {
  397. $this->skipIf(DIRECTORY_SEPARATOR === '\\', 'Cant perform operations using permissions on windows.');
  398. $path = TMP . 'shell_test';
  399. $file = $path . DS . 'no_perms';
  400. if (!is_dir($path)) {
  401. mkdir($path);
  402. }
  403. chmod($path, 0444);
  404. $this->Shell->createFile($file, 'testing');
  405. $this->assertFalse(file_exists($file));
  406. chmod($path, 0744);
  407. rmdir($path);
  408. }
  409. /**
  410. * test hasTask method
  411. *
  412. * @return void
  413. */
  414. public function testHasTask() {
  415. $this->Shell->tasks = array('Extract', 'DbConfig');
  416. $this->Shell->loadTasks();
  417. $this->assertTrue($this->Shell->hasTask('extract'));
  418. $this->assertTrue($this->Shell->hasTask('Extract'));
  419. $this->assertFalse($this->Shell->hasTask('random'));
  420. $this->assertTrue($this->Shell->hasTask('db_config'));
  421. $this->assertTrue($this->Shell->hasTask('DbConfig'));
  422. }
  423. /**
  424. * test the hasMethod
  425. *
  426. * @return void
  427. */
  428. public function testHasMethod() {
  429. $this->assertTrue($this->Shell->hasMethod('doSomething'));
  430. $this->assertFalse($this->Shell->hasMethod('hr'), 'hr is callable');
  431. $this->assertFalse($this->Shell->hasMethod('_secret'), '_secret is callable');
  432. $this->assertFalse($this->Shell->hasMethod('no_access'), 'no_access is callable');
  433. }
  434. /**
  435. * test run command calling main.
  436. *
  437. * @return void
  438. */
  439. public function testRunCommandMain() {
  440. $io = $this->getMock('Cake\Console\ConsoleIo');
  441. $shell = $this->getMock('Cake\Console\Shell', ['main', 'startup'], [$io]);
  442. $shell->expects($this->once())->method('startup');
  443. $shell->expects($this->once())->method('main')
  444. ->with('cakes')
  445. ->will($this->returnValue(true));
  446. $result = $shell->runCommand(['cakes', '--verbose']);
  447. $this->assertTrue($result);
  448. }
  449. /**
  450. * test run command calling a real method with no subcommands defined.
  451. *
  452. * @return void
  453. */
  454. public function testRunCommandWithMethod() {
  455. $io = $this->getMock('Cake\Console\ConsoleIo');
  456. $shell = $this->getMock('Cake\Console\Shell', ['hitMe', 'startup'], [$io]);
  457. $shell->expects($this->once())->method('startup');
  458. $shell->expects($this->once())->method('hitMe')
  459. ->with('cakes')
  460. ->will($this->returnValue(true));
  461. $result = $shell->runCommand(['hit_me', 'cakes', '--verbose'], true);
  462. $this->assertTrue($result);
  463. }
  464. /**
  465. * Test that runCommand() doesn't call public methods when the second arg is false.
  466. *
  467. * @return void
  468. */
  469. public function testRunCommandAutoMethodOff() {
  470. $io = $this->getMock('Cake\Console\ConsoleIo');
  471. $shell = $this->getMock('Cake\Console\Shell', ['hit_me', 'startup'], [$io]);
  472. $shell->expects($this->never())->method('startup');
  473. $shell->expects($this->never())->method('hit_me');
  474. $result = $shell->runCommand(['hit_me', 'baseball'], false);
  475. $this->assertFalse($result);
  476. $result = $shell->runCommand(['hit_me', 'baseball']);
  477. $this->assertFalse($result, 'Default value of runCommand() should be false');
  478. }
  479. /**
  480. * test run command calling a real method with mismatching subcommands defined.
  481. *
  482. * @return void
  483. */
  484. public function testRunCommandWithMethodNotInSubcommands() {
  485. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  486. $io = $this->getMock('Cake\Console\ConsoleIo');
  487. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'roll', 'startup'], [$io]);
  488. $parser->addSubCommand('slice');
  489. $shell->expects($this->any())
  490. ->method('getOptionParser')
  491. ->will($this->returnValue($parser));
  492. $parser->expects($this->once())
  493. ->method('help');
  494. $shell->expects($this->never())->method('startup');
  495. $shell->expects($this->never())->method('roll');
  496. $result = $shell->runCommand(['roll', 'cakes', '--verbose']);
  497. $this->assertFalse($result);
  498. }
  499. /**
  500. * test run command calling a real method with subcommands defined.
  501. *
  502. * @return void
  503. */
  504. public function testRunCommandWithMethodInSubcommands() {
  505. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  506. $io = $this->getMock('Cake\Console\ConsoleIo');
  507. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'slice', 'startup'], [$io]);
  508. $parser->addSubCommand('slice');
  509. $shell->expects($this->any())
  510. ->method('getOptionParser')
  511. ->will($this->returnValue($parser));
  512. $shell->expects($this->once())->method('startup');
  513. $shell->expects($this->once())
  514. ->method('slice')
  515. ->with('cakes');
  516. $shell->runCommand(['slice', 'cakes', '--verbose']);
  517. }
  518. /**
  519. * test run command calling a missing method with subcommands defined.
  520. *
  521. * @return void
  522. */
  523. public function testRunCommandWithMissingMethodInSubcommands() {
  524. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  525. $parser->addSubCommand('slice');
  526. $io = $this->getMock('Cake\Console\ConsoleIo');
  527. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'startup'], [$io]);
  528. $shell->expects($this->any())
  529. ->method('getOptionParser')
  530. ->will($this->returnValue($parser));
  531. $shell->expects($this->never())
  532. ->method('startup');
  533. $parser->expects($this->once())
  534. ->method('help');
  535. $shell->runCommand(['slice', 'cakes', '--verbose']);
  536. }
  537. /**
  538. * test run command causing exception on Shell method.
  539. *
  540. * @return void
  541. */
  542. public function testRunCommandBaseclassMethod() {
  543. $shell = $this->getMock('Cake\Console\Shell', array('startup', 'getOptionParser', 'out'), array(), '', false);
  544. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', array(), array(), '', false);
  545. $parser->expects($this->once())->method('help');
  546. $shell->expects($this->once())->method('getOptionParser')
  547. ->will($this->returnValue($parser));
  548. $shell->expects($this->never())->method('hr');
  549. $shell->expects($this->once())->method('out');
  550. $shell->runCommand(['hr']);
  551. }
  552. /**
  553. * test run command causing exception on Shell method.
  554. *
  555. * @return void
  556. */
  557. public function testRunCommandMissingMethod() {
  558. $shell = $this->getMock('Cake\Console\Shell', array('startup', 'getOptionParser', 'out'), array(), '', false);
  559. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', array(), array(), '', false);
  560. $parser->expects($this->once())->method('help');
  561. $shell->expects($this->once())->method('getOptionParser')
  562. ->will($this->returnValue($parser));
  563. $shell->expects($this->once())->method('out');
  564. $result = $shell->runCommand(['idontexist']);
  565. $this->assertFalse($result);
  566. }
  567. /**
  568. * test that a --help causes help to show.
  569. *
  570. * @return void
  571. */
  572. public function testRunCommandTriggeringHelp() {
  573. $Parser = $this->getMock('Cake\Console\ConsoleOptionParser', array(), array(), '', false);
  574. $Parser->expects($this->once())->method('parse')
  575. ->with(array('--help'))
  576. ->will($this->returnValue(array(array('help' => true), array())));
  577. $Parser->expects($this->once())->method('help');
  578. $Shell = $this->getMock('Cake\Console\Shell', array('getOptionParser', 'out', 'startup', '_welcome'), array(), '', false);
  579. $Shell->expects($this->once())->method('getOptionParser')
  580. ->will($this->returnValue($Parser));
  581. $Shell->expects($this->once())->method('out');
  582. $Shell->runCommand(['--help']);
  583. }
  584. /**
  585. * test that runCommand will not call runCommand on tasks that are not subcommands.
  586. *
  587. * @return void
  588. */
  589. public function testRunCommandNotCallUnexposedTask() {
  590. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'hasTask', 'out'], [], '', false);
  591. $task = $this->getMock('Cake\Console\Shell', ['runCommand'], [], '', false);
  592. $task->expects($this->never())
  593. ->method('runCommand');
  594. $shell->expects($this->any())
  595. ->method('hasTask')
  596. ->will($this->returnValue(true));
  597. $shell->expects($this->never())->method('startup');
  598. $shell->expects($this->once())->method('out');
  599. $shell->RunCommand = $task;
  600. $result = $shell->runCommand(['run_command', 'one']);
  601. $this->assertFalse($result);
  602. }
  603. /**
  604. * test that runCommand will call runCommand on the task.
  605. *
  606. * @return void
  607. */
  608. public function testRunCommandHittingTaskInSubcommand() {
  609. $parser = new ConsoleOptionParser('knife');
  610. $parser->addSubcommand('slice');
  611. $shell = $this->getMock('Cake\Console\Shell', ['hasTask', 'startup', 'getOptionParser'], [], '', false);
  612. $task = $this->getMock('Cake\Console\Shell', ['main', 'runCommand'], [], '', false);
  613. $task->expects($this->once())
  614. ->method('runCommand')
  615. ->with(['one'], false);
  616. $shell->expects($this->once())->method('getOptionParser')
  617. ->will($this->returnValue($parser));
  618. $shell->expects($this->once())->method('startup');
  619. $shell->expects($this->any())
  620. ->method('hasTask')
  621. ->will($this->returnValue(true));
  622. $shell->Slice = $task;
  623. $shell->runCommand(['slice', 'one']);
  624. }
  625. /**
  626. * test wrapBlock wrapping text.
  627. *
  628. * @return void
  629. */
  630. public function testWrapText() {
  631. $text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.';
  632. $result = $this->Shell->wrapText($text, array('width' => 33));
  633. $expected = <<<TEXT
  634. This is the song that never ends.
  635. This is the song that never ends.
  636. This is the song that never ends.
  637. TEXT;
  638. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  639. $result = $this->Shell->wrapText($text, array('indent' => ' ', 'width' => 33));
  640. $expected = <<<TEXT
  641. This is the song that never ends.
  642. This is the song that never ends.
  643. This is the song that never ends.
  644. TEXT;
  645. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  646. }
  647. /**
  648. * Testing camel cased naming of tasks
  649. *
  650. * @return void
  651. */
  652. public function testShellNaming() {
  653. $this->Shell->tasks = array('TestApple');
  654. $this->Shell->loadTasks();
  655. $expected = 'TestApple';
  656. $this->assertEquals($expected, $this->Shell->TestApple->name);
  657. }
  658. /**
  659. * Test reading params
  660. *
  661. * @dataProvider paramReadingDataProvider
  662. */
  663. public function testParamReading($toRead, $expected) {
  664. $this->Shell->params = array(
  665. 'key' => 'value',
  666. 'help' => false,
  667. 'emptykey' => '',
  668. 'truthy' => true
  669. );
  670. $this->assertSame($expected, $this->Shell->param($toRead));
  671. }
  672. /**
  673. * Data provider for testing reading values with Shell::param()
  674. *
  675. * @return array
  676. */
  677. public function paramReadingDataProvider() {
  678. return array(
  679. array(
  680. 'key',
  681. 'value',
  682. ),
  683. array(
  684. 'help',
  685. false,
  686. ),
  687. array(
  688. 'emptykey',
  689. '',
  690. ),
  691. array(
  692. 'truthy',
  693. true,
  694. ),
  695. array(
  696. 'does_not_exist',
  697. null,
  698. )
  699. );
  700. }
  701. /**
  702. * Test that option parsers are created with the correct name/command.
  703. *
  704. * @return void
  705. */
  706. public function testGetOptionParser() {
  707. $this->Shell->name = 'test';
  708. $this->Shell->plugin = 'plugin';
  709. $parser = $this->Shell->getOptionParser();
  710. $this->assertEquals('plugin.test', $parser->command());
  711. }
  712. /**
  713. * Test file and console and logging quiet output
  714. *
  715. * @return void
  716. */
  717. public function testQuietLog() {
  718. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  719. $io->expects($this->once())
  720. ->method('level')
  721. ->with(Shell::QUIET);
  722. $io->expects($this->at(0))
  723. ->method('setLoggers')
  724. ->with(true);
  725. $io->expects($this->at(2))
  726. ->method('setLoggers')
  727. ->with(false);
  728. $this->Shell = $this->getMock(__NAMESPACE__ . '\ShellTestShell', array('_useLogger'), array($io));
  729. $this->Shell->runCommand(['foo', '--quiet']);
  730. }
  731. }