ShellDispatcherTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. * TestShellDispatcher class
  23. *
  24. */
  25. class TestShellDispatcher extends ShellDispatcher {
  26. /**
  27. * params property
  28. *
  29. * @var array
  30. */
  31. public $params = array();
  32. /**
  33. * stopped property
  34. *
  35. * @var string
  36. */
  37. public $stopped = null;
  38. /**
  39. * TestShell
  40. *
  41. * @var mixed
  42. */
  43. public $TestShell;
  44. /**
  45. * _initEnvironment method
  46. *
  47. * @return void
  48. */
  49. protected function _initEnvironment() {
  50. }
  51. /**
  52. * clear method
  53. *
  54. * @return void
  55. */
  56. public function clear() {
  57. }
  58. /**
  59. * _stop method
  60. *
  61. * @return void
  62. */
  63. protected function _stop($status = 0) {
  64. $this->stopped = 'Stopped with status: ' . $status;
  65. return $status;
  66. }
  67. /**
  68. * getShell
  69. *
  70. * @param string $shell
  71. * @return mixed
  72. */
  73. public function getShell($shell) {
  74. return $this->_getShell($shell);
  75. }
  76. /**
  77. * _getShell
  78. *
  79. * @param string $shell
  80. * @return mixed
  81. */
  82. protected function _getShell($shell) {
  83. if (isset($this->TestShell)) {
  84. return $this->TestShell;
  85. }
  86. return parent::_getShell($shell);
  87. }
  88. }
  89. /**
  90. * ShellDispatcherTest
  91. *
  92. */
  93. class ShellDispatcherTest extends TestCase {
  94. /**
  95. * setUp method
  96. *
  97. * @return void
  98. */
  99. public function setUp() {
  100. parent::setUp();
  101. Plugin::load('TestPlugin');
  102. }
  103. /**
  104. * Verify loading of (plugin-) shells
  105. *
  106. * @return void
  107. */
  108. public function testGetShell() {
  109. $this->skipIf(class_exists('SampleShell'), 'SampleShell Class already loaded.');
  110. $this->skipIf(class_exists('ExampleShell'), 'ExampleShell Class already loaded.');
  111. Configure::write('App.namespace', 'TestApp');
  112. $Dispatcher = new TestShellDispatcher();
  113. $result = $Dispatcher->getShell('sample');
  114. $this->assertInstanceOf('TestApp\Console\Command\SampleShell', $result);
  115. $Dispatcher = new TestShellDispatcher();
  116. $result = $Dispatcher->getShell('test_plugin.example');
  117. $this->assertInstanceOf('TestPlugin\Console\Command\ExampleShell', $result);
  118. $this->assertEquals('TestPlugin', $result->plugin);
  119. $this->assertEquals('Example', $result->name);
  120. $Dispatcher = new TestShellDispatcher();
  121. $result = $Dispatcher->getShell('TestPlugin.example');
  122. $this->assertInstanceOf('TestPlugin\Console\Command\ExampleShell', $result);
  123. }
  124. /**
  125. * Verify correct dispatch of Shell subclasses with a main method
  126. *
  127. * @return void
  128. */
  129. public function testDispatchShellWithMain() {
  130. $Dispatcher = new TestShellDispatcher();
  131. $Shell = $this->getMock('Cake\Console\Shell');
  132. $Shell->expects($this->once())->method('initialize');
  133. $Shell->expects($this->once())->method('runCommand')
  134. ->with(null, array())
  135. ->will($this->returnValue(true));
  136. $Dispatcher->TestShell = $Shell;
  137. $Dispatcher->args = array('mock_with_main');
  138. $result = $Dispatcher->dispatch();
  139. $this->assertEquals(0, $result);
  140. $this->assertEquals(array(), $Dispatcher->args);
  141. }
  142. /**
  143. * Verify correct dispatch of Shell subclasses without a main method
  144. *
  145. * @return void
  146. */
  147. public function testDispatchShellWithoutMain() {
  148. $Dispatcher = new TestShellDispatcher();
  149. $Shell = $this->getMock('Cake\Console\Shell');
  150. $Shell->expects($this->once())->method('initialize');
  151. $Shell->expects($this->once())->method('runCommand')
  152. ->with('initdb', array('initdb'))
  153. ->will($this->returnValue(true));
  154. $Dispatcher->TestShell = $Shell;
  155. $Dispatcher->args = array('mock_without_main', 'initdb');
  156. $result = $Dispatcher->dispatch();
  157. $this->assertEquals(0, $result);
  158. }
  159. /**
  160. * Verify correct dispatch of custom classes with a main method
  161. *
  162. * @return void
  163. */
  164. public function testDispatchNotAShellWithMain() {
  165. $Dispatcher = new TestShellDispatcher();
  166. $methods = get_class_methods('Cake\Core\Object');
  167. array_push($methods, 'main', 'initdb', 'initialize', 'loadTasks', 'startup', '_secret');
  168. $Shell = $this->getMock('Cake\Core\Object', $methods);
  169. $Shell->expects($this->never())->method('initialize');
  170. $Shell->expects($this->once())->method('startup');
  171. $Shell->expects($this->once())->method('main')->will($this->returnValue(true));
  172. $Dispatcher->TestShell = $Shell;
  173. $Dispatcher->args = array('mock_with_main_not_a');
  174. $result = $Dispatcher->dispatch();
  175. $this->assertEquals(0, $result);
  176. $this->assertEquals(array(), $Dispatcher->args);
  177. $Shell = $this->getMock('Cake\Core\Object', $methods);
  178. $Shell->expects($this->once())->method('initdb')->will($this->returnValue(true));
  179. $Shell->expects($this->once())->method('startup');
  180. $Dispatcher->TestShell = $Shell;
  181. $Dispatcher->args = array('mock_with_main_not_a', 'initdb');
  182. $result = $Dispatcher->dispatch();
  183. $this->assertEquals(0, $result);
  184. }
  185. /**
  186. * Verify correct dispatch of custom classes without a main method
  187. *
  188. * @return void
  189. */
  190. public function testDispatchNotAShellWithoutMain() {
  191. $Dispatcher = new TestShellDispatcher();
  192. $methods = get_class_methods('Cake\Core\Object');
  193. array_push($methods, 'main', 'initdb', 'initialize', 'loadTasks', 'startup', '_secret');
  194. $Shell = $this->getMock('Cake\Core\Object', $methods);
  195. $Shell->expects($this->never())->method('initialize');
  196. $Shell->expects($this->once())->method('startup');
  197. $Shell->expects($this->once())->method('main')->will($this->returnValue(true));
  198. $Dispatcher->TestShell = $Shell;
  199. $Dispatcher->args = array('mock_without_main_not_a');
  200. $result = $Dispatcher->dispatch();
  201. $this->assertEquals(0, $result);
  202. $this->assertEquals(array(), $Dispatcher->args);
  203. $Shell = $this->getMock('Cake\Core\Object', $methods);
  204. $Shell->expects($this->once())->method('initdb')->will($this->returnValue(true));
  205. $Shell->expects($this->once())->method('startup');
  206. $Dispatcher->TestShell = $Shell;
  207. $Dispatcher->args = array('mock_without_main_not_a', 'initdb');
  208. $result = $Dispatcher->dispatch();
  209. $this->assertEquals(0, $result);
  210. }
  211. /**
  212. * Verify shifting of arguments
  213. *
  214. * @return void
  215. */
  216. public function testShiftArgs() {
  217. $Dispatcher = new TestShellDispatcher();
  218. $Dispatcher->args = array('a', 'b', 'c');
  219. $this->assertEquals('a', $Dispatcher->shiftArgs());
  220. $this->assertSame($Dispatcher->args, array('b', 'c'));
  221. $Dispatcher->args = array('a' => 'b', 'c', 'd');
  222. $this->assertEquals('b', $Dispatcher->shiftArgs());
  223. $this->assertSame($Dispatcher->args, array('c', 'd'));
  224. $Dispatcher->args = array('a', 'b' => 'c', 'd');
  225. $this->assertEquals('a', $Dispatcher->shiftArgs());
  226. $this->assertSame($Dispatcher->args, array('b' => 'c', 'd'));
  227. $Dispatcher->args = array(0 => 'a', 2 => 'b', 30 => 'c');
  228. $this->assertEquals('a', $Dispatcher->shiftArgs());
  229. $this->assertSame($Dispatcher->args, array(0 => 'b', 1 => 'c'));
  230. $Dispatcher->args = array();
  231. $this->assertNull($Dispatcher->shiftArgs());
  232. $this->assertSame(array(), $Dispatcher->args);
  233. }
  234. }