comparison cake/tests/cases/console/libs/tasks/project.test.php @ 0:261e66bd5a0c

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 24 Jul 2011 21:08:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:261e66bd5a0c
1 <?php
2 /**
3 * ProjectTask Test file
4 *
5 * Test Case for project generation shell task
6 *
7 * PHP versions 4 and 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
18 * @subpackage cake.tests.cases.console.libs.tasks
19 * @since CakePHP v 1.3.0
20 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
21 */
22 App::import('Shell', 'Shell', false);
23
24 if (!defined('DISABLE_AUTO_DISPATCH')) {
25 define('DISABLE_AUTO_DISPATCH', true);
26 }
27
28 if (!class_exists('ShellDispatcher')) {
29 ob_start();
30 $argv = false;
31 require CAKE . 'console' . DS . 'cake.php';
32 ob_end_clean();
33 }
34
35 require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'project.php';
36
37 Mock::generatePartial(
38 'ShellDispatcher', 'TestProjectTaskMockShellDispatcher',
39 array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
40 );
41 Mock::generatePartial(
42 'ProjectTask', 'MockProjectTask',
43 array('in', '_stop', 'err', 'out', 'createFile')
44 );
45
46 /**
47 * ProjectTask Test class
48 *
49 * @package cake
50 * @subpackage cake.tests.cases.console.libs.tasks
51 */
52 class ProjectTaskTest extends CakeTestCase {
53
54 /**
55 * startTest method
56 *
57 * @return void
58 * @access public
59 */
60 function startTest() {
61 $this->Dispatcher =& new TestProjectTaskMockShellDispatcher();
62 $this->Dispatcher->shellPaths = App::path('shells');
63 $this->Task =& new MockProjectTask($this->Dispatcher);
64 $this->Task->Dispatch =& $this->Dispatcher;
65 $this->Task->path = TMP . 'tests' . DS;
66 }
67
68 /**
69 * endTest method
70 *
71 * @return void
72 * @access public
73 */
74 function endTest() {
75 ClassRegistry::flush();
76
77 $Folder =& new Folder($this->Task->path . 'bake_test_app');
78 $Folder->delete();
79 }
80
81 /**
82 * creates a test project that is used for testing project task.
83 *
84 * @return void
85 * @access protected
86 */
87 function _setupTestProject() {
88 $skel = CAKE_CORE_INCLUDE_PATH . DS . CAKE . 'console' . DS . 'templates' . DS . 'skel';
89 $this->Task->setReturnValueAt(0, 'in', 'y');
90 $this->Task->setReturnValueAt(1, 'in', 'n');
91 $this->Task->bake($this->Task->path . 'bake_test_app', $skel);
92 }
93
94 /**
95 * test bake() method and directory creation.
96 *
97 * @return void
98 * @access public
99 */
100 function testBake() {
101 $this->_setupTestProject();
102
103 $path = $this->Task->path . 'bake_test_app';
104 $this->assertTrue(is_dir($path), 'No project dir %s');
105 $this->assertTrue(is_dir($path . DS . 'controllers'), 'No controllers dir %s');
106 $this->assertTrue(is_dir($path . DS . 'controllers' . DS .'components'), 'No components dir %s');
107 $this->assertTrue(is_dir($path . DS . 'models'), 'No models dir %s');
108 $this->assertTrue(is_dir($path . DS . 'views'), 'No views dir %s');
109 $this->assertTrue(is_dir($path . DS . 'views' . DS . 'helpers'), 'No helpers dir %s');
110 $this->assertTrue(is_dir($path . DS . 'tests'), 'No tests dir %s');
111 $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'cases'), 'No cases dir %s');
112 $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'groups'), 'No groups dir %s');
113 $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'fixtures'), 'No fixtures dir %s');
114 }
115
116 /**
117 * test bake() method with -empty flag, directory creation and empty files.
118 *
119 * @return void
120 * @access public
121 */
122 function testBakeEmptyFlag() {
123 $this->Task->params['empty'] = true;
124 $this->_setupTestProject();
125 $path = $this->Task->path . 'bake_test_app';
126 $this->assertTrue(is_dir($path), 'No project dir %s');
127 $this->assertTrue(is_dir($path . DS . 'controllers'), 'No controllers dir %s');
128 $this->assertTrue(is_dir($path . DS . 'controllers' . DS .'components'), 'No components dir %s');
129 $this->assertTrue(is_dir($path . DS . 'models'), 'No models dir %s');
130 $this->assertTrue(is_dir($path . DS . 'views'), 'No views dir %s');
131 $this->assertTrue(is_dir($path . DS . 'views' . DS . 'helpers'), 'No helpers dir %s');
132 $this->assertTrue(is_dir($path . DS . 'tests'), 'No tests dir %s');
133 $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'cases'), 'No cases dir %s');
134 $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'groups'), 'No groups dir %s');
135 $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'fixtures'), 'No fixtures dir %s');
136
137 $this->assertTrue(is_file($path . DS . 'controllers' . DS .'components' . DS . 'empty'), 'No empty file in dir %s');
138 $this->assertTrue(is_file($path . DS . 'locale' . DS . 'eng' . DS . 'LC_MESSAGES' . DS . 'empty'), 'No empty file in dir %s');
139 $this->assertTrue(is_file($path . DS . 'models' . DS . 'behaviors' . DS . 'empty'), 'No empty file in dir %s');
140 $this->assertTrue(is_file($path . DS . 'models' . DS . 'datasources' . DS . 'empty'), 'No empty file in dir %s');
141 $this->assertTrue(is_file($path . DS . 'plugins' . DS . 'empty'), 'No empty file in dir %s');
142 $this->assertTrue(is_file($path . DS . 'tests' . DS . 'cases' . DS . 'behaviors' . DS . 'empty'), 'No empty file in dir %s');
143 $this->assertTrue(is_file($path . DS . 'tests' . DS . 'cases' . DS . 'components' . DS . 'empty'), 'No empty file in dir %s');
144 $this->assertTrue(is_file($path . DS . 'tests' . DS . 'cases' . DS . 'controllers' . DS . 'empty'), 'No empty file in dir %s');
145 $this->assertTrue(is_file($path . DS . 'tests' . DS . 'cases' . DS . 'datasources' . DS . 'empty'), 'No empty file in dir %s');
146 $this->assertTrue(is_file($path . DS . 'tests' . DS . 'cases' . DS . 'helpers' . DS . 'empty'), 'No empty file in dir %s');
147 $this->assertTrue(is_file($path . DS . 'tests' . DS . 'cases' . DS . 'models' . DS . 'empty'), 'No empty file in dir %s');
148 $this->assertTrue(is_file($path . DS . 'tests' . DS . 'cases' . DS . 'shells' . DS . 'empty'), 'No empty file in dir %s');
149 $this->assertTrue(is_file($path . DS . 'tests' . DS . 'fixtures' . DS . 'empty'), 'No empty file in dir %s');
150 $this->assertTrue(is_file($path . DS . 'tests' . DS . 'groups' . DS . 'empty'), 'No empty file in dir %s');
151 $this->assertTrue(is_file($path . DS . 'vendors' . DS . 'shells' . DS . 'tasks' . DS . 'empty'), 'No empty file in dir %s');
152 $this->assertTrue(is_file($path . DS . 'views' . DS . 'errors' . DS . 'empty'), 'No empty file in dir %s');
153 $this->assertTrue(is_file($path . DS . 'views' . DS . 'helpers' . DS . 'empty'), 'No empty file in dir %s');
154 $this->assertTrue(is_file($path . DS . 'views' . DS . 'scaffolds' . DS . 'empty'), 'No empty file in dir %s');
155 $this->assertTrue(is_file($path . DS . 'webroot' . DS . 'js' . DS . 'empty'), 'No empty file in dir %s');
156 }
157
158 /**
159 * test generation of Security.salt
160 *
161 * @return void
162 * @access public
163 */
164 function testSecuritySaltGeneration() {
165 $this->_setupTestProject();
166
167 $path = $this->Task->path . 'bake_test_app' . DS;
168 $result = $this->Task->securitySalt($path);
169 $this->assertTrue($result);
170
171 $file =& new File($path . 'config' . DS . 'core.php');
172 $contents = $file->read();
173 $this->assertNoPattern('/DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi/', $contents, 'Default Salt left behind. %s');
174 }
175
176 /**
177 * test generation of Security.cipherSeed
178 *
179 * @return void
180 * @access public
181 */
182 function testSecurityCipherSeedGeneration() {
183 $this->_setupTestProject();
184
185 $path = $this->Task->path . 'bake_test_app' . DS;
186 $result = $this->Task->securityCipherSeed($path);
187 $this->assertTrue($result);
188
189 $file =& new File($path . 'config' . DS . 'core.php');
190 $contents = $file->read();
191 $this->assertNoPattern('/76859309657453542496749683645/', $contents, 'Default CipherSeed left behind. %s');
192 }
193
194 /**
195 * Test that index.php is generated correctly.
196 *
197 * @return void
198 * @access public
199 */
200 function testIndexPhpGeneration() {
201 $this->_setupTestProject();
202
203 $path = $this->Task->path . 'bake_test_app' . DS;
204 $this->Task->corePath($path);
205
206 $file =& new File($path . 'webroot' . DS . 'index.php');
207 $contents = $file->read();
208 $this->assertNoPattern('/define\(\'CAKE_CORE_INCLUDE_PATH\', \'ROOT/', $contents);
209
210 $file =& new File($path . 'webroot' . DS . 'test.php');
211 $contents = $file->read();
212 $this->assertNoPattern('/define\(\'CAKE_CORE_INCLUDE_PATH\', \'ROOT/', $contents);
213 }
214
215 /**
216 * test getPrefix method, and that it returns Routing.prefix or writes to config file.
217 *
218 * @return void
219 * @access public
220 */
221 function testGetPrefix() {
222 Configure::write('Routing.prefixes', array('admin'));
223 $result = $this->Task->getPrefix();
224 $this->assertEqual($result, 'admin_');
225
226 Configure::write('Routing.prefixes', null);
227 $this->_setupTestProject();
228 $this->Task->configPath = $this->Task->path . 'bake_test_app' . DS . 'config' . DS;
229 $this->Task->setReturnValue('in', 'super_duper_admin');
230
231 $result = $this->Task->getPrefix();
232 $this->assertEqual($result, 'super_duper_admin_');
233
234 $file =& new File($this->Task->configPath . 'core.php');
235 $file->delete();
236 }
237
238 /**
239 * test cakeAdmin() writing core.php
240 *
241 * @return void
242 * @access public
243 */
244 function testCakeAdmin() {
245 $file =& new File(CONFIGS . 'core.php');
246 $contents = $file->read();;
247 $file =& new File(TMP . 'tests' . DS . 'core.php');
248 $file->write($contents);
249
250 Configure::write('Routing.prefixes', null);
251 $this->Task->configPath = TMP . 'tests' . DS;
252 $result = $this->Task->cakeAdmin('my_prefix');
253 $this->assertTrue($result);
254
255 $this->assertEqual(Configure::read('Routing.prefixes'), array('my_prefix'));
256 $file->delete();
257 }
258
259 /**
260 * test getting the prefix with more than one prefix setup
261 *
262 * @return void
263 * @access public
264 */
265 function testGetPrefixWithMultiplePrefixes() {
266 Configure::write('Routing.prefixes', array('admin', 'ninja', 'shinobi'));
267 $this->_setupTestProject();
268 $this->Task->configPath = $this->Task->path . 'bake_test_app' . DS . 'config' . DS;
269 $this->Task->setReturnValue('in', 2);
270
271 $result = $this->Task->getPrefix();
272 $this->assertEqual($result, 'ninja_');
273 }
274
275 /**
276 * Test execute method with one param to destination folder.
277 *
278 * @return void
279 * @access public
280 */
281 function testExecute() {
282 $this->Task->params['skel'] = CAKE_CORE_INCLUDE_PATH . DS . CAKE . DS . 'console' . DS. 'templates' . DS . 'skel';
283 $this->Task->params['working'] = TMP . 'tests' . DS;
284
285 $path = $this->Task->path . 'bake_test_app';
286 $this->Task->setReturnValue('in', 'y');
287 $this->Task->setReturnValueAt(0, 'in', $path);
288
289 $this->Task->execute();
290 $this->assertTrue(is_dir($path), 'No project dir %s');
291 $this->assertTrue(is_dir($path . DS . 'controllers'), 'No controllers dir %s');
292 $this->assertTrue(is_dir($path . DS . 'controllers' . DS .'components'), 'No components dir %s');
293 $this->assertTrue(is_dir($path . DS . 'models'), 'No models dir %s');
294 $this->assertTrue(is_dir($path . DS . 'views'), 'No views dir %s');
295 $this->assertTrue(is_dir($path . DS . 'views' . DS . 'helpers'), 'No helpers dir %s');
296 $this->assertTrue(is_dir($path . DS . 'tests'), 'No tests dir %s');
297 $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'cases'), 'No cases dir %s');
298 $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'groups'), 'No groups dir %s');
299 $this->assertTrue(is_dir($path . DS . 'tests' . DS . 'fixtures'), 'No fixtures dir %s');
300 }
301 }