ShellTest.php 36 KB

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