PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/udeshika/fake_twitter
PHP | 339 lines | 197 code | 43 blank | 99 comment | 1 complexity | d9c5a6aa11af3ae5e57ad9839258e6d4 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-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 . '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. $path = $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. $File = new File($path . DS . 'webroot' . DS . 'index.php');
  114. $contents = $File->read();
  115. $this->assertRegExp('/define\(\'CAKE_CORE_INCLUDE_PATH\', .*?DS/', $contents);
  116. $File = new File($path . DS . 'webroot' . DS . 'test.php');
  117. $contents = $File->read();
  118. $this->assertRegExp('/define\(\'CAKE_CORE_INCLUDE_PATH\', .*?DS/', $contents);
  119. }
  120. /**
  121. * test bake with CakePHP on the include path. The constants should remain commented out.
  122. *
  123. * @return void
  124. */
  125. public function testExecuteWithCakeOnIncludePath() {
  126. if (!function_exists('ini_set')) {
  127. $this->markTestAsSkipped('Not access to ini_set, cannot proceed.');
  128. }
  129. $restore = ini_get('include_path');
  130. ini_set('include_path', CAKE_CORE_INCLUDE_PATH . PATH_SEPARATOR . $restore);
  131. $path = $this->Task->args[0] = TMP . 'tests' . DS . 'bake_test_app';
  132. $this->Task->params['skel'] = CAKE . 'Console' . DS . 'Templates' . DS . 'skel';
  133. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  134. $this->Task->execute();
  135. $this->assertTrue(is_dir($this->Task->args[0]), 'No project dir');
  136. $contents = file_get_contents($path . DS . 'webroot' . DS . 'index.php');
  137. $this->assertRegExp('#//define\(\'CAKE_CORE_INCLUDE_PATH#', $contents);
  138. $contents = file_get_contents($path . DS . 'webroot' . DS . 'test.php');
  139. $this->assertRegExp('#//define\(\'CAKE_CORE_INCLUDE_PATH#', $contents);
  140. ini_set('include_path', $restore);
  141. }
  142. /**
  143. * test bake() method with -empty flag, directory creation and empty files.
  144. *
  145. * @return void
  146. */
  147. public function testBakeEmptyFlag() {
  148. $this->Task->params['empty'] = true;
  149. $this->_setupTestProject();
  150. $path = $this->Task->path . 'bake_test_app';
  151. $empty = array(
  152. 'Console' . DS . 'Command' . DS . 'Task' => 'empty',
  153. 'Controller' . DS . 'Component' => 'empty',
  154. 'Model' . DS . 'Behavior' => 'empty',
  155. 'View' . DS . 'Helper' => 'AppHelper.php',
  156. 'View' . DS . 'Errors' => 'empty',
  157. 'View' . DS . 'Scaffolds' => 'empty',
  158. 'Test' . DS . 'Case' . DS . 'Model' . DS . 'Behavior' => 'empty',
  159. 'Test' . DS . 'Case' . DS . 'Controller' . DS . 'Component' => 'empty',
  160. 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper' => 'empty',
  161. 'Test' . DS . 'Fixture' => 'empty',
  162. 'webroot' . DS . 'js' => 'empty'
  163. );
  164. foreach ($empty as $dir => $file) {
  165. $this->assertTrue(is_file($path . DS . $dir . DS . $file), sprintf('Missing %s file in %s', $file, $dir));
  166. }
  167. }
  168. /**
  169. * test generation of Security.salt
  170. *
  171. * @return void
  172. */
  173. public function testSecuritySaltGeneration() {
  174. $this->_setupTestProject();
  175. $path = $this->Task->path . 'bake_test_app' . DS;
  176. $result = $this->Task->securitySalt($path);
  177. $this->assertTrue($result);
  178. $File = new File($path . 'Config' . DS . 'core.php');
  179. $contents = $File->read();
  180. $this->assertNotRegExp('/DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi/', $contents, 'Default Salt left behind. %s');
  181. }
  182. /**
  183. * test generation of Security.cipherSeed
  184. *
  185. * @return void
  186. */
  187. public function testSecurityCipherSeedGeneration() {
  188. $this->_setupTestProject();
  189. $path = $this->Task->path . 'bake_test_app' . DS;
  190. $result = $this->Task->securityCipherSeed($path);
  191. $this->assertTrue($result);
  192. $File = new File($path . 'Config' . DS . 'core.php');
  193. $contents = $File->read();
  194. $this->assertNotRegExp('/76859309657453542496749683645/', $contents, 'Default CipherSeed left behind. %s');
  195. }
  196. /**
  197. * Test that index.php is generated correctly.
  198. *
  199. * @return void
  200. */
  201. public function testIndexPhpGeneration() {
  202. $this->_setupTestProject();
  203. $path = $this->Task->path . 'bake_test_app' . DS;
  204. $this->Task->corePath($path);
  205. $File = new File($path . 'webroot' . DS . 'index.php');
  206. $contents = $File->read();
  207. $this->assertNotRegExp('/define\(\'CAKE_CORE_INCLUDE_PATH\', ROOT/', $contents);
  208. $File = new File($path . 'webroot' . DS . 'test.php');
  209. $contents = $File->read();
  210. $this->assertNotRegExp('/define\(\'CAKE_CORE_INCLUDE_PATH\', ROOT/', $contents);
  211. }
  212. /**
  213. * test getPrefix method, and that it returns Routing.prefix or writes to config file.
  214. *
  215. * @return void
  216. */
  217. public function testGetPrefix() {
  218. Configure::write('Routing.prefixes', array('admin'));
  219. $result = $this->Task->getPrefix();
  220. $this->assertEquals($result, 'admin_');
  221. Configure::write('Routing.prefixes', null);
  222. $this->_setupTestProject();
  223. $this->Task->configPath = $this->Task->path . 'bake_test_app' . DS . 'Config' . DS;
  224. $this->Task->expects($this->once())->method('in')->will($this->returnValue('super_duper_admin'));
  225. $result = $this->Task->getPrefix();
  226. $this->assertEquals($result, 'super_duper_admin_');
  227. $File = new File($this->Task->configPath . 'core.php');
  228. $File->delete();
  229. }
  230. /**
  231. * test cakeAdmin() writing core.php
  232. *
  233. * @return void
  234. */
  235. public function testCakeAdmin() {
  236. $File = new File(APP . 'Config' . DS . 'core.php');
  237. $contents = $File->read();
  238. $File = new File(TMP . 'tests' . DS . 'core.php');
  239. $File->write($contents);
  240. Configure::write('Routing.prefixes', null);
  241. $this->Task->configPath = TMP . 'tests' . DS;
  242. $result = $this->Task->cakeAdmin('my_prefix');
  243. $this->assertTrue($result);
  244. $this->assertEquals(Configure::read('Routing.prefixes'), array('my_prefix'));
  245. $File->delete();
  246. }
  247. /**
  248. * test getting the prefix with more than one prefix setup
  249. *
  250. * @return void
  251. */
  252. public function testGetPrefixWithMultiplePrefixes() {
  253. Configure::write('Routing.prefixes', array('admin', 'ninja', 'shinobi'));
  254. $this->_setupTestProject();
  255. $this->Task->configPath = $this->Task->path . 'bake_test_app' . DS . 'Config' . DS;
  256. $this->Task->expects($this->once())->method('in')->will($this->returnValue(2));
  257. $result = $this->Task->getPrefix();
  258. $this->assertEquals($result, 'ninja_');
  259. }
  260. /**
  261. * Test execute method with one param to destination folder.
  262. *
  263. * @return void
  264. */
  265. public function testExecute() {
  266. $this->Task->params['skel'] = CAKE . 'Console' . DS . 'Templates' . DS . 'skel';
  267. $this->Task->params['working'] = TMP . 'tests' . DS;
  268. $path = $this->Task->path . 'bake_test_app';
  269. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue($path));
  270. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue('y'));
  271. $this->Task->execute();
  272. $this->assertTrue(is_dir($path), 'No project dir');
  273. $this->assertTrue(is_dir($path . DS . 'Controller'), 'No controllers dir ');
  274. $this->assertTrue(is_dir($path . DS . 'Controller' . DS .'Component'), 'No components dir ');
  275. $this->assertTrue(is_dir($path . DS . 'Model'), 'No models dir');
  276. $this->assertTrue(is_dir($path . DS . 'View'), 'No views dir');
  277. $this->assertTrue(is_dir($path . DS . 'View' . DS . 'Helper'), 'No helpers dir');
  278. $this->assertTrue(is_dir($path . DS . 'Test'), 'No tests dir');
  279. $this->assertTrue(is_dir($path . DS . 'Test' . DS . 'Case'), 'No cases dir');
  280. $this->assertTrue(is_dir($path . DS . 'Test' . DS . 'Fixture'), 'No fixtures dir');
  281. }
  282. /**
  283. * test console path
  284. *
  285. * @return void
  286. */
  287. public function testConsolePath() {
  288. $this->_setupTestProject();
  289. $path = $this->Task->path . 'bake_test_app' . DS;
  290. $result = $this->Task->consolePath($path);
  291. $this->assertTrue($result);
  292. $File = new File($path . 'Console' . DS . 'cake.php');
  293. $contents = $File->read();
  294. $this->assertNotRegExp('/__CAKE_PATH__/', $contents, 'Console path placeholder left behind.');
  295. }
  296. }