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

https://github.com/hardsshah/bookmarks · PHP · 483 lines · 323 code · 39 blank · 121 comment · 5 complexity · 3be4b1311253a8efb1ada9be775f2c96 MD5 · raw file

  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * ShellDispatcherTest file
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2007, Cake Software Foundation, Inc.
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake
  20. * @subpackage cake.tests.cases.console
  21. * @since CakePHP(tm) v 1.2.0.5432
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. if (!defined('DISABLE_AUTO_DISPATCH')) {
  28. define('DISABLE_AUTO_DISPATCH', true);
  29. }
  30. if (!class_exists('ShellDispatcher')) {
  31. ob_start();
  32. $argv = false;
  33. require CAKE . 'console' . DS . 'cake.php';
  34. ob_end_clean();
  35. }
  36. /**
  37. * TestShellDispatcher class
  38. *
  39. * @package cake
  40. * @subpackage cake.tests.cases.console
  41. */
  42. class TestShellDispatcher extends ShellDispatcher {
  43. /**
  44. * params property
  45. *
  46. * @var array
  47. * @access public
  48. */
  49. var $params = array();
  50. /**
  51. * stdout property
  52. *
  53. * @var string
  54. * @access public
  55. */
  56. var $stdout = '';
  57. /**
  58. * stderr property
  59. *
  60. * @var string
  61. * @access public
  62. */
  63. var $stderr = '';
  64. /**
  65. * stopped property
  66. *
  67. * @var string
  68. * @access public
  69. */
  70. var $stopped = null;
  71. /**
  72. * _initEnvironment method
  73. *
  74. * @access protected
  75. * @return void
  76. */
  77. function _initEnvironment() {
  78. }
  79. /**
  80. * stderr method
  81. *
  82. * @access public
  83. * @return void
  84. */
  85. function stderr($string) {
  86. $this->stderr .= rtrim($string, ' ');
  87. }
  88. /**
  89. * stdout method
  90. *
  91. * @access public
  92. * @return void
  93. */
  94. function stdout($string, $newline = true) {
  95. if ($newline) {
  96. $this->stdout .= rtrim($string, ' ') . "\n";
  97. } else {
  98. $this->stdout .= rtrim($string, ' ');
  99. }
  100. }
  101. /**
  102. * _stop method
  103. *
  104. * @access protected
  105. * @return void
  106. */
  107. function _stop($status = 0) {
  108. $this->stopped = 'Stopped with status: ' . $status;
  109. }
  110. }
  111. /**
  112. * ShellDispatcherTest
  113. *
  114. * @package cake
  115. * @subpackage cake.tests.cases.libs
  116. */
  117. class ShellDispatcherTest extends UnitTestCase {
  118. /**
  119. * setUp method
  120. *
  121. * @access public
  122. * @return void
  123. */
  124. function setUp() {
  125. if (!isset($this->pluginPaths)) {
  126. $this->pluginPaths = Configure::read('pluginPaths');
  127. $this->shellPaths = Configure::read('shellPaths');
  128. }
  129. Configure::write('pluginPaths', array(
  130. TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS
  131. ));
  132. Configure::write('shellPaths', array(
  133. CORE_PATH ? CONSOLE_LIBS : ROOT . DS . CONSOLE_LIBS,
  134. TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors' . DS . 'shells' . DS
  135. ));
  136. }
  137. /**
  138. * tearDown method
  139. *
  140. * @access public
  141. * @return void
  142. */
  143. function tearDown() {
  144. Configure::write('pluginPaths', $this->pluginPaths);
  145. Configure::write('shellPaths', $this->shellPaths);
  146. }
  147. /**
  148. * testParseParams method
  149. *
  150. * @access public
  151. * @return void
  152. */
  153. function testParseParams() {
  154. $Dispatcher =& new TestShellDispatcher();
  155. $params = array(
  156. '/cake/1.2.x.x/cake/console/cake.php',
  157. 'bake',
  158. '-app',
  159. 'new',
  160. '-working',
  161. '/var/www/htdocs'
  162. );
  163. $expected = array(
  164. 'app' => 'new',
  165. 'webroot' => 'webroot',
  166. 'working' => '/var/www/htdocs/new',
  167. 'root' => '/var/www/htdocs'
  168. );
  169. $Dispatcher->parseParams($params);
  170. $this->assertEqual($expected, $Dispatcher->params);
  171. $params = array('cake.php');
  172. $expected = array(
  173. 'app' => 'app',
  174. 'webroot' => 'webroot',
  175. 'working' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH . DS . 'app'),
  176. 'root' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH),
  177. );
  178. $Dispatcher->params = $Dispatcher->args = array();
  179. $Dispatcher->parseParams($params);
  180. $this->assertEqual($expected, $Dispatcher->params);
  181. $params = array(
  182. 'cake.php',
  183. '-app',
  184. 'new',
  185. );
  186. $expected = array(
  187. 'app' => 'new',
  188. 'webroot' => 'webroot',
  189. 'working' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH . DS . 'new'),
  190. 'root' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH)
  191. );
  192. $Dispatcher->params = $Dispatcher->args = array();
  193. $Dispatcher->parseParams($params);
  194. $this->assertEqual($expected, $Dispatcher->params);
  195. $params = array(
  196. './cake.php',
  197. 'bake',
  198. '-app',
  199. 'new',
  200. '-working',
  201. '/cake/1.2.x.x/cake/console'
  202. );
  203. $expected = array(
  204. 'app' => 'new',
  205. 'webroot' => 'webroot',
  206. 'working' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH . DS . 'new'),
  207. 'root' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH)
  208. );
  209. $Dispatcher->params = $Dispatcher->args = array();
  210. $Dispatcher->parseParams($params);
  211. $this->assertEqual($expected, $Dispatcher->params);
  212. $params = array(
  213. './console/cake.php',
  214. 'bake',
  215. '-app',
  216. 'new',
  217. '-working',
  218. '/cake/1.2.x.x/cake'
  219. );
  220. $expected = array(
  221. 'app' => 'new',
  222. 'webroot' => 'webroot',
  223. 'working' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH . DS . 'new'),
  224. 'root' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH)
  225. );
  226. $Dispatcher->params = $Dispatcher->args = array();
  227. $Dispatcher->parseParams($params);
  228. $this->assertEqual($expected, $Dispatcher->params);
  229. $params = array(
  230. './console/cake.php',
  231. 'bake',
  232. '-app',
  233. 'new',
  234. '-dry',
  235. '-working',
  236. '/cake/1.2.x.x/cake'
  237. );
  238. $expected = array(
  239. 'app' => 'new',
  240. 'webroot' => 'webroot',
  241. 'working' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH . DS . 'new'),
  242. 'root' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH),
  243. 'dry' => 1
  244. );
  245. $Dispatcher->params = $Dispatcher->args = array();
  246. $Dispatcher->parseParams($params);
  247. $this->assertEqual($expected, $Dispatcher->params);
  248. $params = array(
  249. './console/cake.php',
  250. '-working',
  251. '/cake/1.2.x.x/cake',
  252. 'schema',
  253. 'run',
  254. 'create',
  255. '-dry',
  256. '-f',
  257. '-name',
  258. 'DbAcl'
  259. );
  260. $expected = array(
  261. 'app' => 'app',
  262. 'webroot' => 'webroot',
  263. 'working' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH . DS . 'app'),
  264. 'root' => str_replace('\\', '/', CAKE_CORE_INCLUDE_PATH),
  265. 'dry' => 1,
  266. 'f' => 1,
  267. 'name' => 'DbAcl'
  268. );
  269. $Dispatcher->params = $Dispatcher->args = array();
  270. $Dispatcher->parseParams($params);
  271. $this->assertEqual($expected, $Dispatcher->params);
  272. $expected = array('./console/cake.php', 'schema', 'run', 'create');
  273. $this->assertEqual($expected, $Dispatcher->args);
  274. $params = array(
  275. '/cake/1.2.x.x/cake/console/cake.php',
  276. '-working',
  277. '/cake/1.2.x.x/app',
  278. 'schema',
  279. 'run',
  280. 'create',
  281. '-dry',
  282. '-name',
  283. 'DbAcl'
  284. );
  285. $expected = array(
  286. 'app' => 'app',
  287. 'webroot' => 'webroot',
  288. 'working' => '/cake/1.2.x.x/app',
  289. 'root' => '/cake/1.2.x.x',
  290. 'dry' => 1,
  291. 'name' => 'DbAcl'
  292. );
  293. $Dispatcher->params = $Dispatcher->args = array();
  294. $Dispatcher->parseParams($params);
  295. $this->assertEqual($expected, $Dispatcher->params);
  296. $expected = array('/cake/1.2.x.x/cake/console/cake.php', 'schema', 'run', 'create');
  297. $this->assertEqual($expected, $Dispatcher->args);
  298. $params = array(
  299. 'cake.php',
  300. '-working',
  301. 'C:/wamp/www/cake/app',
  302. 'bake',
  303. '-app',
  304. 'C:/wamp/www/apps/cake/app',
  305. );
  306. $expected = array(
  307. 'app' => 'app',
  308. 'webroot' => 'webroot',
  309. 'working' => 'C:\wamp\www\apps\cake\app',
  310. 'root' => 'C:\wamp\www\apps\cake'
  311. );
  312. $Dispatcher->params = $Dispatcher->args = array();
  313. $Dispatcher->parseParams($params);
  314. $this->assertEqual($expected, $Dispatcher->params);
  315. $params = array(
  316. 'cake.php',
  317. '-working',
  318. 'C:\wamp\www\cake\app',
  319. 'bake',
  320. '-app',
  321. 'C:\wamp\www\apps\cake\app',
  322. );
  323. $expected = array(
  324. 'app' => 'app',
  325. 'webroot' => 'webroot',
  326. 'working' => 'C:\wamp\www\apps\cake\app',
  327. 'root' => 'C:\wamp\www\apps\cake'
  328. );
  329. $Dispatcher->params = $Dispatcher->args = array();
  330. $Dispatcher->parseParams($params);
  331. $this->assertEqual($expected, $Dispatcher->params);
  332. $params = array(
  333. 'cake.php',
  334. '-working',
  335. 'C:\wamp\www\apps',
  336. 'bake',
  337. '-app',
  338. 'cake\app',
  339. '-url',
  340. 'http://example.com/some/url/with/a/path'
  341. );
  342. $expected = array(
  343. 'app' => 'app',
  344. 'webroot' => 'webroot',
  345. 'working' => 'C:\wamp\www\apps\cake\app',
  346. 'root' => 'C:\wamp\www\apps\cake',
  347. 'url' => 'http://example.com/some/url/with/a/path'
  348. );
  349. $Dispatcher->params = $Dispatcher->args = array();
  350. $Dispatcher->parseParams($params);
  351. $this->assertEqual($expected, $Dispatcher->params);
  352. $params = array(
  353. '/home/amelo/dev/cake-common/cake/console/cake.php',
  354. '-root',
  355. '/home/amelo/dev/lsbu-vacancy',
  356. '-working',
  357. '/home/amelo/dev/lsbu-vacancy',
  358. '-app',
  359. 'app',
  360. );
  361. $expected = array(
  362. 'app' => 'app',
  363. 'webroot' => 'webroot',
  364. 'working' => '/home/amelo/dev/lsbu-vacancy/app',
  365. 'root' => '/home/amelo/dev/lsbu-vacancy',
  366. );
  367. $Dispatcher->params = $Dispatcher->args = array();
  368. $Dispatcher->parseParams($params);
  369. $this->assertEqual($expected, $Dispatcher->params);
  370. }
  371. /**
  372. * testBuildPaths method
  373. *
  374. * @access public
  375. * @return void
  376. */
  377. function testBuildPaths() {
  378. $Dispatcher =& new TestShellDispatcher();
  379. $result = $Dispatcher->shellPaths;
  380. $expected = array(
  381. TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'test_plugin' . DS . 'vendors' . DS . 'shells' . DS,
  382. TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'test_plugin_two' . DS . 'vendors' . DS . 'shells' . DS,
  383. APP . 'vendors' . DS . 'shells' . DS,
  384. VENDORS . 'shells' . DS,
  385. CORE_PATH ? CONSOLE_LIBS : ROOT . DS . CONSOLE_LIBS,
  386. TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors' . DS . 'shells' . DS,
  387. );
  388. $this->assertIdentical(array_diff($result, $expected), array());
  389. $this->assertIdentical(array_diff($expected, $result), array());
  390. }
  391. /**
  392. * testDispatch method
  393. *
  394. * @access public
  395. * @return void
  396. */
  397. function testDispatch() {
  398. $Dispatcher =& new TestShellDispatcher(array('sample'));
  399. $this->assertPattern('/This is the main method called from SampleShell/', $Dispatcher->stdout);
  400. $Dispatcher =& new TestShellDispatcher(array('test_plugin_two.example'));
  401. $this->assertPattern('/This is the main method called from TestPluginTwo.ExampleShell/', $Dispatcher->stdout);
  402. $Dispatcher =& new TestShellDispatcher(array('test_plugin_two.welcome', 'say_hello'));
  403. $this->assertPattern('/This is the say_hello method called from TestPluginTwo.WelcomeShell/', $Dispatcher->stdout);
  404. }
  405. /**
  406. * testHelpCommand method
  407. *
  408. * @access public
  409. * @return void
  410. */
  411. function testHelpCommand() {
  412. $Dispatcher =& new TestShellDispatcher();
  413. $expected = "/ CORE(\\\|\/)tests(\\\|\/)test_app(\\\|\/)plugins(\\\|\/)test_plugin(\\\|\/)vendors(\\\|\/)shells:";
  414. $expected .= "\n\t example";
  415. $expected .= "\n/";
  416. $this->assertPattern($expected, $Dispatcher->stdout);
  417. $expected = "/ CORE(\\\|\/)tests(\\\|\/)test_app(\\\|\/)plugins(\\\|\/)test_plugin_two(\\\|\/)vendors(\\\|\/)shells:";
  418. $expected .= "\n\t example";
  419. $expected .= "\n\t welcome";
  420. $expected .= "\n/";
  421. $this->assertPattern($expected, $Dispatcher->stdout);
  422. $expected = "/ APP(\\\|\/)vendors(\\\|\/)shells:";
  423. $expected .= "\n/";
  424. $this->assertPattern($expected, $Dispatcher->stdout);
  425. $expected = "/ ROOT(\\\|\/)vendors(\\\|\/)shells:";
  426. $expected .= "\n/";
  427. $this->assertPattern($expected, $Dispatcher->stdout);
  428. $expected = "/ CORE(\\\|\/)console(\\\|\/)libs:";
  429. $expected .= "\n\t acl";
  430. $expected .= "\n\t api";
  431. $expected .= "\n\t bake";
  432. $expected .= "\n\t console";
  433. $expected .= "\n\t i18n";
  434. $expected .= "\n\t schema";
  435. $expected .= "\n\t testsuite";
  436. $expected .= "\n/";
  437. $this->assertPattern($expected, $Dispatcher->stdout);
  438. $expected = "/ CORE(\\\|\/)tests(\\\|\/)test_app(\\\|\/)vendors(\\\|\/)shells:";
  439. $expected .= "\n\t sample";
  440. $expected .= "\n/";
  441. $this->assertPattern($expected, $Dispatcher->stdout);
  442. }
  443. }
  444. ?>