ShellTest.php 33 KB

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