ProjectTaskTest.php 8.3 KB

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