PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/Console/Command/Task/ProjectTaskTest.php

https://gitlab.com/manuperazafa/elsartenbackend
PHP | 386 lines | 236 code | 47 blank | 103 comment | 1 complexity | e3fb89115a3383f0ab2b806bd9b521a5 MD5 | raw file
  1. <?php
  2. /**
  3. * ProjectTask Test file
  4. *
  5. * Test Case for project generation shell task
  6. *
  7. * CakePHP : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP Project
  16. * @package Cake.Test.Case.Console.Command.Task
  17. * @since CakePHP v 1.3.0
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('ShellDispatcher', 'Console');
  21. App::uses('ConsoleOutput', 'Console');
  22. App::uses('ConsoleInput', 'Console');
  23. App::uses('Shell', 'Console');
  24. App::uses('ProjectTask', 'Console/Command/Task');
  25. App::uses('Folder', 'Utility');
  26. App::uses('File', 'Utility');
  27. /**
  28. * ProjectTask Test class
  29. *
  30. * @package Cake.Test.Case.Console.Command.Task
  31. */
  32. class ProjectTaskTest extends CakeTestCase {
  33. /**
  34. * setUp method
  35. *
  36. * @return void
  37. */
  38. public function setUp() {
  39. parent::setUp();
  40. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  41. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  42. $this->Task = $this->getMock('ProjectTask',
  43. array('in', 'err', 'createFile', '_stop'),
  44. array($out, $out, $in)
  45. );
  46. $this->Task->path = TMP . 'tests' . DS;
  47. }
  48. /**
  49. * tearDown method
  50. *
  51. * @return void
  52. */
  53. public function tearDown() {
  54. parent::tearDown();
  55. $Folder = new Folder($this->Task->path . 'bake_test_app');
  56. $Folder->delete();
  57. unset($this->Task);
  58. }
  59. /**
  60. * creates a test project that is used for testing project task.
  61. *
  62. * @return void
  63. */
  64. protected function _setupTestProject() {
  65. $skel = CAKE . 'Console' . DS . 'Templates' . DS . 'skel';
  66. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  67. $this->Task->bake($this->Task->path . 'bake_test_app', $skel);
  68. }
  69. /**
  70. * test bake() method and directory creation.
  71. *
  72. * @return void
  73. */
  74. public function testBake() {
  75. $this->_setupTestProject();
  76. $path = $this->Task->path . 'bake_test_app';
  77. $this->assertTrue(is_dir($path), 'No project dir %s');
  78. $dirs = array(
  79. 'Config',
  80. 'Config' . DS . 'Schema',
  81. 'Console',
  82. 'Console' . DS . 'Command',
  83. 'Console' . DS . 'Templates',
  84. 'Console' . DS . 'Command' . DS . 'Task',
  85. 'Controller',
  86. 'Controller' . DS . 'Component',
  87. 'Locale',
  88. 'Model',
  89. 'Model' . DS . 'Behavior',
  90. 'Model' . DS . 'Datasource',
  91. 'Plugin',
  92. 'Test',
  93. 'Test' . DS . 'Case',
  94. 'Test' . DS . 'Case' . DS . 'Controller',
  95. 'Test' . DS . 'Case' . DS . 'Controller' . DS . 'Component',
  96. 'Test' . DS . 'Case' . DS . 'Model',
  97. 'Test' . DS . 'Case' . DS . 'Model' . DS . 'Behavior',
  98. 'Test' . DS . 'Fixture',
  99. 'Vendor',
  100. 'View',
  101. 'View' . DS . 'Helper',
  102. 'tmp',
  103. 'tmp' . DS . 'cache',
  104. 'tmp' . DS . 'cache' . DS . 'models',
  105. 'tmp' . DS . 'cache' . DS . 'persistent',
  106. 'tmp' . DS . 'cache' . DS . 'views',
  107. 'tmp' . DS . 'logs',
  108. 'tmp' . DS . 'sessions',
  109. 'tmp' . DS . 'tests',
  110. 'webroot',
  111. 'webroot' . DS . 'css',
  112. 'webroot' . DS . 'files',
  113. 'webroot' . DS . 'img',
  114. 'webroot' . DS . 'js',
  115. );
  116. foreach ($dirs as $dir) {
  117. $this->assertTrue(is_dir($path . DS . $dir), 'Missing ' . $dir);
  118. }
  119. }
  120. /**
  121. * test bake with an absolute path.
  122. *
  123. * @return void
  124. */
  125. public function testExecuteWithAbsolutePath() {
  126. $path = $this->Task->args[0] = TMP . 'tests' . DS . 'bake_test_app';
  127. $this->Task->params['skel'] = CAKE . 'Console' . DS . 'Templates' . DS . 'skel';
  128. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  129. $this->Task->execute();
  130. $this->assertTrue(is_dir($this->Task->args[0]), 'No project dir');
  131. $File = new File($path . DS . 'webroot' . DS . 'index.php');
  132. $contents = $File->read();
  133. $this->assertRegExp('/define\(\'CAKE_CORE_INCLUDE_PATH\', .*?DS/', $contents);
  134. $File = new File($path . DS . 'webroot' . DS . 'test.php');
  135. $contents = $File->read();
  136. $this->assertRegExp('/define\(\'CAKE_CORE_INCLUDE_PATH\', .*?DS/', $contents);
  137. }
  138. /**
  139. * test bake with CakePHP on the include path. The constants should remain commented out.
  140. *
  141. * @return void
  142. */
  143. public function testExecuteWithCakeOnIncludePath() {
  144. if (!function_exists('ini_set')) {
  145. $this->markTestAsSkipped('Not access to ini_set, cannot proceed.');
  146. }
  147. $restore = ini_get('include_path');
  148. ini_set('include_path', CAKE_CORE_INCLUDE_PATH . PATH_SEPARATOR . $restore);
  149. $path = $this->Task->args[0] = TMP . 'tests' . DS . 'bake_test_app';
  150. $this->Task->params['skel'] = CAKE . 'Console' . DS . 'Templates' . DS . 'skel';
  151. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  152. $this->Task->execute();
  153. $this->assertTrue(is_dir($this->Task->args[0]), 'No project dir');
  154. $contents = file_get_contents($path . DS . 'webroot' . DS . 'index.php');
  155. $this->assertRegExp('#//define\(\'CAKE_CORE_INCLUDE_PATH#', $contents);
  156. $contents = file_get_contents($path . DS . 'webroot' . DS . 'test.php');
  157. $this->assertRegExp('#//define\(\'CAKE_CORE_INCLUDE_PATH#', $contents);
  158. ini_set('include_path', $restore);
  159. }
  160. /**
  161. * test bake() method with -empty flag, directory creation and empty files.
  162. *
  163. * @return void
  164. */
  165. public function testBakeEmptyFlag() {
  166. $this->Task->params['empty'] = true;
  167. $this->_setupTestProject();
  168. $path = $this->Task->path . 'bake_test_app';
  169. $empty = array(
  170. 'Console' . DS . 'Command' . DS . 'Task' => 'empty',
  171. 'Controller' . DS . 'Component' => 'empty',
  172. 'Lib' => 'empty',
  173. 'Model' . DS . 'Behavior' => 'empty',
  174. 'Model' . DS . 'Datasource' => 'empty',
  175. 'Plugin' => 'empty',
  176. 'Test' . DS . 'Case' . DS . 'Model' . DS . 'Behavior' => 'empty',
  177. 'Test' . DS . 'Case' . DS . 'Controller' . DS . 'Component' => 'empty',
  178. 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper' => 'empty',
  179. 'Test' . DS . 'Fixture' => 'empty',
  180. 'Vendor' => 'empty',
  181. 'View' . DS . 'Elements' => 'empty',
  182. 'View' . DS . 'Scaffolds' => 'empty',
  183. 'tmp' . DS . 'cache' . DS . 'models' => 'empty',
  184. 'tmp' . DS . 'cache' . DS . 'persistent' => 'empty',
  185. 'tmp' . DS . 'cache' . DS . 'views' => 'empty',
  186. 'tmp' . DS . 'logs' => 'empty',
  187. 'tmp' . DS . 'sessions' => 'empty',
  188. 'tmp' . DS . 'tests' => 'empty',
  189. 'webroot' . DS . 'js' => 'empty',
  190. 'webroot' . DS . 'files' => 'empty'
  191. );
  192. foreach ($empty as $dir => $file) {
  193. $this->assertTrue(is_file($path . DS . $dir . DS . $file), sprintf('Missing %s file in %s', $file, $dir));
  194. }
  195. }
  196. /**
  197. * test generation of Security.salt
  198. *
  199. * @return void
  200. */
  201. public function testSecuritySaltGeneration() {
  202. $this->_setupTestProject();
  203. $path = $this->Task->path . 'bake_test_app' . DS;
  204. $result = $this->Task->securitySalt($path);
  205. $this->assertTrue($result);
  206. $File = new File($path . 'Config' . DS . 'core.php');
  207. $contents = $File->read();
  208. $this->assertNotRegExp('/DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi/', $contents, 'Default Salt left behind. %s');
  209. }
  210. /**
  211. * test generation of Security.cipherSeed
  212. *
  213. * @return void
  214. */
  215. public function testSecurityCipherSeedGeneration() {
  216. $this->_setupTestProject();
  217. $path = $this->Task->path . 'bake_test_app' . DS;
  218. $result = $this->Task->securityCipherSeed($path);
  219. $this->assertTrue($result);
  220. $File = new File($path . 'Config' . DS . 'core.php');
  221. $contents = $File->read();
  222. $this->assertNotRegExp('/76859309657453542496749683645/', $contents, 'Default CipherSeed left behind. %s');
  223. }
  224. /**
  225. * test generation of cache prefix
  226. *
  227. * @return void
  228. */
  229. public function testCachePrefixGeneration() {
  230. $this->_setupTestProject();
  231. $path = $this->Task->path . 'bake_test_app' . DS;
  232. $result = $this->Task->cachePrefix($path);
  233. $this->assertTrue($result);
  234. $File = new File($path . 'Config' . DS . 'core.php');
  235. $contents = $File->read();
  236. $this->assertRegExp('/\$prefix = \'.+\';/', $contents, '$prefix is not defined');
  237. $this->assertNotRegExp('/\$prefix = \'myapp_\';/', $contents, 'Default cache prefix left behind. %s');
  238. }
  239. /**
  240. * Test that index.php is generated correctly.
  241. *
  242. * @return void
  243. */
  244. public function testIndexPhpGeneration() {
  245. $this->_setupTestProject();
  246. $path = $this->Task->path . 'bake_test_app' . DS;
  247. $this->Task->corePath($path);
  248. $File = new File($path . 'webroot' . DS . 'index.php');
  249. $contents = $File->read();
  250. $this->assertNotRegExp('/define\(\'CAKE_CORE_INCLUDE_PATH\', ROOT/', $contents);
  251. $File = new File($path . 'webroot' . DS . 'test.php');
  252. $contents = $File->read();
  253. $this->assertNotRegExp('/define\(\'CAKE_CORE_INCLUDE_PATH\', ROOT/', $contents);
  254. }
  255. /**
  256. * test getPrefix method, and that it returns Routing.prefix or writes to config file.
  257. *
  258. * @return void
  259. */
  260. public function testGetPrefix() {
  261. Configure::write('Routing.prefixes', array('admin'));
  262. $result = $this->Task->getPrefix();
  263. $this->assertEquals('admin_', $result);
  264. Configure::write('Routing.prefixes', null);
  265. $this->_setupTestProject();
  266. $this->Task->configPath = $this->Task->path . 'bake_test_app' . DS . 'Config' . DS;
  267. $this->Task->expects($this->once())->method('in')->will($this->returnValue('super_duper_admin'));
  268. $result = $this->Task->getPrefix();
  269. $this->assertEquals('super_duper_admin_', $result);
  270. $File = new File($this->Task->configPath . 'core.php');
  271. $File->delete();
  272. }
  273. /**
  274. * test cakeAdmin() writing core.php
  275. *
  276. * @return void
  277. */
  278. public function testCakeAdmin() {
  279. $File = new File(APP . 'Config' . DS . 'core.php');
  280. $contents = $File->read();
  281. $File = new File(TMP . 'tests' . DS . 'core.php');
  282. $File->write($contents);
  283. Configure::write('Routing.prefixes', null);
  284. $this->Task->configPath = TMP . 'tests' . DS;
  285. $result = $this->Task->cakeAdmin('my_prefix');
  286. $this->assertTrue($result);
  287. $this->assertEquals(Configure::read('Routing.prefixes'), array('my_prefix'));
  288. $File->delete();
  289. }
  290. /**
  291. * test getting the prefix with more than one prefix setup
  292. *
  293. * @return void
  294. */
  295. public function testGetPrefixWithMultiplePrefixes() {
  296. Configure::write('Routing.prefixes', array('admin', 'ninja', 'shinobi'));
  297. $this->_setupTestProject();
  298. $this->Task->configPath = $this->Task->path . 'bake_test_app' . DS . 'Config' . DS;
  299. $this->Task->expects($this->once())->method('in')->will($this->returnValue(2));
  300. $result = $this->Task->getPrefix();
  301. $this->assertEquals('ninja_', $result);
  302. }
  303. /**
  304. * Test execute method with one param to destination folder.
  305. *
  306. * @return void
  307. */
  308. public function testExecute() {
  309. $this->Task->params['skel'] = CAKE . 'Console' . DS . 'Templates' . DS . 'skel';
  310. $this->Task->params['working'] = TMP . 'tests' . DS;
  311. $path = $this->Task->path . 'bake_test_app';
  312. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue($path));
  313. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue('y'));
  314. $this->Task->execute();
  315. $this->assertTrue(is_dir($path), 'No project dir');
  316. $this->assertTrue(is_dir($path . DS . 'Controller'), 'No controllers dir ');
  317. $this->assertTrue(is_dir($path . DS . 'Controller' . DS . 'Component'), 'No components dir ');
  318. $this->assertTrue(is_dir($path . DS . 'Model'), 'No models dir');
  319. $this->assertTrue(is_dir($path . DS . 'View'), 'No views dir');
  320. $this->assertTrue(is_dir($path . DS . 'View' . DS . 'Helper'), 'No helpers dir');
  321. $this->assertTrue(is_dir($path . DS . 'Test'), 'No tests dir');
  322. $this->assertTrue(is_dir($path . DS . 'Test' . DS . 'Case'), 'No cases dir');
  323. $this->assertTrue(is_dir($path . DS . 'Test' . DS . 'Fixture'), 'No fixtures dir');
  324. }
  325. /**
  326. * test console path
  327. *
  328. * @return void
  329. */
  330. public function testConsolePath() {
  331. $this->_setupTestProject();
  332. $path = $this->Task->path . 'bake_test_app' . DS;
  333. $result = $this->Task->consolePath($path);
  334. $this->assertTrue($result);
  335. $File = new File($path . 'Console' . DS . 'cake.php');
  336. $contents = $File->read();
  337. $this->assertNotRegExp('/__CAKE_PATH__/', $contents, 'Console path placeholder left behind.');
  338. }
  339. }