ShellTest.php 31 KB

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