ShellTest.php 21 KB

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