PageRenderTime 29ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/Console/ShellDispatcherTest.php

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