PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/cake/tests/cases/console/cake.test.php

https://github.com/msadouni/cakephp2x
PHP | 944 lines | 631 code | 132 blank | 181 comment | 5 complexity | 9824fb60079dd36d78b4b70ae95dd327 MD5 | raw file
  1. <?php
  2. /**
  3. * ShellDispatcherTest file
  4. *
  5. * Long description for file
  6. *
  7. * PHP Version 5.x
  8. *
  9. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  10. * Copyright 2005-2009, Cake Software Foundation, Inc.
  11. *
  12. * Licensed under The Open Group Test Suite License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2009, Cake Software Foundation, Inc.
  16. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  17. * @package cake
  18. * @subpackage cake.tests.cases.console
  19. * @since CakePHP(tm) v 1.2.0.5432
  20. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  21. */
  22. if (!defined('DISABLE_AUTO_DISPATCH')) {
  23. define('DISABLE_AUTO_DISPATCH', true);
  24. }
  25. if (!class_exists('ShellDispatcher')) {
  26. ob_start();
  27. $argv = false;
  28. require CAKE . 'console' . DS . 'cake.php';
  29. ob_end_clean();
  30. }
  31. require_once CONSOLE_LIBS . 'shell.php';
  32. /**
  33. * TestShellDispatcher class
  34. *
  35. * @package cake
  36. * @subpackage cake.tests.cases.console
  37. */
  38. class TestShellDispatcher extends ShellDispatcher {
  39. /**
  40. * params property
  41. *
  42. * @var array
  43. * @access public
  44. */
  45. var $params = array();
  46. /**
  47. * stdout property
  48. *
  49. * @var string
  50. * @access public
  51. */
  52. var $stdout = '';
  53. /**
  54. * stderr property
  55. *
  56. * @var string
  57. * @access public
  58. */
  59. var $stderr = '';
  60. /**
  61. * stopped property
  62. *
  63. * @var string
  64. * @access public
  65. */
  66. var $stopped = null;
  67. /**
  68. * TestShell
  69. *
  70. * @var mixed
  71. * @access public
  72. */
  73. var $TestShell;
  74. /**
  75. * _initEnvironment method
  76. *
  77. * @return void
  78. * @access protected
  79. */
  80. function _initEnvironment() {
  81. }
  82. /**
  83. * stderr method
  84. *
  85. * @return void
  86. * @access public
  87. */
  88. function stderr($string) {
  89. $this->stderr .= rtrim($string, ' ');
  90. }
  91. /**
  92. * stdout method
  93. *
  94. * @return void
  95. * @access public
  96. */
  97. function stdout($string, $newline = true) {
  98. if ($newline) {
  99. $this->stdout .= rtrim($string, ' ') . "\n";
  100. } else {
  101. $this->stdout .= rtrim($string, ' ');
  102. }
  103. }
  104. /**
  105. * clear method
  106. *
  107. * @return void
  108. * @access public
  109. */
  110. function clear() {
  111. }
  112. /**
  113. * _stop method
  114. *
  115. * @return void
  116. * @access protected
  117. */
  118. function _stop($status = 0) {
  119. $this->stopped = 'Stopped with status: ' . $status;
  120. return $status;
  121. }
  122. /**
  123. * getShell
  124. *
  125. * @param mixed $plugin
  126. * @return mixed
  127. * @access public
  128. */
  129. function getShell($plugin = null) {
  130. return $this->_getShell($plugin);
  131. }
  132. /**
  133. * _getShell
  134. *
  135. * @param mixed $plugin
  136. * @return mixed
  137. * @access protected
  138. */
  139. function _getShell($plugin = null) {
  140. if (isset($this->TestShell)) {
  141. return $this->TestShell;
  142. }
  143. return parent::_getShell($plugin);
  144. }
  145. }
  146. /**
  147. * ShellDispatcherTest
  148. *
  149. * @package cake
  150. * @subpackage cake.tests.cases.libs
  151. */
  152. class ShellDispatcherTest extends CakeTestCase {
  153. /**
  154. * setUp method
  155. *
  156. * @return void
  157. * @access public
  158. */
  159. function setUp() {
  160. App::build(array(
  161. 'plugins' => array(
  162. TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS
  163. ),
  164. 'shells' => array(
  165. CORE_PATH ? CONSOLE_LIBS : ROOT . DS . CONSOLE_LIBS,
  166. TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors' . DS . 'shells' . DS
  167. )
  168. ), true);
  169. }
  170. /**
  171. * tearDown method
  172. *
  173. * @return void
  174. * @access public
  175. */
  176. function tearDown() {
  177. App::build();
  178. }
  179. /**
  180. * testParseParams method
  181. *
  182. * @return void
  183. * @access public
  184. */
  185. function testParseParams() {
  186. $Dispatcher = new TestShellDispatcher();
  187. $params = array(
  188. '/cake/1.2.x.x/cake/console/cake.php',
  189. 'bake',
  190. '-app',
  191. 'new',
  192. '-working',
  193. '/var/www/htdocs'
  194. );
  195. $expected = array(
  196. 'app' => 'new',
  197. 'webroot' => 'webroot',
  198. 'working' => '/var/www/htdocs/new',
  199. 'root' => '/var/www/htdocs'
  200. );
  201. $Dispatcher->parseParams($params);
  202. $this->assertEqual($expected, $Dispatcher->params);
  203. $params = array('cake.php');
  204. $expected = array(
  205. 'app' => 'app',
  206. 'webroot' => 'webroot',
  207. 'working' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH . DS . 'app'),
  208. 'root' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH),
  209. );
  210. $Dispatcher->params = $Dispatcher->args = array();
  211. $Dispatcher->parseParams($params);
  212. $this->assertEqual($expected, $Dispatcher->params);
  213. $params = array(
  214. 'cake.php',
  215. '-app',
  216. 'new',
  217. );
  218. $expected = array(
  219. 'app' => 'new',
  220. 'webroot' => 'webroot',
  221. 'working' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH . DS . 'new'),
  222. 'root' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH)
  223. );
  224. $Dispatcher->params = $Dispatcher->args = array();
  225. $Dispatcher->parseParams($params);
  226. $this->assertEqual($expected, $Dispatcher->params);
  227. $params = array(
  228. './cake.php',
  229. 'bake',
  230. '-app',
  231. 'new',
  232. '-working',
  233. '/cake/1.2.x.x/cake/console'
  234. );
  235. $expected = array(
  236. 'app' => 'new',
  237. 'webroot' => 'webroot',
  238. 'working' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH . DS . 'new'),
  239. 'root' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH)
  240. );
  241. $Dispatcher->params = $Dispatcher->args = array();
  242. $Dispatcher->parseParams($params);
  243. $this->assertEqual($expected, $Dispatcher->params);
  244. $params = array(
  245. './console/cake.php',
  246. 'bake',
  247. '-app',
  248. 'new',
  249. '-working',
  250. '/cake/1.2.x.x/cake'
  251. );
  252. $expected = array(
  253. 'app' => 'new',
  254. 'webroot' => 'webroot',
  255. 'working' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH . DS . 'new'),
  256. 'root' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH)
  257. );
  258. $Dispatcher->params = $Dispatcher->args = array();
  259. $Dispatcher->parseParams($params);
  260. $this->assertEqual($expected, $Dispatcher->params);
  261. $params = array(
  262. './console/cake.php',
  263. 'bake',
  264. '-app',
  265. 'new',
  266. '-dry',
  267. '-working',
  268. '/cake/1.2.x.x/cake'
  269. );
  270. $expected = array(
  271. 'app' => 'new',
  272. 'webroot' => 'webroot',
  273. 'working' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH . DS . 'new'),
  274. 'root' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH),
  275. 'dry' => 1
  276. );
  277. $Dispatcher->params = $Dispatcher->args = array();
  278. $Dispatcher->parseParams($params);
  279. $this->assertEqual($expected, $Dispatcher->params);
  280. $params = array(
  281. './console/cake.php',
  282. '-working',
  283. '/cake/1.2.x.x/cake',
  284. 'schema',
  285. 'run',
  286. 'create',
  287. '-dry',
  288. '-f',
  289. '-name',
  290. 'DbAcl'
  291. );
  292. $expected = array(
  293. 'app' => 'app',
  294. 'webroot' => 'webroot',
  295. 'working' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH . DS . 'app'),
  296. 'root' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH),
  297. 'dry' => 1,
  298. 'f' => 1,
  299. 'name' => 'DbAcl'
  300. );
  301. $Dispatcher->params = $Dispatcher->args = array();
  302. $Dispatcher->parseParams($params);
  303. $this->assertEqual($expected, $Dispatcher->params);
  304. $expected = array('./console/cake.php', 'schema', 'run', 'create');
  305. $this->assertEqual($expected, $Dispatcher->args);
  306. $params = array(
  307. '/cake/1.2.x.x/cake/console/cake.php',
  308. '-working',
  309. '/cake/1.2.x.x/app',
  310. 'schema',
  311. 'run',
  312. 'create',
  313. '-dry',
  314. '-name',
  315. 'DbAcl'
  316. );
  317. $expected = array(
  318. 'app' => 'app',
  319. 'webroot' => 'webroot',
  320. 'working' => '/cake/1.2.x.x/app',
  321. 'root' => '/cake/1.2.x.x',
  322. 'dry' => 1,
  323. 'name' => 'DbAcl'
  324. );
  325. $Dispatcher->params = $Dispatcher->args = array();
  326. $Dispatcher->parseParams($params);
  327. $this->assertEqual($expected, $Dispatcher->params);
  328. $expected = array('/cake/1.2.x.x/cake/console/cake.php', 'schema', 'run', 'create');
  329. $this->assertEqual($expected, $Dispatcher->args);
  330. $params = array(
  331. 'cake.php',
  332. '-working',
  333. 'C:/wamp/www/cake/app',
  334. 'bake',
  335. '-app',
  336. 'C:/wamp/www/apps/cake/app',
  337. );
  338. $expected = array(
  339. 'app' => 'app',
  340. 'webroot' => 'webroot',
  341. 'working' => 'C:\wamp\www\apps\cake\app',
  342. 'root' => 'C:\wamp\www\apps\cake'
  343. );
  344. $Dispatcher->params = $Dispatcher->args = array();
  345. $Dispatcher->parseParams($params);
  346. $this->assertEqual($expected, $Dispatcher->params);
  347. $params = array(
  348. 'cake.php',
  349. '-working',
  350. 'C:\wamp\www\cake\app',
  351. 'bake',
  352. '-app',
  353. 'C:\wamp\www\apps\cake\app',
  354. );
  355. $expected = array(
  356. 'app' => 'app',
  357. 'webroot' => 'webroot',
  358. 'working' => 'C:\wamp\www\apps\cake\app',
  359. 'root' => 'C:\wamp\www\apps\cake'
  360. );
  361. $Dispatcher->params = $Dispatcher->args = array();
  362. $Dispatcher->parseParams($params);
  363. $this->assertEqual($expected, $Dispatcher->params);
  364. $params = array(
  365. 'cake.php',
  366. '-working',
  367. 'C:\wamp\www\apps',
  368. 'bake',
  369. '-app',
  370. 'cake\app',
  371. '-url',
  372. 'http://example.com/some/url/with/a/path'
  373. );
  374. $expected = array(
  375. 'app' => 'app',
  376. 'webroot' => 'webroot',
  377. 'working' => 'C:\wamp\www\apps\cake\app',
  378. 'root' => 'C:\wamp\www\apps\cake',
  379. 'url' => 'http://example.com/some/url/with/a/path'
  380. );
  381. $Dispatcher->params = $Dispatcher->args = array();
  382. $Dispatcher->parseParams($params);
  383. $this->assertEqual($expected, $Dispatcher->params);
  384. $params = array(
  385. '/home/amelo/dev/cake-common/cake/console/cake.php',
  386. '-root',
  387. '/home/amelo/dev/lsbu-vacancy',
  388. '-working',
  389. '/home/amelo/dev/lsbu-vacancy',
  390. '-app',
  391. 'app',
  392. );
  393. $expected = array(
  394. 'app' => 'app',
  395. 'webroot' => 'webroot',
  396. 'working' => '/home/amelo/dev/lsbu-vacancy/app',
  397. 'root' => '/home/amelo/dev/lsbu-vacancy',
  398. );
  399. $Dispatcher->params = $Dispatcher->args = array();
  400. $Dispatcher->parseParams($params);
  401. $this->assertEqual($expected, $Dispatcher->params);
  402. $params = array(
  403. 'cake.php',
  404. '-working',
  405. 'D:\www',
  406. 'bake',
  407. 'my_app',
  408. );
  409. $expected = array(
  410. 'working' => 'D:\www',
  411. 'app' => 'www',
  412. 'root' => 'D:',
  413. 'webroot' => 'webroot'
  414. );
  415. $Dispatcher->params = $Dispatcher->args = array();
  416. $Dispatcher->parseParams($params);
  417. $this->assertEqual($expected, $Dispatcher->params);
  418. }
  419. /**
  420. * testBuildPaths method
  421. *
  422. * @return void
  423. * @access public
  424. */
  425. function testBuildPaths() {
  426. $Dispatcher = new TestShellDispatcher();
  427. $result = $Dispatcher->shellPaths;
  428. $expected = array(
  429. TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'test_plugin' . DS . 'vendors' . DS . 'shells' . DS,
  430. TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'test_plugin_two' . DS . 'vendors' . DS . 'shells' . DS,
  431. APP . 'vendors' . DS . 'shells' . DS,
  432. VENDORS . 'shells' . DS,
  433. CORE_PATH ? CONSOLE_LIBS : ROOT . DS . CONSOLE_LIBS,
  434. TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors' . DS . 'shells' . DS,
  435. );
  436. $this->assertIdentical(array_diff($result, $expected), array());
  437. $this->assertIdentical(array_diff($expected, $result), array());
  438. }
  439. /**
  440. * Verify loading of (plugin-) shells
  441. *
  442. * @return void
  443. * @access public
  444. */
  445. function testGetShell() {
  446. $this->skipIf(class_exists('SampleShell'), '%s SampleShell Class already loaded');
  447. $this->skipIf(class_exists('ExampleShell'), '%s ExampleShell Class already loaded');
  448. $Dispatcher = new TestShellDispatcher();
  449. $Dispatcher->shell = 'sample';
  450. $Dispatcher->shellName = 'Sample';
  451. $Dispatcher->shellClass = 'SampleShell';
  452. $result = $Dispatcher->getShell();
  453. $this->assertIsA($result, 'SampleShell');
  454. $Dispatcher = new TestShellDispatcher();
  455. $Dispatcher->shell = 'example';
  456. $Dispatcher->shellName = 'Example';
  457. $Dispatcher->shellClass = 'ExampleShell';
  458. $result = $Dispatcher->getShell('test_plugin');
  459. $this->assertIsA($result, 'ExampleShell');
  460. }
  461. /**
  462. * Verify correct dispatch of Shell subclasses with a main method
  463. *
  464. * @return void
  465. * @access public
  466. */
  467. function testDispatchShellWithMain() {
  468. Mock::generate('Shell', 'MockWithMainShell', array('main', '_secret'));
  469. $Dispatcher = new TestShellDispatcher();
  470. $Shell = new MockWithMainShell();
  471. $Shell->setReturnValue('main', true);
  472. $Shell->expectOnce('initialize');
  473. $Shell->expectOnce('loadTasks');
  474. $Shell->expectOnce('startup');
  475. $Shell->expectOnce('main');
  476. $Dispatcher->TestShell = $Shell;
  477. $Dispatcher->args = array('mock_with_main');
  478. $result = $Dispatcher->dispatch();
  479. $this->assertTrue($result);
  480. $this->assertEqual($Dispatcher->args, array());
  481. $Shell = new MockWithMainShell();
  482. $Shell->setReturnValue('main', true);
  483. $Shell->expectOnce('startup');
  484. $Shell->expectOnce('main');
  485. $Dispatcher->TestShell = $Shell;
  486. $Dispatcher->args = array('mock_with_main', 'initdb');
  487. $result = $Dispatcher->dispatch();
  488. $this->assertTrue($result);
  489. $this->assertEqual($Dispatcher->args, array('initdb'));
  490. $Shell = new MockWithMainShell();
  491. $Shell->setReturnValue('main', true);
  492. $Shell->expectOnce('startup');
  493. $Shell->expectOnce('help');
  494. $Dispatcher->TestShell = $Shell;
  495. $Dispatcher->args = array('mock_with_main', 'help');
  496. $result = $Dispatcher->dispatch();
  497. $this->assertNull($result);
  498. $this->assertEqual($Dispatcher->args, array());
  499. $Shell = new MockWithMainShell();
  500. $Shell->setReturnValue('main', true);
  501. $Shell->expectNever('hr');
  502. $Shell->expectOnce('startup');
  503. $Shell->expectOnce('main');
  504. $Dispatcher->TestShell = $Shell;
  505. $Dispatcher->args = array('mock_with_main', 'hr');
  506. $result = $Dispatcher->dispatch();
  507. $this->assertTrue($result);
  508. $this->assertEqual($Dispatcher->args, array('hr'));
  509. $Shell = new MockWithMainShell();
  510. $Shell->setReturnValue('main', true);
  511. $Shell->expectOnce('startup');
  512. $Shell->expectOnce('main');
  513. $Dispatcher->TestShell = $Shell;
  514. $Dispatcher->args = array('mock_with_main', 'dispatch');
  515. $result = $Dispatcher->dispatch();
  516. $this->assertTrue($result);
  517. $this->assertEqual($Dispatcher->args, array('dispatch'));
  518. $Shell = new MockWithMainShell();
  519. $Shell->setReturnValue('main', true);
  520. $Shell->expectOnce('startup');
  521. $Shell->expectOnce('main');
  522. $Dispatcher->TestShell = $Shell;
  523. $Dispatcher->args = array('mock_with_main', 'idontexist');
  524. $result = $Dispatcher->dispatch();
  525. $this->assertTrue($result);
  526. $this->assertEqual($Dispatcher->args, array('idontexist'));
  527. $Shell = new MockWithMainShell();
  528. $Shell->expectNever('startup');
  529. $Shell->expectNever('main');
  530. $Shell->expectNever('_secret');
  531. $Dispatcher->TestShell = $Shell;
  532. $Dispatcher->args = array('mock_with_main', '_secret');
  533. $result = $Dispatcher->dispatch();
  534. $this->assertFalse($result);
  535. }
  536. /**
  537. * Verify correct dispatch of Shell subclasses without a main method
  538. *
  539. * @return void
  540. * @access public
  541. */
  542. function testDispatchShellWithoutMain() {
  543. Mock::generate('Shell', 'MockWithoutMainShell', array('initDb', '_secret'));
  544. $Dispatcher = new TestShellDispatcher();
  545. $Shell = new MockWithoutMainShell();
  546. $Shell->setReturnValue('initDb', true);
  547. $Shell->expectOnce('initialize');
  548. $Shell->expectOnce('loadTasks');
  549. $Shell->expectNever('startup');
  550. $Dispatcher->TestShell = $Shell;
  551. $Dispatcher->args = array('mock_without_main');
  552. $result = $Dispatcher->dispatch();
  553. $this->assertFalse($result);
  554. $this->assertEqual($Dispatcher->args, array());
  555. $Shell = new MockWithoutMainShell();
  556. $Shell->setReturnValue('initDb', true);
  557. $Shell->expectOnce('startup');
  558. $Shell->expectOnce('initDb');
  559. $Dispatcher->TestShell = $Shell;
  560. $Dispatcher->args = array('mock_without_main', 'initdb');
  561. $result = $Dispatcher->dispatch();
  562. $this->assertTrue($result);
  563. $this->assertEqual($Dispatcher->args, array());
  564. $Shell = new MockWithoutMainShell();
  565. $Shell->setReturnValue('initDb', true);
  566. $Shell->expectNever('startup');
  567. $Shell->expectNever('hr');
  568. $Dispatcher->TestShell = $Shell;
  569. $Dispatcher->args = array('mock_without_main', 'hr');
  570. $result = $Dispatcher->dispatch();
  571. $this->assertFalse($result);
  572. $this->assertEqual($Dispatcher->args, array('hr'));
  573. $Shell = new MockWithoutMainShell();
  574. $Shell->setReturnValue('initDb', true);
  575. $Shell->expectNever('startup');
  576. $Dispatcher->TestShell = $Shell;
  577. $Dispatcher->args = array('mock_without_main', 'dispatch');
  578. $result = $Dispatcher->dispatch();
  579. $this->assertFalse($result);
  580. $Shell = new MockWithoutMainShell();
  581. $Shell->expectNever('startup');
  582. $Dispatcher->TestShell = $Shell;
  583. $Dispatcher->args = array('mock_without_main', 'idontexist');
  584. $result = $Dispatcher->dispatch();
  585. $this->assertFalse($result);
  586. $Shell = new MockWithoutMainShell();
  587. $Shell->expectNever('startup');
  588. $Shell->expectNever('_secret');
  589. $Dispatcher->TestShell = $Shell;
  590. $Dispatcher->args = array('mock_without_main', '_secret');
  591. $result = $Dispatcher->dispatch();
  592. $this->assertFalse($result);
  593. }
  594. /**
  595. * Verify correct dispatch of custom classes with a main method
  596. *
  597. * @return void
  598. * @access public
  599. */
  600. function testDispatchNotAShellWithMain() {
  601. Mock::generate('Object', 'MockWithMainNotAShell',
  602. array('main', 'initialize', 'loadTasks', 'startup', '_secret'));
  603. $Dispatcher = new TestShellDispatcher();
  604. $Shell = new MockWithMainNotAShell();
  605. $Shell->setReturnValue('main', true);
  606. $Shell->expectNever('initialize');
  607. $Shell->expectNever('loadTasks');
  608. $Shell->expectOnce('startup');
  609. $Shell->expectOnce('main');
  610. $Dispatcher->TestShell = $Shell;
  611. $Dispatcher->args = array('mock_with_main_not_a');
  612. $result = $Dispatcher->dispatch();
  613. $this->assertTrue($result);
  614. $this->assertEqual($Dispatcher->args, array());
  615. $Shell = new MockWithMainNotAShell();
  616. $Shell->setReturnValue('main', true);
  617. $Shell->expectOnce('startup');
  618. $Shell->expectOnce('main');
  619. $Dispatcher->TestShell = $Shell;
  620. $Dispatcher->args = array('mock_with_main_not_a', 'initdb');
  621. $result = $Dispatcher->dispatch();
  622. $this->assertTrue($result);
  623. $this->assertEqual($Dispatcher->args, array('initdb'));
  624. $Shell = new MockWithMainNotAShell();
  625. $Shell->setReturnValue('main', true);
  626. $Shell->expectOnce('startup');
  627. $Shell->expectOnce('main');
  628. $Dispatcher->TestShell = $Shell;
  629. $Dispatcher->args = array('mock_with_main_not_a', 'hr');
  630. $result = $Dispatcher->dispatch();
  631. $this->assertTrue($result);
  632. $this->assertEqual($Dispatcher->args, array('hr'));
  633. $Shell = new MockWithMainNotAShell();
  634. $Shell->setReturnValue('main', true);
  635. $Shell->expectOnce('startup');
  636. $Shell->expectOnce('main');
  637. $Dispatcher->TestShell = $Shell;
  638. $Dispatcher->args = array('mock_with_main_not_a', 'dispatch');
  639. $result = $Dispatcher->dispatch();
  640. $this->assertTrue($result);
  641. $this->assertEqual($Dispatcher->args, array('dispatch'));
  642. $Shell = new MockWithMainNotAShell();
  643. $Shell->setReturnValue('main', true);
  644. $Shell->expectOnce('startup');
  645. $Shell->expectOnce('main');
  646. $Dispatcher->TestShell = $Shell;
  647. $Dispatcher->args = array('mock_with_main_not_a', 'idontexist');
  648. $result = $Dispatcher->dispatch();
  649. $this->assertTrue($result);
  650. $this->assertEqual($Dispatcher->args, array('idontexist'));
  651. $Shell = new MockWithMainNotAShell();
  652. $Shell->expectNever('startup');
  653. $Shell->expectNever('main');
  654. $Shell->expectNever('_secret');
  655. $Dispatcher->TestShell = $Shell;
  656. $Dispatcher->args = array('mock_with_main_not_a', '_secret');
  657. $result = $Dispatcher->dispatch();
  658. $this->assertFalse($result);
  659. }
  660. /**
  661. * Verify correct dispatch of custom classes without a main method
  662. *
  663. * @return void
  664. * @access public
  665. */
  666. function testDispatchNotAShellWithoutMain() {
  667. Mock::generate('Object', 'MockWithoutMainNotAShell',
  668. array('initDb', 'initialize', 'loadTasks', 'startup', '_secret'));
  669. $Dispatcher = new TestShellDispatcher();
  670. $Shell = new MockWithoutMainNotAShell();
  671. $Shell->setReturnValue('initDb', true);
  672. $Shell->expectNever('initialize');
  673. $Shell->expectNever('loadTasks');
  674. $Shell->expectNever('startup');
  675. $Dispatcher->TestShell = $Shell;
  676. $Dispatcher->args = array('mock_without_main_not_a');
  677. $result = $Dispatcher->dispatch();
  678. $this->assertFalse($result);
  679. $Shell = new MockWithoutMainNotAShell();
  680. $Shell->setReturnValue('initDb', true);
  681. $Shell->expectOnce('startup');
  682. $Shell->expectOnce('initDb');
  683. $Dispatcher->TestShell = $Shell;
  684. $Dispatcher->args = array('mock_without_main_not_a', 'initdb');
  685. $result = $Dispatcher->dispatch();
  686. $this->assertTrue($result);
  687. $this->assertEqual($Dispatcher->args, array());
  688. $Shell = new MockWithoutMainNotAShell();
  689. $Shell->setReturnValue('initDb', true);
  690. $Shell->expectNever('startup');
  691. $Dispatcher->TestShell = $Shell;
  692. $Dispatcher->args = array('mock_without_main_not_a', 'hr');
  693. $result = $Dispatcher->dispatch();
  694. $this->assertFalse($result);
  695. $Shell = new MockWithoutMainNotAShell();
  696. $Shell->setReturnValue('initDb', true);
  697. $Shell->expectNever('startup');
  698. $Dispatcher->TestShell = $Shell;
  699. $Dispatcher->args = array('mock_without_main_not_a', 'dispatch');
  700. $result = $Dispatcher->dispatch();
  701. $this->assertFalse($result);
  702. $Shell = new MockWithoutMainNotAShell();
  703. $Shell->expectNever('startup');
  704. $Dispatcher->TestShell = $Shell;
  705. $Dispatcher->args = array('mock_without_main_not_a', 'idontexist');
  706. $result = $Dispatcher->dispatch();
  707. $this->assertFalse($result);
  708. $Shell = new MockWithoutMainNotAShell();
  709. $Shell->expectNever('startup');
  710. $Shell->expectNever('_secret');
  711. $Dispatcher->TestShell = $Shell;
  712. $Dispatcher->args = array('mock_without_main_not_a', '_secret');
  713. $result = $Dispatcher->dispatch();
  714. $this->assertFalse($result);
  715. }
  716. /**
  717. * Verify that a task is called instead of the shell if the first arg equals
  718. * the name of the task
  719. *
  720. * @return void
  721. * @access public
  722. */
  723. function testDispatchTask() {
  724. Mock::generate('Shell', 'MockWeekShell', array('main'));
  725. Mock::generate('Shell', 'MockOnSundayTask', array('execute'));
  726. $Dispatcher = new TestShellDispatcher();
  727. $Shell = new MockWeekShell();
  728. $Shell->expectOnce('initialize');
  729. $Shell->expectOnce('loadTasks');
  730. $Shell->expectNever('startup');
  731. $Shell->expectNever('main');
  732. $Task = new MockOnSundayTask();
  733. $Task->setReturnValue('execute', true);
  734. $Task->expectOnce('initialize');
  735. $Task->expectOnce('loadTasks');
  736. $Task->expectOnce('startup');
  737. $Task->expectOnce('execute');
  738. $Shell->MockOnSunday = $Task;
  739. $Shell->setReturnValue('tasks', array('MockOnSunday'));
  740. // $Shell->taskNames = array('MockOnSunday');
  741. $Dispatcher->TestShell = $Shell;
  742. $Dispatcher->args = array('mock_week', 'mock_on_sunday');
  743. $result = $Dispatcher->dispatch();
  744. $this->assertTrue($result);
  745. $this->assertEqual($Dispatcher->args, array());
  746. $Shell = new MockWeekShell();
  747. $Task = new MockOnSundayTask();
  748. $Task->expectNever('execute');
  749. $Task->expectOnce('help');
  750. $Shell->MockOnSunday = $Task;
  751. $Shell->setReturnValue('tasks', array('MockOnSunday'));
  752. // $Shell->taskNames = array('MockOnSunday');
  753. $Dispatcher->TestShell = $Shell;
  754. $Dispatcher->args = array('mock_week', 'mock_on_sunday', 'help');
  755. $result = $Dispatcher->dispatch();
  756. $this->assertTrue($result);
  757. }
  758. /**
  759. * Verify shifting of arguments
  760. *
  761. * @return void
  762. * @access public
  763. */
  764. function testShiftArgs() {
  765. $Dispatcher = new TestShellDispatcher();
  766. $Dispatcher->args = array('a', 'b', 'c');
  767. $this->assertEqual($Dispatcher->shiftArgs(), 'a');
  768. $this->assertIdentical($Dispatcher->args, array('b', 'c'));
  769. $Dispatcher->args = array('a' => 'b', 'c', 'd');
  770. $this->assertEqual($Dispatcher->shiftArgs(), 'b');
  771. $this->assertIdentical($Dispatcher->args, array('c', 'd'));
  772. $Dispatcher->args = array('a', 'b' => 'c', 'd');
  773. $this->assertEqual($Dispatcher->shiftArgs(), 'a');
  774. $this->assertIdentical($Dispatcher->args, array('b' => 'c', 'd'));
  775. $Dispatcher->args = array(0 => 'a', 2 => 'b', 30 => 'c');
  776. $this->assertEqual($Dispatcher->shiftArgs(), 'a');
  777. $this->assertIdentical($Dispatcher->args, array(0 => 'b', 1 => 'c'));
  778. $Dispatcher->args = array();
  779. $this->assertNull($Dispatcher->shiftArgs());
  780. $this->assertIdentical($Dispatcher->args, array());
  781. }
  782. /**
  783. * testHelpCommand method
  784. *
  785. * @return void
  786. * @access public
  787. */
  788. function testHelpCommand() {
  789. $Dispatcher = new TestShellDispatcher();
  790. $expected = "/example \[.*TestPlugin, TestPluginTwo.*\]/";
  791. $this->assertPattern($expected, $Dispatcher->stdout);
  792. $expected = "/welcome \[.*TestPluginTwo.*\]/";
  793. $this->assertPattern($expected, $Dispatcher->stdout);
  794. $expected = "/acl \[.*CORE.*\]/";
  795. $this->assertPattern($expected, $Dispatcher->stdout);
  796. $expected = "/api \[.*CORE.*\]/";
  797. $this->assertPattern($expected, $Dispatcher->stdout);
  798. $expected = "/bake \[.*CORE.*\]/";
  799. $this->assertPattern($expected, $Dispatcher->stdout);
  800. $expected = "/console \[.*CORE.*\]/";
  801. $this->assertPattern($expected, $Dispatcher->stdout);
  802. $expected = "/i18n \[.*CORE.*\]/";
  803. $this->assertPattern($expected, $Dispatcher->stdout);
  804. $expected = "/schema \[.*CORE.*\]/";
  805. $this->assertPattern($expected, $Dispatcher->stdout);
  806. $expected = "/testsuite \[.*CORE.*\]/";
  807. $this->assertPattern($expected, $Dispatcher->stdout);
  808. $expected = "/sample \[.*test_app.*\]/";
  809. $this->assertPattern($expected, $Dispatcher->stdout);
  810. }
  811. }
  812. ?>