ShellDispatcherTest.php 14 KB

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