comparison cake/tests/cases/console/libs/tasks/fixture.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 * FixtureTask Test case
4 *
5 * PHP versions 4 and 5
6 *
7 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
9 *
10 * Licensed under The MIT License
11 * Redistributions of files must retain the above copyright notice.
12 *
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link http://cakephp.org CakePHP(tm) Project
15 * @package cake
16 * @subpackage cake.tests.cases.console.libs.tasks
17 * @since CakePHP(tm) v 1.3
18 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
19 */
20 App::import('Shell', 'Shell', false);
21
22 if (!defined('DISABLE_AUTO_DISPATCH')) {
23 define('DISABLE_AUTO_DISPATCH', true);
24 }
25
26 if (!class_exists('ShellDispatcher')) {
27 ob_start();
28 $argv = false;
29 require CAKE . 'console' . DS . 'cake.php';
30 ob_end_clean();
31 }
32
33 require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'template.php';
34 require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'fixture.php';
35
36 Mock::generatePartial(
37 'ShellDispatcher', 'TestFixtureTaskMockShellDispatcher',
38 array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
39 );
40
41 Mock::generatePartial(
42 'FixtureTask', 'MockFixtureTask',
43 array('in', 'out', 'err', 'createFile', '_stop')
44 );
45
46 Mock::generatePartial(
47 'Shell', 'MockFixtureModelTask',
48 array('in', 'out', 'err', 'createFile', '_stop', 'getName', 'getTable', 'listAll')
49 );
50
51 /**
52 * FixtureTaskTest class
53 *
54 * @package cake
55 * @subpackage cake.tests.cases.console.libs.tasks
56 */
57 class FixtureTaskTest extends CakeTestCase {
58
59 /**
60 * fixtures
61 *
62 * @var array
63 * @access public
64 */
65 var $fixtures = array('core.article', 'core.comment', 'core.datatype', 'core.binary_test');
66
67 /**
68 * startTest method
69 *
70 * @return void
71 * @access public
72 */
73 function startTest() {
74 $this->Dispatcher =& new TestFixtureTaskMockShellDispatcher();
75 $this->Task =& new MockFixtureTask();
76 $this->Task->Model =& new MockFixtureModelTask();
77 $this->Task->Dispatch =& $this->Dispatcher;
78 $this->Task->Template =& new TemplateTask($this->Task->Dispatch);
79 $this->Task->Dispatch->shellPaths = App::path('shells');
80 $this->Task->Template->initialize();
81 }
82
83 /**
84 * endTest method
85 *
86 * @return void
87 * @access public
88 */
89 function endTest() {
90 unset($this->Task, $this->Dispatcher);
91 ClassRegistry::flush();
92 }
93
94 /**
95 * test that initialize sets the path
96 *
97 * @return void
98 * @access public
99 */
100 function testConstruct() {
101 $this->Dispatch->params['working'] = DS . 'my' . DS . 'path';
102 $Task =& new FixtureTask($this->Dispatch);
103
104 $expected = DS . 'my' . DS . 'path' . DS . 'tests' . DS . 'fixtures' . DS;
105 $this->assertEqual($Task->path, $expected);
106 }
107
108 /**
109 * test import option array generation
110 *
111 * @return void
112 * @access public
113 */
114 function testImportOptions() {
115 $this->Task->setReturnValueAt(0, 'in', 'y');
116 $this->Task->setReturnValueAt(1, 'in', 'y');
117
118 $result = $this->Task->importOptions('Article');
119 $expected = array('schema' => 'Article', 'records' => true);
120 $this->assertEqual($result, $expected);
121
122 $this->Task->setReturnValueAt(2, 'in', 'n');
123 $this->Task->setReturnValueAt(3, 'in', 'n');
124 $this->Task->setReturnValueAt(4, 'in', 'n');
125
126 $result = $this->Task->importOptions('Article');
127 $expected = array();
128 $this->assertEqual($result, $expected);
129
130 $this->Task->setReturnValueAt(5, 'in', 'n');
131 $this->Task->setReturnValueAt(6, 'in', 'n');
132 $this->Task->setReturnValueAt(7, 'in', 'y');
133 $result = $this->Task->importOptions('Article');
134 $expected = array('fromTable' => true);
135 $this->assertEqual($result, $expected);
136 }
137
138 /**
139 * test that connection gets set to the import options when a different connection is used.
140 *
141 * @return void
142 */
143 function testImportOptionsAlternateConnection() {
144 $this->Task->connection = 'test';
145 $result = $this->Task->bake('Article', false, array('schema' => 'Article'));
146 $this->assertPattern("/'connection' => 'test'/", $result);
147 }
148
149 /**
150 * test generating a fixture with database conditions.
151 *
152 * @return void
153 * @access public
154 */
155 function testImportRecordsFromDatabaseWithConditions() {
156 $this->Task->interactive = true;
157 $this->Task->setReturnValueAt(0, 'in', 'WHERE 1=1 LIMIT 10');
158 $this->Task->connection = 'test_suite';
159 $this->Task->path = '/my/path/';
160 $result = $this->Task->bake('Article', false, array('fromTable' => true, 'schema' => 'Article', 'records' => false));
161
162 $this->assertPattern('/class ArticleFixture extends CakeTestFixture/', $result);
163 $this->assertPattern('/var \$records/', $result);
164 $this->assertPattern('/var \$import/', $result);
165 $this->assertPattern("/'title' => 'First Article'/", $result, 'Missing import data %s');
166 $this->assertPattern('/Second Article/', $result, 'Missing import data %s');
167 $this->assertPattern('/Third Article/', $result, 'Missing import data %s');
168 }
169
170 /**
171 * test that execute passes runs bake depending with named model.
172 *
173 * @return void
174 * @access public
175 */
176 function testExecuteWithNamedModel() {
177 $this->Task->connection = 'test_suite';
178 $this->Task->path = '/my/path/';
179 $this->Task->args = array('article');
180 $filename = '/my/path/article_fixture.php';
181 $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
182 $this->Task->execute();
183 }
184
185 /**
186 * test that execute passes runs bake depending with named model.
187 *
188 * @return void
189 * @access public
190 */
191 function testExecuteWithNamedModelVariations() {
192 $this->Task->connection = 'test_suite';
193 $this->Task->path = '/my/path/';
194
195 $this->Task->args = array('article');
196 $filename = '/my/path/article_fixture.php';
197 $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
198 $this->Task->execute();
199
200 $this->Task->args = array('articles');
201 $filename = '/my/path/article_fixture.php';
202 $this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
203 $this->Task->execute();
204
205 $this->Task->args = array('Articles');
206 $filename = '/my/path/article_fixture.php';
207 $this->Task->expectAt(2, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
208 $this->Task->execute();
209
210 $this->Task->args = array('Article');
211 $filename = '/my/path/article_fixture.php';
212 $this->Task->expectAt(3, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
213 $this->Task->execute();
214 }
215
216 /**
217 * test that execute runs all() when args[0] = all
218 *
219 * @return void
220 * @access public
221 */
222 function testExecuteIntoAll() {
223 $this->Task->connection = 'test_suite';
224 $this->Task->path = '/my/path/';
225 $this->Task->args = array('all');
226 $this->Task->Model->setReturnValue('listAll', array('articles', 'comments'));
227
228 $filename = '/my/path/article_fixture.php';
229 $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
230 $this->Task->execute();
231
232 $filename = '/my/path/comment_fixture.php';
233 $this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/class CommentFixture/')));
234 $this->Task->execute();
235 }
236
237 /**
238 * test using all() with -count and -records
239 *
240 * @return void
241 * @access public
242 */
243 function testAllWithCountAndRecordsFlags() {
244 $this->Task->connection = 'test_suite';
245 $this->Task->path = '/my/path/';
246 $this->Task->args = array('all');
247 $this->Task->params = array('count' => 10, 'records' => true);
248 $this->Task->Model->setReturnValue('listAll', array('articles', 'comments'));
249
250 $filename = '/my/path/article_fixture.php';
251 $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/title\' => \'Third Article\'/')));
252
253 $filename = '/my/path/comment_fixture.php';
254 $this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/comment\' => \'First Comment for First Article/')));
255 $this->Task->expectCallCount('createFile', 2);
256 $this->Task->all();
257 }
258
259 /**
260 * test interactive mode of execute
261 *
262 * @return void
263 * @access public
264 */
265 function testExecuteInteractive() {
266 $this->Task->connection = 'test_suite';
267 $this->Task->path = '/my/path/';
268
269 $this->Task->setReturnValue('in', 'y');
270 $this->Task->Model->setReturnValue('getName', 'Article');
271 $this->Task->Model->setReturnValue('getTable', 'articles', array('Article'));
272
273 $filename = '/my/path/article_fixture.php';
274 $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
275 $this->Task->execute();
276 }
277
278 /**
279 * Test that bake works
280 *
281 * @return void
282 * @access public
283 */
284 function testBake() {
285 $this->Task->connection = 'test_suite';
286 $this->Task->path = '/my/path/';
287
288 $result = $this->Task->bake('Article');
289 $this->assertPattern('/class ArticleFixture extends CakeTestFixture/', $result);
290 $this->assertPattern('/var \$fields/', $result);
291 $this->assertPattern('/var \$records/', $result);
292 $this->assertNoPattern('/var \$import/', $result);
293
294 $result = $this->Task->bake('Article', 'comments');
295 $this->assertPattern('/class ArticleFixture extends CakeTestFixture/', $result);
296 $this->assertPattern('/var \$name \= \'Article\';/', $result);
297 $this->assertPattern('/var \$table \= \'comments\';/', $result);
298 $this->assertPattern('/var \$fields = array\(/', $result);
299
300 $result = $this->Task->bake('Article', 'comments', array('records' => true));
301 $this->assertPattern(
302 "/var \\\$import \= array\('records' \=\> true, 'connection' => 'test_suite'\);/",
303 $result
304 );
305 $this->assertNoPattern('/var \$records/', $result);
306
307 $result = $this->Task->bake('Article', 'comments', array('schema' => 'Article'));
308 $this->assertPattern(
309 "/var \\\$import \= array\('model' \=\> 'Article', 'connection' => 'test_suite'\);/",
310 $result
311 );
312 $this->assertNoPattern('/var \$fields/', $result);
313
314 $result = $this->Task->bake('Article', 'comments', array('schema' => 'Article', 'records' => true));
315 $this->assertPattern(
316 "/var \\\$import \= array\('model' \=\> 'Article'\, 'records' \=\> true, 'connection' => 'test_suite'\);/",
317 $result
318 );
319 $this->assertNoPattern('/var \$fields/', $result);
320 $this->assertNoPattern('/var \$records/', $result);
321 }
322
323 /**
324 * test record generation with float and binary types
325 *
326 * @return void
327 * @access public
328 */
329 function testRecordGenerationForBinaryAndFloat() {
330 $this->Task->connection = 'test_suite';
331 $this->Task->path = '/my/path/';
332
333 $result = $this->Task->bake('Article', 'datatypes');
334 $this->assertPattern("/'float_field' => 1/", $result);
335
336 $result = $this->Task->bake('Article', 'binary_tests');
337 $this->assertPattern("/'data' => 'Lorem ipsum dolor sit amet'/", $result);
338 }
339
340 /**
341 * Test that file generation includes headers and correct path for plugins.
342 *
343 * @return void
344 * @access public
345 */
346 function testGenerateFixtureFile() {
347 $this->Task->connection = 'test_suite';
348 $this->Task->path = '/my/path/';
349 $filename = '/my/path/article_fixture.php';
350
351 $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/Article/')));
352 $result = $this->Task->generateFixtureFile('Article', array());
353
354 $this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/\<\?php(.*)\?\>/ms')));
355 $result = $this->Task->generateFixtureFile('Article', array());
356 }
357
358 /**
359 * test generating files into plugins.
360 *
361 * @return void
362 * @access public
363 */
364 function testGeneratePluginFixtureFile() {
365 $this->Task->connection = 'test_suite';
366 $this->Task->path = '/my/path/';
367 $this->Task->plugin = 'TestFixture';
368 $filename = APP . 'plugins' . DS . 'test_fixture' . DS . 'tests' . DS . 'fixtures' . DS . 'article_fixture.php';
369
370 $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/Article/')));
371 $result = $this->Task->generateFixtureFile('Article', array());
372 }
373 }