ShellDispatcherTest.php 8.6 KB

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