ShellDispatcherTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Console;
  16. use Cake\Console\Shell;
  17. use Cake\Console\ShellDispatcher;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * ShellDispatcherTest
  23. */
  24. class ShellDispatcherTest extends TestCase
  25. {
  26. /**
  27. * setUp method
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. parent::setUp();
  34. Plugin::load('TestPlugin');
  35. Configure::write('App.namespace', 'TestApp');
  36. $this->dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  37. ->setMethods(['_stop'])
  38. ->getMock();
  39. }
  40. /**
  41. * teardown
  42. *
  43. * @return void
  44. */
  45. public function tearDown()
  46. {
  47. parent::tearDown();
  48. ShellDispatcher::resetAliases();
  49. }
  50. /**
  51. * Test error on missing shell
  52. *
  53. * @expectedException \Cake\Console\Exception\MissingShellException
  54. * @return void
  55. */
  56. public function testFindShellMissing()
  57. {
  58. $this->dispatcher->findShell('nope');
  59. }
  60. /**
  61. * Test error on missing plugin shell
  62. *
  63. * @expectedException \Cake\Console\Exception\MissingShellException
  64. * @return void
  65. */
  66. public function testFindShellMissingPlugin()
  67. {
  68. $this->dispatcher->findShell('test_plugin.nope');
  69. }
  70. /**
  71. * Verify loading of (plugin-) shells
  72. *
  73. * @return void
  74. */
  75. public function testFindShell()
  76. {
  77. $result = $this->dispatcher->findShell('sample');
  78. $this->assertInstanceOf('TestApp\Shell\SampleShell', $result);
  79. $result = $this->dispatcher->findShell('test_plugin.example');
  80. $this->assertInstanceOf('TestPlugin\Shell\ExampleShell', $result);
  81. $this->assertEquals('TestPlugin', $result->plugin);
  82. $this->assertEquals('Example', $result->name);
  83. $result = $this->dispatcher->findShell('TestPlugin.example');
  84. $this->assertInstanceOf('TestPlugin\Shell\ExampleShell', $result);
  85. $result = $this->dispatcher->findShell('TestPlugin.Example');
  86. $this->assertInstanceOf('TestPlugin\Shell\ExampleShell', $result);
  87. }
  88. /**
  89. * testAddShortPluginAlias
  90. *
  91. * @return void
  92. */
  93. public function testAddShortPluginAlias()
  94. {
  95. $expected = [
  96. 'Company' => 'Company/TestPluginThree.company',
  97. 'Example' => 'TestPlugin.example'
  98. ];
  99. $result = $this->dispatcher->addShortPluginAliases();
  100. $this->assertSame($expected, $result, 'Should return the list of aliased plugin shells');
  101. ShellDispatcher::alias('Example', 'SomeOther.PluginsShell');
  102. $expected = [
  103. 'Company' => 'Company/TestPluginThree.company',
  104. 'Example' => 'SomeOther.PluginsShell'
  105. ];
  106. $result = $this->dispatcher->addShortPluginAliases();
  107. $this->assertSame($expected, $result, 'Should not overwrite existing aliases');
  108. }
  109. /**
  110. * Test getting shells with aliases.
  111. *
  112. * @return void
  113. */
  114. public function testFindShellAliased()
  115. {
  116. ShellDispatcher::alias('short', 'test_plugin.example');
  117. $result = $this->dispatcher->findShell('short');
  118. $this->assertInstanceOf('TestPlugin\Shell\ExampleShell', $result);
  119. $this->assertEquals('TestPlugin', $result->plugin);
  120. $this->assertEquals('Example', $result->name);
  121. }
  122. /**
  123. * Test finding a shell that has a matching alias.
  124. *
  125. * Aliases should not overload concrete shells.
  126. *
  127. * @return void
  128. */
  129. public function testFindShellAliasedAppShadow()
  130. {
  131. ShellDispatcher::alias('sample', 'test_plugin.example');
  132. $result = $this->dispatcher->findShell('sample');
  133. $this->assertInstanceOf('TestApp\Shell\SampleShell', $result);
  134. $this->assertEmpty($result->plugin);
  135. $this->assertEquals('Sample', $result->name);
  136. }
  137. /**
  138. * Verify dispatch handling stop errors
  139. *
  140. * @return void
  141. */
  142. public function testDispatchShellWithAbort()
  143. {
  144. $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
  145. $shell = $this->getMockBuilder('Cake\Console\Shell')
  146. ->setMethods(['main'])
  147. ->setConstructorArgs([$io])
  148. ->getMock();
  149. $shell->expects($this->once())
  150. ->method('main')
  151. ->will($this->returnCallback(function () use ($shell) {
  152. $shell->abort('Bad things', 99);
  153. }));
  154. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  155. ->setMethods(['findShell'])
  156. ->getMock();
  157. $dispatcher->expects($this->any())
  158. ->method('findShell')
  159. ->with('aborter')
  160. ->will($this->returnValue($shell));
  161. $dispatcher->args = ['aborter'];
  162. $result = $dispatcher->dispatch();
  163. $this->assertSame(99, $result, 'Should return the exception error code.');
  164. }
  165. /**
  166. * Verify correct dispatch of Shell subclasses with a main method
  167. *
  168. * @return void
  169. */
  170. public function testDispatchShellWithMain()
  171. {
  172. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  173. ->setMethods(['findShell'])
  174. ->getMock();
  175. $Shell = $this->getMockBuilder('Cake\Console\Shell')->getMock();
  176. $Shell->expects($this->exactly(2))->method('initialize');
  177. $Shell->expects($this->at(0))->method('runCommand')
  178. ->will($this->returnValue(true));
  179. $Shell->expects($this->at(1))->method('runCommand')
  180. ->will($this->returnValue(null));
  181. $dispatcher->expects($this->any())
  182. ->method('findShell')
  183. ->with('mock_with_main')
  184. ->will($this->returnValue($Shell));
  185. $dispatcher->args = ['mock_with_main'];
  186. $result = $dispatcher->dispatch();
  187. $this->assertSame(Shell::CODE_SUCCESS, $result);
  188. $this->assertEquals([], $dispatcher->args);
  189. $dispatcher->args = ['mock_with_main'];
  190. $result = $dispatcher->dispatch();
  191. $this->assertSame(Shell::CODE_SUCCESS, $result);
  192. $this->assertEquals([], $dispatcher->args);
  193. }
  194. /**
  195. * Verifies correct dispatch of Shell subclasses with integer exit codes.
  196. *
  197. * @return void
  198. */
  199. public function testDispatchShellWithIntegerSuccessCode()
  200. {
  201. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  202. ->setMethods(['findShell'])
  203. ->getMock();
  204. $Shell = $this->getMockBuilder('Cake\Console\Shell')->getMock();
  205. $Shell->expects($this->once())->method('initialize');
  206. $Shell->expects($this->once())->method('runCommand')
  207. ->with(['initdb'])
  208. ->will($this->returnValue(Shell::CODE_SUCCESS));
  209. $dispatcher->expects($this->any())
  210. ->method('findShell')
  211. ->with('mock_without_main')
  212. ->will($this->returnValue($Shell));
  213. $dispatcher->args = ['mock_without_main', 'initdb'];
  214. $result = $dispatcher->dispatch();
  215. $this->assertSame(Shell::CODE_SUCCESS, $result);
  216. }
  217. /**
  218. * Verifies correct dispatch of Shell subclasses with custom integer exit codes.
  219. *
  220. * @return void
  221. */
  222. public function testDispatchShellWithCustomIntegerCodes()
  223. {
  224. $customErrorCode = 3;
  225. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  226. ->setMethods(['findShell'])
  227. ->getMock();
  228. $Shell = $this->getMockBuilder('Cake\Console\Shell')->getMock();
  229. $Shell->expects($this->once())->method('initialize');
  230. $Shell->expects($this->once())->method('runCommand')
  231. ->with(['initdb'])
  232. ->will($this->returnValue($customErrorCode));
  233. $dispatcher->expects($this->any())
  234. ->method('findShell')
  235. ->with('mock_without_main')
  236. ->will($this->returnValue($Shell));
  237. $dispatcher->args = ['mock_without_main', 'initdb'];
  238. $result = $dispatcher->dispatch();
  239. $this->assertSame($customErrorCode, $result);
  240. }
  241. /**
  242. * Verify correct dispatch of Shell subclasses without a main method
  243. *
  244. * @return void
  245. */
  246. public function testDispatchShellWithoutMain()
  247. {
  248. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  249. ->setMethods(['findShell'])
  250. ->getMock();
  251. $Shell = $this->getMockBuilder('Cake\Console\Shell')->getMock();
  252. $Shell->expects($this->once())->method('initialize');
  253. $Shell->expects($this->once())->method('runCommand')
  254. ->with(['initdb'])
  255. ->will($this->returnValue(true));
  256. $dispatcher->expects($this->any())
  257. ->method('findShell')
  258. ->with('mock_without_main')
  259. ->will($this->returnValue($Shell));
  260. $dispatcher->args = ['mock_without_main', 'initdb'];
  261. $result = $dispatcher->dispatch();
  262. $this->assertSame(Shell::CODE_SUCCESS, $result);
  263. }
  264. /**
  265. * Verify you can dispatch a plugin's main shell with the shell name alone
  266. *
  267. * @return void
  268. */
  269. public function testDispatchShortPluginAlias()
  270. {
  271. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  272. ->setMethods(['_shellExists', '_createShell'])
  273. ->getMock();
  274. $Shell = $this->getMockBuilder('Cake\Console\Shell')->getMock();
  275. $dispatcher->expects($this->at(1))
  276. ->method('_shellExists')
  277. ->with('TestPlugin.Example')
  278. ->will($this->returnValue('TestPlugin\Console\Command\TestPluginShell'));
  279. $dispatcher->expects($this->at(2))
  280. ->method('_createShell')
  281. ->with('TestPlugin\Console\Command\TestPluginShell', 'TestPlugin.Example')
  282. ->will($this->returnValue($Shell));
  283. $dispatcher->args = ['example'];
  284. $result = $dispatcher->dispatch();
  285. $this->assertSame(Shell::CODE_SUCCESS, $result);
  286. }
  287. /**
  288. * Ensure short plugin shell usage is case/camelized insensitive
  289. *
  290. * @return void
  291. */
  292. public function testDispatchShortPluginAliasCamelized()
  293. {
  294. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  295. ->setMethods(['_shellExists', '_createShell'])
  296. ->getMock();
  297. $Shell = $this->getMockBuilder('Cake\Console\Shell')->getMock();
  298. $dispatcher->expects($this->at(1))
  299. ->method('_shellExists')
  300. ->with('TestPlugin.Example')
  301. ->will($this->returnValue('TestPlugin\Console\Command\TestPluginShell'));
  302. $dispatcher->expects($this->at(2))
  303. ->method('_createShell')
  304. ->with('TestPlugin\Console\Command\TestPluginShell', 'TestPlugin.Example')
  305. ->will($this->returnValue($Shell));
  306. $dispatcher->args = ['Example'];
  307. $result = $dispatcher->dispatch();
  308. $this->assertSame(Shell::CODE_SUCCESS, $result);
  309. }
  310. /**
  311. * Verify that in case of conflict, app shells take precedence in alias list
  312. *
  313. * @return void
  314. */
  315. public function testDispatchShortPluginAliasConflict()
  316. {
  317. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  318. ->setMethods(['_shellExists', '_createShell'])
  319. ->getMock();
  320. $Shell = $this->getMockBuilder('Cake\Console\Shell')->getMock();
  321. $dispatcher->expects($this->at(1))
  322. ->method('_shellExists')
  323. ->with('Sample')
  324. ->will($this->returnValue('App\Shell\SampleShell'));
  325. $dispatcher->expects($this->at(2))
  326. ->method('_createShell')
  327. ->with('App\Shell\SampleShell', 'Sample')
  328. ->will($this->returnValue($Shell));
  329. $dispatcher->args = ['sample'];
  330. $result = $dispatcher->dispatch();
  331. $this->assertSame(Shell::CODE_SUCCESS, $result);
  332. }
  333. /**
  334. * Verify shifting of arguments
  335. *
  336. * @return void
  337. */
  338. public function testShiftArgs()
  339. {
  340. $this->dispatcher->args = ['a', 'b', 'c'];
  341. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  342. $this->assertSame($this->dispatcher->args, ['b', 'c']);
  343. $this->dispatcher->args = ['a' => 'b', 'c', 'd'];
  344. $this->assertEquals('b', $this->dispatcher->shiftArgs());
  345. $this->assertSame($this->dispatcher->args, ['c', 'd']);
  346. $this->dispatcher->args = ['a', 'b' => 'c', 'd'];
  347. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  348. $this->assertSame($this->dispatcher->args, ['b' => 'c', 'd']);
  349. $this->dispatcher->args = [0 => 'a', 2 => 'b', 30 => 'c'];
  350. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  351. $this->assertSame($this->dispatcher->args, [0 => 'b', 1 => 'c']);
  352. $this->dispatcher->args = [];
  353. $this->assertNull($this->dispatcher->shiftArgs());
  354. $this->assertSame([], $this->dispatcher->args);
  355. }
  356. /**
  357. * Test how `bin/cake --help` works.
  358. *
  359. * @return void
  360. */
  361. public function testHelpOption()
  362. {
  363. $mockShell = $this->getMockBuilder('Cake\Shell\CommandListShell')
  364. ->setMethods(['main', 'initialize', 'startup'])
  365. ->getMock();
  366. $mockShell->expects($this->once())
  367. ->method('main');
  368. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  369. ->setMethods(['findShell', '_stop'])
  370. ->getMock();
  371. $dispatcher->expects($this->once())
  372. ->method('findShell')
  373. ->with('command_list')
  374. ->will($this->returnValue($mockShell));
  375. $dispatcher->args = ['--help'];
  376. $dispatcher->dispatch();
  377. }
  378. /**
  379. * Test how `bin/cake --version` works.
  380. *
  381. * @return void
  382. */
  383. public function testVersionOption()
  384. {
  385. $mockShell = $this->getMockBuilder('Cake\Shell\CommandListShell')
  386. ->setMethods(['main', 'initialize', 'startup'])
  387. ->getMock();
  388. $mockShell->expects($this->once())
  389. ->method('main');
  390. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  391. ->setMethods(['findShell', '_stop'])
  392. ->getMock();
  393. $dispatcher->expects($this->once())
  394. ->method('findShell')
  395. ->with('command_list')
  396. ->will($this->returnValue($mockShell));
  397. $dispatcher->args = ['--version'];
  398. $dispatcher->dispatch();
  399. }
  400. }