ShellDispatcherTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. * Test getting shells with aliases.
  83. *
  84. * @return void
  85. */
  86. public function testFindShellAliased() {
  87. ShellDispatcher::alias('short', 'test_plugin.example');
  88. $result = $this->dispatcher->findShell('short');
  89. $this->assertInstanceOf('TestPlugin\Shell\ExampleShell', $result);
  90. $this->assertEquals('TestPlugin', $result->plugin);
  91. $this->assertEquals('Example', $result->name);
  92. }
  93. /**
  94. * Test finding a shell that has a matching alias.
  95. *
  96. * Aliases should not overload concrete shells.
  97. *
  98. * @return void
  99. */
  100. public function testFindShellAliasedAppShadow() {
  101. ShellDispatcher::alias('sample', 'test_plugin.example');
  102. $result = $this->dispatcher->findShell('sample');
  103. $this->assertInstanceOf('TestApp\Shell\SampleShell', $result);
  104. $this->assertEmpty($result->plugin);
  105. $this->assertEquals('Sample', $result->name);
  106. }
  107. /**
  108. * Verify correct dispatch of Shell subclasses with a main method
  109. *
  110. * @return void
  111. */
  112. public function testDispatchShellWithMain() {
  113. $dispatcher = $this->getMock('Cake\Console\ShellDispatcher', ['findShell']);
  114. $Shell = $this->getMock('Cake\Console\Shell');
  115. $Shell->expects($this->once())->method('initialize');
  116. $Shell->expects($this->once())->method('runCommand')
  117. ->with([])
  118. ->will($this->returnValue(true));
  119. $dispatcher->expects($this->any())
  120. ->method('findShell')
  121. ->with('mock_with_main')
  122. ->will($this->returnValue($Shell));
  123. $dispatcher->args = array('mock_with_main');
  124. $result = $dispatcher->dispatch();
  125. $this->assertEquals(0, $result);
  126. $this->assertEquals(array(), $dispatcher->args);
  127. }
  128. /**
  129. * Verify correct dispatch of Shell subclasses without a main method
  130. *
  131. * @return void
  132. */
  133. public function testDispatchShellWithoutMain() {
  134. $dispatcher = $this->getMock('Cake\Console\ShellDispatcher', ['findShell']);
  135. $Shell = $this->getMock('Cake\Console\Shell');
  136. $Shell->expects($this->once())->method('initialize');
  137. $Shell->expects($this->once())->method('runCommand')
  138. ->with(['initdb'])
  139. ->will($this->returnValue(true));
  140. $dispatcher->expects($this->any())
  141. ->method('findShell')
  142. ->with('mock_without_main')
  143. ->will($this->returnValue($Shell));
  144. $dispatcher->args = array('mock_without_main', 'initdb');
  145. $result = $dispatcher->dispatch();
  146. $this->assertEquals(0, $result);
  147. }
  148. /**
  149. * Verify you can dispatch a plugin's main shell with the shell name alone
  150. *
  151. * @return void
  152. */
  153. public function testDispatchShortPluginAlias() {
  154. $dispatcher = $this->getMock(
  155. 'Cake\Console\ShellDispatcher',
  156. ['_shellExists', '_createShell']
  157. );
  158. $Shell = $this->getMock('Cake\Console\Shell');
  159. $dispatcher->expects($this->at(1))
  160. ->method('_shellExists')
  161. ->with('TestPlugin.Example')
  162. ->will($this->returnValue('TestPlugin\Console\Command\TestPluginShell'));
  163. $dispatcher->expects($this->at(2))
  164. ->method('_createShell')
  165. ->with('TestPlugin\Console\Command\TestPluginShell', 'TestPlugin.Example')
  166. ->will($this->returnValue($Shell));
  167. $dispatcher->args = array('example');
  168. $result = $dispatcher->dispatch();
  169. $this->assertEquals(1, $result);
  170. }
  171. /**
  172. * Ensure short plugin shell usage is case/camelized insensitive
  173. *
  174. * @return void
  175. */
  176. public function testDispatchShortPluginAliasCamelized() {
  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 = ['Example'];
  191. $result = $dispatcher->dispatch();
  192. $this->assertEquals(1, $result);
  193. }
  194. /**
  195. * Verify that in case of conflict, app shells take precedence in alias list
  196. *
  197. * @return void
  198. */
  199. public function testDispatchShortPluginAliasConflict() {
  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('Sample')
  208. ->will($this->returnValue('App\Shell\SampleShell'));
  209. $dispatcher->expects($this->at(2))
  210. ->method('_createShell')
  211. ->with('App\Shell\SampleShell', 'Sample')
  212. ->will($this->returnValue($Shell));
  213. $dispatcher->args = array('sample');
  214. $result = $dispatcher->dispatch();
  215. $this->assertEquals(1, $result);
  216. }
  217. /**
  218. * Verify shifting of arguments
  219. *
  220. * @return void
  221. */
  222. public function testShiftArgs() {
  223. $this->dispatcher->args = array('a', 'b', 'c');
  224. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  225. $this->assertSame($this->dispatcher->args, array('b', 'c'));
  226. $this->dispatcher->args = array('a' => 'b', 'c', 'd');
  227. $this->assertEquals('b', $this->dispatcher->shiftArgs());
  228. $this->assertSame($this->dispatcher->args, array('c', 'd'));
  229. $this->dispatcher->args = array('a', 'b' => 'c', 'd');
  230. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  231. $this->assertSame($this->dispatcher->args, array('b' => 'c', 'd'));
  232. $this->dispatcher->args = array(0 => 'a', 2 => 'b', 30 => 'c');
  233. $this->assertEquals('a', $this->dispatcher->shiftArgs());
  234. $this->assertSame($this->dispatcher->args, array(0 => 'b', 1 => 'c'));
  235. $this->dispatcher->args = array();
  236. $this->assertNull($this->dispatcher->shiftArgs());
  237. $this->assertSame(array(), $this->dispatcher->args);
  238. }
  239. }