ShellDispatcherTest.php 15 KB

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