ProjectTaskTest.php 10 KB

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