ShellTest.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  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 = Shell::CODE_SUCCESS)
  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->assertFileExists($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->assertFileExists($file);
  449. $contents = "My content";
  450. $result = $this->Shell->createFile($file, $contents);
  451. $this->assertFileExists($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->assertFileExists($file);
  471. $contents = "My content";
  472. $result = $this->Shell->createFile($file, $contents);
  473. $this->assertFileExists($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->assertFileExists($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->assertFileExists($file);
  516. $result = $this->Shell->createFile($file, $contents);
  517. $this->assertFileExists($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->assertFileNotExists($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 run command missing parameters
  999. *
  1000. * @return void
  1001. */
  1002. public function testRunCommandMainMissingArgument()
  1003. {
  1004. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  1005. $shell = $this->getMockBuilder('Cake\Console\Shell')
  1006. ->setMethods(['main', 'startup', 'getOptionParser'])
  1007. ->setConstructorArgs([$io])
  1008. ->getMock();
  1009. $parser = new ConsoleOptionParser('test');
  1010. $parser->addArgument('filename', [
  1011. 'required' => true,
  1012. 'help' => 'a file',
  1013. ]);
  1014. $shell->expects($this->once())
  1015. ->method('getOptionParser')
  1016. ->will($this->returnValue($parser));
  1017. $shell->expects($this->never())->method('main');
  1018. $io->expects($this->once())
  1019. ->method('err')
  1020. ->with('<error>Error: Missing required arguments. filename is required.</error>');
  1021. $result = $shell->runCommand([]);
  1022. $this->assertFalse($result, 'Shell should fail');
  1023. }
  1024. /**
  1025. * test wrapBlock wrapping text.
  1026. *
  1027. * @return void
  1028. */
  1029. public function testWrapText()
  1030. {
  1031. $text = 'This is the song that never ends. This is the song that never ends. This is the song that never ends.';
  1032. $result = $this->Shell->wrapText($text, ['width' => 33]);
  1033. $expected = <<<TEXT
  1034. This is the song that never ends.
  1035. This is the song that never ends.
  1036. This is the song that never ends.
  1037. TEXT;
  1038. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  1039. $result = $this->Shell->wrapText($text, ['indent' => ' ', 'width' => 33]);
  1040. $expected = <<<TEXT
  1041. This is the song that never ends.
  1042. This is the song that never ends.
  1043. This is the song that never ends.
  1044. TEXT;
  1045. $this->assertTextEquals($expected, $result, 'Text not wrapped.');
  1046. }
  1047. /**
  1048. * Testing camel cased naming of tasks
  1049. *
  1050. * @return void
  1051. */
  1052. public function testShellNaming()
  1053. {
  1054. $this->Shell->tasks = ['TestApple'];
  1055. $this->Shell->loadTasks();
  1056. $expected = 'TestApple';
  1057. $this->assertEquals($expected, $this->Shell->TestApple->name);
  1058. }
  1059. /**
  1060. * Test reading params
  1061. *
  1062. * @dataProvider paramReadingDataProvider
  1063. */
  1064. public function testParamReading($toRead, $expected)
  1065. {
  1066. $this->Shell->params = [
  1067. 'key' => 'value',
  1068. 'help' => false,
  1069. 'emptykey' => '',
  1070. 'truthy' => true
  1071. ];
  1072. $this->assertSame($expected, $this->Shell->param($toRead));
  1073. }
  1074. /**
  1075. * Data provider for testing reading values with Shell::param()
  1076. *
  1077. * @return array
  1078. */
  1079. public function paramReadingDataProvider()
  1080. {
  1081. return [
  1082. [
  1083. 'key',
  1084. 'value',
  1085. ],
  1086. [
  1087. 'help',
  1088. false,
  1089. ],
  1090. [
  1091. 'emptykey',
  1092. '',
  1093. ],
  1094. [
  1095. 'truthy',
  1096. true,
  1097. ],
  1098. [
  1099. 'does_not_exist',
  1100. null,
  1101. ]
  1102. ];
  1103. }
  1104. /**
  1105. * Test that option parsers are created with the correct name/command.
  1106. *
  1107. * @return void
  1108. */
  1109. public function testGetOptionParser()
  1110. {
  1111. $this->Shell->name = 'test';
  1112. $this->Shell->plugin = 'plugin';
  1113. $parser = $this->Shell->getOptionParser();
  1114. $this->assertEquals('plugin.test', $parser->command());
  1115. }
  1116. /**
  1117. * Test file and console and logging quiet output
  1118. *
  1119. * @return void
  1120. */
  1121. public function testQuietLog()
  1122. {
  1123. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')
  1124. ->disableOriginalConstructor()
  1125. ->getMock();
  1126. $io->expects($this->once())
  1127. ->method('level')
  1128. ->with(Shell::QUIET);
  1129. $io->expects($this->at(0))
  1130. ->method('setLoggers')
  1131. ->with(true);
  1132. $io->expects($this->at(2))
  1133. ->method('setLoggers')
  1134. ->with(ConsoleIo::QUIET);
  1135. $this->Shell = $this->getMockBuilder(__NAMESPACE__ . '\ShellTestShell')
  1136. ->setMethods(['welcome'])
  1137. ->setConstructorArgs([$io])
  1138. ->getMock();
  1139. $this->Shell->runCommand(['foo', '--quiet']);
  1140. }
  1141. /**
  1142. * Test getIo() and setIo() methods
  1143. *
  1144. * @return void
  1145. */
  1146. public function testGetSetIo()
  1147. {
  1148. $this->Shell->setIo($this->io);
  1149. $this->assertSame($this->Shell->getIo(), $this->io);
  1150. }
  1151. /**
  1152. * Tests __debugInfo
  1153. *
  1154. * @return void
  1155. */
  1156. public function testDebugInfo()
  1157. {
  1158. $expected = [
  1159. 'name' => 'ShellTestShell',
  1160. 'plugin' => null,
  1161. 'command' => null,
  1162. 'tasks' => [],
  1163. 'params' => [],
  1164. 'args' => [],
  1165. 'interactive' => true
  1166. ];
  1167. $result = $this->Shell->__debugInfo();
  1168. $this->assertEquals($expected, $result);
  1169. }
  1170. }