ShellDispatcherTest.php 15 KB

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