ShellTest.php 37 KB

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