ShellTest.php 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  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. * testInfo method
  276. *
  277. * @return void
  278. */
  279. public function testInfo()
  280. {
  281. $this->io->expects($this->once())
  282. ->method('out')
  283. ->with('<info>Just a test</info>', 1);
  284. $this->Shell->info('Just a test');
  285. }
  286. /**
  287. * testWarn method
  288. *
  289. * @return void
  290. */
  291. public function testWarn()
  292. {
  293. $this->io->expects($this->once())
  294. ->method('err')
  295. ->with('<warning>Just a test</warning>', 1);
  296. $this->Shell->warn('Just a test');
  297. }
  298. /**
  299. * testSuccess method
  300. *
  301. * @return void
  302. */
  303. public function testSuccess()
  304. {
  305. $this->io->expects($this->once())
  306. ->method('out')
  307. ->with('<success>Just a test</success>', 1);
  308. $this->Shell->success('Just a test');
  309. }
  310. /**
  311. * testNl
  312. *
  313. * @return void
  314. */
  315. public function testNl()
  316. {
  317. $this->io->expects($this->once())
  318. ->method('nl')
  319. ->with(2);
  320. $this->Shell->nl(2);
  321. }
  322. /**
  323. * testHr
  324. *
  325. * @return void
  326. */
  327. public function testHr()
  328. {
  329. $this->io->expects($this->once())
  330. ->method('hr')
  331. ->with(2);
  332. $this->Shell->hr(2);
  333. }
  334. /**
  335. * testError
  336. *
  337. * @return void
  338. */
  339. public function testError()
  340. {
  341. $this->io->expects($this->at(0))
  342. ->method('err')
  343. ->with('<error>Error:</error> Foo Not Found');
  344. $this->io->expects($this->at(1))
  345. ->method('err')
  346. ->with("Searched all...");
  347. $this->Shell->error('Foo Not Found', 'Searched all...');
  348. $this->assertSame($this->Shell->stopped, 1);
  349. }
  350. /**
  351. * testLoadTasks method
  352. *
  353. * @return void
  354. */
  355. public function testLoadTasks()
  356. {
  357. $this->assertTrue($this->Shell->loadTasks());
  358. $this->Shell->tasks = null;
  359. $this->assertTrue($this->Shell->loadTasks());
  360. $this->Shell->tasks = false;
  361. $this->assertTrue($this->Shell->loadTasks());
  362. $this->Shell->tasks = true;
  363. $this->assertTrue($this->Shell->loadTasks());
  364. $this->Shell->tasks = [];
  365. $this->assertTrue($this->Shell->loadTasks());
  366. $this->Shell->tasks = ['TestApple'];
  367. $this->assertTrue($this->Shell->loadTasks());
  368. $this->assertInstanceOf('Cake\Shell\Task\TestAppleTask', $this->Shell->TestApple);
  369. $this->Shell->tasks = 'TestBanana';
  370. $this->assertTrue($this->Shell->loadTasks());
  371. $this->assertInstanceOf('Cake\Shell\Task\TestAppleTask', $this->Shell->TestApple);
  372. $this->assertInstanceOf('Cake\Shell\Task\TestBananaTask', $this->Shell->TestBanana);
  373. unset($this->Shell->ShellTestApple, $this->Shell->TestBanana);
  374. $this->Shell->tasks = ['TestApple', 'TestBanana'];
  375. $this->assertTrue($this->Shell->loadTasks());
  376. $this->assertInstanceOf('Cake\Shell\Task\TestAppleTask', $this->Shell->TestApple);
  377. $this->assertInstanceOf('Cake\Shell\Task\TestBananaTask', $this->Shell->TestBanana);
  378. }
  379. /**
  380. * test that __get() makes args and params references
  381. *
  382. * @return void
  383. */
  384. public function testMagicGetArgAndParamReferences()
  385. {
  386. $this->Shell->tasks = ['TestApple'];
  387. $this->Shell->args = ['one'];
  388. $this->Shell->params = ['help' => false];
  389. $this->Shell->loadTasks();
  390. $result = $this->Shell->TestApple;
  391. $this->Shell->args = ['one', 'two'];
  392. $this->assertSame($this->Shell->args, $result->args);
  393. $this->assertSame($this->Shell->params, $result->params);
  394. }
  395. /**
  396. * testShortPath method
  397. *
  398. * @return void
  399. */
  400. public function testShortPath()
  401. {
  402. $path = $expected = DS . 'tmp/ab/cd';
  403. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  404. $path = $expected = DS . 'tmp/ab/cd/';
  405. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  406. $path = $expected = DS . 'tmp/ab/index.php';
  407. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  408. $path = DS . 'tmp/ab/' . DS . 'cd';
  409. $expected = DS . 'tmp/ab/cd';
  410. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  411. $path = 'tmp/ab';
  412. $expected = 'tmp/ab';
  413. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  414. $path = 'tmp/ab';
  415. $expected = 'tmp/ab';
  416. $this->assertPathEquals($expected, $this->Shell->shortPath($path));
  417. $path = APP;
  418. $result = $this->Shell->shortPath($path);
  419. $this->assertNotContains(ROOT, $result, 'Short paths should not contain ROOT');
  420. }
  421. /**
  422. * testCreateFile method
  423. *
  424. * @return void
  425. */
  426. public function testCreateFileNonInteractive()
  427. {
  428. $eol = PHP_EOL;
  429. $path = TMP . 'shell_test';
  430. $file = $path . DS . 'file1.php';
  431. new Folder($path, true);
  432. $contents = "<?php{$eol}echo 'test';${eol}\$te = 'st';{$eol}";
  433. $result = $this->Shell->createFile($file, $contents);
  434. $this->assertTrue($result);
  435. $this->assertTrue(file_exists($file));
  436. $this->assertEquals(file_get_contents($file), $contents);
  437. }
  438. /**
  439. * Test that files are not changed with a 'n' reply.
  440. *
  441. * @return void
  442. */
  443. public function testCreateFileNoReply()
  444. {
  445. $eol = PHP_EOL;
  446. $path = TMP . 'shell_test';
  447. $file = $path . DS . 'file1.php';
  448. new Folder($path, true);
  449. $this->io->expects($this->once())
  450. ->method('askChoice')
  451. ->will($this->returnValue('n'));
  452. touch($file);
  453. $this->assertTrue(file_exists($file));
  454. $contents = "My content";
  455. $result = $this->Shell->createFile($file, $contents);
  456. $this->assertTrue(file_exists($file));
  457. $this->assertTextEquals('', file_get_contents($file));
  458. $this->assertFalse($result, 'Did not create file.');
  459. }
  460. /**
  461. * Test that files are changed with a 'y' reply.
  462. *
  463. * @return void
  464. */
  465. public function testCreateFileOverwrite()
  466. {
  467. $eol = PHP_EOL;
  468. $path = TMP . 'shell_test';
  469. $file = $path . DS . 'file1.php';
  470. new Folder($path, true);
  471. $this->io->expects($this->once())
  472. ->method('askChoice')
  473. ->will($this->returnValue('y'));
  474. touch($file);
  475. $this->assertTrue(file_exists($file));
  476. $contents = "My content";
  477. $result = $this->Shell->createFile($file, $contents);
  478. $this->assertTrue(file_exists($file));
  479. $this->assertTextEquals($contents, file_get_contents($file));
  480. $this->assertTrue($result, 'Did create file.');
  481. }
  482. /**
  483. * Test that there is no user prompt in non-interactive mode while file already exists.
  484. *
  485. * @return void
  486. */
  487. public function testCreateFileOverwriteNonInteractive()
  488. {
  489. $path = TMP . 'shell_test';
  490. $file = $path . DS . 'file1.php';
  491. new Folder($path, true);
  492. touch($file);
  493. $this->assertTrue(file_exists($file));
  494. $this->io->expects($this->never())->method('askChoice');
  495. $this->Shell->interactive = false;
  496. $result = $this->Shell->createFile($file, 'My content');
  497. $this->assertTrue($result);
  498. $this->assertEquals(file_get_contents($file), 'My content');
  499. }
  500. /**
  501. * Test that all files are changed with a 'a' reply.
  502. *
  503. * @return void
  504. */
  505. public function testCreateFileOverwriteAll()
  506. {
  507. $eol = PHP_EOL;
  508. $path = TMP . 'shell_test';
  509. $files = [
  510. $path . DS . 'file1.php' => 'My first content',
  511. $path . DS . 'file2.php' => 'My second content',
  512. $path . DS . 'file3.php' => 'My third content'
  513. ];
  514. new Folder($path, true);
  515. $this->io->expects($this->once())
  516. ->method('askChoice')
  517. ->will($this->returnValue('a'));
  518. foreach ($files as $file => $contents) {
  519. touch($file);
  520. $this->assertTrue(file_exists($file));
  521. $result = $this->Shell->createFile($file, $contents);
  522. $this->assertTrue(file_exists($file));
  523. $this->assertTextEquals($contents, file_get_contents($file));
  524. $this->assertTrue($result, 'Did create file.');
  525. }
  526. }
  527. /**
  528. * Test that you can't create files that aren't writable.
  529. *
  530. * @return void
  531. */
  532. public function testCreateFileNoPermissions()
  533. {
  534. $this->skipIf(DS === '\\', 'Cant perform operations using permissions on windows.');
  535. $path = TMP . 'shell_test';
  536. $file = $path . DS . 'no_perms';
  537. if (!is_dir($path)) {
  538. mkdir($path);
  539. }
  540. chmod($path, 0444);
  541. $this->Shell->createFile($file, 'testing');
  542. $this->assertFalse(file_exists($file));
  543. chmod($path, 0744);
  544. rmdir($path);
  545. }
  546. /**
  547. * test hasTask method
  548. *
  549. * @return void
  550. */
  551. public function testHasTask()
  552. {
  553. $this->Shell->tasks = ['Extract', 'DbConfig'];
  554. $this->Shell->loadTasks();
  555. $this->assertTrue($this->Shell->hasTask('extract'));
  556. $this->assertTrue($this->Shell->hasTask('Extract'));
  557. $this->assertFalse($this->Shell->hasTask('random'));
  558. $this->assertTrue($this->Shell->hasTask('db_config'));
  559. $this->assertTrue($this->Shell->hasTask('DbConfig'));
  560. }
  561. /**
  562. * test the hasMethod
  563. *
  564. * @return void
  565. */
  566. public function testHasMethod()
  567. {
  568. $this->assertTrue($this->Shell->hasMethod('doSomething'));
  569. $this->assertFalse($this->Shell->hasMethod('hr'), 'hr is callable');
  570. $this->assertFalse($this->Shell->hasMethod('_secret'), '_secret is callable');
  571. $this->assertFalse($this->Shell->hasMethod('no_access'), 'no_access is callable');
  572. }
  573. /**
  574. * test run command calling main.
  575. *
  576. * @return void
  577. */
  578. public function testRunCommandMain()
  579. {
  580. $io = $this->getMock('Cake\Console\ConsoleIo');
  581. $shell = $this->getMock('Cake\Console\Shell', ['main', 'startup'], [$io]);
  582. $shell->expects($this->once())->method('startup');
  583. $shell->expects($this->once())->method('main')
  584. ->with('cakes')
  585. ->will($this->returnValue(true));
  586. $result = $shell->runCommand(['cakes', '--verbose']);
  587. $this->assertTrue($result);
  588. $this->assertEquals('main', $shell->command);
  589. }
  590. /**
  591. * test run command calling a real method with no subcommands defined.
  592. *
  593. * @return void
  594. */
  595. public function testRunCommandWithMethod()
  596. {
  597. $io = $this->getMock('Cake\Console\ConsoleIo');
  598. $shell = $this->getMock('Cake\Console\Shell', ['hitMe', 'startup'], [$io]);
  599. $shell->expects($this->once())->method('startup');
  600. $shell->expects($this->once())->method('hitMe')
  601. ->with('cakes')
  602. ->will($this->returnValue(true));
  603. $result = $shell->runCommand(['hit_me', 'cakes', '--verbose'], true);
  604. $this->assertTrue($result);
  605. $this->assertEquals('hit_me', $shell->command);
  606. }
  607. /**
  608. * test that a command called with an extra parameter passed merges the extra parameters
  609. * to the shell's one
  610. * Also tests that if an extra `requested` parameter prevents the welcome message from
  611. * being displayed
  612. *
  613. * @return void
  614. */
  615. public function testRunCommandWithExtra()
  616. {
  617. $Parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  618. $io = $this->getMock('Cake\Console\ConsoleIo');
  619. $Shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'slice', '_welcome', 'param'], [$io]);
  620. $Parser->addSubCommand('slice');
  621. $Shell->expects($this->once())
  622. ->method('getOptionParser')
  623. ->will($this->returnValue($Parser));
  624. $Shell->expects($this->once())
  625. ->method('slice')
  626. ->with('cakes');
  627. $Shell->expects($this->never())->method('_welcome');
  628. $Shell->expects($this->once())->method('param')
  629. ->with('requested')
  630. ->will($this->returnValue(true));
  631. $Shell->runCommand(['slice', 'cakes'], false, ['requested' => true]);
  632. }
  633. /**
  634. * Test the dispatchShell() arguments parser
  635. *
  636. * @return void
  637. */
  638. public function testDispatchShellArgsParser()
  639. {
  640. $Shell = new Shell();
  641. $expected = [['schema', 'create', 'DbAcl'], []];
  642. // Shell::dispatchShell('schema create DbAcl');
  643. $result = $Shell->parseDispatchArguments(['schema create DbAcl']);
  644. $this->assertEquals($expected, $result);
  645. // Shell::dispatchShell('schema', 'create', 'DbAcl');
  646. $result = $Shell->parseDispatchArguments(['schema', 'create', 'DbAcl']);
  647. $this->assertEquals($expected, $result);
  648. // Shell::dispatchShell(['command' => 'schema create DbAcl']);
  649. $result = $Shell->parseDispatchArguments([[
  650. 'command' => 'schema create DbAcl'
  651. ]]);
  652. $this->assertEquals($expected, $result);
  653. // Shell::dispatchShell(['command' => ['schema', 'create', 'DbAcl']]);
  654. $result = $Shell->parseDispatchArguments([[
  655. 'command' => ['schema', 'create', 'DbAcl']
  656. ]]);
  657. $this->assertEquals($expected, $result);
  658. $expected[1] = ['param' => 'value'];
  659. // Shell::dispatchShell(['command' => 'schema create DbAcl', 'extra' => ['param' => 'value']]);
  660. $result = $Shell->parseDispatchArguments([[
  661. 'command' => 'schema create DbAcl',
  662. 'extra' => ['param' => 'value']
  663. ]]);
  664. $this->assertEquals($expected, $result);
  665. // Shell::dispatchShell(['command' => ['schema', 'create', 'DbAcl'], 'extra' => ['param' => 'value']]);
  666. $result = $Shell->parseDispatchArguments([[
  667. 'command' => ['schema', 'create', 'DbAcl'],
  668. 'extra' => ['param' => 'value']
  669. ]]);
  670. $this->assertEquals($expected, $result);
  671. }
  672. /**
  673. * test calling a shell that dispatch another one
  674. *
  675. * @return void
  676. */
  677. public function testDispatchShell()
  678. {
  679. $Shell = new TestingDispatchShell();
  680. ob_start();
  681. $Shell->runCommand(['test_task'], true);
  682. $result = ob_get_clean();
  683. $expected = <<<TEXT
  684. <info>Welcome to CakePHP Console</info>
  685. I am a test task, I dispatch another Shell
  686. I am a dispatched Shell
  687. TEXT;
  688. $this->assertEquals($expected, $result);
  689. ob_start();
  690. $Shell->runCommand(['test_task_dispatch_array'], true);
  691. $result = ob_get_clean();
  692. $this->assertEquals($expected, $result);
  693. ob_start();
  694. $Shell->runCommand(['test_task_dispatch_command_string'], true);
  695. $result = ob_get_clean();
  696. $this->assertEquals($expected, $result);
  697. ob_start();
  698. $Shell->runCommand(['test_task_dispatch_command_array'], true);
  699. $result = ob_get_clean();
  700. $this->assertEquals($expected, $result);
  701. $expected = <<<TEXT
  702. <info>Welcome to CakePHP Console</info>
  703. I am a test task, I dispatch another Shell
  704. I am a dispatched Shell. My param `foo` has the value `bar`
  705. TEXT;
  706. ob_start();
  707. $Shell->runCommand(['test_task_dispatch_with_param'], true);
  708. $result = ob_get_clean();
  709. $this->assertEquals($expected, $result);
  710. $expected = <<<TEXT
  711. <info>Welcome to CakePHP Console</info>
  712. I am a test task, I dispatch another Shell
  713. I am a dispatched Shell. My param `foo` has the value `bar`
  714. My param `fooz` has the value `baz`
  715. TEXT;
  716. ob_start();
  717. $Shell->runCommand(['test_task_dispatch_with_multiple_params'], true);
  718. $result = ob_get_clean();
  719. $this->assertEquals($expected, $result);
  720. $expected = <<<TEXT
  721. <info>Welcome to CakePHP Console</info>
  722. I am a test task, I dispatch another Shell
  723. <info>Welcome to CakePHP Console</info>
  724. I am a dispatched Shell
  725. TEXT;
  726. ob_start();
  727. $Shell->runCommand(['test_task_dispatch_with_requested_off'], true);
  728. $result = ob_get_clean();
  729. $this->assertEquals($expected, $result);
  730. }
  731. /**
  732. * Test that runCommand() doesn't call public methods when the second arg is false.
  733. *
  734. * @return void
  735. */
  736. public function testRunCommandAutoMethodOff()
  737. {
  738. $io = $this->getMock('Cake\Console\ConsoleIo');
  739. $shell = $this->getMock('Cake\Console\Shell', ['hit_me', 'startup'], [$io]);
  740. $shell->expects($this->never())->method('startup');
  741. $shell->expects($this->never())->method('hit_me');
  742. $result = $shell->runCommand(['hit_me', 'baseball'], false);
  743. $this->assertFalse($result);
  744. $result = $shell->runCommand(['hit_me', 'baseball']);
  745. $this->assertFalse($result, 'Default value of runCommand() should be false');
  746. }
  747. /**
  748. * test run command calling a real method with mismatching subcommands defined.
  749. *
  750. * @return void
  751. */
  752. public function testRunCommandWithMethodNotInSubcommands()
  753. {
  754. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  755. $io = $this->getMock('Cake\Console\ConsoleIo');
  756. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'roll', 'startup'], [$io]);
  757. $parser->addSubCommand('slice');
  758. $shell->expects($this->any())
  759. ->method('getOptionParser')
  760. ->will($this->returnValue($parser));
  761. $parser->expects($this->once())
  762. ->method('help');
  763. $shell->expects($this->never())->method('startup');
  764. $shell->expects($this->never())->method('roll');
  765. $result = $shell->runCommand(['roll', 'cakes', '--verbose']);
  766. $this->assertFalse($result);
  767. }
  768. /**
  769. * test run command calling a real method with subcommands defined.
  770. *
  771. * @return void
  772. */
  773. public function testRunCommandWithMethodInSubcommands()
  774. {
  775. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  776. $io = $this->getMock('Cake\Console\ConsoleIo');
  777. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'slice', 'startup'], [$io]);
  778. $parser->addSubCommand('slice');
  779. $shell->expects($this->any())
  780. ->method('getOptionParser')
  781. ->will($this->returnValue($parser));
  782. $shell->expects($this->once())->method('startup');
  783. $shell->expects($this->once())
  784. ->method('slice')
  785. ->with('cakes');
  786. $shell->runCommand(['slice', 'cakes', '--verbose']);
  787. }
  788. /**
  789. * test run command calling a missing method with subcommands defined.
  790. *
  791. * @return void
  792. */
  793. public function testRunCommandWithMissingMethodInSubcommands()
  794. {
  795. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', ['help'], ['knife']);
  796. $parser->addSubCommand('slice');
  797. $io = $this->getMock('Cake\Console\ConsoleIo');
  798. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'startup'], [$io]);
  799. $shell->expects($this->any())
  800. ->method('getOptionParser')
  801. ->will($this->returnValue($parser));
  802. $shell->expects($this->never())
  803. ->method('startup');
  804. $parser->expects($this->once())
  805. ->method('help');
  806. $shell->runCommand(['slice', 'cakes', '--verbose']);
  807. }
  808. /**
  809. * test run command causing exception on Shell method.
  810. *
  811. * @return void
  812. */
  813. public function testRunCommandBaseclassMethod()
  814. {
  815. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'getOptionParser', 'out'], [], '', false);
  816. $shell->io($this->getMock('Cake\Console\ConsoleIo'));
  817. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [], '', false);
  818. $parser->expects($this->once())->method('help');
  819. $shell->expects($this->once())->method('getOptionParser')
  820. ->will($this->returnValue($parser));
  821. $shell->expects($this->never())->method('hr');
  822. $shell->expects($this->once())->method('out');
  823. $shell->runCommand(['hr']);
  824. }
  825. /**
  826. * test run command causing exception on Shell method.
  827. *
  828. * @return void
  829. */
  830. public function testRunCommandMissingMethod()
  831. {
  832. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'getOptionParser', 'out'], [], '', false);
  833. $shell->io($this->getMock('Cake\Console\ConsoleIo'));
  834. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [], '', false);
  835. $parser->expects($this->once())->method('help');
  836. $shell->expects($this->once())->method('getOptionParser')
  837. ->will($this->returnValue($parser));
  838. $shell->expects($this->once())->method('out');
  839. $result = $shell->runCommand(['idontexist']);
  840. $this->assertFalse($result);
  841. }
  842. /**
  843. * test that a --help causes help to show.
  844. *
  845. * @return void
  846. */
  847. public function testRunCommandTriggeringHelp()
  848. {
  849. $parser = $this->getMock('Cake\Console\ConsoleOptionParser', [], [], '', false);
  850. $parser->expects($this->once())->method('parse')
  851. ->with(['--help'])
  852. ->will($this->returnValue([['help' => true], []]));
  853. $parser->expects($this->once())->method('help');
  854. $shell = $this->getMock('Cake\Console\Shell', ['getOptionParser', 'out', 'startup', '_welcome'], [], '', false);
  855. $shell->io($this->getMock('Cake\Console\ConsoleIo'));
  856. $shell->expects($this->once())->method('getOptionParser')
  857. ->will($this->returnValue($parser));
  858. $shell->expects($this->once())->method('out');
  859. $shell->runCommand(['--help']);
  860. }
  861. /**
  862. * test that runCommand will not call runCommand on tasks that are not subcommands.
  863. *
  864. * @return void
  865. */
  866. public function testRunCommandNotCallUnexposedTask()
  867. {
  868. $shell = $this->getMock('Cake\Console\Shell', ['startup', 'hasTask', 'out'], [], '', false);
  869. $shell->io($this->getMock('Cake\Console\ConsoleIo'));
  870. $task = $this->getMock('Cake\Console\Shell', ['runCommand'], [], '', false);
  871. $task->expects($this->never())
  872. ->method('runCommand');
  873. $shell->expects($this->any())
  874. ->method('hasTask')
  875. ->will($this->returnValue(true));
  876. $shell->expects($this->never())->method('startup');
  877. $shell->expects($this->once())->method('out');
  878. $shell->RunCommand = $task;
  879. $result = $shell->runCommand(['run_command', 'one']);
  880. $this->assertFalse($result);
  881. }
  882. /**
  883. * test that runCommand will call runCommand on the task.
  884. *
  885. * @return void
  886. */
  887. public function testRunCommandHittingTaskInSubcommand()
  888. {
  889. $parser = new ConsoleOptionParser('knife');
  890. $parser->addSubcommand('slice');
  891. $io = $this->getMock('Cake\Console\ConsoleIo');
  892. $shell = $this->getMock('Cake\Console\Shell', ['hasTask', 'startup', 'getOptionParser'], [], '', false);
  893. $shell->io($io);
  894. $task = $this->getMock('Cake\Console\Shell', ['main', 'runCommand'], [], '', false);
  895. $task->io($io);
  896. $task->expects($this->once())
  897. ->method('runCommand')
  898. ->with(['one'], false);
  899. $shell->expects($this->once())->method('getOptionParser')
  900. ->will($this->returnValue($parser));
  901. $shell->expects($this->once())->method('startup');
  902. $shell->expects($this->any())
  903. ->method('hasTask')
  904. ->will($this->returnValue(true));
  905. $shell->Slice = $task;
  906. $shell->runCommand(['slice', 'one']);
  907. }
  908. /**
  909. * test wrapBlock wrapping text.
  910. *
  911. * @return void
  912. */
  913. public function testWrapText()
  914. {
  915. $text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.';
  916. $result = $this->Shell->wrapText($text, ['width' => 33]);
  917. $expected = <<<TEXT
  918. This is the song that never ends.
  919. This is the song that never ends.
  920. This is the song that never ends.
  921. TEXT;
  922. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  923. $result = $this->Shell->wrapText($text, ['indent' => ' ', 'width' => 33]);
  924. $expected = <<<TEXT
  925. This is the song that never ends.
  926. This is the song that never ends.
  927. This is the song that never ends.
  928. TEXT;
  929. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  930. }
  931. /**
  932. * Testing camel cased naming of tasks
  933. *
  934. * @return void
  935. */
  936. public function testShellNaming()
  937. {
  938. $this->Shell->tasks = ['TestApple'];
  939. $this->Shell->loadTasks();
  940. $expected = 'TestApple';
  941. $this->assertEquals($expected, $this->Shell->TestApple->name);
  942. }
  943. /**
  944. * Test reading params
  945. *
  946. * @dataProvider paramReadingDataProvider
  947. */
  948. public function testParamReading($toRead, $expected)
  949. {
  950. $this->Shell->params = [
  951. 'key' => 'value',
  952. 'help' => false,
  953. 'emptykey' => '',
  954. 'truthy' => true
  955. ];
  956. $this->assertSame($expected, $this->Shell->param($toRead));
  957. }
  958. /**
  959. * Data provider for testing reading values with Shell::param()
  960. *
  961. * @return array
  962. */
  963. public function paramReadingDataProvider()
  964. {
  965. return [
  966. [
  967. 'key',
  968. 'value',
  969. ],
  970. [
  971. 'help',
  972. false,
  973. ],
  974. [
  975. 'emptykey',
  976. '',
  977. ],
  978. [
  979. 'truthy',
  980. true,
  981. ],
  982. [
  983. 'does_not_exist',
  984. null,
  985. ]
  986. ];
  987. }
  988. /**
  989. * Test that option parsers are created with the correct name/command.
  990. *
  991. * @return void
  992. */
  993. public function testGetOptionParser()
  994. {
  995. $this->Shell->name = 'test';
  996. $this->Shell->plugin = 'plugin';
  997. $parser = $this->Shell->getOptionParser();
  998. $this->assertEquals('plugin.test', $parser->command());
  999. }
  1000. /**
  1001. * Test file and console and logging quiet output
  1002. *
  1003. * @return void
  1004. */
  1005. public function testQuietLog()
  1006. {
  1007. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  1008. $io->expects($this->once())
  1009. ->method('level')
  1010. ->with(Shell::QUIET);
  1011. $io->expects($this->at(0))
  1012. ->method('setLoggers')
  1013. ->with(true);
  1014. $io->expects($this->at(3))
  1015. ->method('setLoggers')
  1016. ->with(ConsoleIo::QUIET);
  1017. $this->Shell = $this->getMock(__NAMESPACE__ . '\ShellTestShell', ['welcome'], [$io]);
  1018. $this->Shell->runCommand(['foo', '--quiet']);
  1019. }
  1020. /**
  1021. * Tests __debugInfo
  1022. *
  1023. * @return void
  1024. */
  1025. public function testDebugInfo()
  1026. {
  1027. $expected = [
  1028. 'name' => 'ShellTestShell',
  1029. 'plugin' => null,
  1030. 'command' => null,
  1031. 'tasks' => [],
  1032. 'params' => [],
  1033. 'args' => [],
  1034. 'interactive' => true
  1035. ];
  1036. $result = $this->Shell->__debugInfo();
  1037. $this->assertEquals($expected, $result);
  1038. }
  1039. }