ShellDispatcherTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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\App;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * ShellDispatcherTest
  23. *
  24. */
  25. class ShellDispatcherTest extends TestCase
  26. {
  27. /**
  28. * setUp method
  29. *
  30. * @return void
  31. */
  32. public function setUp()
  33. {
  34. parent::setUp();
  35. Plugin::load('TestPlugin');
  36. Configure::write('App.namespace', 'TestApp');
  37. $this->dispatcher = $this->getMock('Cake\Console\ShellDispatcher', ['_stop']);
  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->getMock('Cake\Console\ConsoleIo');
  142. $shell = $this->getMock('Cake\Console\Shell', ['main'], [$io]);
  143. $shell->expects($this->once())
  144. ->method('main')
  145. ->will($this->returnCallback(function () use ($shell) {
  146. $shell->abort('Bad things', 99);
  147. }));
  148. $dispatcher = $this->getMock('Cake\Console\ShellDispatcher', ['findShell']);
  149. $dispatcher->expects($this->any())
  150. ->method('findShell')
  151. ->with('aborter')
  152. ->will($this->returnValue($shell));
  153. $dispatcher->args = ['aborter'];
  154. $result = $dispatcher->dispatch();
  155. $this->assertSame(99, $result, 'Should return the exception error code.');
  156. }
  157. /**
  158. * Verify correct dispatch of Shell subclasses with a main method
  159. *
  160. * @return void
  161. */
  162. public function testDispatchShellWithMain()
  163. {
  164. $dispatcher = $this->getMock('Cake\Console\ShellDispatcher', ['findShell']);
  165. $Shell = $this->getMock('Cake\Console\Shell');
  166. $Shell->expects($this->exactly(2))->method('initialize');
  167. $Shell->expects($this->at(0))->method('runCommand')
  168. ->will($this->returnValue(true));
  169. $Shell->expects($this->at(1))->method('runCommand')
  170. ->will($this->returnValue(null));
  171. $dispatcher->expects($this->any())
  172. ->method('findShell')
  173. ->with('mock_with_main')
  174. ->will($this->returnValue($Shell));
  175. $dispatcher->args = ['mock_with_main'];
  176. $result = $dispatcher->dispatch();
  177. $this->assertSame(0, $result);
  178. $this->assertEquals([], $dispatcher->args);
  179. $dispatcher->args = ['mock_with_main'];
  180. $result = $dispatcher->dispatch();
  181. $this->assertSame(0, $result);
  182. $this->assertEquals([], $dispatcher->args);
  183. }
  184. /**
  185. * Verify correct dispatch of Shell subclasses without a main method
  186. *
  187. * @return void
  188. */
  189. public function testDispatchShellWithoutMain()
  190. {
  191. $dispatcher = $this->getMock('Cake\Console\ShellDispatcher', ['findShell']);
  192. $Shell = $this->getMock('Cake\Console\Shell');
  193. $Shell->expects($this->once())->method('initialize');
  194. $Shell->expects($this->once())->method('runCommand')
  195. ->with(['initdb'])
  196. ->will($this->returnValue(true));
  197. $dispatcher->expects($this->any())
  198. ->method('findShell')
  199. ->with('mock_without_main')
  200. ->will($this->returnValue($Shell));
  201. $dispatcher->args = ['mock_without_main', 'initdb'];
  202. $result = $dispatcher->dispatch();
  203. $this->assertEquals(0, $result);
  204. }
  205. /**
  206. * Verify you can dispatch a plugin's main shell with the shell name alone
  207. *
  208. * @return void
  209. */
  210. public function testDispatchShortPluginAlias()
  211. {
  212. $dispatcher = $this->getMock(
  213. 'Cake\Console\ShellDispatcher',
  214. ['_shellExists', '_createShell']
  215. );
  216. $Shell = $this->getMock('Cake\Console\Shell');
  217. $dispatcher->expects($this->at(1))
  218. ->method('_shellExists')
  219. ->with('TestPlugin.Example')
  220. ->will($this->returnValue('TestPlugin\Console\Command\TestPluginShell'));
  221. $dispatcher->expects($this->at(2))
  222. ->method('_createShell')
  223. ->with('TestPlugin\Console\Command\TestPluginShell', 'TestPlugin.Example')
  224. ->will($this->returnValue($Shell));
  225. $dispatcher->args = ['example'];
  226. $result = $dispatcher->dispatch();
  227. $this->assertEquals(0, $result);
  228. }
  229. /**
  230. * Ensure short plugin shell usage is case/camelized insensitive
  231. *
  232. * @return void
  233. */
  234. public function testDispatchShortPluginAliasCamelized()
  235. {
  236. $dispatcher = $this->getMock(
  237. 'Cake\Console\ShellDispatcher',
  238. ['_shellExists', '_createShell']
  239. );
  240. $Shell = $this->getMock('Cake\Console\Shell');
  241. $dispatcher->expects($this->at(1))
  242. ->method('_shellExists')
  243. ->with('TestPlugin.Example')
  244. ->will($this->returnValue('TestPlugin\Console\Command\TestPluginShell'));
  245. $dispatcher->expects($this->at(2))
  246. ->method('_createShell')
  247. ->with('TestPlugin\Console\Command\TestPluginShell', 'TestPlugin.Example')
  248. ->will($this->returnValue($Shell));
  249. $dispatcher->args = ['Example'];
  250. $result = $dispatcher->dispatch();
  251. $this->assertEquals(0, $result);
  252. }
  253. /**
  254. * Verify that in case of conflict, app shells take precedence in alias list
  255. *
  256. * @return void
  257. */
  258. public function testDispatchShortPluginAliasConflict()
  259. {
  260. $dispatcher = $this->getMock(
  261. 'Cake\Console\ShellDispatcher',
  262. ['_shellExists', '_createShell']
  263. );
  264. $Shell = $this->getMock('Cake\Console\Shell');
  265. $dispatcher->expects($this->at(1))
  266. ->method('_shellExists')
  267. ->with('Sample')
  268. ->will($this->returnValue('App\Shell\SampleShell'));
  269. $dispatcher->expects($this->at(2))
  270. ->method('_createShell')
  271. ->with('App\Shell\SampleShell', 'Sample')
  272. ->will($this->returnValue($Shell));
  273. $dispatcher->args = ['sample'];
  274. $result = $dispatcher->dispatch();
  275. $this->assertEquals(0, $result);
  276. }
  277. /**
  278. * Verify shifting of arguments
  279. *
  280. * @return void
  281. */
  282. public function testShiftArgs()
  283. {
  284. $this->dispatcher->args = ['a', 'b', 'c'];
  285. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  286. $this->assertSame($this->dispatcher->args, ['b', 'c']);
  287. $this->dispatcher->args = ['a' => 'b', 'c', 'd'];
  288. $this->assertEquals('b', $this->dispatcher->shiftArgs());
  289. $this->assertSame($this->dispatcher->args, ['c', 'd']);
  290. $this->dispatcher->args = ['a', 'b' => 'c', 'd'];
  291. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  292. $this->assertSame($this->dispatcher->args, ['b' => 'c', 'd']);
  293. $this->dispatcher->args = [0 => 'a', 2 => 'b', 30 => 'c'];
  294. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  295. $this->assertSame($this->dispatcher->args, [0 => 'b', 1 => 'c']);
  296. $this->dispatcher->args = [];
  297. $this->assertNull($this->dispatcher->shiftArgs());
  298. $this->assertSame([], $this->dispatcher->args);
  299. }
  300. }