PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/console/libs/tasks/project.test.php

https://github.com/msadouni/cakephp2x
PHP | 286 lines | 162 code | 32 blank | 92 comment | 3 complexity | 977ec29f17c31882dc74eb4dfe14eab4 MD5 | raw file
  1. <?php
  2. /**
  3. * ProjectTask Test file
  4. *
  5. * Test Case for project generation shell task
  6. *
  7. * PHP Version 5.x
  8. *
  9. * CakePHP : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2006-2009, Cake Software Foundation, Inc.
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2006-2009, Cake Software Foundation, Inc.
  16. * @link http://cakephp.org CakePHP Project
  17. * @package cake
  18. * @subpackage cake.tests.cases.console.libs.tasks
  19. * @since CakePHP v 1.3.0
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. App::import('Shell', 'Shell', false);
  23. if (!defined('DISABLE_AUTO_DISPATCH')) {
  24. define('DISABLE_AUTO_DISPATCH', true);
  25. }
  26. if (!class_exists('ShellDispatcher')) {
  27. ob_start();
  28. $argv = false;
  29. require CAKE . 'console' . DS . 'cake.php';
  30. ob_end_clean();
  31. }
  32. if (!class_exists('ProjectTask')) {
  33. require CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'project.php';
  34. }
  35. Mock::generatePartial(
  36. 'ShellDispatcher', 'TestProjectTaskMockShellDispatcher',
  37. array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
  38. );
  39. Mock::generatePartial(
  40. 'ProjectTask', 'MockProjectTask',
  41. array('in', '_stop', 'err', 'out', 'createFile')
  42. );
  43. /**
  44. * ProjectTask Test class
  45. *
  46. * @package cake
  47. * @subpackage cake.tests.cases.console.libs.tasks
  48. */
  49. class ProjectTaskTest extends CakeTestCase {
  50. /**
  51. * startTest method
  52. *
  53. * @return void
  54. * @access public
  55. */
  56. function startTest() {
  57. $this->Dispatcher =& new TestProjectTaskMockShellDispatcher();
  58. $this->Dispatcher->shellPaths = App::path('shells');
  59. $this->Task =& new MockProjectTask($this->Dispatcher);
  60. $this->Task->Dispatch =& $this->Dispatcher;
  61. $this->Task->path = TMP . 'tests' . DS;
  62. }
  63. /**
  64. * endTest method
  65. *
  66. * @return void
  67. * @access public
  68. */
  69. function endTest() {
  70. ClassRegistry::flush();
  71. $Folder =& new Folder($this->Task->path . 'bake_test_app');
  72. $Folder->delete();
  73. }
  74. /**
  75. * creates a test project that is used for testing project task.
  76. *
  77. * @return void
  78. * @access protected
  79. */
  80. function _setupTestProject() {
  81. $skel = CAKE_CORE_INCLUDE_PATH . DS . CAKE . 'console' . DS . 'templates' . DS . 'skel';
  82. $this->Task->setReturnValueAt(0, 'in', 'y');
  83. $this->Task->setReturnValueAt(1, 'in', 'n');
  84. $this->Task->bake($this->Task->path . 'bake_test_app', $skel);
  85. }
  86. /**
  87. * test bake() method and directory creation.
  88. *
  89. * @return void
  90. * @access public
  91. */
  92. function testBake() {
  93. $this->_setupTestProject();
  94. $path = $this->Task->path . 'bake_test_app';
  95. $this->assertTrue(is_dir($path), 'No project dir %s');
  96. $this->assertTrue(is_dir($path . DS . 'controllers'), 'No controllers dir %s');
  97. $this->assertTrue(is_dir($path . DS . 'controllers' . DS .'components'), 'No components dir %s');
  98. $this->assertTrue(is_dir($path . DS . 'models'), 'No models dir %s');
  99. $this->assertTrue(is_dir($path . DS . 'views'), 'No views dir %s');
  100. $this->assertTrue(is_dir($path . DS . 'views' . DS . 'helpers'), 'No helpers dir %s');
  101. $this->assertTrue(is_dir($path . DS . 'tests'), 'No tests dir %s');
  102. $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'cases'), 'No cases dir %s');
  103. $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'groups'), 'No groups dir %s');
  104. $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'fixtures'), 'No fixtures dir %s');
  105. }
  106. /**
  107. * test bake() method with -empty flag, directory creation and empty files.
  108. *
  109. * @return void
  110. * @access public
  111. */
  112. function testBakeEmptyFlag() {
  113. $this->Task->params['empty'] = true;
  114. $this->_setupTestProject();
  115. $path = $this->Task->path . 'bake_test_app';
  116. $this->assertTrue(is_dir($path), 'No project dir %s');
  117. $this->assertTrue(is_dir($path . DS . 'controllers'), 'No controllers dir %s');
  118. $this->assertTrue(is_dir($path . DS . 'controllers' . DS .'components'), 'No components dir %s');
  119. $this->assertTrue(is_dir($path . DS . 'models'), 'No models dir %s');
  120. $this->assertTrue(is_dir($path . DS . 'views'), 'No views dir %s');
  121. $this->assertTrue(is_dir($path . DS . 'views' . DS . 'helpers'), 'No helpers dir %s');
  122. $this->assertTrue(is_dir($path . DS . 'tests'), 'No tests dir %s');
  123. $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'cases'), 'No cases dir %s');
  124. $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'groups'), 'No groups dir %s');
  125. $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'fixtures'), 'No fixtures dir %s');
  126. $this->assertTrue(is_file($path . DS . 'controllers' . DS .'components' . DS . 'empty'), 'No empty file in dir %s');
  127. $this->assertTrue(is_file($path . DS . 'locale' . DS . 'eng' . DS . 'LC_MESSAGES' . DS . 'empty'), 'No empty file in dir %s');
  128. $this->assertTrue(is_file($path . DS . 'models' . DS . 'behaviors' . DS . 'empty'), 'No empty file in dir %s');
  129. $this->assertTrue(is_file($path . DS . 'models' . DS . 'datasources' . DS . 'empty'), 'No empty file in dir %s');
  130. $this->assertTrue(is_file($path . DS . 'plugins' . DS . 'empty'), 'No empty file in dir %s');
  131. $this->assertTrue(is_file($path . DS . 'tests' . DS . 'cases' . DS . 'behaviors' . DS . 'empty'), 'No empty file in dir %s');
  132. $this->assertTrue(is_file($path . DS . 'tests' . DS . 'cases' . DS . 'components' . DS . 'empty'), 'No empty file in dir %s');
  133. $this->assertTrue(is_file($path . DS . 'tests' . DS . 'cases' . DS . 'controllers' . DS . 'empty'), 'No empty file in dir %s');
  134. $this->assertTrue(is_file($path . DS . 'tests' . DS . 'cases' . DS . 'datasources' . DS . 'empty'), 'No empty file in dir %s');
  135. $this->assertTrue(is_file($path . DS . 'tests' . DS . 'cases' . DS . 'helpers' . DS . 'empty'), 'No empty file in dir %s');
  136. $this->assertTrue(is_file($path . DS . 'tests' . DS . 'cases' . DS . 'models' . DS . 'empty'), 'No empty file in dir %s');
  137. $this->assertTrue(is_file($path . DS . 'tests' . DS . 'cases' . DS . 'shells' . DS . 'empty'), 'No empty file in dir %s');
  138. $this->assertTrue(is_file($path . DS . 'tests' . DS . 'fixtures' . DS . 'empty'), 'No empty file in dir %s');
  139. $this->assertTrue(is_file($path . DS . 'tests' . DS . 'groups' . DS . 'empty'), 'No empty file in dir %s');
  140. $this->assertTrue(is_file($path . DS . 'vendors' . DS . 'shells' . DS . 'tasks' . DS . 'empty'), 'No empty file in dir %s');
  141. $this->assertTrue(is_file($path . DS . 'views' . DS . 'errors' . DS . 'empty'), 'No empty file in dir %s');
  142. $this->assertTrue(is_file($path . DS . 'views' . DS . 'helpers' . DS . 'empty'), 'No empty file in dir %s');
  143. $this->assertTrue(is_file($path . DS . 'views' . DS . 'scaffolds' . DS . 'empty'), 'No empty file in dir %s');
  144. $this->assertTrue(is_file($path . DS . 'webroot' . DS . 'js' . DS . 'empty'), 'No empty file in dir %s');
  145. }
  146. /**
  147. * test generation of Security.salt
  148. *
  149. * @return void
  150. * @access public
  151. */
  152. function testSecuritySaltGeneration() {
  153. $this->_setupTestProject();
  154. $path = $this->Task->path . 'bake_test_app' . DS;
  155. $result = $this->Task->securitySalt($path);
  156. $this->assertTrue($result);
  157. $file =& new File($path . 'config' . DS . 'core.php');
  158. $contents = $file->read();
  159. $this->assertNoPattern('/DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi/', $contents, 'Default Salt left behind. %s');
  160. }
  161. /**
  162. * Test that index.php is generated correctly.
  163. *
  164. * @return void
  165. * @access public
  166. */
  167. function testIndexPhpGeneration() {
  168. $this->_setupTestProject();
  169. $path = $this->Task->path . 'bake_test_app' . DS;
  170. $this->Task->corePath($path);
  171. $file =& new File($path . 'webroot' . DS . 'index.php');
  172. $contents = $file->read();
  173. $this->assertNoPattern('/define\(\'CAKE_CORE_INCLUDE_PATH\', \'ROOT/', $contents);
  174. $file =& new File($path . 'webroot' . DS . 'test.php');
  175. $contents = $file->read();
  176. $this->assertNoPattern('/define\(\'CAKE_CORE_INCLUDE_PATH\', \'ROOT/', $contents);
  177. }
  178. /**
  179. * test getPrefix method, and that it returns Routing.prefix or writes to config file.
  180. *
  181. * @return void
  182. * @access public
  183. */
  184. function testGetPrefix() {
  185. Configure::write('Routing.prefixes', array('admin'));
  186. $result = $this->Task->getPrefix();
  187. $this->assertEqual($result, 'admin_');
  188. Configure::write('Routing.prefixes', null);
  189. $this->_setupTestProject();
  190. $this->Task->configPath = $this->Task->path . 'bake_test_app' . DS . 'config' . DS;
  191. $this->Task->setReturnValue('in', 'super_duper_admin');
  192. $result = $this->Task->getPrefix();
  193. $this->assertEqual($result, 'super_duper_admin_');
  194. $file =& new File($this->Task->configPath . 'core.php');
  195. $file->delete();
  196. }
  197. /**
  198. * test cakeAdmin() writing core.php
  199. *
  200. * @return void
  201. * @access public
  202. */
  203. function testCakeAdmin() {
  204. $file =& new File(CONFIGS . 'core.php');
  205. $contents = $file->read();;
  206. $file =& new File(TMP . 'tests' . DS . 'core.php');
  207. $file->write($contents);
  208. Configure::write('Routing.prefixes', null);
  209. $this->Task->configPath = TMP . 'tests' . DS;
  210. $result = $this->Task->cakeAdmin('my_prefix');
  211. $this->assertTrue($result);
  212. $this->assertEqual(Configure::read('Routing.prefixes'), array('my_prefix'));
  213. $file->delete();
  214. }
  215. /**
  216. * test getting the prefix with more than one prefix setup
  217. *
  218. * @return void
  219. * @access public
  220. */
  221. function testGetPrefixWithMultiplePrefixes() {
  222. Configure::write('Routing.prefixes', array('admin', 'ninja', 'shinobi'));
  223. $this->_setupTestProject();
  224. $this->Task->configPath = $this->Task->path . 'bake_test_app' . DS . 'config' . DS;
  225. $this->Task->setReturnValue('in', 2);
  226. $result = $this->Task->getPrefix();
  227. $this->assertEqual($result, 'ninja_');
  228. }
  229. /**
  230. * Test execute method with one param to destination folder.
  231. *
  232. * @return void
  233. * @access public
  234. */
  235. function testExecute() {
  236. $this->Task->params['skel'] = CAKE_CORE_INCLUDE_PATH . DS . CAKE . DS . 'console' . DS. 'templates' . DS . 'skel';
  237. $this->Task->params['working'] = TMP . 'tests' . DS;
  238. $path = $this->Task->path . 'bake_test_app';
  239. $this->Task->setReturnValue('in', 'y');
  240. $this->Task->setReturnValueAt(0, 'in', $path);
  241. $this->Task->execute();
  242. $this->assertTrue(is_dir($path), 'No project dir %s');
  243. $this->assertTrue(is_dir($path . DS . 'controllers'), 'No controllers dir %s');
  244. $this->assertTrue(is_dir($path . DS . 'controllers' . DS .'components'), 'No components dir %s');
  245. $this->assertTrue(is_dir($path . DS . 'models'), 'No models dir %s');
  246. $this->assertTrue(is_dir($path . DS . 'views'), 'No views dir %s');
  247. $this->assertTrue(is_dir($path . DS . 'views' . DS . 'helpers'), 'No helpers dir %s');
  248. $this->assertTrue(is_dir($path . DS . 'tests'), 'No tests dir %s');
  249. $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'cases'), 'No cases dir %s');
  250. $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'groups'), 'No groups dir %s');
  251. $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'fixtures'), 'No fixtures dir %s');
  252. }
  253. }
  254. ?>