ShellDispatcherTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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\Plugin;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * ShellDispatcherTest
  22. */
  23. class ShellDispatcherTest extends TestCase
  24. {
  25. /**
  26. * setUp method
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. $this->loadPlugins(['TestPlugin', 'Company/TestPluginThree']);
  34. static::setAppNamespace();
  35. $this->dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  36. ->setMethods(['_stop'])
  37. ->getMock();
  38. }
  39. /**
  40. * teardown
  41. *
  42. * @return void
  43. */
  44. public function tearDown()
  45. {
  46. parent::tearDown();
  47. ShellDispatcher::resetAliases();
  48. $this->clearPlugins();
  49. }
  50. /**
  51. * Test error on missing shell
  52. *
  53. * @return void
  54. */
  55. public function testFindShellMissing()
  56. {
  57. $this->expectException(\Cake\Console\Exception\MissingShellException::class);
  58. $this->dispatcher->findShell('nope');
  59. }
  60. /**
  61. * Test error on missing plugin shell
  62. *
  63. * @return void
  64. */
  65. public function testFindShellMissingPlugin()
  66. {
  67. $this->expectException(\Cake\Console\Exception\MissingShellException::class);
  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')
  176. ->disableOriginalConstructor()
  177. ->getMock();
  178. $Shell->expects($this->exactly(2))->method('initialize');
  179. $Shell->expects($this->at(0))->method('runCommand')
  180. ->will($this->returnValue(true));
  181. $Shell->expects($this->at(1))->method('runCommand')
  182. ->will($this->returnValue(null));
  183. $dispatcher->expects($this->any())
  184. ->method('findShell')
  185. ->with('mock_with_main')
  186. ->will($this->returnValue($Shell));
  187. $dispatcher->args = ['mock_with_main'];
  188. $result = $dispatcher->dispatch();
  189. $this->assertSame(Shell::CODE_SUCCESS, $result);
  190. $this->assertEquals([], $dispatcher->args);
  191. $dispatcher->args = ['mock_with_main'];
  192. $result = $dispatcher->dispatch();
  193. $this->assertSame(Shell::CODE_SUCCESS, $result);
  194. $this->assertEquals([], $dispatcher->args);
  195. }
  196. /**
  197. * Verifies correct dispatch of Shell subclasses with integer exit codes.
  198. *
  199. * @return void
  200. */
  201. public function testDispatchShellWithIntegerSuccessCode()
  202. {
  203. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  204. ->setMethods(['findShell'])
  205. ->getMock();
  206. $Shell = $this->getMockBuilder('Cake\Console\Shell')
  207. ->disableOriginalConstructor()
  208. ->getMock();
  209. $Shell->expects($this->once())->method('initialize');
  210. $Shell->expects($this->once())->method('runCommand')
  211. ->with(['initdb'])
  212. ->will($this->returnValue(Shell::CODE_SUCCESS));
  213. $dispatcher->expects($this->any())
  214. ->method('findShell')
  215. ->with('mock_without_main')
  216. ->will($this->returnValue($Shell));
  217. $dispatcher->args = ['mock_without_main', 'initdb'];
  218. $result = $dispatcher->dispatch();
  219. $this->assertSame(Shell::CODE_SUCCESS, $result);
  220. }
  221. /**
  222. * Verifies correct dispatch of Shell subclasses with custom integer exit codes.
  223. *
  224. * @return void
  225. */
  226. public function testDispatchShellWithCustomIntegerCodes()
  227. {
  228. $customErrorCode = 3;
  229. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  230. ->setMethods(['findShell'])
  231. ->getMock();
  232. $Shell = $this->getMockBuilder('Cake\Console\Shell')
  233. ->disableOriginalConstructor()
  234. ->getMock();
  235. $Shell->expects($this->once())->method('initialize');
  236. $Shell->expects($this->once())->method('runCommand')
  237. ->with(['initdb'])
  238. ->will($this->returnValue($customErrorCode));
  239. $dispatcher->expects($this->any())
  240. ->method('findShell')
  241. ->with('mock_without_main')
  242. ->will($this->returnValue($Shell));
  243. $dispatcher->args = ['mock_without_main', 'initdb'];
  244. $result = $dispatcher->dispatch();
  245. $this->assertSame($customErrorCode, $result);
  246. }
  247. /**
  248. * Verify correct dispatch of Shell subclasses without a main method
  249. *
  250. * @return void
  251. */
  252. public function testDispatchShellWithoutMain()
  253. {
  254. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  255. ->setMethods(['findShell'])
  256. ->getMock();
  257. $Shell = $this->getMockBuilder('Cake\Console\Shell')
  258. ->disableOriginalConstructor()
  259. ->getMock();
  260. $Shell->expects($this->once())->method('initialize');
  261. $Shell->expects($this->once())->method('runCommand')
  262. ->with(['initdb'])
  263. ->will($this->returnValue(true));
  264. $dispatcher->expects($this->any())
  265. ->method('findShell')
  266. ->with('mock_without_main')
  267. ->will($this->returnValue($Shell));
  268. $dispatcher->args = ['mock_without_main', 'initdb'];
  269. $result = $dispatcher->dispatch();
  270. $this->assertSame(Shell::CODE_SUCCESS, $result);
  271. }
  272. /**
  273. * Verify you can dispatch a plugin's main shell with the shell name alone
  274. *
  275. * @return void
  276. */
  277. public function testDispatchShortPluginAlias()
  278. {
  279. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  280. ->setMethods(['_shellExists', '_createShell'])
  281. ->getMock();
  282. $Shell = $this->getMockBuilder('Cake\Console\Shell')
  283. ->disableOriginalConstructor()
  284. ->getMock();
  285. $dispatcher->expects($this->at(1))
  286. ->method('_shellExists')
  287. ->with('TestPlugin.Example')
  288. ->will($this->returnValue('TestPlugin\Console\Command\TestPluginShell'));
  289. $dispatcher->expects($this->at(2))
  290. ->method('_createShell')
  291. ->with('TestPlugin\Console\Command\TestPluginShell', 'TestPlugin.Example')
  292. ->will($this->returnValue($Shell));
  293. $dispatcher->args = ['example'];
  294. $result = $dispatcher->dispatch();
  295. $this->assertSame(Shell::CODE_SUCCESS, $result);
  296. }
  297. /**
  298. * Ensure short plugin shell usage is case/camelized insensitive
  299. *
  300. * @return void
  301. */
  302. public function testDispatchShortPluginAliasCamelized()
  303. {
  304. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  305. ->setMethods(['_shellExists', '_createShell'])
  306. ->getMock();
  307. $Shell = $this->getMockBuilder('Cake\Console\Shell')
  308. ->disableOriginalConstructor()
  309. ->getMock();
  310. $dispatcher->expects($this->at(1))
  311. ->method('_shellExists')
  312. ->with('TestPlugin.Example')
  313. ->will($this->returnValue('TestPlugin\Console\Command\TestPluginShell'));
  314. $dispatcher->expects($this->at(2))
  315. ->method('_createShell')
  316. ->with('TestPlugin\Console\Command\TestPluginShell', 'TestPlugin.Example')
  317. ->will($this->returnValue($Shell));
  318. $dispatcher->args = ['Example'];
  319. $result = $dispatcher->dispatch();
  320. $this->assertSame(Shell::CODE_SUCCESS, $result);
  321. }
  322. /**
  323. * Verify that in case of conflict, app shells take precedence in alias list
  324. *
  325. * @return void
  326. */
  327. public function testDispatchShortPluginAliasConflict()
  328. {
  329. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  330. ->setMethods(['_shellExists', '_createShell'])
  331. ->getMock();
  332. $Shell = $this->getMockBuilder('Cake\Console\Shell')
  333. ->disableOriginalConstructor()
  334. ->getMock();
  335. $dispatcher->expects($this->at(1))
  336. ->method('_shellExists')
  337. ->with('Sample')
  338. ->will($this->returnValue('App\Shell\SampleShell'));
  339. $dispatcher->expects($this->at(2))
  340. ->method('_createShell')
  341. ->with('App\Shell\SampleShell', 'Sample')
  342. ->will($this->returnValue($Shell));
  343. $dispatcher->args = ['sample'];
  344. $result = $dispatcher->dispatch();
  345. $this->assertSame(Shell::CODE_SUCCESS, $result);
  346. }
  347. /**
  348. * Verify shifting of arguments
  349. *
  350. * @return void
  351. */
  352. public function testShiftArgs()
  353. {
  354. $this->dispatcher->args = ['a', 'b', 'c'];
  355. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  356. $this->assertSame($this->dispatcher->args, ['b', 'c']);
  357. $this->dispatcher->args = ['a' => 'b', 'c', 'd'];
  358. $this->assertEquals('b', $this->dispatcher->shiftArgs());
  359. $this->assertSame($this->dispatcher->args, ['c', 'd']);
  360. $this->dispatcher->args = ['a', 'b' => 'c', 'd'];
  361. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  362. $this->assertSame($this->dispatcher->args, ['b' => 'c', 'd']);
  363. $this->dispatcher->args = [0 => 'a', 2 => 'b', 30 => 'c'];
  364. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  365. $this->assertSame($this->dispatcher->args, [0 => 'b', 1 => 'c']);
  366. $this->dispatcher->args = [];
  367. $this->assertNull($this->dispatcher->shiftArgs());
  368. $this->assertSame([], $this->dispatcher->args);
  369. }
  370. /**
  371. * Test how `bin/cake --help` works.
  372. *
  373. * @return void
  374. */
  375. public function testHelpOption()
  376. {
  377. $mockShell = $this->getMockBuilder('Cake\Shell\CommandListShell')
  378. ->setMethods(['main', 'initialize', 'startup'])
  379. ->getMock();
  380. $mockShell->expects($this->once())
  381. ->method('main');
  382. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  383. ->setMethods(['findShell', '_stop'])
  384. ->getMock();
  385. $dispatcher->expects($this->once())
  386. ->method('findShell')
  387. ->with('command_list')
  388. ->will($this->returnValue($mockShell));
  389. $dispatcher->args = ['--help'];
  390. $dispatcher->dispatch();
  391. }
  392. /**
  393. * Test how `bin/cake --version` works.
  394. *
  395. * @return void
  396. */
  397. public function testVersionOption()
  398. {
  399. $mockShell = $this->getMockBuilder('Cake\Shell\CommandListShell')
  400. ->setMethods(['main', 'initialize', 'startup'])
  401. ->getMock();
  402. $mockShell->expects($this->once())
  403. ->method('main');
  404. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  405. ->setMethods(['findShell', '_stop'])
  406. ->getMock();
  407. $dispatcher->expects($this->once())
  408. ->method('findShell')
  409. ->with('command_list')
  410. ->will($this->returnValue($mockShell));
  411. $dispatcher->args = ['--version'];
  412. $dispatcher->dispatch();
  413. }
  414. }