PageRenderTime 59ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

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

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