ShellDispatcherTest.php 14 KB

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