ShellDispatcherTest.php 13 KB

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