ShellDispatcherTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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->getMock('Cake\Console\ShellDispatcher', ['_stop']);
  37. }
  38. /**
  39. * teardown
  40. *
  41. * @return void
  42. */
  43. public function tearDown()
  44. {
  45. parent::tearDown();
  46. ShellDispatcher::resetAliases();
  47. }
  48. /**
  49. * Test error on missing shell
  50. *
  51. * @expectedException \Cake\Console\Exception\MissingShellException
  52. * @return void
  53. */
  54. public function testFindShellMissing()
  55. {
  56. $this->dispatcher->findShell('nope');
  57. }
  58. /**
  59. * Test error on missing plugin shell
  60. *
  61. * @expectedException \Cake\Console\Exception\MissingShellException
  62. * @return void
  63. */
  64. public function testFindShellMissingPlugin()
  65. {
  66. $this->dispatcher->findShell('test_plugin.nope');
  67. }
  68. /**
  69. * Verify loading of (plugin-) shells
  70. *
  71. * @return void
  72. */
  73. public function testFindShell()
  74. {
  75. $result = $this->dispatcher->findShell('sample');
  76. $this->assertInstanceOf('TestApp\Shell\SampleShell', $result);
  77. $result = $this->dispatcher->findShell('test_plugin.example');
  78. $this->assertInstanceOf('TestPlugin\Shell\ExampleShell', $result);
  79. $this->assertEquals('TestPlugin', $result->plugin);
  80. $this->assertEquals('Example', $result->name);
  81. $result = $this->dispatcher->findShell('TestPlugin.example');
  82. $this->assertInstanceOf('TestPlugin\Shell\ExampleShell', $result);
  83. $result = $this->dispatcher->findShell('TestPlugin.Example');
  84. $this->assertInstanceOf('TestPlugin\Shell\ExampleShell', $result);
  85. }
  86. /**
  87. * testAddShortPluginAlias
  88. *
  89. * @return void
  90. */
  91. public function testAddShortPluginAlias()
  92. {
  93. $expected = [
  94. 'Example' => 'TestPlugin.example'
  95. ];
  96. $result = $this->dispatcher->addShortPluginAliases();
  97. $this->assertSame($expected, $result, 'Should return the list of aliased plugin shells');
  98. ShellDispatcher::alias('Example', 'SomeOther.PluginsShell');
  99. $expected = [
  100. 'Example' => 'SomeOther.PluginsShell'
  101. ];
  102. $result = $this->dispatcher->addShortPluginAliases();
  103. $this->assertSame($expected, $result, 'Should not overwrite existing aliases');
  104. }
  105. /**
  106. * Test getting shells with aliases.
  107. *
  108. * @return void
  109. */
  110. public function testFindShellAliased()
  111. {
  112. ShellDispatcher::alias('short', 'test_plugin.example');
  113. $result = $this->dispatcher->findShell('short');
  114. $this->assertInstanceOf('TestPlugin\Shell\ExampleShell', $result);
  115. $this->assertEquals('TestPlugin', $result->plugin);
  116. $this->assertEquals('Example', $result->name);
  117. }
  118. /**
  119. * Test finding a shell that has a matching alias.
  120. *
  121. * Aliases should not overload concrete shells.
  122. *
  123. * @return void
  124. */
  125. public function testFindShellAliasedAppShadow()
  126. {
  127. ShellDispatcher::alias('sample', 'test_plugin.example');
  128. $result = $this->dispatcher->findShell('sample');
  129. $this->assertInstanceOf('TestApp\Shell\SampleShell', $result);
  130. $this->assertEmpty($result->plugin);
  131. $this->assertEquals('Sample', $result->name);
  132. }
  133. /**
  134. * Verify dispatch handling stop errors
  135. *
  136. * @return void
  137. */
  138. public function testDispatchShellWithAbort()
  139. {
  140. $io = $this->getMock('Cake\Console\ConsoleIo');
  141. $shell = $this->getMock('Cake\Console\Shell', ['main'], [$io]);
  142. $shell->expects($this->once())
  143. ->method('main')
  144. ->will($this->returnCallback(function () use ($shell) {
  145. $shell->abort('Bad things', 99);
  146. }));
  147. $dispatcher = $this->getMock('Cake\Console\ShellDispatcher', ['findShell']);
  148. $dispatcher->expects($this->any())
  149. ->method('findShell')
  150. ->with('aborter')
  151. ->will($this->returnValue($shell));
  152. $dispatcher->args = ['aborter'];
  153. $result = $dispatcher->dispatch();
  154. $this->assertSame(99, $result, 'Should return the exception error code.');
  155. }
  156. /**
  157. * Verify correct dispatch of Shell subclasses with a main method
  158. *
  159. * @return void
  160. */
  161. public function testDispatchShellWithMain()
  162. {
  163. $dispatcher = $this->getMock('Cake\Console\ShellDispatcher', ['findShell']);
  164. $Shell = $this->getMock('Cake\Console\Shell');
  165. $Shell->expects($this->exactly(2))->method('initialize');
  166. $Shell->expects($this->at(0))->method('runCommand')
  167. ->will($this->returnValue(true));
  168. $Shell->expects($this->at(1))->method('runCommand')
  169. ->will($this->returnValue(null));
  170. $dispatcher->expects($this->any())
  171. ->method('findShell')
  172. ->with('mock_with_main')
  173. ->will($this->returnValue($Shell));
  174. $dispatcher->args = ['mock_with_main'];
  175. $result = $dispatcher->dispatch();
  176. $this->assertSame(0, $result);
  177. $this->assertEquals([], $dispatcher->args);
  178. $dispatcher->args = ['mock_with_main'];
  179. $result = $dispatcher->dispatch();
  180. $this->assertSame(0, $result);
  181. $this->assertEquals([], $dispatcher->args);
  182. }
  183. /**
  184. * Verify correct dispatch of Shell subclasses without a main method
  185. *
  186. * @return void
  187. */
  188. public function testDispatchShellWithoutMain()
  189. {
  190. $dispatcher = $this->getMock('Cake\Console\ShellDispatcher', ['findShell']);
  191. $Shell = $this->getMock('Cake\Console\Shell');
  192. $Shell->expects($this->once())->method('initialize');
  193. $Shell->expects($this->once())->method('runCommand')
  194. ->with(['initdb'])
  195. ->will($this->returnValue(true));
  196. $dispatcher->expects($this->any())
  197. ->method('findShell')
  198. ->with('mock_without_main')
  199. ->will($this->returnValue($Shell));
  200. $dispatcher->args = ['mock_without_main', 'initdb'];
  201. $result = $dispatcher->dispatch();
  202. $this->assertEquals(0, $result);
  203. }
  204. /**
  205. * Verify you can dispatch a plugin's main shell with the shell name alone
  206. *
  207. * @return void
  208. */
  209. public function testDispatchShortPluginAlias()
  210. {
  211. $dispatcher = $this->getMock(
  212. 'Cake\Console\ShellDispatcher',
  213. ['_shellExists', '_createShell']
  214. );
  215. $Shell = $this->getMock('Cake\Console\Shell');
  216. $dispatcher->expects($this->at(1))
  217. ->method('_shellExists')
  218. ->with('TestPlugin.Example')
  219. ->will($this->returnValue('TestPlugin\Console\Command\TestPluginShell'));
  220. $dispatcher->expects($this->at(2))
  221. ->method('_createShell')
  222. ->with('TestPlugin\Console\Command\TestPluginShell', 'TestPlugin.Example')
  223. ->will($this->returnValue($Shell));
  224. $dispatcher->args = ['example'];
  225. $result = $dispatcher->dispatch();
  226. $this->assertEquals(0, $result);
  227. }
  228. /**
  229. * Ensure short plugin shell usage is case/camelized insensitive
  230. *
  231. * @return void
  232. */
  233. public function testDispatchShortPluginAliasCamelized()
  234. {
  235. $dispatcher = $this->getMock(
  236. 'Cake\Console\ShellDispatcher',
  237. ['_shellExists', '_createShell']
  238. );
  239. $Shell = $this->getMock('Cake\Console\Shell');
  240. $dispatcher->expects($this->at(1))
  241. ->method('_shellExists')
  242. ->with('TestPlugin.Example')
  243. ->will($this->returnValue('TestPlugin\Console\Command\TestPluginShell'));
  244. $dispatcher->expects($this->at(2))
  245. ->method('_createShell')
  246. ->with('TestPlugin\Console\Command\TestPluginShell', 'TestPlugin.Example')
  247. ->will($this->returnValue($Shell));
  248. $dispatcher->args = ['Example'];
  249. $result = $dispatcher->dispatch();
  250. $this->assertEquals(0, $result);
  251. }
  252. /**
  253. * Verify that in case of conflict, app shells take precedence in alias list
  254. *
  255. * @return void
  256. */
  257. public function testDispatchShortPluginAliasConflict()
  258. {
  259. $dispatcher = $this->getMock(
  260. 'Cake\Console\ShellDispatcher',
  261. ['_shellExists', '_createShell']
  262. );
  263. $Shell = $this->getMock('Cake\Console\Shell');
  264. $dispatcher->expects($this->at(1))
  265. ->method('_shellExists')
  266. ->with('Sample')
  267. ->will($this->returnValue('App\Shell\SampleShell'));
  268. $dispatcher->expects($this->at(2))
  269. ->method('_createShell')
  270. ->with('App\Shell\SampleShell', 'Sample')
  271. ->will($this->returnValue($Shell));
  272. $dispatcher->args = ['sample'];
  273. $result = $dispatcher->dispatch();
  274. $this->assertEquals(0, $result);
  275. }
  276. /**
  277. * Verify shifting of arguments
  278. *
  279. * @return void
  280. */
  281. public function testShiftArgs()
  282. {
  283. $this->dispatcher->args = ['a', 'b', 'c'];
  284. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  285. $this->assertSame($this->dispatcher->args, ['b', 'c']);
  286. $this->dispatcher->args = ['a' => 'b', 'c', 'd'];
  287. $this->assertEquals('b', $this->dispatcher->shiftArgs());
  288. $this->assertSame($this->dispatcher->args, ['c', 'd']);
  289. $this->dispatcher->args = ['a', 'b' => 'c', 'd'];
  290. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  291. $this->assertSame($this->dispatcher->args, ['b' => 'c', 'd']);
  292. $this->dispatcher->args = [0 => 'a', 2 => 'b', 30 => 'c'];
  293. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  294. $this->assertSame($this->dispatcher->args, [0 => 'b', 1 => 'c']);
  295. $this->dispatcher->args = [];
  296. $this->assertNull($this->dispatcher->shiftArgs());
  297. $this->assertSame([], $this->dispatcher->args);
  298. }
  299. }