ShellTest.php 38 KB

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