ProjectTaskTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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.tests.cases.console.libs.tasks
  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.tests.cases.console.libs.tasks
  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 . 'Command' . DS . 'Task',
  85. 'Controller',
  86. 'Model',
  87. 'View',
  88. 'View' . DS . 'Helper',
  89. 'Test',
  90. 'Test' . DS . 'Case',
  91. 'Test' . DS . 'Case' . DS . 'Model',
  92. 'Test' . DS . 'Fixture',
  93. 'tmp',
  94. 'webroot',
  95. 'webroot' . DS . 'js',
  96. 'webroot' . DS . 'css',
  97. );
  98. foreach ($dirs as $dir) {
  99. $this->assertTrue(is_dir($path . DS . $dir), 'Missing ' . $dir);
  100. }
  101. }
  102. /**
  103. * test bake with an absolute path.
  104. *
  105. * @return void
  106. */
  107. public function testExecuteWithAbsolutePath() {
  108. $this->Task->args[0] = TMP . 'tests' . DS . 'bake_test_app';
  109. $this->Task->params['skel'] = CAKE . 'Console' . DS . 'Templates' . DS . 'skel';
  110. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  111. $this->Task->execute();
  112. $this->assertTrue(is_dir($this->Task->args[0]), 'No project dir');
  113. }
  114. /**
  115. * test bake() method with -empty flag, directory creation and empty files.
  116. *
  117. * @return void
  118. */
  119. public function testBakeEmptyFlag() {
  120. $this->Task->params['empty'] = true;
  121. $this->_setupTestProject();
  122. $path = $this->Task->path . 'bake_test_app';
  123. $empty = array(
  124. 'Console' . DS . 'Command' . DS . 'Task',
  125. 'Controller' . DS . 'Component',
  126. 'Model' . DS . 'Behavior',
  127. 'View' . DS . 'Helper',
  128. 'View' . DS . 'Errors',
  129. 'View' . DS . 'Scaffolds',
  130. 'Test' . DS . 'Case' . DS . 'Model',
  131. 'Test' . DS . 'Case' . DS . 'Controller',
  132. 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper',
  133. 'Test' . DS . 'Fixture',
  134. 'webroot' . DS . 'js'
  135. );
  136. foreach ($empty as $dir) {
  137. $this->assertTrue(is_file($path . DS . $dir . DS . 'empty'), 'Missing empty file in ' . $dir);
  138. }
  139. }
  140. /**
  141. * test generation of Security.salt
  142. *
  143. * @return void
  144. */
  145. public function testSecuritySaltGeneration() {
  146. $this->_setupTestProject();
  147. $path = $this->Task->path . 'bake_test_app' . DS;
  148. $result = $this->Task->securitySalt($path);
  149. $this->assertTrue($result);
  150. $file = new File($path . 'Config' . DS . 'core.php');
  151. $contents = $file->read();
  152. $this->assertNoPattern('/DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi/', $contents, 'Default Salt left behind. %s');
  153. }
  154. /**
  155. * test generation of Security.cipherSeed
  156. *
  157. * @return void
  158. */
  159. public function testSecurityCipherSeedGeneration() {
  160. $this->_setupTestProject();
  161. $path = $this->Task->path . 'bake_test_app' . DS;
  162. $result = $this->Task->securityCipherSeed($path);
  163. $this->assertTrue($result);
  164. $file = new File($path . 'Config' . DS . 'core.php');
  165. $contents = $file->read();
  166. $this->assertNoPattern('/76859309657453542496749683645/', $contents, 'Default CipherSeed left behind. %s');
  167. }
  168. /**
  169. * Test that index.php is generated correctly.
  170. *
  171. * @return void
  172. */
  173. public function testIndexPhpGeneration() {
  174. $this->_setupTestProject();
  175. $path = $this->Task->path . 'bake_test_app' . DS;
  176. $this->Task->corePath($path);
  177. $file = new File($path . 'webroot' . DS . 'index.php');
  178. $contents = $file->read();
  179. $this->assertNoPattern('/define\(\'CAKE_CORE_INCLUDE_PATH\', \'ROOT/', $contents);
  180. $file = new File($path . 'webroot' . DS . 'test.php');
  181. $contents = $file->read();
  182. $this->assertNoPattern('/define\(\'CAKE_CORE_INCLUDE_PATH\', \'ROOT/', $contents);
  183. }
  184. /**
  185. * test getPrefix method, and that it returns Routing.prefix or writes to config file.
  186. *
  187. * @return void
  188. */
  189. public function testGetPrefix() {
  190. Configure::write('Routing.prefixes', array('admin'));
  191. $result = $this->Task->getPrefix();
  192. $this->assertEqual($result, 'admin_');
  193. Configure::write('Routing.prefixes', null);
  194. $this->_setupTestProject();
  195. $this->Task->configPath = $this->Task->path . 'bake_test_app' . DS . 'Config' . DS;
  196. $this->Task->expects($this->once())->method('in')->will($this->returnValue('super_duper_admin'));
  197. $result = $this->Task->getPrefix();
  198. $this->assertEqual($result, 'super_duper_admin_');
  199. $file = new File($this->Task->configPath . 'core.php');
  200. $file->delete();
  201. }
  202. /**
  203. * test cakeAdmin() writing core.php
  204. *
  205. * @return void
  206. */
  207. public function testCakeAdmin() {
  208. $file = new File(APP . 'Config' . DS . 'core.php');
  209. $contents = $file->read();;
  210. $file = new File(TMP . 'tests' . DS . 'core.php');
  211. $file->write($contents);
  212. Configure::write('Routing.prefixes', null);
  213. $this->Task->configPath = TMP . 'tests' . DS;
  214. $result = $this->Task->cakeAdmin('my_prefix');
  215. $this->assertTrue($result);
  216. $this->assertEqual(Configure::read('Routing.prefixes'), array('my_prefix'));
  217. $file->delete();
  218. }
  219. /**
  220. * test getting the prefix with more than one prefix setup
  221. *
  222. * @return void
  223. */
  224. public function testGetPrefixWithMultiplePrefixes() {
  225. Configure::write('Routing.prefixes', array('admin', 'ninja', 'shinobi'));
  226. $this->_setupTestProject();
  227. $this->Task->configPath = $this->Task->path . 'bake_test_app' . DS . 'Config' . DS;
  228. $this->Task->expects($this->once())->method('in')->will($this->returnValue(2));
  229. $result = $this->Task->getPrefix();
  230. $this->assertEqual($result, 'ninja_');
  231. }
  232. /**
  233. * Test execute method with one param to destination folder.
  234. *
  235. * @return void
  236. */
  237. public function testExecute() {
  238. $this->Task->params['skel'] = CAKE . 'Console' . DS. 'Templates' . DS . 'skel';
  239. $this->Task->params['working'] = TMP . 'tests' . DS;
  240. $path = $this->Task->path . 'bake_test_app';
  241. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue($path));
  242. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue('y'));
  243. $this->Task->execute();
  244. $this->assertTrue(is_dir($path), 'No project dir');
  245. $this->assertTrue(is_dir($path . DS . 'Controller'), 'No controllers dir ');
  246. $this->assertTrue(is_dir($path . DS . 'Controller' . DS .'Component'), 'No components dir ');
  247. $this->assertTrue(is_dir($path . DS . 'Model'), 'No models dir');
  248. $this->assertTrue(is_dir($path . DS . 'View'), 'No views dir');
  249. $this->assertTrue(is_dir($path . DS . 'View' . DS . 'Helper'), 'No helpers dir');
  250. $this->assertTrue(is_dir($path . DS . 'Test'), 'No tests dir');
  251. $this->assertTrue(is_dir($path . DS . 'Test' . DS . 'Case'), 'No cases dir');
  252. $this->assertTrue(is_dir($path . DS . 'Test' . DS . 'Fixture'), 'No fixtures dir');
  253. }
  254. /**
  255. * test console path
  256. *
  257. * @return void
  258. */
  259. public function testConsolePath() {
  260. $this->_setupTestProject();
  261. $path = $this->Task->path . 'bake_test_app' . DS;
  262. $result = $this->Task->consolePath($path);
  263. $this->assertTrue($result);
  264. $file = new File($path . 'Console' . DS . 'cake.php');
  265. $contents = $file->read();
  266. $this->assertNoPattern('/__CAKE_PATH__/', $contents, 'Console path placeholder left behind.');
  267. }
  268. }