ShellTest.php 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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. use TestApp\Shell\TestingDispatchShell;
  26. /**
  27. * Class for testing merging vars
  28. */
  29. class MergeShell extends Shell
  30. {
  31. public $tasks = ['DbConfig', 'Fixture'];
  32. public $modelClass = 'Articles';
  33. }
  34. /**
  35. * ShellTestShell class
  36. *
  37. */
  38. class ShellTestShell extends Shell
  39. {
  40. /**
  41. * name property
  42. *
  43. * @var name
  44. */
  45. public $name = 'ShellTestShell';
  46. /**
  47. * stopped property
  48. *
  49. * @var int
  50. */
  51. public $stopped;
  52. /**
  53. * testMessage property
  54. *
  55. * @var string
  56. */
  57. public $testMessage = 'all your base are belong to us';
  58. /**
  59. * stop method
  60. *
  61. * @param int $status
  62. * @return void
  63. */
  64. protected function _stop($status = 0)
  65. {
  66. $this->stopped = $status;
  67. }
  68. protected function _secret()
  69. {
  70. }
  71. //@codingStandardsIgnoreStart
  72. public function doSomething()
  73. {
  74. }
  75. protected function noAccess()
  76. {
  77. }
  78. public function logSomething()
  79. {
  80. $this->log($this->testMessage);
  81. }
  82. //@codingStandardsIgnoreEnd
  83. public function useLogger($enable = true)
  84. {
  85. $this->_useLogger($enable);
  86. }
  87. }
  88. /**
  89. * TestAppleTask class
  90. *
  91. */
  92. class TestAppleTask extends Shell
  93. {
  94. }
  95. /**
  96. * TestBananaTask class
  97. *
  98. */
  99. class TestBananaTask extends Shell
  100. {
  101. }
  102. class_alias(__NAMESPACE__ . '\TestAppleTask', 'Cake\Shell\Task\TestAppleTask');
  103. class_alias(__NAMESPACE__ . '\TestBananaTask', 'Cake\Shell\Task\TestBananaTask');
  104. /**
  105. * ShellTest class
  106. *
  107. */
  108. class ShellTest extends TestCase
  109. {
  110. /**
  111. * Fixtures used in this test case
  112. *
  113. * @var array
  114. */
  115. public $fixtures = [
  116. 'core.posts',
  117. 'core.comments',
  118. 'core.articles',
  119. 'core.users',
  120. 'core.tags',
  121. 'core.articles_tags',
  122. 'core.attachments'
  123. ];
  124. /**
  125. * setUp method
  126. *
  127. * @return void
  128. */
  129. public function setUp()
  130. {
  131. parent::setUp();
  132. $this->io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  133. $this->Shell = new ShellTestShell($this->io);
  134. if (is_dir(TMP . 'shell_test')) {
  135. $Folder = new Folder(TMP . 'shell_test');
  136. $Folder->delete();
  137. }
  138. }
  139. /**
  140. * testConstruct method
  141. *
  142. * @return void
  143. */
  144. public function testConstruct()
  145. {
  146. $this->assertEquals('ShellTestShell', $this->Shell->name);
  147. $this->assertInstanceOf('Cake\Console\ConsoleIo', $this->Shell->io());
  148. }
  149. /**
  150. * testInitialize method
  151. *
  152. * @return void
  153. */
  154. public function testInitialize()
  155. {
  156. Configure::write('App.namespace', 'TestApp');
  157. Plugin::load('TestPlugin');
  158. $this->Shell->tasks = ['DbConfig' => ['one', 'two']];
  159. $this->Shell->plugin = 'TestPlugin';
  160. $this->Shell->modelClass = 'TestPlugin.TestPluginComments';
  161. $this->Shell->initialize();
  162. $this->Shell->loadModel();
  163. $this->assertTrue(isset($this->Shell->TestPluginComments));
  164. $this->assertInstanceOf(
  165. 'TestPlugin\Model\Table\TestPluginCommentsTable',
  166. $this->Shell->TestPluginComments
  167. );
  168. }
  169. /**
  170. * test LoadModel method
  171. *
  172. * @return void
  173. */
  174. public function testLoadModel()
  175. {
  176. Configure::write('App.namespace', 'TestApp');
  177. $Shell = new MergeShell();
  178. $this->assertInstanceOf(
  179. 'TestApp\Model\Table\ArticlesTable',
  180. $Shell->Articles
  181. );
  182. $this->assertEquals('Articles', $Shell->modelClass);
  183. Plugin::load('TestPlugin');
  184. $result = $this->Shell->loadModel('TestPlugin.TestPluginComments');
  185. $this->assertInstanceOf(
  186. 'TestPlugin\Model\Table\TestPluginCommentsTable',
  187. $result
  188. );
  189. $this->assertInstanceOf(
  190. 'TestPlugin\Model\Table\TestPluginCommentsTable',
  191. $this->Shell->TestPluginComments
  192. );
  193. }
  194. /**
  195. * testIn method
  196. *
  197. * @return void
  198. */
  199. public function testIn()
  200. {
  201. $this->io->expects($this->at(0))
  202. ->method('askChoice')
  203. ->with('Just a test?', ['y', 'n'], 'n')
  204. ->will($this->returnValue('n'));
  205. $this->io->expects($this->at(1))
  206. ->method('ask')
  207. ->with('Just a test?', 'n')
  208. ->will($this->returnValue('n'));
  209. $result = $this->Shell->in('Just a test?', ['y', 'n'], 'n');
  210. $this->assertEquals('n', $result);
  211. $result = $this->Shell->in('Just a test?', null, 'n');
  212. $this->assertEquals('n', $result);
  213. }
  214. /**
  215. * Test in() when not interactive.
  216. *
  217. * @return void
  218. */
  219. public function testInNonInteractive()
  220. {
  221. $this->io->expects($this->never())
  222. ->method('askChoice');
  223. $this->io->expects($this->never())
  224. ->method('ask');
  225. $this->Shell->interactive = false;
  226. $result = $this->Shell->in('Just a test?', 'y/n', 'n');
  227. $this->assertEquals('n', $result);
  228. }
  229. /**
  230. * testOut method
  231. *
  232. * @return void
  233. */
  234. public function testOut()
  235. {
  236. $this->io->expects($this->once())
  237. ->method('out')
  238. ->with('Just a test', 1);
  239. $this->Shell->out('Just a test');
  240. }
  241. /**
  242. * testErr method
  243. *
  244. * @return void
  245. */
  246. public function testErr()
  247. {
  248. $this->io->expects($this->once())
  249. ->method('err')
  250. ->with('Just a test', 1);
  251. $this->Shell->err('Just a test');
  252. }
  253. /**
  254. * testNl
  255. *
  256. * @return void
  257. */
  258. public function testNl()
  259. {
  260. $this->io->expects($this->once())
  261. ->method('nl')
  262. ->with(2);
  263. $this->Shell->nl(2);
  264. }
  265. /**
  266. * testHr
  267. *
  268. * @return void
  269. */
  270. public function testHr()
  271. {
  272. $this->io->expects($this->once())
  273. ->method('hr')
  274. ->with(2);
  275. $this->Shell->hr(2);
  276. }
  277. /**
  278. * testError
  279. *
  280. * @return void
  281. */
  282. public function testError()
  283. {
  284. $this->io->expects($this->at(0))
  285. ->method('err')
  286. ->with('<error>Error:</error> Foo Not Found');
  287. $this->io->expects($this->at(1))
  288. ->method('err')
  289. ->with("Searched all...");
  290. $this->Shell->error('Foo Not Found', 'Searched all...');
  291. $this->assertSame($this->Shell->stopped, 1);
  292. }
  293. /**
  294. * testLoadTasks method
  295. *
  296. * @return void
  297. */
  298. public function testLoadTasks()
  299. {
  300. $this->assertTrue($this->Shell->loadTasks());
  301. $this->Shell->tasks = null;
  302. $this->assertTrue($this->Shell->loadTasks());
  303. $this->Shell->tasks = false;
  304. $this->assertTrue($this->Shell->loadTasks());
  305. $this->Shell->tasks = true;
  306. $this->assertTrue($this->Shell->loadTasks());
  307. $this->Shell->tasks = [];
  308. $this->assertTrue($this->Shell->loadTasks());
  309. $this->Shell->tasks = ['TestApple'];
  310. $this->assertTrue($this->Shell->loadTasks());
  311. $this->assertInstanceOf('Cake\Shell\Task\TestAppleTask', $this->Shell->TestApple);
  312. $this->Shell->tasks = 'TestBanana';
  313. $this->assertTrue($this->Shell->loadTasks());
  314. $this->assertInstanceOf('Cake\Shell\Task\TestAppleTask', $this->Shell->TestApple);
  315. $this->assertInstanceOf('Cake\Shell\Task\TestBananaTask', $this->Shell->TestBanana);
  316. unset($this->Shell->ShellTestApple, $this->Shell->TestBanana);
  317. $this->Shell->tasks = ['TestApple', 'TestBanana'];
  318. $this->assertTrue($this->Shell->loadTasks());
  319. $this->assertInstanceOf('Cake\Shell\Task\TestAppleTask', $this->Shell->TestApple);
  320. $this->assertInstanceOf('Cake\Shell\Task\TestBananaTask', $this->Shell->TestBanana);
  321. }
  322. /**
  323. * test that __get() makes args and params references
  324. *
  325. * @return void
  326. */
  327. public function testMagicGetArgAndParamReferences()
  328. {
  329. $this->Shell->tasks = ['TestApple'];
  330. $this->Shell->args = ['one'];
  331. $this->Shell->params = ['help' => false];
  332. $this->Shell->loadTasks();
  333. $result = $this->Shell->TestApple;
  334. $this->Shell->args = ['one', 'two'];
  335. $this->assertSame($this->Shell->args, $result->args);
  336. $this->assertSame($this->Shell->params, $result->params);
  337. }
  338. /**
  339. * testShortPath method
  340. *
  341. * @return void
  342. */
  343. public function testShortPath()
  344. {
  345. $path = $expected = DS . 'tmp/ab/cd';
  346. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  347. $path = $expected = DS . 'tmp/ab/cd/';
  348. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  349. $path = $expected = DS . 'tmp/ab/index.php';
  350. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  351. $path = DS . 'tmp/ab/' . DS . 'cd';
  352. $expected = DS . 'tmp/ab/cd';
  353. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  354. $path = 'tmp/ab';
  355. $expected = 'tmp/ab';
  356. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  357. $path = 'tmp/ab';
  358. $expected = 'tmp/ab';
  359. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  360. $path = APP;
  361. $result = $this->Shell->shortPath($path);
  362. $this->assertNotContains(ROOT, $result, 'Short paths should not contain ROOT');
  363. }
  364. /**
  365. * testCreateFile method
  366. *
  367. * @return void
  368. */
  369. public function testCreateFileNonInteractive()
  370. {
  371. $eol = PHP_EOL;
  372. $path = TMP . 'shell_test';
  373. $file = $path . DS . 'file1.php';
  374. new Folder($path, true);
  375. $contents = "<?php{$eol}echo 'test';${eol}\$te = 'st';{$eol}";
  376. $result = $this->Shell->createFile($file, $contents);
  377. $this->assertTrue($result);
  378. $this->assertTrue(file_exists($file));
  379. $this->assertEquals(file_get_contents($file), $contents);
  380. }
  381. /**
  382. * Test that files are not changed with a 'n' reply.
  383. *
  384. * @return void
  385. */
  386. public function testCreateFileNoReply()
  387. {
  388. $eol = PHP_EOL;
  389. $path = TMP . 'shell_test';
  390. $file = $path . DS . 'file1.php';
  391. new Folder($path, true);
  392. $this->io->expects($this->once())
  393. ->method('askChoice')
  394. ->will($this->returnValue('n'));
  395. touch($file);
  396. $this->assertTrue(file_exists($file));
  397. $contents = "My content";
  398. $result = $this->Shell->createFile($file, $contents);
  399. $this->assertTrue(file_exists($file));
  400. $this->assertTextEquals('', file_get_contents($file));
  401. $this->assertFalse($result, 'Did not create file.');
  402. }
  403. /**
  404. * Test that files are changed with a 'y' reply.
  405. *
  406. * @return void
  407. */
  408. public function testCreateFileOverwrite()
  409. {
  410. $eol = PHP_EOL;
  411. $path = TMP . 'shell_test';
  412. $file = $path . DS . 'file1.php';
  413. new Folder($path, true);
  414. $this->io->expects($this->once())
  415. ->method('askChoice')
  416. ->will($this->returnValue('y'));
  417. touch($file);
  418. $this->assertTrue(file_exists($file));
  419. $contents = "My content";
  420. $result = $this->Shell->createFile($file, $contents);
  421. $this->assertTrue(file_exists($file));
  422. $this->assertTextEquals($contents, file_get_contents($file));
  423. $this->assertTrue($result, 'Did create file.');
  424. }
  425. /**
  426. * Test that there is no user prompt in non-interactive mode while file already exists.
  427. *
  428. * @return void
  429. */
  430. public function testCreateFileOverwriteNonInteractive()
  431. {
  432. $path = TMP . 'shell_test';
  433. $file = $path . DS . 'file1.php';
  434. new Folder($path, true);
  435. touch($file);
  436. $this->assertTrue(file_exists($file));
  437. $this->io->expects($this->never())->method('askChoice');
  438. $this->Shell->interactive = false;
  439. $result = $this->Shell->createFile($file, 'My content');
  440. $this->assertTrue($result);
  441. $this->assertEquals(file_get_contents($file), 'My content');
  442. }
  443. /**
  444. * Test that all files are changed with a 'a' reply.
  445. *
  446. * @return void
  447. */
  448. public function testCreateFileOverwriteAll()
  449. {
  450. $eol = PHP_EOL;
  451. $path = TMP . 'shell_test';
  452. $files = [
  453. $path . DS . 'file1.php' => 'My first content',
  454. $path . DS . 'file2.php' => 'My second content',
  455. $path . DS . 'file3.php' => 'My third content'
  456. ];
  457. new Folder($path, true);
  458. $this->io->expects($this->once())
  459. ->method('askChoice')
  460. ->will($this->returnValue('a'));
  461. foreach ($files as $file => $contents) {
  462. touch($file);
  463. $this->assertTrue(file_exists($file));
  464. $result = $this->Shell->createFile($file, $contents);
  465. $this->assertTrue(file_exists($file));
  466. $this->assertTextEquals($contents, file_get_contents($file));
  467. $this->assertTrue($result, 'Did create file.');
  468. }
  469. }
  470. /**
  471. * Test that you can't create files that aren't writable.
  472. *
  473. * @return void
  474. */
  475. public function testCreateFileNoPermissions()
  476. {
  477. $this->skipIf(DS === '\\', 'Cant perform operations using permissions on windows.');
  478. $path = TMP . 'shell_test';
  479. $file = $path . DS . 'no_perms';
  480. if (!is_dir($path)) {
  481. mkdir($path);
  482. }
  483. chmod($path, 0444);
  484. $this->Shell->createFile($file, 'testing');
  485. $this->assertFalse(file_exists($file));
  486. chmod($path, 0744);
  487. rmdir($path);
  488. }
  489. /**
  490. * test hasTask method
  491. *
  492. * @return void
  493. */
  494. public function testHasTask()
  495. {
  496. $this->Shell->tasks = ['Extract', 'DbConfig'];
  497. $this->Shell->loadTasks();
  498. $this->assertTrue($this->Shell->hasTask('extract'));
  499. $this->assertTrue($this->Shell->hasTask('Extract'));
  500. $this->assertFalse($this->Shell->hasTask('random'));
  501. $this->assertTrue($this->Shell->hasTask('db_config'));
  502. $this->assertTrue($this->Shell->hasTask('DbConfig'));
  503. }
  504. /**
  505. * test the hasMethod
  506. *
  507. * @return void
  508. */
  509. public function testHasMethod()
  510. {
  511. $this->assertTrue($this->Shell->hasMethod('doSomething'));
  512. $this->assertFalse($this->Shell->hasMethod('hr'), 'hr is callable');
  513. $this->assertFalse($this->Shell->hasMethod('_secret'), '_secret is callable');
  514. $this->assertFalse($this->Shell->hasMethod('no_access'), 'no_access is callable');
  515. }
  516. /**
  517. * test run command calling main.
  518. *
  519. * @return void
  520. */
  521. public function testRunCommandMain()
  522. {
  523. $io = $this->getMock('Cake\Console\ConsoleIo');
  524. $shell = $this->getMock('Cake\Console\Shell', ['main', 'startup'], [$io]);
  525. $shell->expects($this->once())->method('startup');
  526. $shell->expects($this->once())->method('main')
  527. ->with('cakes')
  528. ->will($this->returnValue(true));
  529. $result = $shell->runCommand(['cakes', '--verbose']);
  530. $this->assertTrue($result);
  531. }
  532. /**
  533. * test run command calling a real method with no subcommands defined.
  534. *
  535. * @return void
  536. */
  537. public function testRunCommandWithMethod()
  538. {
  539. $io = $this->getMock('Cake\Console\ConsoleIo');
  540. $shell = $this->getMock('Cake\Console\Shell', ['hitMe', 'startup'], [$io]);
  541. $shell->expects($this->once())->method('startup');
  542. $shell->expects($this->once())->method('hitMe')
  543. ->with('cakes')
  544. ->will($this->returnValue(true));
  545. $result = $shell->runCommand(['hit_me', 'cakes', '--verbose'], true);
  546. $this->assertTrue($result);
  547. }
  548. /**
  549. * Test that runCommand() doesn't call public methods when the second arg is false.
  550. *
  551. * @return void
  552. */
  553. public function testRunCommandAutoMethodOff()
  554. {
  555. $io = $this->getMock('Cake\Console\ConsoleIo');
  556. $shell = $this->getMock('Cake\Console\Shell', ['hit_me', 'startup'], [$io]);
  557. $shell->expects($this->never())->method('startup');
  558. $shell->expects($this->never())->method('hit_me');
  559. $result = $shell->runCommand(['hit_me', 'baseball'], false);
  560. $this->assertFalse($result);
  561. $result = $shell->runCommand(['hit_me', 'baseball']);
  562. $this->assertFalse($result, 'Default value of runCommand() should be false');
  563. }
  564. /**
  565. * test run command calling a real method with mismatching subcommands defined.
  566. *
  567. * @return void
  568. */
  569. public function testRunCommandWithMethodNotInSubcommands()
  570. {
  571. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  572. $io = $this->getMock('Cake\Console\ConsoleIo');
  573. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'roll', 'startup'], [$io]);
  574. $parser->addSubCommand('slice');
  575. $shell->expects($this->any())
  576. ->method('getOptionParser')
  577. ->will($this->returnValue($parser));
  578. $parser->expects($this->once())
  579. ->method('help');
  580. $shell->expects($this->never())->method('startup');
  581. $shell->expects($this->never())->method('roll');
  582. $result = $shell->runCommand(['roll', 'cakes', '--verbose']);
  583. $this->assertFalse($result);
  584. }
  585. /**
  586. * test run command calling a real method with subcommands defined.
  587. *
  588. * @return void
  589. */
  590. public function testRunCommandWithMethodInSubcommands()
  591. {
  592. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  593. $io = $this->getMock('Cake\Console\ConsoleIo');
  594. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'slice', 'startup'], [$io]);
  595. $parser->addSubCommand('slice');
  596. $shell->expects($this->any())
  597. ->method('getOptionParser')
  598. ->will($this->returnValue($parser));
  599. $shell->expects($this->once())->method('startup');
  600. $shell->expects($this->once())
  601. ->method('slice')
  602. ->with('cakes');
  603. $shell->runCommand(['slice', 'cakes', '--verbose']);
  604. }
  605. /**
  606. * test run command calling a missing method with subcommands defined.
  607. *
  608. * @return void
  609. */
  610. public function testRunCommandWithMissingMethodInSubcommands()
  611. {
  612. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  613. $parser->addSubCommand('slice');
  614. $io = $this->getMock('Cake\Console\ConsoleIo');
  615. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'startup'], [$io]);
  616. $shell->expects($this->any())
  617. ->method('getOptionParser')
  618. ->will($this->returnValue($parser));
  619. $shell->expects($this->never())
  620. ->method('startup');
  621. $parser->expects($this->once())
  622. ->method('help');
  623. $shell->runCommand(['slice', 'cakes', '--verbose']);
  624. }
  625. /**
  626. * test run command causing exception on Shell method.
  627. *
  628. * @return void
  629. */
  630. public function testRunCommandBaseclassMethod()
  631. {
  632. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'getOptionParser', 'out'], [], '', false);
  633. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [], '', false);
  634. $parser->expects($this->once())->method('help');
  635. $shell->expects($this->once())->method('getOptionParser')
  636. ->will($this->returnValue($parser));
  637. $shell->expects($this->never())->method('hr');
  638. $shell->expects($this->once())->method('out');
  639. $shell->runCommand(['hr']);
  640. }
  641. /**
  642. * test run command causing exception on Shell method.
  643. *
  644. * @return void
  645. */
  646. public function testRunCommandMissingMethod()
  647. {
  648. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'getOptionParser', 'out'], [], '', false);
  649. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [], '', false);
  650. $parser->expects($this->once())->method('help');
  651. $shell->expects($this->once())->method('getOptionParser')
  652. ->will($this->returnValue($parser));
  653. $shell->expects($this->once())->method('out');
  654. $result = $shell->runCommand(['idontexist']);
  655. $this->assertFalse($result);
  656. }
  657. /**
  658. * test that a --help causes help to show.
  659. *
  660. * @return void
  661. */
  662. public function testRunCommandTriggeringHelp()
  663. {
  664. $Parser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [], '', false);
  665. $Parser->expects($this->once())->method('parse')
  666. ->with(['--help'])
  667. ->will($this->returnValue([['help' => true], []]));
  668. $Parser->expects($this->once())->method('help');
  669. $Shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'out', 'startup', '_welcome'], [], '', false);
  670. $Shell->expects($this->once())->method('getOptionParser')
  671. ->will($this->returnValue($Parser));
  672. $Shell->expects($this->once())->method('out');
  673. $Shell->runCommand(['--help']);
  674. }
  675. /**
  676. * test that runCommand will not call runCommand on tasks that are not subcommands.
  677. *
  678. * @return void
  679. */
  680. public function testRunCommandNotCallUnexposedTask()
  681. {
  682. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'hasTask', 'out'], [], '', false);
  683. $task = $this->getMock('Cake\Console\Shell', ['runCommand'], [], '', false);
  684. $task->expects($this->never())
  685. ->method('runCommand');
  686. $shell->expects($this->any())
  687. ->method('hasTask')
  688. ->will($this->returnValue(true));
  689. $shell->expects($this->never())->method('startup');
  690. $shell->expects($this->once())->method('out');
  691. $shell->RunCommand = $task;
  692. $result = $shell->runCommand(['run_command', 'one']);
  693. $this->assertFalse($result);
  694. }
  695. /**
  696. * test that runCommand will call runCommand on the task.
  697. *
  698. * @return void
  699. */
  700. public function testRunCommandHittingTaskInSubcommand()
  701. {
  702. $parser = new ConsoleOptionParser('knife');
  703. $parser->addSubcommand('slice');
  704. $shell = $this->getMock('Cake\Console\Shell', ['hasTask', 'startup', 'getOptionParser'], [], '', false);
  705. $task = $this->getMock('Cake\Console\Shell', ['main', 'runCommand'], [], '', false);
  706. $task->expects($this->once())
  707. ->method('runCommand')
  708. ->with(['one'], false);
  709. $shell->expects($this->once())->method('getOptionParser')
  710. ->will($this->returnValue($parser));
  711. $shell->expects($this->once())->method('startup');
  712. $shell->expects($this->any())
  713. ->method('hasTask')
  714. ->will($this->returnValue(true));
  715. $shell->Slice = $task;
  716. $shell->runCommand(['slice', 'one']);
  717. }
  718. /**
  719. * test calling a shell that dispatch another one
  720. *
  721. * @return void
  722. */
  723. public function testDispatchShell()
  724. {
  725. $Shell = new TestingDispatchShell();
  726. ob_start();
  727. $Shell->runCommand(['test_task'], true);
  728. $result = ob_get_clean();
  729. $expected = <<<TEXT
  730. <info>Welcome to CakePHP Console</info>
  731. I am a test task, I dispatch another Shell
  732. <info>Welcome to CakePHP Console</info>
  733. I am a dispatched Shell
  734. TEXT;
  735. $this->assertEquals($expected, $result);
  736. }
  737. /**
  738. * test wrapBlock wrapping text.
  739. *
  740. * @return void
  741. */
  742. public function testWrapText()
  743. {
  744. $text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.';
  745. $result = $this->Shell->wrapText($text, ['width' => 33]);
  746. $expected = <<<TEXT
  747. This is the song that never ends.
  748. This is the song that never ends.
  749. This is the song that never ends.
  750. TEXT;
  751. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  752. $result = $this->Shell->wrapText($text, ['indent' => ' ', 'width' => 33]);
  753. $expected = <<<TEXT
  754. This is the song that never ends.
  755. This is the song that never ends.
  756. This is the song that never ends.
  757. TEXT;
  758. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  759. }
  760. /**
  761. * Testing camel cased naming of tasks
  762. *
  763. * @return void
  764. */
  765. public function testShellNaming()
  766. {
  767. $this->Shell->tasks = ['TestApple'];
  768. $this->Shell->loadTasks();
  769. $expected = 'TestApple';
  770. $this->assertEquals($expected, $this->Shell->TestApple->name);
  771. }
  772. /**
  773. * Test reading params
  774. *
  775. * @dataProvider paramReadingDataProvider
  776. */
  777. public function testParamReading($toRead, $expected)
  778. {
  779. $this->Shell->params = [
  780. 'key' => 'value',
  781. 'help' => false,
  782. 'emptykey' => '',
  783. 'truthy' => true
  784. ];
  785. $this->assertSame($expected, $this->Shell->param($toRead));
  786. }
  787. /**
  788. * Data provider for testing reading values with Shell::param()
  789. *
  790. * @return array
  791. */
  792. public function paramReadingDataProvider()
  793. {
  794. return [
  795. [
  796. 'key',
  797. 'value',
  798. ],
  799. [
  800. 'help',
  801. false,
  802. ],
  803. [
  804. 'emptykey',
  805. '',
  806. ],
  807. [
  808. 'truthy',
  809. true,
  810. ],
  811. [
  812. 'does_not_exist',
  813. null,
  814. ]
  815. ];
  816. }
  817. /**
  818. * Test that option parsers are created with the correct name/command.
  819. *
  820. * @return void
  821. */
  822. public function testGetOptionParser()
  823. {
  824. $this->Shell->name = 'test';
  825. $this->Shell->plugin = 'plugin';
  826. $parser = $this->Shell->getOptionParser();
  827. $this->assertEquals('plugin.test', $parser->command());
  828. }
  829. /**
  830. * Test file and console and logging quiet output
  831. *
  832. * @return void
  833. */
  834. public function testQuietLog()
  835. {
  836. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  837. $io->expects($this->once())
  838. ->method('level')
  839. ->with(Shell::QUIET);
  840. $io->expects($this->at(0))
  841. ->method('setLoggers')
  842. ->with(true);
  843. $io->expects($this->at(2))
  844. ->method('setLoggers')
  845. ->with(false);
  846. $this->Shell = $this->getMock(__NAMESPACE__ . '\ShellTestShell', ['_useLogger'], [$io]);
  847. $this->Shell->runCommand(['foo', '--quiet']);
  848. }
  849. /**
  850. * Tests __debugInfo
  851. *
  852. * @return void
  853. */
  854. public function testDebugInfo()
  855. {
  856. $expected = [
  857. 'name' => 'ShellTestShell',
  858. 'plugin' => null,
  859. 'command' => null,
  860. 'tasks' => [],
  861. 'params' => [],
  862. 'args' => [],
  863. 'interactive' => true
  864. ];
  865. $result = $this->Shell->__debugInfo();
  866. $this->assertEquals($expected, $result);
  867. }
  868. }