Mercurial > hg > Members > shoshi > webvirt
comparison cake/tests/cases/console/libs/tasks/model.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 * ModelTaskTest file | |
4 * | |
5 * Test Case for test 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.2.6 | |
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 . 'model.php'; | |
36 require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'fixture.php'; | |
37 require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'template.php'; | |
38 | |
39 Mock::generatePartial( | |
40 'ShellDispatcher', 'TestModelTaskMockShellDispatcher', | |
41 array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment') | |
42 ); | |
43 Mock::generatePartial( | |
44 'ModelTask', 'MockModelTask', | |
45 array('in', 'out', 'hr', 'err', 'createFile', '_stop', '_checkUnitTest') | |
46 ); | |
47 | |
48 Mock::generate( | |
49 'Model', 'MockModelTaskModel' | |
50 ); | |
51 | |
52 Mock::generate( | |
53 'FixtureTask', 'MockModelTaskFixtureTask' | |
54 ); | |
55 | |
56 /** | |
57 * ModelTaskTest class | |
58 * | |
59 * @package cake | |
60 * @subpackage cake.tests.cases.console.libs.tasks | |
61 */ | |
62 class ModelTaskTest extends CakeTestCase { | |
63 | |
64 /** | |
65 * fixtures | |
66 * | |
67 * @var array | |
68 * @access public | |
69 */ | |
70 var $fixtures = array('core.article', 'core.comment', 'core.articles_tag', 'core.tag', 'core.category_thread'); | |
71 | |
72 /** | |
73 * starTest method | |
74 * | |
75 * @return void | |
76 * @access public | |
77 */ | |
78 function startTest() { | |
79 $this->Dispatcher =& new TestModelTaskMockShellDispatcher(); | |
80 $this->Task =& new MockModelTask($this->Dispatcher); | |
81 $this->Task->name = 'ModelTask'; | |
82 $this->Task->interactive = true; | |
83 $this->Task->Dispatch =& $this->Dispatcher; | |
84 $this->Task->Dispatch->shellPaths = App::path('shells'); | |
85 $this->Task->Template =& new TemplateTask($this->Task->Dispatch); | |
86 $this->Task->Fixture =& new MockModelTaskFixtureTask(); | |
87 $this->Task->Test =& new MockModelTaskFixtureTask(); | |
88 } | |
89 | |
90 /** | |
91 * endTest method | |
92 * | |
93 * @return void | |
94 * @access public | |
95 */ | |
96 function endTest() { | |
97 unset($this->Task, $this->Dispatcher); | |
98 ClassRegistry::flush(); | |
99 } | |
100 | |
101 /** | |
102 * Test that listAll scans the database connection and lists all the tables in it.s | |
103 * | |
104 * @return void | |
105 * @access public | |
106 */ | |
107 function testListAll() { | |
108 $this->Task->expectAt(1, 'out', array('1. Article')); | |
109 $this->Task->expectAt(2, 'out', array('2. ArticlesTag')); | |
110 $this->Task->expectAt(3, 'out', array('3. CategoryThread')); | |
111 $this->Task->expectAt(4, 'out', array('4. Comment')); | |
112 $this->Task->expectAt(5, 'out', array('5. Tag')); | |
113 $result = $this->Task->listAll('test_suite'); | |
114 $expected = array('articles', 'articles_tags', 'category_threads', 'comments', 'tags'); | |
115 $this->assertEqual($result, $expected); | |
116 | |
117 $this->Task->expectAt(7, 'out', array('1. Article')); | |
118 $this->Task->expectAt(8, 'out', array('2. ArticlesTag')); | |
119 $this->Task->expectAt(9, 'out', array('3. CategoryThread')); | |
120 $this->Task->expectAt(10, 'out', array('4. Comment')); | |
121 $this->Task->expectAt(11, 'out', array('5. Tag')); | |
122 | |
123 $this->Task->connection = 'test_suite'; | |
124 $result = $this->Task->listAll(); | |
125 $expected = array('articles', 'articles_tags', 'category_threads', 'comments', 'tags'); | |
126 $this->assertEqual($result, $expected); | |
127 } | |
128 | |
129 /** | |
130 * Test that getName interacts with the user and returns the model name. | |
131 * | |
132 * @return void | |
133 * @access public | |
134 */ | |
135 function testGetName() { | |
136 $this->Task->setReturnValue('in', 1); | |
137 | |
138 $this->Task->setReturnValueAt(0, 'in', 'q'); | |
139 $this->Task->expectOnce('_stop'); | |
140 $this->Task->getName('test_suite'); | |
141 | |
142 $this->Task->setReturnValueAt(1, 'in', 1); | |
143 $result = $this->Task->getName('test_suite'); | |
144 $expected = 'Article'; | |
145 $this->assertEqual($result, $expected); | |
146 | |
147 $this->Task->setReturnValueAt(2, 'in', 4); | |
148 $result = $this->Task->getName('test_suite'); | |
149 $expected = 'Comment'; | |
150 $this->assertEqual($result, $expected); | |
151 | |
152 $this->Task->setReturnValueAt(3, 'in', 10); | |
153 $result = $this->Task->getName('test_suite'); | |
154 $this->Task->expectOnce('err'); | |
155 } | |
156 | |
157 /** | |
158 * Test table name interactions | |
159 * | |
160 * @return void | |
161 * @access public | |
162 */ | |
163 function testGetTableName() { | |
164 $this->Task->setReturnValueAt(0, 'in', 'y'); | |
165 $result = $this->Task->getTable('Article', 'test_suite'); | |
166 $expected = 'articles'; | |
167 $this->assertEqual($result, $expected); | |
168 | |
169 $this->Task->setReturnValueAt(1, 'in', 'n'); | |
170 $this->Task->setReturnValueAt(2, 'in', 'my_table'); | |
171 $result = $this->Task->getTable('Article', 'test_suite'); | |
172 $expected = 'my_table'; | |
173 $this->assertEqual($result, $expected); | |
174 } | |
175 | |
176 /** | |
177 * test that initializing the validations works. | |
178 * | |
179 * @return void | |
180 * @access public | |
181 */ | |
182 function testInitValidations() { | |
183 $result = $this->Task->initValidations(); | |
184 $this->assertTrue(in_array('notempty', $result)); | |
185 } | |
186 | |
187 /** | |
188 * test that individual field validation works, with interactive = false | |
189 * tests the guessing features of validation | |
190 * | |
191 * @return void | |
192 * @access public | |
193 */ | |
194 function testFieldValidationGuessing() { | |
195 $this->Task->interactive = false; | |
196 $this->Task->initValidations(); | |
197 | |
198 $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false)); | |
199 $expected = array('notempty' => 'notempty'); | |
200 $this->assertEqual($expected, $result); | |
201 | |
202 $result = $this->Task->fieldValidation('text', array('type' => 'date', 'length' => 10, 'null' => false)); | |
203 $expected = array('date' => 'date'); | |
204 $this->assertEqual($expected, $result); | |
205 | |
206 $result = $this->Task->fieldValidation('text', array('type' => 'time', 'length' => 10, 'null' => false)); | |
207 $expected = array('time' => 'time'); | |
208 $this->assertEqual($expected, $result); | |
209 | |
210 $result = $this->Task->fieldValidation('email', array('type' => 'string', 'length' => 10, 'null' => false)); | |
211 $expected = array('email' => 'email'); | |
212 $this->assertEqual($expected, $result); | |
213 | |
214 $result = $this->Task->fieldValidation('test', array('type' => 'integer', 'length' => 10, 'null' => false)); | |
215 $expected = array('numeric' => 'numeric'); | |
216 $this->assertEqual($expected, $result); | |
217 | |
218 $result = $this->Task->fieldValidation('test', array('type' => 'boolean', 'length' => 10, 'null' => false)); | |
219 $expected = array('boolean' => 'boolean'); | |
220 $this->assertEqual($expected, $result); | |
221 | |
222 $result = $this->Task->fieldValidation('test', array('type' => 'string', 'length' => 36, 'null' => false)); | |
223 $expected = array('uuid' => 'uuid'); | |
224 $this->assertEqual($expected, $result); | |
225 } | |
226 | |
227 /** | |
228 * test that interactive field validation works and returns multiple validators. | |
229 * | |
230 * @return void | |
231 * @access public | |
232 */ | |
233 function testInteractiveFieldValidation() { | |
234 $this->Task->initValidations(); | |
235 $this->Task->interactive = true; | |
236 $this->Task->setReturnValueAt(0, 'in', '19'); | |
237 $this->Task->setReturnValueAt(1, 'in', 'y'); | |
238 $this->Task->setReturnValueAt(2, 'in', '15'); | |
239 $this->Task->setReturnValueAt(3, 'in', 'n'); | |
240 | |
241 $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false)); | |
242 $expected = array('notempty' => 'notempty', 'maxlength' => 'maxlength'); | |
243 $this->assertEqual($result, $expected); | |
244 } | |
245 | |
246 /** | |
247 * test that a bogus response doesn't cause errors to bubble up. | |
248 * | |
249 * @return void | |
250 */ | |
251 function testInteractiveFieldValidationWithBogusResponse() { | |
252 $this->Task->initValidations(); | |
253 $this->Task->interactive = true; | |
254 $this->Task->setReturnValueAt(0, 'in', '999999'); | |
255 $this->Task->setReturnValueAt(1, 'in', '19'); | |
256 $this->Task->setReturnValueAt(2, 'in', 'n'); | |
257 $this->Task->expectAt(4, 'out', array(new PatternExpectation('/make a valid/'))); | |
258 | |
259 $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false)); | |
260 $expected = array('notempty' => 'notempty'); | |
261 $this->assertEqual($result, $expected); | |
262 } | |
263 | |
264 /** | |
265 * test that a regular expression can be used for validation. | |
266 * | |
267 * @return void | |
268 */ | |
269 function testInteractiveFieldValidationWithRegexp() { | |
270 $this->Task->initValidations(); | |
271 $this->Task->interactive = true; | |
272 $this->Task->setReturnValueAt(0, 'in', '/^[a-z]{0,9}$/'); | |
273 $this->Task->setReturnValueAt(1, 'in', 'n'); | |
274 | |
275 $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false)); | |
276 $expected = array('a_z_0_9' => '/^[a-z]{0,9}$/'); | |
277 $this->assertEqual($result, $expected); | |
278 } | |
279 | |
280 /** | |
281 * test the validation Generation routine | |
282 * | |
283 * @return void | |
284 * @access public | |
285 */ | |
286 function testNonInteractiveDoValidation() { | |
287 $Model =& new MockModelTaskModel(); | |
288 $Model->primaryKey = 'id'; | |
289 $Model->setReturnValue('schema', array( | |
290 'id' => array( | |
291 'type' => 'integer', | |
292 'length' => 11, | |
293 'null' => false, | |
294 'key' => 'primary', | |
295 ), | |
296 'name' => array( | |
297 'type' => 'string', | |
298 'length' => 20, | |
299 'null' => false, | |
300 ), | |
301 'email' => array( | |
302 'type' => 'string', | |
303 'length' => 255, | |
304 'null' => false, | |
305 ), | |
306 'some_date' => array( | |
307 'type' => 'date', | |
308 'length' => '', | |
309 'null' => false, | |
310 ), | |
311 'some_time' => array( | |
312 'type' => 'time', | |
313 'length' => '', | |
314 'null' => false, | |
315 ), | |
316 'created' => array( | |
317 'type' => 'datetime', | |
318 'length' => '', | |
319 'null' => false, | |
320 ) | |
321 )); | |
322 $this->Task->interactive = false; | |
323 | |
324 $result = $this->Task->doValidation($Model); | |
325 $expected = array( | |
326 'name' => array( | |
327 'notempty' => 'notempty' | |
328 ), | |
329 'email' => array( | |
330 'email' => 'email', | |
331 ), | |
332 'some_date' => array( | |
333 'date' => 'date' | |
334 ), | |
335 'some_time' => array( | |
336 'time' => 'time' | |
337 ), | |
338 ); | |
339 $this->assertEqual($result, $expected); | |
340 } | |
341 | |
342 /** | |
343 * test that finding primary key works | |
344 * | |
345 * @return void | |
346 * @access public | |
347 */ | |
348 function testFindPrimaryKey() { | |
349 $fields = array( | |
350 'one' => array(), | |
351 'two' => array(), | |
352 'key' => array('key' => 'primary') | |
353 ); | |
354 $this->Task->expectAt(0, 'in', array('*', null, 'key')); | |
355 $this->Task->setReturnValue('in', 'my_field'); | |
356 $result = $this->Task->findPrimaryKey($fields); | |
357 $expected = 'my_field'; | |
358 $this->assertEqual($result, $expected); | |
359 } | |
360 | |
361 /** | |
362 * test finding Display field | |
363 * | |
364 * @return void | |
365 * @access public | |
366 */ | |
367 function testFindDisplayField() { | |
368 $fields = array('id' => array(), 'tagname' => array(), 'body' => array(), | |
369 'created' => array(), 'modified' => array()); | |
370 | |
371 $this->Task->setReturnValue('in', 'n'); | |
372 $this->Task->setReturnValueAt(0, 'in', 'n'); | |
373 $result = $this->Task->findDisplayField($fields); | |
374 $this->assertFalse($result); | |
375 | |
376 $this->Task->setReturnValueAt(1, 'in', 'y'); | |
377 $this->Task->setReturnValueAt(2, 'in', 2); | |
378 $result = $this->Task->findDisplayField($fields); | |
379 $this->assertEqual($result, 'tagname'); | |
380 } | |
381 | |
382 /** | |
383 * test that belongsTo generation works. | |
384 * | |
385 * @return void | |
386 * @access public | |
387 */ | |
388 function testBelongsToGeneration() { | |
389 $model = new Model(array('ds' => 'test_suite', 'name' => 'Comment')); | |
390 $result = $this->Task->findBelongsTo($model, array()); | |
391 $expected = array( | |
392 'belongsTo' => array( | |
393 array( | |
394 'alias' => 'Article', | |
395 'className' => 'Article', | |
396 'foreignKey' => 'article_id', | |
397 ), | |
398 array( | |
399 'alias' => 'User', | |
400 'className' => 'User', | |
401 'foreignKey' => 'user_id', | |
402 ), | |
403 ) | |
404 ); | |
405 $this->assertEqual($result, $expected); | |
406 | |
407 $model = new Model(array('ds' => 'test_suite', 'name' => 'CategoryThread')); | |
408 $result = $this->Task->findBelongsTo($model, array()); | |
409 $expected = array( | |
410 'belongsTo' => array( | |
411 array( | |
412 'alias' => 'ParentCategoryThread', | |
413 'className' => 'CategoryThread', | |
414 'foreignKey' => 'parent_id', | |
415 ), | |
416 ) | |
417 ); | |
418 $this->assertEqual($result, $expected); | |
419 } | |
420 | |
421 /** | |
422 * test that hasOne and/or hasMany relations are generated properly. | |
423 * | |
424 * @return void | |
425 * @access public | |
426 */ | |
427 function testHasManyHasOneGeneration() { | |
428 $model = new Model(array('ds' => 'test_suite', 'name' => 'Article')); | |
429 $this->Task->connection = 'test_suite'; | |
430 $this->Task->listAll(); | |
431 $result = $this->Task->findHasOneAndMany($model, array()); | |
432 $expected = array( | |
433 'hasMany' => array( | |
434 array( | |
435 'alias' => 'Comment', | |
436 'className' => 'Comment', | |
437 'foreignKey' => 'article_id', | |
438 ), | |
439 ), | |
440 'hasOne' => array( | |
441 array( | |
442 'alias' => 'Comment', | |
443 'className' => 'Comment', | |
444 'foreignKey' => 'article_id', | |
445 ), | |
446 ), | |
447 ); | |
448 $this->assertEqual($result, $expected); | |
449 | |
450 $model = new Model(array('ds' => 'test_suite', 'name' => 'CategoryThread')); | |
451 $result = $this->Task->findHasOneAndMany($model, array()); | |
452 $expected = array( | |
453 'hasOne' => array( | |
454 array( | |
455 'alias' => 'ChildCategoryThread', | |
456 'className' => 'CategoryThread', | |
457 'foreignKey' => 'parent_id', | |
458 ), | |
459 ), | |
460 'hasMany' => array( | |
461 array( | |
462 'alias' => 'ChildCategoryThread', | |
463 'className' => 'CategoryThread', | |
464 'foreignKey' => 'parent_id', | |
465 ), | |
466 ) | |
467 ); | |
468 $this->assertEqual($result, $expected); | |
469 } | |
470 | |
471 /** | |
472 * Test that HABTM generation works | |
473 * | |
474 * @return void | |
475 * @access public | |
476 */ | |
477 function testHasAndBelongsToManyGeneration() { | |
478 $model = new Model(array('ds' => 'test_suite', 'name' => 'Article')); | |
479 $this->Task->connection = 'test_suite'; | |
480 $this->Task->listAll(); | |
481 $result = $this->Task->findHasAndBelongsToMany($model, array()); | |
482 $expected = array( | |
483 'hasAndBelongsToMany' => array( | |
484 array( | |
485 'alias' => 'Tag', | |
486 'className' => 'Tag', | |
487 'foreignKey' => 'article_id', | |
488 'joinTable' => 'articles_tags', | |
489 'associationForeignKey' => 'tag_id', | |
490 ), | |
491 ), | |
492 ); | |
493 $this->assertEqual($result, $expected); | |
494 } | |
495 | |
496 /** | |
497 * test non interactive doAssociations | |
498 * | |
499 * @return void | |
500 * @access public | |
501 */ | |
502 function testDoAssociationsNonInteractive() { | |
503 $this->Task->connection = 'test_suite'; | |
504 $this->Task->interactive = false; | |
505 $model = new Model(array('ds' => 'test_suite', 'name' => 'Article')); | |
506 $result = $this->Task->doAssociations($model); | |
507 $expected = array( | |
508 'hasMany' => array( | |
509 array( | |
510 'alias' => 'Comment', | |
511 'className' => 'Comment', | |
512 'foreignKey' => 'article_id', | |
513 ), | |
514 ), | |
515 'hasAndBelongsToMany' => array( | |
516 array( | |
517 'alias' => 'Tag', | |
518 'className' => 'Tag', | |
519 'foreignKey' => 'article_id', | |
520 'joinTable' => 'articles_tags', | |
521 'associationForeignKey' => 'tag_id', | |
522 ), | |
523 ), | |
524 ); | |
525 } | |
526 | |
527 /** | |
528 * Ensure that the fixutre object is correctly called. | |
529 * | |
530 * @return void | |
531 * @access public | |
532 */ | |
533 function testBakeFixture() { | |
534 $this->Task->plugin = 'test_plugin'; | |
535 $this->Task->interactive = true; | |
536 $this->Task->Fixture->expectAt(0, 'bake', array('Article', 'articles')); | |
537 $this->Task->bakeFixture('Article', 'articles'); | |
538 | |
539 $this->assertEqual($this->Task->plugin, $this->Task->Fixture->plugin); | |
540 $this->assertEqual($this->Task->connection, $this->Task->Fixture->connection); | |
541 $this->assertEqual($this->Task->interactive, $this->Task->Fixture->interactive); | |
542 } | |
543 | |
544 /** | |
545 * Ensure that the test object is correctly called. | |
546 * | |
547 * @return void | |
548 * @access public | |
549 */ | |
550 function testBakeTest() { | |
551 $this->Task->plugin = 'test_plugin'; | |
552 $this->Task->interactive = true; | |
553 $this->Task->Test->expectAt(0, 'bake', array('Model', 'Article')); | |
554 $this->Task->bakeTest('Article'); | |
555 | |
556 $this->assertEqual($this->Task->plugin, $this->Task->Test->plugin); | |
557 $this->assertEqual($this->Task->connection, $this->Task->Test->connection); | |
558 $this->assertEqual($this->Task->interactive, $this->Task->Test->interactive); | |
559 } | |
560 | |
561 /** | |
562 * test confirming of associations, and that when an association is hasMany | |
563 * a question for the hasOne is also not asked. | |
564 * | |
565 * @return void | |
566 * @access public | |
567 */ | |
568 function testConfirmAssociations() { | |
569 $associations = array( | |
570 'hasOne' => array( | |
571 array( | |
572 'alias' => 'ChildCategoryThread', | |
573 'className' => 'CategoryThread', | |
574 'foreignKey' => 'parent_id', | |
575 ), | |
576 ), | |
577 'hasMany' => array( | |
578 array( | |
579 'alias' => 'ChildCategoryThread', | |
580 'className' => 'CategoryThread', | |
581 'foreignKey' => 'parent_id', | |
582 ), | |
583 ), | |
584 'belongsTo' => array( | |
585 array( | |
586 'alias' => 'User', | |
587 'className' => 'User', | |
588 'foreignKey' => 'user_id', | |
589 ), | |
590 ) | |
591 ); | |
592 $model = new Model(array('ds' => 'test_suite', 'name' => 'CategoryThread')); | |
593 $this->Task->setReturnValueAt(0, 'in', 'y'); | |
594 $result = $this->Task->confirmAssociations($model, $associations); | |
595 $this->assertTrue(empty($result['hasOne'])); | |
596 | |
597 $this->Task->setReturnValue('in', 'n'); | |
598 $result = $this->Task->confirmAssociations($model, $associations); | |
599 $this->assertTrue(empty($result['hasMany'])); | |
600 $this->assertTrue(empty($result['hasOne'])); | |
601 } | |
602 | |
603 /** | |
604 * test that inOptions generates questions and only accepts a valid answer | |
605 * | |
606 * @return void | |
607 * @access public | |
608 */ | |
609 function testInOptions() { | |
610 $options = array('one', 'two', 'three'); | |
611 $this->Task->expectAt(0, 'out', array('1. one')); | |
612 $this->Task->expectAt(1, 'out', array('2. two')); | |
613 $this->Task->expectAt(2, 'out', array('3. three')); | |
614 $this->Task->setReturnValueAt(0, 'in', 10); | |
615 | |
616 $this->Task->expectAt(3, 'out', array('1. one')); | |
617 $this->Task->expectAt(4, 'out', array('2. two')); | |
618 $this->Task->expectAt(5, 'out', array('3. three')); | |
619 $this->Task->setReturnValueAt(1, 'in', 2); | |
620 $result = $this->Task->inOptions($options, 'Pick a number'); | |
621 $this->assertEqual($result, 1); | |
622 } | |
623 | |
624 /** | |
625 * test baking validation | |
626 * | |
627 * @return void | |
628 * @access public | |
629 */ | |
630 function testBakeValidation() { | |
631 $validate = array( | |
632 'name' => array( | |
633 'notempty' => 'notempty' | |
634 ), | |
635 'email' => array( | |
636 'email' => 'email', | |
637 ), | |
638 'some_date' => array( | |
639 'date' => 'date' | |
640 ), | |
641 'some_time' => array( | |
642 'time' => 'time' | |
643 ) | |
644 ); | |
645 $result = $this->Task->bake('Article', compact('validate')); | |
646 $this->assertPattern('/class Article extends AppModel \{/', $result); | |
647 $this->assertPattern('/\$name \= \'Article\'/', $result); | |
648 $this->assertPattern('/\$validate \= array\(/', $result); | |
649 $expected = <<< STRINGEND | |
650 array( | |
651 'notempty' => array( | |
652 'rule' => array('notempty'), | |
653 //'message' => 'Your custom message here', | |
654 //'allowEmpty' => false, | |
655 //'required' => false, | |
656 //'last' => false, // Stop validation after this rule | |
657 //'on' => 'create', // Limit validation to 'create' or 'update' operations | |
658 ), | |
659 STRINGEND; | |
660 $this->assertPattern('/' . preg_quote(str_replace("\r\n", "\n", $expected), '/') . '/', $result); | |
661 } | |
662 | |
663 /** | |
664 * test baking relations | |
665 * | |
666 * @return void | |
667 * @access public | |
668 */ | |
669 function testBakeRelations() { | |
670 $associations = array( | |
671 'belongsTo' => array( | |
672 array( | |
673 'alias' => 'SomethingElse', | |
674 'className' => 'SomethingElse', | |
675 'foreignKey' => 'something_else_id', | |
676 ), | |
677 array( | |
678 'alias' => 'User', | |
679 'className' => 'User', | |
680 'foreignKey' => 'user_id', | |
681 ), | |
682 ), | |
683 'hasOne' => array( | |
684 array( | |
685 'alias' => 'OtherModel', | |
686 'className' => 'OtherModel', | |
687 'foreignKey' => 'other_model_id', | |
688 ), | |
689 ), | |
690 'hasMany' => array( | |
691 array( | |
692 'alias' => 'Comment', | |
693 'className' => 'Comment', | |
694 'foreignKey' => 'parent_id', | |
695 ), | |
696 ), | |
697 'hasAndBelongsToMany' => array( | |
698 array( | |
699 'alias' => 'Tag', | |
700 'className' => 'Tag', | |
701 'foreignKey' => 'article_id', | |
702 'joinTable' => 'articles_tags', | |
703 'associationForeignKey' => 'tag_id', | |
704 ), | |
705 ) | |
706 ); | |
707 $result = $this->Task->bake('Article', compact('associations')); | |
708 $this->assertPattern('/\$hasAndBelongsToMany \= array\(/', $result); | |
709 $this->assertPattern('/\$hasMany \= array\(/', $result); | |
710 $this->assertPattern('/\$belongsTo \= array\(/', $result); | |
711 $this->assertPattern('/\$hasOne \= array\(/', $result); | |
712 $this->assertPattern('/Tag/', $result); | |
713 $this->assertPattern('/OtherModel/', $result); | |
714 $this->assertPattern('/SomethingElse/', $result); | |
715 $this->assertPattern('/Comment/', $result); | |
716 } | |
717 | |
718 /** | |
719 * test bake() with a -plugin param | |
720 * | |
721 * @return void | |
722 * @access public | |
723 */ | |
724 function testBakeWithPlugin() { | |
725 $this->Task->plugin = 'ControllerTest'; | |
726 | |
727 $path = APP . 'plugins' . DS . 'controller_test' . DS . 'models' . DS . 'article.php'; | |
728 $this->Task->expectAt(0, 'createFile', array($path, '*')); | |
729 $this->Task->bake('Article', array(), array()); | |
730 | |
731 $this->Task->plugin = 'controllerTest'; | |
732 | |
733 $path = APP . 'plugins' . DS . 'controller_test' . DS . 'models' . DS . 'article.php'; | |
734 $this->Task->expectAt(1, 'createFile', array( | |
735 $path, new PatternExpectation('/Article extends ControllerTestAppModel/'))); | |
736 $this->Task->bake('Article', array(), array()); | |
737 | |
738 $this->assertEqual(count(ClassRegistry::keys()), 0); | |
739 $this->assertEqual(count(ClassRegistry::mapKeys()), 0); | |
740 } | |
741 | |
742 /** | |
743 * test that execute passes runs bake depending with named model. | |
744 * | |
745 * @return void | |
746 * @access public | |
747 */ | |
748 function testExecuteWithNamedModel() { | |
749 $this->Task->connection = 'test_suite'; | |
750 $this->Task->path = '/my/path/'; | |
751 $this->Task->args = array('article'); | |
752 $filename = '/my/path/article.php'; | |
753 $this->Task->setReturnValue('_checkUnitTest', 1); | |
754 $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class Article extends AppModel/'))); | |
755 $this->Task->execute(); | |
756 | |
757 $this->assertEqual(count(ClassRegistry::keys()), 0); | |
758 $this->assertEqual(count(ClassRegistry::mapKeys()), 0); | |
759 } | |
760 | |
761 /** | |
762 * test that execute passes with different inflections of the same name. | |
763 * | |
764 * @return void | |
765 * @access public | |
766 */ | |
767 function testExecuteWithNamedModelVariations() { | |
768 $this->Task->connection = 'test_suite'; | |
769 $this->Task->path = '/my/path/'; | |
770 $this->Task->setReturnValue('_checkUnitTest', 1); | |
771 | |
772 $this->Task->args = array('article'); | |
773 $filename = '/my/path/article.php'; | |
774 | |
775 $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class Article extends AppModel/'))); | |
776 $this->Task->execute(); | |
777 | |
778 $this->Task->args = array('Articles'); | |
779 $this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/class Article extends AppModel/'))); | |
780 $this->Task->execute(); | |
781 | |
782 $this->Task->args = array('articles'); | |
783 $this->Task->expectAt(2, 'createFile', array($filename, new PatternExpectation('/class Article extends AppModel/'))); | |
784 $this->Task->execute(); | |
785 } | |
786 | |
787 /** | |
788 * test that execute with a model name picks up hasMany associations. | |
789 * | |
790 * @return void | |
791 * @access public | |
792 */ | |
793 function testExecuteWithNamedModelHasManyCreated() { | |
794 $this->Task->connection = 'test_suite'; | |
795 $this->Task->path = '/my/path/'; | |
796 $this->Task->args = array('article'); | |
797 $filename = '/my/path/article.php'; | |
798 $this->Task->setReturnValue('_checkUnitTest', 1); | |
799 $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation("/'Comment' \=\> array\(/"))); | |
800 $this->Task->execute(); | |
801 } | |
802 | |
803 /** | |
804 * test that execute runs all() when args[0] = all | |
805 * | |
806 * @return void | |
807 * @access public | |
808 */ | |
809 function testExecuteIntoAll() { | |
810 $this->Task->connection = 'test_suite'; | |
811 $this->Task->path = '/my/path/'; | |
812 $this->Task->args = array('all'); | |
813 $this->Task->setReturnValue('_checkUnitTest', true); | |
814 | |
815 $this->Task->Fixture->expectCallCount('bake', 5); | |
816 $this->Task->Test->expectCallCount('bake', 5); | |
817 | |
818 $filename = '/my/path/article.php'; | |
819 $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class Article/'))); | |
820 | |
821 $filename = '/my/path/articles_tag.php'; | |
822 $this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/class ArticlesTag/'))); | |
823 | |
824 $filename = '/my/path/category_thread.php'; | |
825 $this->Task->expectAt(2, 'createFile', array($filename, new PatternExpectation('/class CategoryThread/'))); | |
826 | |
827 $filename = '/my/path/comment.php'; | |
828 $this->Task->expectAt(3, 'createFile', array($filename, new PatternExpectation('/class Comment/'))); | |
829 | |
830 $filename = '/my/path/tag.php'; | |
831 $this->Task->expectAt(4, 'createFile', array($filename, new PatternExpectation('/class Tag/'))); | |
832 | |
833 $this->Task->execute(); | |
834 | |
835 $this->assertEqual(count(ClassRegistry::keys()), 0); | |
836 $this->assertEqual(count(ClassRegistry::mapKeys()), 0); | |
837 } | |
838 | |
839 /** | |
840 * test that skipTables changes how all() works. | |
841 * | |
842 * @return void | |
843 */ | |
844 function testSkipTablesAndAll() { | |
845 $this->Task->connection = 'test_suite'; | |
846 $this->Task->path = '/my/path/'; | |
847 $this->Task->args = array('all'); | |
848 $this->Task->setReturnValue('_checkUnitTest', true); | |
849 $this->Task->skipTables = array('tags'); | |
850 | |
851 $this->Task->Fixture->expectCallCount('bake', 4); | |
852 $this->Task->Test->expectCallCount('bake', 4); | |
853 | |
854 $filename = '/my/path/article.php'; | |
855 $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class Article/'))); | |
856 | |
857 $filename = '/my/path/articles_tag.php'; | |
858 $this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/class ArticlesTag/'))); | |
859 | |
860 $filename = '/my/path/category_thread.php'; | |
861 $this->Task->expectAt(2, 'createFile', array($filename, new PatternExpectation('/class CategoryThread/'))); | |
862 | |
863 $filename = '/my/path/comment.php'; | |
864 $this->Task->expectAt(3, 'createFile', array($filename, new PatternExpectation('/class Comment/'))); | |
865 | |
866 $this->Task->execute(); | |
867 } | |
868 | |
869 /** | |
870 * test the interactive side of bake. | |
871 * | |
872 * @return void | |
873 * @access public | |
874 */ | |
875 function testExecuteIntoInteractive() { | |
876 $this->Task->connection = 'test_suite'; | |
877 $this->Task->path = '/my/path/'; | |
878 $this->Task->interactive = true; | |
879 | |
880 $this->Task->setReturnValueAt(0, 'in', '1'); //choose article | |
881 $this->Task->setReturnValueAt(1, 'in', 'n'); //no validation | |
882 $this->Task->setReturnValueAt(2, 'in', 'y'); //yes to associations | |
883 $this->Task->setReturnValueAt(3, 'in', 'y'); //yes to comment relation | |
884 $this->Task->setReturnValueAt(4, 'in', 'y'); //yes to user relation | |
885 $this->Task->setReturnValueAt(5, 'in', 'y'); //yes to tag relation | |
886 $this->Task->setReturnValueAt(6, 'in', 'n'); //no to additional assocs | |
887 $this->Task->setReturnValueAt(7, 'in', 'y'); //yes to looksGood? | |
888 $this->Task->setReturnValue('_checkUnitTest', true); | |
889 | |
890 $this->Task->Test->expectOnce('bake'); | |
891 $this->Task->Fixture->expectOnce('bake'); | |
892 | |
893 $filename = '/my/path/article.php'; | |
894 $this->Task->expectOnce('createFile'); | |
895 $this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class Article/'))); | |
896 $this->Task->execute(); | |
897 | |
898 $this->assertEqual(count(ClassRegistry::keys()), 0); | |
899 $this->assertEqual(count(ClassRegistry::mapKeys()), 0); | |
900 } | |
901 | |
902 /** | |
903 * test using bake interactively with a table that does not exist. | |
904 * | |
905 * @return void | |
906 * @access public | |
907 */ | |
908 function testExecuteWithNonExistantTableName() { | |
909 $this->Task->connection = 'test_suite'; | |
910 $this->Task->path = '/my/path/'; | |
911 | |
912 $this->Task->expectOnce('_stop'); | |
913 $this->Task->expectOnce('err'); | |
914 | |
915 $this->Task->setReturnValueAt(0, 'in', 'Foobar'); | |
916 $this->Task->setReturnValueAt(1, 'in', 'y'); | |
917 $this->Task->execute(); | |
918 } | |
919 } |