ShellTest.php 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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 a command called with an extra parameter passed merges the extra parameters
  550. * to the shell's one
  551. * Also tests that if an extra `requested` parameter prevents the welcome message from
  552. * being displayed
  553. *
  554. * @return void
  555. */
  556. public function testRunCommandWithExtra()
  557. {
  558. $Parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  559. $io = $this->getMock('Cake\Console\ConsoleIo');
  560. $Shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'slice', '_welcome', 'param'], [$io]);
  561. $Parser->addSubCommand('slice');
  562. $Shell->expects($this->once())
  563. ->method('getOptionParser')
  564. ->will($this->returnValue($Parser));
  565. $Shell->expects($this->once())
  566. ->method('slice')
  567. ->with('cakes');
  568. $Shell->expects($this->never())->method('_welcome');
  569. $Shell->expects($this->once())->method('param')
  570. ->with('requested')
  571. ->will($this->returnValue(true));
  572. $Shell->runCommand(['slice', 'cakes'], false, ['requested' => true]);
  573. }
  574. /**
  575. * Test the dispatchShell() arguments parser
  576. *
  577. * @return void
  578. */
  579. public function testDispatchShellArgsParser()
  580. {
  581. $Shell = new Shell();
  582. $expected = [['schema', 'create', 'DbAcl'], []];
  583. // Shell::dispatchShell('schema create DbAcl');
  584. $result = $Shell->parseDispatchArguments(['schema create DbAcl']);
  585. $this->assertEquals($expected, $result);
  586. // Shell::dispatchShell('schema', 'create', 'DbAcl');
  587. $result = $Shell->parseDispatchArguments(['schema', 'create', 'DbAcl']);
  588. $this->assertEquals($expected, $result);
  589. // Shell::dispatchShell(['command' => 'schema create DbAcl']);
  590. $result = $Shell->parseDispatchArguments([[
  591. 'command' => 'schema create DbAcl'
  592. ]]);
  593. $this->assertEquals($expected, $result);
  594. // Shell::dispatchShell(['command' => ['schema', 'create', 'DbAcl']]);
  595. $result = $Shell->parseDispatchArguments([[
  596. 'command' => ['schema', 'create', 'DbAcl']
  597. ]]);
  598. $this->assertEquals($expected, $result);
  599. $expected[1] = ['param' => 'value'];
  600. // Shell::dispatchShell(['command' => 'schema create DbAcl', 'extra' => ['param' => 'value']]);
  601. $result = $Shell->parseDispatchArguments([[
  602. 'command' => 'schema create DbAcl',
  603. 'extra' => ['param' => 'value']
  604. ]]);
  605. $this->assertEquals($expected, $result);
  606. // Shell::dispatchShell(['command' => ['schema', 'create', 'DbAcl'], 'extra' => ['param' => 'value']]);
  607. $result = $Shell->parseDispatchArguments([[
  608. 'command' => ['schema', 'create', 'DbAcl'],
  609. 'extra' => ['param' => 'value']
  610. ]]);
  611. $this->assertEquals($expected, $result);
  612. }
  613. /**
  614. * test calling a shell that dispatch another one
  615. *
  616. * @return void
  617. */
  618. public function testDispatchShell()
  619. {
  620. $Shell = new TestingDispatchShell();
  621. ob_start();
  622. $Shell->runCommand(['test_task'], true);
  623. $result = ob_get_clean();
  624. $expected = <<<TEXT
  625. <info>Welcome to CakePHP Console</info>
  626. I am a test task, I dispatch another Shell
  627. I am a dispatched Shell
  628. TEXT;
  629. $this->assertEquals($expected, $result);
  630. ob_start();
  631. $Shell->runCommand(['test_task_dispatch_array'], true);
  632. $result = ob_get_clean();
  633. $this->assertEquals($expected, $result);
  634. ob_start();
  635. $Shell->runCommand(['test_task_dispatch_command_string'], true);
  636. $result = ob_get_clean();
  637. $this->assertEquals($expected, $result);
  638. ob_start();
  639. $Shell->runCommand(['test_task_dispatch_command_array'], true);
  640. $result = ob_get_clean();
  641. $this->assertEquals($expected, $result);
  642. $expected = <<<TEXT
  643. <info>Welcome to CakePHP Console</info>
  644. I am a test task, I dispatch another Shell
  645. I am a dispatched Shell. My param `foo` has the value `bar`
  646. TEXT;
  647. ob_start();
  648. $Shell->runCommand(['test_task_dispatch_with_param'], true);
  649. $result = ob_get_clean();
  650. $this->assertEquals($expected, $result);
  651. $expected = <<<TEXT
  652. <info>Welcome to CakePHP Console</info>
  653. I am a test task, I dispatch another Shell
  654. I am a dispatched Shell. My param `foo` has the value `bar`
  655. My param `fooz` has the value `baz`
  656. TEXT;
  657. ob_start();
  658. $Shell->runCommand(['test_task_dispatch_with_multiple_params'], true);
  659. $result = ob_get_clean();
  660. $this->assertEquals($expected, $result);
  661. $expected = <<<TEXT
  662. <info>Welcome to CakePHP Console</info>
  663. I am a test task, I dispatch another Shell
  664. <info>Welcome to CakePHP Console</info>
  665. I am a dispatched Shell
  666. TEXT;
  667. ob_start();
  668. $Shell->runCommand(['test_task_dispatch_with_requested_off'], true);
  669. $result = ob_get_clean();
  670. $this->assertEquals($expected, $result);
  671. }
  672. /**
  673. * Test that runCommand() doesn't call public methods when the second arg is false.
  674. *
  675. * @return void
  676. */
  677. public function testRunCommandAutoMethodOff()
  678. {
  679. $io = $this->getMock('Cake\Console\ConsoleIo');
  680. $shell = $this->getMock('Cake\Console\Shell', ['hit_me', 'startup'], [$io]);
  681. $shell->expects($this->never())->method('startup');
  682. $shell->expects($this->never())->method('hit_me');
  683. $result = $shell->runCommand(['hit_me', 'baseball'], false);
  684. $this->assertFalse($result);
  685. $result = $shell->runCommand(['hit_me', 'baseball']);
  686. $this->assertFalse($result, 'Default value of runCommand() should be false');
  687. }
  688. /**
  689. * test run command calling a real method with mismatching subcommands defined.
  690. *
  691. * @return void
  692. */
  693. public function testRunCommandWithMethodNotInSubcommands()
  694. {
  695. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  696. $io = $this->getMock('Cake\Console\ConsoleIo');
  697. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'roll', 'startup'], [$io]);
  698. $parser->addSubCommand('slice');
  699. $shell->expects($this->any())
  700. ->method('getOptionParser')
  701. ->will($this->returnValue($parser));
  702. $parser->expects($this->once())
  703. ->method('help');
  704. $shell->expects($this->never())->method('startup');
  705. $shell->expects($this->never())->method('roll');
  706. $result = $shell->runCommand(['roll', 'cakes', '--verbose']);
  707. $this->assertFalse($result);
  708. }
  709. /**
  710. * test run command calling a real method with subcommands defined.
  711. *
  712. * @return void
  713. */
  714. public function testRunCommandWithMethodInSubcommands()
  715. {
  716. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  717. $io = $this->getMock('Cake\Console\ConsoleIo');
  718. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'slice', 'startup'], [$io]);
  719. $parser->addSubCommand('slice');
  720. $shell->expects($this->any())
  721. ->method('getOptionParser')
  722. ->will($this->returnValue($parser));
  723. $shell->expects($this->once())->method('startup');
  724. $shell->expects($this->once())
  725. ->method('slice')
  726. ->with('cakes');
  727. $shell->runCommand(['slice', 'cakes', '--verbose']);
  728. }
  729. /**
  730. * test run command calling a missing method with subcommands defined.
  731. *
  732. * @return void
  733. */
  734. public function testRunCommandWithMissingMethodInSubcommands()
  735. {
  736. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  737. $parser->addSubCommand('slice');
  738. $io = $this->getMock('Cake\Console\ConsoleIo');
  739. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'startup'], [$io]);
  740. $shell->expects($this->any())
  741. ->method('getOptionParser')
  742. ->will($this->returnValue($parser));
  743. $shell->expects($this->never())
  744. ->method('startup');
  745. $parser->expects($this->once())
  746. ->method('help');
  747. $shell->runCommand(['slice', 'cakes', '--verbose']);
  748. }
  749. /**
  750. * test run command causing exception on Shell method.
  751. *
  752. * @return void
  753. */
  754. public function testRunCommandBaseclassMethod()
  755. {
  756. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'getOptionParser', 'out'], [], '', false);
  757. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [], '', false);
  758. $parser->expects($this->once())->method('help');
  759. $shell->expects($this->once())->method('getOptionParser')
  760. ->will($this->returnValue($parser));
  761. $shell->expects($this->never())->method('hr');
  762. $shell->expects($this->once())->method('out');
  763. $shell->runCommand(['hr']);
  764. }
  765. /**
  766. * test run command causing exception on Shell method.
  767. *
  768. * @return void
  769. */
  770. public function testRunCommandMissingMethod()
  771. {
  772. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'getOptionParser', 'out'], [], '', false);
  773. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [], '', false);
  774. $parser->expects($this->once())->method('help');
  775. $shell->expects($this->once())->method('getOptionParser')
  776. ->will($this->returnValue($parser));
  777. $shell->expects($this->once())->method('out');
  778. $result = $shell->runCommand(['idontexist']);
  779. $this->assertFalse($result);
  780. }
  781. /**
  782. * test that a --help causes help to show.
  783. *
  784. * @return void
  785. */
  786. public function testRunCommandTriggeringHelp()
  787. {
  788. $Parser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [], '', false);
  789. $Parser->expects($this->once())->method('parse')
  790. ->with(['--help'])
  791. ->will($this->returnValue([['help' => true], []]));
  792. $Parser->expects($this->once())->method('help');
  793. $Shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'out', 'startup', '_welcome'], [], '', false);
  794. $Shell->expects($this->once())->method('getOptionParser')
  795. ->will($this->returnValue($Parser));
  796. $Shell->expects($this->once())->method('out');
  797. $Shell->runCommand(['--help']);
  798. }
  799. /**
  800. * test that runCommand will not call runCommand on tasks that are not subcommands.
  801. *
  802. * @return void
  803. */
  804. public function testRunCommandNotCallUnexposedTask()
  805. {
  806. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'hasTask', 'out'], [], '', false);
  807. $task = $this->getMock('Cake\Console\Shell', ['runCommand'], [], '', false);
  808. $task->expects($this->never())
  809. ->method('runCommand');
  810. $shell->expects($this->any())
  811. ->method('hasTask')
  812. ->will($this->returnValue(true));
  813. $shell->expects($this->never())->method('startup');
  814. $shell->expects($this->once())->method('out');
  815. $shell->RunCommand = $task;
  816. $result = $shell->runCommand(['run_command', 'one']);
  817. $this->assertFalse($result);
  818. }
  819. /**
  820. * test that runCommand will call runCommand on the task.
  821. *
  822. * @return void
  823. */
  824. public function testRunCommandHittingTaskInSubcommand()
  825. {
  826. $parser = new ConsoleOptionParser('knife');
  827. $parser->addSubcommand('slice');
  828. $shell = $this->getMock('Cake\Console\Shell', ['hasTask', 'startup', 'getOptionParser'], [], '', false);
  829. $task = $this->getMock('Cake\Console\Shell', ['main', 'runCommand'], [], '', false);
  830. $task->expects($this->once())
  831. ->method('runCommand')
  832. ->with(['one'], false);
  833. $shell->expects($this->once())->method('getOptionParser')
  834. ->will($this->returnValue($parser));
  835. $shell->expects($this->once())->method('startup');
  836. $shell->expects($this->any())
  837. ->method('hasTask')
  838. ->will($this->returnValue(true));
  839. $shell->Slice = $task;
  840. $shell->runCommand(['slice', 'one']);
  841. }
  842. /**
  843. * test wrapBlock wrapping text.
  844. *
  845. * @return void
  846. */
  847. public function testWrapText()
  848. {
  849. $text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.';
  850. $result = $this->Shell->wrapText($text, ['width' => 33]);
  851. $expected = <<<TEXT
  852. This is the song that never ends.
  853. This is the song that never ends.
  854. This is the song that never ends.
  855. TEXT;
  856. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  857. $result = $this->Shell->wrapText($text, ['indent' => ' ', 'width' => 33]);
  858. $expected = <<<TEXT
  859. This is the song that never ends.
  860. This is the song that never ends.
  861. This is the song that never ends.
  862. TEXT;
  863. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  864. }
  865. /**
  866. * Testing camel cased naming of tasks
  867. *
  868. * @return void
  869. */
  870. public function testShellNaming()
  871. {
  872. $this->Shell->tasks = ['TestApple'];
  873. $this->Shell->loadTasks();
  874. $expected = 'TestApple';
  875. $this->assertEquals($expected, $this->Shell->TestApple->name);
  876. }
  877. /**
  878. * Test reading params
  879. *
  880. * @dataProvider paramReadingDataProvider
  881. */
  882. public function testParamReading($toRead, $expected)
  883. {
  884. $this->Shell->params = [
  885. 'key' => 'value',
  886. 'help' => false,
  887. 'emptykey' => '',
  888. 'truthy' => true
  889. ];
  890. $this->assertSame($expected, $this->Shell->param($toRead));
  891. }
  892. /**
  893. * Data provider for testing reading values with Shell::param()
  894. *
  895. * @return array
  896. */
  897. public function paramReadingDataProvider()
  898. {
  899. return [
  900. [
  901. 'key',
  902. 'value',
  903. ],
  904. [
  905. 'help',
  906. false,
  907. ],
  908. [
  909. 'emptykey',
  910. '',
  911. ],
  912. [
  913. 'truthy',
  914. true,
  915. ],
  916. [
  917. 'does_not_exist',
  918. null,
  919. ]
  920. ];
  921. }
  922. /**
  923. * Test that option parsers are created with the correct name/command.
  924. *
  925. * @return void
  926. */
  927. public function testGetOptionParser()
  928. {
  929. $this->Shell->name = 'test';
  930. $this->Shell->plugin = 'plugin';
  931. $parser = $this->Shell->getOptionParser();
  932. $this->assertEquals('plugin.test', $parser->command());
  933. }
  934. /**
  935. * Test file and console and logging quiet output
  936. *
  937. * @return void
  938. */
  939. public function testQuietLog()
  940. {
  941. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  942. $io->expects($this->once())
  943. ->method('level')
  944. ->with(Shell::QUIET);
  945. $io->expects($this->at(0))
  946. ->method('setLoggers')
  947. ->with(true);
  948. $io->expects($this->at(2))
  949. ->method('setLoggers')
  950. ->with(false);
  951. $this->Shell = $this->getMock(__NAMESPACE__ . '\ShellTestShell', ['_useLogger'], [$io]);
  952. $this->Shell->runCommand(['foo', '--quiet']);
  953. }
  954. /**
  955. * Tests __debugInfo
  956. *
  957. * @return void
  958. */
  959. public function testDebugInfo()
  960. {
  961. $expected = [
  962. 'name' => 'ShellTestShell',
  963. 'plugin' => null,
  964. 'command' => null,
  965. 'tasks' => [],
  966. 'params' => [],
  967. 'args' => [],
  968. 'interactive' => true
  969. ];
  970. $result = $this->Shell->__debugInfo();
  971. $this->assertEquals($expected, $result);
  972. }
  973. }