ShellDispatcherTest.php 15 KB

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