ProjectTaskTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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-2011, 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-2011, 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 . 'Errors' => 'empty',
  184. 'View' . DS . 'Scaffolds' => 'empty',
  185. 'tmp' . DS . 'cache' . DS . 'models' => 'empty',
  186. 'tmp' . DS . 'cache' . DS . 'persistent' => 'empty',
  187. 'tmp' . DS . 'cache' . DS . 'views' => 'empty',
  188. 'tmp' . DS . 'logs' => 'empty',
  189. 'tmp' . DS . 'sessions' => 'empty',
  190. 'tmp' . DS . 'tests' => 'empty',
  191. 'webroot' . DS . 'js' => 'empty',
  192. 'webroot' . DS . 'files' => 'empty'
  193. );
  194. foreach ($empty as $dir => $file) {
  195. $this->assertTrue(is_file($path . DS . $dir . DS . $file), sprintf('Missing %s file in %s', $file, $dir));
  196. }
  197. }
  198. /**
  199. * test generation of Security.salt
  200. *
  201. * @return void
  202. */
  203. public function testSecuritySaltGeneration() {
  204. $this->_setupTestProject();
  205. $path = $this->Task->path . 'bake_test_app' . DS;
  206. $result = $this->Task->securitySalt($path);
  207. $this->assertTrue($result);
  208. $File = new File($path . 'Config' . DS . 'core.php');
  209. $contents = $File->read();
  210. $this->assertNotRegExp('/DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi/', $contents, 'Default Salt left behind. %s');
  211. }
  212. /**
  213. * test generation of Security.cipherSeed
  214. *
  215. * @return void
  216. */
  217. public function testSecurityCipherSeedGeneration() {
  218. $this->_setupTestProject();
  219. $path = $this->Task->path . 'bake_test_app' . DS;
  220. $result = $this->Task->securityCipherSeed($path);
  221. $this->assertTrue($result);
  222. $File = new File($path . 'Config' . DS . 'core.php');
  223. $contents = $File->read();
  224. $this->assertNotRegExp('/76859309657453542496749683645/', $contents, 'Default CipherSeed left behind. %s');
  225. }
  226. /**
  227. * Test that index.php is generated correctly.
  228. *
  229. * @return void
  230. */
  231. public function testIndexPhpGeneration() {
  232. $this->_setupTestProject();
  233. $path = $this->Task->path . 'bake_test_app' . DS;
  234. $this->Task->corePath($path);
  235. $File = new File($path . 'webroot' . DS . 'index.php');
  236. $contents = $File->read();
  237. $this->assertNotRegExp('/define\(\'CAKE_CORE_INCLUDE_PATH\', ROOT/', $contents);
  238. $File = new File($path . 'webroot' . DS . 'test.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($result, 'admin_');
  251. Configure::write('Routing.prefixes', null);
  252. $this->_setupTestProject();
  253. $this->Task->configPath = $this->Task->path . 'bake_test_app' . DS . 'Config' . DS;
  254. $this->Task->expects($this->once())->method('in')->will($this->returnValue('super_duper_admin'));
  255. $result = $this->Task->getPrefix();
  256. $this->assertEquals($result, 'super_duper_admin_');
  257. $File = new File($this->Task->configPath . 'core.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' . DS . 'core.php');
  267. $contents = $File->read();
  268. $File = new File(TMP . 'tests' . DS . 'core.php');
  269. $File->write($contents);
  270. Configure::write('Routing.prefixes', null);
  271. $this->Task->configPath = TMP . 'tests' . DS;
  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' . DS . 'Config' . DS;
  286. $this->Task->expects($this->once())->method('in')->will($this->returnValue(2));
  287. $result = $this->Task->getPrefix();
  288. $this->assertEquals($result, 'ninja_');
  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' . DS . 'Templates' . DS . 'skel';
  297. $this->Task->params['working'] = TMP . 'tests' . DS;
  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' . DS .'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' . DS . 'Helper'), 'No helpers dir');
  308. $this->assertTrue(is_dir($path . DS . 'Test'), 'No tests dir');
  309. $this->assertTrue(is_dir($path . DS . 'Test' . DS . 'Case'), 'No cases dir');
  310. $this->assertTrue(is_dir($path . DS . 'Test' . DS . 'Fixture'), 'No fixtures dir');
  311. }
  312. /**
  313. * test console path
  314. *
  315. * @return void
  316. */
  317. public function testConsolePath() {
  318. $this->_setupTestProject();
  319. $path = $this->Task->path . 'bake_test_app' . DS;
  320. $result = $this->Task->consolePath($path);
  321. $this->assertTrue($result);
  322. $File = new File($path . 'Console' . DS . 'cake.php');
  323. $contents = $File->read();
  324. $this->assertNotRegExp('/__CAKE_PATH__/', $contents, 'Console path placeholder left behind.');
  325. }
  326. }