ShellDispatcherTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 correct dispatch of Shell subclasses with a main method
  136. *
  137. * @return void
  138. */
  139. public function testDispatchShellWithMain()
  140. {
  141. $dispatcher = $this->getMock('Cake\Console\ShellDispatcher', ['findShell']);
  142. $Shell = $this->getMock('Cake\Console\Shell');
  143. $Shell->expects($this->exactly(2))->method('initialize');
  144. $Shell->expects($this->at(0))->method('runCommand')
  145. ->will($this->returnValue(true));
  146. $Shell->expects($this->at(1))->method('runCommand')
  147. ->will($this->returnValue(null));
  148. $dispatcher->expects($this->any())
  149. ->method('findShell')
  150. ->with('mock_with_main')
  151. ->will($this->returnValue($Shell));
  152. $dispatcher->args = ['mock_with_main'];
  153. $result = $dispatcher->dispatch();
  154. $this->assertSame(0, $result);
  155. $this->assertEquals([], $dispatcher->args);
  156. $dispatcher->args = ['mock_with_main'];
  157. $result = $dispatcher->dispatch();
  158. $this->assertSame(0, $result);
  159. $this->assertEquals([], $dispatcher->args);
  160. }
  161. /**
  162. * Verify correct dispatch of Shell subclasses without a main method
  163. *
  164. * @return void
  165. */
  166. public function testDispatchShellWithoutMain()
  167. {
  168. $dispatcher = $this->getMock('Cake\Console\ShellDispatcher', ['findShell']);
  169. $Shell = $this->getMock('Cake\Console\Shell');
  170. $Shell->expects($this->once())->method('initialize');
  171. $Shell->expects($this->once())->method('runCommand')
  172. ->with(['initdb'])
  173. ->will($this->returnValue(true));
  174. $dispatcher->expects($this->any())
  175. ->method('findShell')
  176. ->with('mock_without_main')
  177. ->will($this->returnValue($Shell));
  178. $dispatcher->args = ['mock_without_main', 'initdb'];
  179. $result = $dispatcher->dispatch();
  180. $this->assertEquals(0, $result);
  181. }
  182. /**
  183. * Verify you can dispatch a plugin's main shell with the shell name alone
  184. *
  185. * @return void
  186. */
  187. public function testDispatchShortPluginAlias()
  188. {
  189. $dispatcher = $this->getMock(
  190. 'Cake\Console\ShellDispatcher',
  191. ['_shellExists', '_createShell']
  192. );
  193. $Shell = $this->getMock('Cake\Console\Shell');
  194. $dispatcher->expects($this->at(1))
  195. ->method('_shellExists')
  196. ->with('TestPlugin.Example')
  197. ->will($this->returnValue('TestPlugin\Console\Command\TestPluginShell'));
  198. $dispatcher->expects($this->at(2))
  199. ->method('_createShell')
  200. ->with('TestPlugin\Console\Command\TestPluginShell', 'TestPlugin.Example')
  201. ->will($this->returnValue($Shell));
  202. $dispatcher->args = ['example'];
  203. $result = $dispatcher->dispatch();
  204. $this->assertEquals(0, $result);
  205. }
  206. /**
  207. * Ensure short plugin shell usage is case/camelized insensitive
  208. *
  209. * @return void
  210. */
  211. public function testDispatchShortPluginAliasCamelized()
  212. {
  213. $dispatcher = $this->getMock(
  214. 'Cake\Console\ShellDispatcher',
  215. ['_shellExists', '_createShell']
  216. );
  217. $Shell = $this->getMock('Cake\Console\Shell');
  218. $dispatcher->expects($this->at(1))
  219. ->method('_shellExists')
  220. ->with('TestPlugin.Example')
  221. ->will($this->returnValue('TestPlugin\Console\Command\TestPluginShell'));
  222. $dispatcher->expects($this->at(2))
  223. ->method('_createShell')
  224. ->with('TestPlugin\Console\Command\TestPluginShell', 'TestPlugin.Example')
  225. ->will($this->returnValue($Shell));
  226. $dispatcher->args = ['Example'];
  227. $result = $dispatcher->dispatch();
  228. $this->assertEquals(0, $result);
  229. }
  230. /**
  231. * Verify that in case of conflict, app shells take precedence in alias list
  232. *
  233. * @return void
  234. */
  235. public function testDispatchShortPluginAliasConflict()
  236. {
  237. $dispatcher = $this->getMock(
  238. 'Cake\Console\ShellDispatcher',
  239. ['_shellExists', '_createShell']
  240. );
  241. $Shell = $this->getMock('Cake\Console\Shell');
  242. $dispatcher->expects($this->at(1))
  243. ->method('_shellExists')
  244. ->with('Sample')
  245. ->will($this->returnValue('App\Shell\SampleShell'));
  246. $dispatcher->expects($this->at(2))
  247. ->method('_createShell')
  248. ->with('App\Shell\SampleShell', 'Sample')
  249. ->will($this->returnValue($Shell));
  250. $dispatcher->args = ['sample'];
  251. $result = $dispatcher->dispatch();
  252. $this->assertEquals(0, $result);
  253. }
  254. /**
  255. * Verify shifting of arguments
  256. *
  257. * @return void
  258. */
  259. public function testShiftArgs()
  260. {
  261. $this->dispatcher->args = ['a', 'b', 'c'];
  262. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  263. $this->assertSame($this->dispatcher->args, ['b', 'c']);
  264. $this->dispatcher->args = ['a' => 'b', 'c', 'd'];
  265. $this->assertEquals('b', $this->dispatcher->shiftArgs());
  266. $this->assertSame($this->dispatcher->args, ['c', 'd']);
  267. $this->dispatcher->args = ['a', 'b' => 'c', 'd'];
  268. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  269. $this->assertSame($this->dispatcher->args, ['b' => 'c', 'd']);
  270. $this->dispatcher->args = [0 => 'a', 2 => 'b', 30 => 'c'];
  271. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  272. $this->assertSame($this->dispatcher->args, [0 => 'b', 1 => 'c']);
  273. $this->dispatcher->args = [];
  274. $this->assertNull($this->dispatcher->shiftArgs());
  275. $this->assertSame([], $this->dispatcher->args);
  276. }
  277. }