PageRenderTime 54ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

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

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