ShellDispatcherTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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\ShellDispatcher;
  17. use Cake\Core\Configure;
  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. Configure::write('App.namespace', 'TestApp');
  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(0, $result);
  185. $this->assertEquals([], $dispatcher->args);
  186. $dispatcher->args = ['mock_with_main'];
  187. $result = $dispatcher->dispatch();
  188. $this->assertSame(0, $result);
  189. $this->assertEquals([], $dispatcher->args);
  190. }
  191. /**
  192. * Verify correct dispatch of Shell subclasses without a main method
  193. *
  194. * @return void
  195. */
  196. public function testDispatchShellWithoutMain()
  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(true));
  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->assertEquals(0, $result);
  213. }
  214. /**
  215. * Verify you can dispatch a plugin's main shell with the shell name alone
  216. *
  217. * @return void
  218. */
  219. public function testDispatchShortPluginAlias()
  220. {
  221. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  222. ->setMethods(['_shellExists', '_createShell'])
  223. ->getMock();
  224. $Shell = $this->getMockBuilder('Cake\Console\Shell')->getMock();
  225. $dispatcher->expects($this->at(1))
  226. ->method('_shellExists')
  227. ->with('TestPlugin.Example')
  228. ->will($this->returnValue('TestPlugin\Console\Command\TestPluginShell'));
  229. $dispatcher->expects($this->at(2))
  230. ->method('_createShell')
  231. ->with('TestPlugin\Console\Command\TestPluginShell', 'TestPlugin.Example')
  232. ->will($this->returnValue($Shell));
  233. $dispatcher->args = ['example'];
  234. $result = $dispatcher->dispatch();
  235. $this->assertEquals(0, $result);
  236. }
  237. /**
  238. * Ensure short plugin shell usage is case/camelized insensitive
  239. *
  240. * @return void
  241. */
  242. public function testDispatchShortPluginAliasCamelized()
  243. {
  244. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  245. ->setMethods(['_shellExists', '_createShell'])
  246. ->getMock();
  247. $Shell = $this->getMockBuilder('Cake\Console\Shell')->getMock();
  248. $dispatcher->expects($this->at(1))
  249. ->method('_shellExists')
  250. ->with('TestPlugin.Example')
  251. ->will($this->returnValue('TestPlugin\Console\Command\TestPluginShell'));
  252. $dispatcher->expects($this->at(2))
  253. ->method('_createShell')
  254. ->with('TestPlugin\Console\Command\TestPluginShell', 'TestPlugin.Example')
  255. ->will($this->returnValue($Shell));
  256. $dispatcher->args = ['Example'];
  257. $result = $dispatcher->dispatch();
  258. $this->assertEquals(0, $result);
  259. }
  260. /**
  261. * Verify that in case of conflict, app shells take precedence in alias list
  262. *
  263. * @return void
  264. */
  265. public function testDispatchShortPluginAliasConflict()
  266. {
  267. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  268. ->setMethods(['_shellExists', '_createShell'])
  269. ->getMock();
  270. $Shell = $this->getMockBuilder('Cake\Console\Shell')->getMock();
  271. $dispatcher->expects($this->at(1))
  272. ->method('_shellExists')
  273. ->with('Sample')
  274. ->will($this->returnValue('App\Shell\SampleShell'));
  275. $dispatcher->expects($this->at(2))
  276. ->method('_createShell')
  277. ->with('App\Shell\SampleShell', 'Sample')
  278. ->will($this->returnValue($Shell));
  279. $dispatcher->args = ['sample'];
  280. $result = $dispatcher->dispatch();
  281. $this->assertEquals(0, $result);
  282. }
  283. /**
  284. * Verify shifting of arguments
  285. *
  286. * @return void
  287. */
  288. public function testShiftArgs()
  289. {
  290. $this->dispatcher->args = ['a', 'b', 'c'];
  291. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  292. $this->assertSame($this->dispatcher->args, ['b', 'c']);
  293. $this->dispatcher->args = ['a' => 'b', 'c', 'd'];
  294. $this->assertEquals('b', $this->dispatcher->shiftArgs());
  295. $this->assertSame($this->dispatcher->args, ['c', 'd']);
  296. $this->dispatcher->args = ['a', 'b' => 'c', 'd'];
  297. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  298. $this->assertSame($this->dispatcher->args, ['b' => 'c', 'd']);
  299. $this->dispatcher->args = [0 => 'a', 2 => 'b', 30 => 'c'];
  300. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  301. $this->assertSame($this->dispatcher->args, [0 => 'b', 1 => 'c']);
  302. $this->dispatcher->args = [];
  303. $this->assertNull($this->dispatcher->shiftArgs());
  304. $this->assertSame([], $this->dispatcher->args);
  305. }
  306. /**
  307. * Test how `bin/cake --help` works.
  308. *
  309. * @return void
  310. */
  311. public function testHelpOption()
  312. {
  313. $mockShell = $this->getMockBuilder('Cake\Shell\CommandListShell')
  314. ->setMethods(['main', 'initialize', 'startup'])
  315. ->getMock();
  316. $mockShell->expects($this->once())
  317. ->method('main');
  318. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  319. ->setMethods(['findShell', '_stop'])
  320. ->getMock();
  321. $dispatcher->expects($this->once())
  322. ->method('findShell')
  323. ->with('command_list')
  324. ->will($this->returnValue($mockShell));
  325. $dispatcher->args = ['--help'];
  326. $dispatcher->dispatch();
  327. }
  328. /**
  329. * Test how `bin/cake --version` works.
  330. *
  331. * @return void
  332. */
  333. public function testVersionOption()
  334. {
  335. $mockShell = $this->getMockBuilder('Cake\Shell\CommandListShell')
  336. ->setMethods(['main', 'initialize', 'startup'])
  337. ->getMock();
  338. $mockShell->expects($this->once())
  339. ->method('main');
  340. $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
  341. ->setMethods(['findShell', '_stop'])
  342. ->getMock();
  343. $dispatcher->expects($this->once())
  344. ->method('findShell')
  345. ->with('command_list')
  346. ->will($this->returnValue($mockShell));
  347. $dispatcher->args = ['--version'];
  348. $dispatcher->dispatch();
  349. }
  350. }