ShellDispatcherTest.php 15 KB

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