ShellDispatcherTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <?php
  2. /**
  3. * ShellDispatcherTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @since CakePHP(tm) v 1.2.0.5432
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Console;
  18. use Cake\Console\ShellDispatcher;
  19. use Cake\Core\App;
  20. use Cake\Core\Configure;
  21. use Cake\Core\Plugin;
  22. use Cake\TestSuite\TestCase;
  23. /**
  24. * TestShellDispatcher class
  25. *
  26. */
  27. class TestShellDispatcher extends ShellDispatcher {
  28. /**
  29. * params property
  30. *
  31. * @var array
  32. */
  33. public $params = array();
  34. /**
  35. * stopped property
  36. *
  37. * @var string
  38. */
  39. public $stopped = null;
  40. /**
  41. * TestShell
  42. *
  43. * @var mixed
  44. */
  45. public $TestShell;
  46. /**
  47. * _initEnvironment method
  48. *
  49. * @return void
  50. */
  51. protected function _initEnvironment() {
  52. }
  53. /**
  54. * clear method
  55. *
  56. * @return void
  57. */
  58. public function clear() {
  59. }
  60. /**
  61. * _stop method
  62. *
  63. * @return void
  64. */
  65. protected function _stop($status = 0) {
  66. $this->stopped = 'Stopped with status: ' . $status;
  67. return $status;
  68. }
  69. /**
  70. * getShell
  71. *
  72. * @param string $shell
  73. * @return mixed
  74. */
  75. public function getShell($shell) {
  76. return $this->_getShell($shell);
  77. }
  78. /**
  79. * _getShell
  80. *
  81. * @param string $shell
  82. * @return mixed
  83. */
  84. protected function _getShell($shell) {
  85. if (isset($this->TestShell)) {
  86. return $this->TestShell;
  87. }
  88. return parent::_getShell($shell);
  89. }
  90. }
  91. /**
  92. * ShellDispatcherTest
  93. *
  94. */
  95. class ShellDispatcherTest extends TestCase {
  96. /**
  97. * setUp method
  98. *
  99. * @return void
  100. */
  101. public function setUp() {
  102. parent::setUp();
  103. Plugin::load('TestPlugin');
  104. }
  105. /**
  106. * testParseParams method
  107. *
  108. * @return void
  109. */
  110. public function testParseParams() {
  111. $Dispatcher = new TestShellDispatcher();
  112. $params = array(
  113. '/cake/1.2.x.x/cake/console/cake.php',
  114. 'bake',
  115. '-app',
  116. 'new',
  117. '-working',
  118. '/var/www/htdocs'
  119. );
  120. $expected = array(
  121. 'app' => 'new',
  122. 'webroot' => 'webroot',
  123. 'working' => str_replace('/', DS, '/var/www/htdocs/new'),
  124. 'root' => str_replace('/', DS, '/var/www/htdocs')
  125. );
  126. $Dispatcher->parseParams($params);
  127. $this->assertEquals($expected, $Dispatcher->params);
  128. $params = array('cake.php');
  129. $expected = array(
  130. 'app' => 'App',
  131. 'webroot' => 'webroot',
  132. 'working' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH) . DS . 'App'),
  133. 'root' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH)),
  134. );
  135. $Dispatcher->params = $Dispatcher->args = array();
  136. $Dispatcher->parseParams($params);
  137. $this->assertEquals($expected, $Dispatcher->params);
  138. $params = array(
  139. 'cake.php',
  140. '-app',
  141. 'new',
  142. );
  143. $expected = array(
  144. 'app' => 'new',
  145. 'webroot' => 'webroot',
  146. 'working' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH) . DS . 'new'),
  147. 'root' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH))
  148. );
  149. $Dispatcher->params = $Dispatcher->args = array();
  150. $Dispatcher->parseParams($params);
  151. $this->assertEquals($expected, $Dispatcher->params);
  152. $params = array(
  153. './cake.php',
  154. 'bake',
  155. '-app',
  156. 'new',
  157. '-working',
  158. '/cake/1.2.x.x/cake/console'
  159. );
  160. $expected = array(
  161. 'app' => 'new',
  162. 'webroot' => 'webroot',
  163. 'working' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH) . DS . 'new'),
  164. 'root' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH))
  165. );
  166. $Dispatcher->params = $Dispatcher->args = array();
  167. $Dispatcher->parseParams($params);
  168. $this->assertEquals($expected, $Dispatcher->params);
  169. $params = array(
  170. './console/cake.php',
  171. 'bake',
  172. '-app',
  173. 'new',
  174. '-working',
  175. '/cake/1.2.x.x/cake'
  176. );
  177. $expected = array(
  178. 'app' => 'new',
  179. 'webroot' => 'webroot',
  180. 'working' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH) . DS . 'new'),
  181. 'root' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH))
  182. );
  183. $Dispatcher->params = $Dispatcher->args = array();
  184. $Dispatcher->parseParams($params);
  185. $this->assertEquals($expected, $Dispatcher->params);
  186. $params = array(
  187. './console/cake.php',
  188. 'bake',
  189. '-app',
  190. 'new',
  191. '-dry',
  192. '-working',
  193. '/cake/1.2.x.x/cake'
  194. );
  195. $expected = array(
  196. 'app' => 'new',
  197. 'working' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH) . DS . 'new'),
  198. 'root' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH)),
  199. 'webroot' => 'webroot'
  200. );
  201. $Dispatcher->params = $Dispatcher->args = array();
  202. $Dispatcher->parseParams($params);
  203. $this->assertEquals($expected, $Dispatcher->params);
  204. $params = array(
  205. './console/cake.php',
  206. '-working',
  207. '/cake/1.2.x.x/cake',
  208. 'schema',
  209. 'run',
  210. 'create',
  211. '-dry',
  212. '-f',
  213. '-name',
  214. 'DbAcl'
  215. );
  216. $expected = array(
  217. 'app' => 'App',
  218. 'webroot' => 'webroot',
  219. 'working' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH) . DS . 'App'),
  220. 'root' => str_replace('\\', DS, dirname(CAKE_CORE_INCLUDE_PATH)),
  221. );
  222. $Dispatcher->params = $Dispatcher->args = array();
  223. $Dispatcher->parseParams($params);
  224. $this->assertEquals($expected, $Dispatcher->params);
  225. $expected = array(
  226. './console/cake.php', 'schema', 'run', 'create', '-dry', '-f', '-name', 'DbAcl'
  227. );
  228. $this->assertEquals($expected, $Dispatcher->args);
  229. $params = array(
  230. '/cake/1.2.x.x/cake/console/cake.php',
  231. '-working',
  232. '/cake/1.2.x.x/app',
  233. 'schema',
  234. 'run',
  235. 'create',
  236. '-dry',
  237. '-name',
  238. 'DbAcl'
  239. );
  240. $expected = array(
  241. 'app' => 'app',
  242. 'webroot' => 'webroot',
  243. 'working' => str_replace('/', DS, '/cake/1.2.x.x/app'),
  244. 'root' => str_replace('/', DS, '/cake/1.2.x.x'),
  245. );
  246. $Dispatcher->params = $Dispatcher->args = array();
  247. $Dispatcher->parseParams($params);
  248. $this->assertEquals($expected, $Dispatcher->params);
  249. $params = array(
  250. 'cake.php',
  251. '-working',
  252. 'C:/wamp/www/cake/app',
  253. 'bake',
  254. '-app',
  255. 'C:/wamp/www/apps/cake/app',
  256. );
  257. $expected = array(
  258. 'app' => 'app',
  259. 'webroot' => 'webroot',
  260. 'working' => 'C:\wamp\www\apps\cake\app',
  261. 'root' => 'C:\wamp\www\apps\cake'
  262. );
  263. $Dispatcher->params = $Dispatcher->args = array();
  264. $Dispatcher->parseParams($params);
  265. $this->assertEquals($expected, $Dispatcher->params);
  266. $params = array(
  267. 'cake.php',
  268. '-working',
  269. 'C:\wamp\www\cake\app',
  270. 'bake',
  271. '-app',
  272. 'C:\wamp\www\apps\cake\app',
  273. );
  274. $expected = array(
  275. 'app' => 'app',
  276. 'webroot' => 'webroot',
  277. 'working' => 'C:\wamp\www\apps\cake\app',
  278. 'root' => 'C:\wamp\www\apps\cake'
  279. );
  280. $Dispatcher->params = $Dispatcher->args = array();
  281. $Dispatcher->parseParams($params);
  282. $this->assertEquals($expected, $Dispatcher->params);
  283. $params = array(
  284. 'cake.php',
  285. '-working',
  286. 'C:\wamp\www\apps',
  287. 'bake',
  288. '-app',
  289. 'cake\app',
  290. '-url',
  291. 'http://example.com/some/url/with/a/path'
  292. );
  293. $expected = array(
  294. 'app' => 'app',
  295. 'webroot' => 'webroot',
  296. 'working' => 'C:\wamp\www\apps\cake\app',
  297. 'root' => 'C:\wamp\www\apps\cake',
  298. );
  299. $Dispatcher->params = $Dispatcher->args = array();
  300. $Dispatcher->parseParams($params);
  301. $this->assertEquals($expected, $Dispatcher->params);
  302. $params = array(
  303. '/home/amelo/dev/cake-common/cake/console/cake.php',
  304. '-root',
  305. '/home/amelo/dev/lsbu-vacancy',
  306. '-working',
  307. '/home/amelo/dev/lsbu-vacancy',
  308. '-app',
  309. 'app',
  310. );
  311. $expected = array(
  312. 'app' => 'app',
  313. 'webroot' => 'webroot',
  314. 'working' => '/home/amelo/dev/lsbu-vacancy/app',
  315. 'root' => '/home/amelo/dev/lsbu-vacancy',
  316. );
  317. $Dispatcher->params = $Dispatcher->args = array();
  318. $Dispatcher->parseParams($params);
  319. $this->assertEquals($expected, $Dispatcher->params);
  320. $params = array(
  321. '/cake/1.2.x.x/cake/console/cake.php',
  322. 'bake',
  323. '-app',
  324. 'new',
  325. '-app',
  326. 'old',
  327. '-working',
  328. '/var/www/htdocs'
  329. );
  330. $expected = array(
  331. 'app' => 'old',
  332. 'webroot' => 'webroot',
  333. 'working' => str_replace('/', DS, '/var/www/htdocs/old'),
  334. 'root' => str_replace('/', DS, '/var/www/htdocs')
  335. );
  336. $Dispatcher->parseParams($params);
  337. $this->assertEquals($expected, $Dispatcher->params);
  338. if (DS === '\\') {
  339. $params = array(
  340. 'cake.php',
  341. '-working',
  342. 'D:\www',
  343. 'bake',
  344. 'my_app',
  345. );
  346. $expected = array(
  347. 'working' => 'D:\\\\www',
  348. 'app' => 'www',
  349. 'root' => 'D:\\',
  350. 'webroot' => 'webroot'
  351. );
  352. $Dispatcher->params = $Dispatcher->args = array();
  353. $Dispatcher->parseParams($params);
  354. $this->assertEquals($expected, $Dispatcher->params);
  355. }
  356. }
  357. /**
  358. * Verify loading of (plugin-) shells
  359. *
  360. * @return void
  361. */
  362. public function testGetShell() {
  363. $this->skipIf(class_exists('SampleShell'), 'SampleShell Class already loaded.');
  364. $this->skipIf(class_exists('ExampleShell'), 'ExampleShell Class already loaded.');
  365. Configure::write('App.namespace', 'TestApp');
  366. $Dispatcher = new TestShellDispatcher();
  367. $result = $Dispatcher->getShell('sample');
  368. $this->assertInstanceOf('TestApp\Console\Command\SampleShell', $result);
  369. $Dispatcher = new TestShellDispatcher();
  370. $result = $Dispatcher->getShell('test_plugin.example');
  371. $this->assertInstanceOf('TestPlugin\Console\Command\ExampleShell', $result);
  372. $this->assertEquals('TestPlugin', $result->plugin);
  373. $this->assertEquals('Example', $result->name);
  374. $Dispatcher = new TestShellDispatcher();
  375. $result = $Dispatcher->getShell('TestPlugin.example');
  376. $this->assertInstanceOf('TestPlugin\Console\Command\ExampleShell', $result);
  377. }
  378. /**
  379. * Verify correct dispatch of Shell subclasses with a main method
  380. *
  381. * @return void
  382. */
  383. public function testDispatchShellWithMain() {
  384. $Dispatcher = new TestShellDispatcher();
  385. $Shell = $this->getMock('Cake\Console\Shell');
  386. $Shell->expects($this->once())->method('initialize');
  387. $Shell->expects($this->once())->method('runCommand')
  388. ->with(null, array())
  389. ->will($this->returnValue(true));
  390. $Dispatcher->TestShell = $Shell;
  391. $Dispatcher->args = array('mock_with_main');
  392. $result = $Dispatcher->dispatch();
  393. $this->assertEquals(0, $result);
  394. $this->assertEquals(array(), $Dispatcher->args);
  395. }
  396. /**
  397. * Verify correct dispatch of Shell subclasses without a main method
  398. *
  399. * @return void
  400. */
  401. public function testDispatchShellWithoutMain() {
  402. $Dispatcher = new TestShellDispatcher();
  403. $Shell = $this->getMock('Cake\Console\Shell');
  404. $Shell->expects($this->once())->method('initialize');
  405. $Shell->expects($this->once())->method('runCommand')
  406. ->with('initdb', array('initdb'))
  407. ->will($this->returnValue(true));
  408. $Dispatcher->TestShell = $Shell;
  409. $Dispatcher->args = array('mock_without_main', 'initdb');
  410. $result = $Dispatcher->dispatch();
  411. $this->assertEquals(0, $result);
  412. }
  413. /**
  414. * Verify correct dispatch of custom classes with a main method
  415. *
  416. * @return void
  417. */
  418. public function testDispatchNotAShellWithMain() {
  419. $Dispatcher = new TestShellDispatcher();
  420. $methods = get_class_methods('Cake\Core\Object');
  421. array_push($methods, 'main', 'initdb', 'initialize', 'loadTasks', 'startup', '_secret');
  422. $Shell = $this->getMock('Cake\Core\Object', $methods);
  423. $Shell->expects($this->never())->method('initialize');
  424. $Shell->expects($this->once())->method('startup');
  425. $Shell->expects($this->once())->method('main')->will($this->returnValue(true));
  426. $Dispatcher->TestShell = $Shell;
  427. $Dispatcher->args = array('mock_with_main_not_a');
  428. $result = $Dispatcher->dispatch();
  429. $this->assertEquals(0, $result);
  430. $this->assertEquals(array(), $Dispatcher->args);
  431. $Shell = $this->getMock('Cake\Core\Object', $methods);
  432. $Shell->expects($this->once())->method('initdb')->will($this->returnValue(true));
  433. $Shell->expects($this->once())->method('startup');
  434. $Dispatcher->TestShell = $Shell;
  435. $Dispatcher->args = array('mock_with_main_not_a', 'initdb');
  436. $result = $Dispatcher->dispatch();
  437. $this->assertEquals(0, $result);
  438. }
  439. /**
  440. * Verify correct dispatch of custom classes without a main method
  441. *
  442. * @return void
  443. */
  444. public function testDispatchNotAShellWithoutMain() {
  445. $Dispatcher = new TestShellDispatcher();
  446. $methods = get_class_methods('Cake\Core\Object');
  447. array_push($methods, 'main', 'initdb', 'initialize', 'loadTasks', 'startup', '_secret');
  448. $Shell = $this->getMock('Cake\Core\Object', $methods);
  449. $Shell->expects($this->never())->method('initialize');
  450. $Shell->expects($this->once())->method('startup');
  451. $Shell->expects($this->once())->method('main')->will($this->returnValue(true));
  452. $Dispatcher->TestShell = $Shell;
  453. $Dispatcher->args = array('mock_without_main_not_a');
  454. $result = $Dispatcher->dispatch();
  455. $this->assertEquals(0, $result);
  456. $this->assertEquals(array(), $Dispatcher->args);
  457. $Shell = $this->getMock('Cake\Core\Object', $methods);
  458. $Shell->expects($this->once())->method('initdb')->will($this->returnValue(true));
  459. $Shell->expects($this->once())->method('startup');
  460. $Dispatcher->TestShell = $Shell;
  461. $Dispatcher->args = array('mock_without_main_not_a', 'initdb');
  462. $result = $Dispatcher->dispatch();
  463. $this->assertEquals(0, $result);
  464. }
  465. /**
  466. * Verify shifting of arguments
  467. *
  468. * @return void
  469. */
  470. public function testShiftArgs() {
  471. $Dispatcher = new TestShellDispatcher();
  472. $Dispatcher->args = array('a', 'b', 'c');
  473. $this->assertEquals('a', $Dispatcher->shiftArgs());
  474. $this->assertSame($Dispatcher->args, array('b', 'c'));
  475. $Dispatcher->args = array('a' => 'b', 'c', 'd');
  476. $this->assertEquals('b', $Dispatcher->shiftArgs());
  477. $this->assertSame($Dispatcher->args, array('c', 'd'));
  478. $Dispatcher->args = array('a', 'b' => 'c', 'd');
  479. $this->assertEquals('a', $Dispatcher->shiftArgs());
  480. $this->assertSame($Dispatcher->args, array('b' => 'c', 'd'));
  481. $Dispatcher->args = array(0 => 'a', 2 => 'b', 30 => 'c');
  482. $this->assertEquals('a', $Dispatcher->shiftArgs());
  483. $this->assertSame($Dispatcher->args, array(0 => 'b', 1 => 'c'));
  484. $Dispatcher->args = array();
  485. $this->assertNull($Dispatcher->shiftArgs());
  486. $this->assertSame(array(), $Dispatcher->args);
  487. }
  488. }