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

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

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