Mercurial > hg > Members > shoshi > webvirt
comparison cake/tests/cases/libs/cake_test_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 * CakeTestFixture file | |
4 * | |
5 * PHP versions 4 and 5 | |
6 * | |
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing> | |
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
9 * | |
10 * Licensed under The Open Group Test Suite 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://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests | |
15 * @package cake | |
16 * @subpackage cake.cake.tests.libs | |
17 * @since CakePHP(tm) v 1.2.0.4667 | |
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License | |
19 */ | |
20 App::import('Datasource', 'DboSource', false); | |
21 | |
22 /** | |
23 * CakeTestFixtureTestFixture class | |
24 * | |
25 * @package cake | |
26 * @subpackage cake.cake.tests.cases.libs | |
27 */ | |
28 class CakeTestFixtureTestFixture extends CakeTestFixture { | |
29 | |
30 /** | |
31 * name Property | |
32 * | |
33 * @var string | |
34 */ | |
35 var $name = 'FixtureTest'; | |
36 | |
37 /** | |
38 * table property | |
39 * | |
40 * @var string | |
41 */ | |
42 var $table = 'fixture_tests'; | |
43 | |
44 /** | |
45 * Fields array | |
46 * | |
47 * @var array | |
48 */ | |
49 var $fields = array( | |
50 'id' => array('type' => 'integer', 'key' => 'primary'), | |
51 'name' => array('type' => 'string', 'length' => '255'), | |
52 'created' => array('type' => 'datetime') | |
53 ); | |
54 | |
55 /** | |
56 * Records property | |
57 * | |
58 * @var array | |
59 */ | |
60 var $records = array( | |
61 array('name' => 'Gandalf', 'created' => '2009-04-28 19:20:00'), | |
62 array('name' => 'Captain Picard', 'created' => '2009-04-28 19:20:00'), | |
63 array('name' => 'Chewbacca', 'created' => '2009-04-28 19:20:00') | |
64 ); | |
65 } | |
66 | |
67 /** | |
68 * StringFieldsTestFixture class | |
69 * | |
70 * @package cake | |
71 * @subpackage cake.cake.tests.cases.libs | |
72 */ | |
73 class StringsTestFixture extends CakeTestFixture { | |
74 | |
75 /** | |
76 * name Property | |
77 * | |
78 * @var string | |
79 */ | |
80 var $name = 'Strings'; | |
81 | |
82 /** | |
83 * table property | |
84 * | |
85 * @var string | |
86 */ | |
87 var $table = 'strings'; | |
88 | |
89 /** | |
90 * Fields array | |
91 * | |
92 * @var array | |
93 */ | |
94 var $fields = array( | |
95 'id' => array('type' => 'integer', 'key' => 'primary'), | |
96 'name' => array('type' => 'string', 'length' => '255'), | |
97 'email' => array('type' => 'string', 'length' => '255'), | |
98 'age' => array('type' => 'integer', 'default' => 10) | |
99 ); | |
100 | |
101 /** | |
102 * Records property | |
103 * | |
104 * @var array | |
105 */ | |
106 var $records = array( | |
107 array('name' => 'John Doe', 'email' => 'john.doe@email.com', 'age' => 20), | |
108 array('email' => 'jane.doe@email.com', 'name' => 'Jane Doe', 'age' => 30), | |
109 array('name' => 'Mark Doe', 'email' => 'mark.doe@email.com') | |
110 ); | |
111 } | |
112 | |
113 | |
114 /** | |
115 * CakeTestFixtureImportFixture class | |
116 * | |
117 * @package cake | |
118 * @subpackage cake.cake.tests.cases.libs | |
119 */ | |
120 class CakeTestFixtureImportFixture extends CakeTestFixture { | |
121 | |
122 /** | |
123 * Name property | |
124 * | |
125 * @var string | |
126 */ | |
127 var $name = 'ImportFixture'; | |
128 | |
129 /** | |
130 * Import property | |
131 * | |
132 * @var mixed | |
133 */ | |
134 var $import = array('table' => 'fixture_tests', 'connection' => 'test_suite'); | |
135 } | |
136 | |
137 /** | |
138 * CakeTestFixtureDefaultImportFixture class | |
139 * | |
140 * @package cake | |
141 * @subpackage cake.cake.tests.cases.libs | |
142 */ | |
143 class CakeTestFixtureDefaultImportFixture extends CakeTestFixture { | |
144 | |
145 /** | |
146 * Name property | |
147 * | |
148 * @var string | |
149 */ | |
150 var $name = 'ImportFixture'; | |
151 } | |
152 | |
153 /** | |
154 * FixtureImportTestModel class | |
155 * | |
156 * @package default | |
157 * @subpackage cake.cake.tests.cases.libs. | |
158 */ | |
159 class FixtureImportTestModel extends Model { | |
160 var $name = 'FixtureImport'; | |
161 var $useTable = 'fixture_tests'; | |
162 var $useDbConfig = 'test_suite'; | |
163 } | |
164 | |
165 class FixturePrefixTest extends Model { | |
166 var $name = 'FixturePrefix'; | |
167 var $useTable = '_tests'; | |
168 var $tablePrefix = 'fixture'; | |
169 var $useDbConfig = 'test_suite'; | |
170 } | |
171 | |
172 Mock::generate('DboSource', 'BaseFixtureMockDboSource'); | |
173 | |
174 class FixtureMockDboSource extends BaseFixtureMockDboSource { | |
175 var $insertMulti; | |
176 | |
177 function value($string) { | |
178 return is_string($string) ? '\'' . $string . '\'' : $string; | |
179 } | |
180 | |
181 function insertMulti($table, $fields, $values) { | |
182 $this->insertMulti = compact('table', 'fields', 'values'); | |
183 return true; | |
184 } | |
185 } | |
186 | |
187 /** | |
188 * Test case for CakeTestFixture | |
189 * | |
190 * @package cake | |
191 * @subpackage cake.cake.tests.cases.libs | |
192 */ | |
193 class CakeTestFixtureTest extends CakeTestCase { | |
194 | |
195 /** | |
196 * setUp method | |
197 * | |
198 * @access public | |
199 * @return void | |
200 */ | |
201 function setUp() { | |
202 $this->criticDb =& new FixtureMockDboSource(); | |
203 $this->criticDb->fullDebug = true; | |
204 } | |
205 | |
206 /** | |
207 * tearDown | |
208 * | |
209 * @access public | |
210 * @return void | |
211 */ | |
212 function tearDown() { | |
213 unset($this->criticDb); | |
214 } | |
215 | |
216 /** | |
217 * testInit | |
218 * | |
219 * @access public | |
220 * @return void | |
221 */ | |
222 function testInit() { | |
223 $Fixture =& new CakeTestFixtureTestFixture(); | |
224 unset($Fixture->table); | |
225 $Fixture->init(); | |
226 $this->assertEqual($Fixture->table, 'fixture_tests'); | |
227 $this->assertEqual($Fixture->primaryKey, 'id'); | |
228 | |
229 $Fixture =& new CakeTestFixtureTestFixture(); | |
230 $Fixture->primaryKey = 'my_random_key'; | |
231 $Fixture->init(); | |
232 $this->assertEqual($Fixture->primaryKey, 'my_random_key'); | |
233 } | |
234 | |
235 /** | |
236 * test that init() correctly sets the fixture table when the connection or model have prefixes defined. | |
237 * | |
238 * @return void | |
239 */ | |
240 function testInitDbPrefix() { | |
241 $this->_initDb(); | |
242 $Source =& new CakeTestFixtureTestFixture(); | |
243 $Source->create($this->db); | |
244 $Source->insert($this->db); | |
245 | |
246 $Fixture =& new CakeTestFixtureImportFixture(); | |
247 $expected = array('id', 'name', 'created'); | |
248 $this->assertEqual(array_keys($Fixture->fields), $expected); | |
249 | |
250 $db =& ConnectionManager::getDataSource('test_suite'); | |
251 $config = $db->config; | |
252 $config['prefix'] = 'fixture_test_suite_'; | |
253 ConnectionManager::create('fixture_test_suite', $config); | |
254 | |
255 $Fixture->fields = $Fixture->records = null; | |
256 $Fixture->import = array('table' => 'fixture_tests', 'connection' => 'test_suite', 'records' => true); | |
257 $Fixture->init(); | |
258 $this->assertEqual(count($Fixture->records), count($Source->records)); | |
259 | |
260 $Fixture =& new CakeTestFixtureImportFixture(); | |
261 $Fixture->fields = $Fixture->records = $Fixture->table = null; | |
262 $Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'test_suite'); | |
263 $Fixture->init(); | |
264 $this->assertEqual(array_keys($Fixture->fields), array('id', 'name', 'created')); | |
265 $this->assertEqual($Fixture->table, 'fixture_tests'); | |
266 | |
267 $keys = array_flip(ClassRegistry::keys()); | |
268 $this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys)); | |
269 | |
270 $Source->drop($this->db); | |
271 } | |
272 | |
273 /** | |
274 * test that fixtures don't duplicate the test db prefix. | |
275 * | |
276 * @return void | |
277 */ | |
278 function testInitDbPrefixDuplication() { | |
279 $this->_initDb(); | |
280 $backPrefix = $this->db->config['prefix']; | |
281 $this->db->config['prefix'] = 'cake_fixture_test_'; | |
282 | |
283 $Source =& new CakeTestFixtureTestFixture(); | |
284 $Source->create($this->db); | |
285 $Source->insert($this->db); | |
286 | |
287 $Fixture =& new CakeTestFixtureImportFixture(); | |
288 $Fixture->fields = $Fixture->records = $Fixture->table = null; | |
289 $Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'test_suite'); | |
290 | |
291 $Fixture->init(); | |
292 $this->assertEqual(array_keys($Fixture->fields), array('id', 'name', 'created')); | |
293 $this->assertEqual($Fixture->table, 'fixture_tests'); | |
294 | |
295 $Source->drop($this->db); | |
296 $this->db->config['prefix'] = $backPrefix; | |
297 } | |
298 | |
299 /** | |
300 * test init with a model that has a tablePrefix declared. | |
301 * | |
302 * @return void | |
303 */ | |
304 function testInitModelTablePrefix() { | |
305 $this->_initDb(); | |
306 $hasPrefix = !empty($this->db->config['prefix']); | |
307 if ($this->skipIf($hasPrefix, 'Cannot run this test, you have a database connection prefix.')) { | |
308 return; | |
309 } | |
310 $Source =& new CakeTestFixtureTestFixture(); | |
311 $Source->create($this->db); | |
312 $Source->insert($this->db); | |
313 | |
314 $Fixture =& new CakeTestFixtureImportFixture(); | |
315 unset($Fixture->table); | |
316 $Fixture->fields = $Fixture->records = null; | |
317 $Fixture->import = array('model' => 'FixturePrefixTest', 'connection' => 'test_suite', 'records' => false); | |
318 $Fixture->init(); | |
319 $this->assertEqual($Fixture->table, 'fixture_tests'); | |
320 | |
321 $keys = array_flip(ClassRegistry::keys()); | |
322 $this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys)); | |
323 | |
324 $Source->drop($this->db); | |
325 } | |
326 | |
327 /** | |
328 * testImport | |
329 * | |
330 * @access public | |
331 * @return void | |
332 */ | |
333 function testImport() { | |
334 $this->_initDb(); | |
335 | |
336 $defaultDb =& ConnectionManager::getDataSource('default'); | |
337 $testSuiteDb =& ConnectionManager::getDataSource('test_suite'); | |
338 $defaultConfig = $defaultDb->config; | |
339 $testSuiteConfig = $testSuiteDb->config; | |
340 ConnectionManager::create('new_test_suite', array_merge($testSuiteConfig, array('prefix' => 'new_' . $testSuiteConfig['prefix']))); | |
341 $newTestSuiteDb =& ConnectionManager::getDataSource('new_test_suite'); | |
342 | |
343 $Source =& new CakeTestFixtureTestFixture(); | |
344 $Source->create($newTestSuiteDb); | |
345 $Source->insert($newTestSuiteDb); | |
346 | |
347 $defaultDb->config = $newTestSuiteDb->config; | |
348 | |
349 $Fixture =& new CakeTestFixtureDefaultImportFixture(); | |
350 $Fixture->fields = $Fixture->records = null; | |
351 $Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'new_test_suite'); | |
352 $Fixture->init(); | |
353 $this->assertEqual(array_keys($Fixture->fields), array('id', 'name', 'created')); | |
354 | |
355 $defaultDb->config = $defaultConfig; | |
356 | |
357 $keys = array_flip(ClassRegistry::keys()); | |
358 $this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys)); | |
359 | |
360 $Source->drop($newTestSuiteDb); | |
361 } | |
362 | |
363 /** | |
364 * test that importing with records works. Make sure to try with postgres as its | |
365 * handling of aliases is a workaround at best. | |
366 * | |
367 * @return void | |
368 */ | |
369 function testImportWithRecords() { | |
370 $this->_initDb(); | |
371 | |
372 $defaultDb =& ConnectionManager::getDataSource('default'); | |
373 $testSuiteDb =& ConnectionManager::getDataSource('test_suite'); | |
374 $defaultConfig = $defaultDb->config; | |
375 $testSuiteConfig = $testSuiteDb->config; | |
376 ConnectionManager::create('new_test_suite', array_merge($testSuiteConfig, array('prefix' => 'new_' . $testSuiteConfig['prefix']))); | |
377 $newTestSuiteDb =& ConnectionManager::getDataSource('new_test_suite'); | |
378 | |
379 $Source =& new CakeTestFixtureTestFixture(); | |
380 $Source->create($newTestSuiteDb); | |
381 $Source->insert($newTestSuiteDb); | |
382 | |
383 $defaultDb->config = $newTestSuiteDb->config; | |
384 | |
385 $Fixture =& new CakeTestFixtureDefaultImportFixture(); | |
386 $Fixture->fields = $Fixture->records = null; | |
387 $Fixture->import = array( | |
388 'model' => 'FixtureImportTestModel', 'connection' => 'new_test_suite', 'records' => true | |
389 ); | |
390 $Fixture->init(); | |
391 $this->assertEqual(array_keys($Fixture->fields), array('id', 'name', 'created')); | |
392 $this->assertFalse(empty($Fixture->records[0]), 'No records loaded on importing fixture.'); | |
393 $this->assertTrue(isset($Fixture->records[0]['name']), 'No name loaded for first record'); | |
394 | |
395 $defaultDb->config = $defaultConfig; | |
396 | |
397 $Source->drop($newTestSuiteDb); | |
398 } | |
399 | |
400 /** | |
401 * test create method | |
402 * | |
403 * @access public | |
404 * @return void | |
405 */ | |
406 function testCreate() { | |
407 $Fixture =& new CakeTestFixtureTestFixture(); | |
408 $this->criticDb->expectAtLeastOnce('execute'); | |
409 $this->criticDb->expectAtLeastOnce('createSchema'); | |
410 $return = $Fixture->create($this->criticDb); | |
411 $this->assertTrue($this->criticDb->fullDebug); | |
412 $this->assertTrue($return); | |
413 | |
414 unset($Fixture->fields); | |
415 $return = $Fixture->create($this->criticDb); | |
416 $this->assertFalse($return); | |
417 } | |
418 | |
419 /** | |
420 * test the insert method | |
421 * | |
422 * @access public | |
423 * @return void | |
424 */ | |
425 function testInsert() { | |
426 $Fixture =& new CakeTestFixtureTestFixture(); | |
427 | |
428 $this->criticDb->insertMulti = array(); | |
429 $return = $Fixture->insert($this->criticDb); | |
430 $this->assertTrue(!empty($this->criticDb->insertMulti)); | |
431 $this->assertTrue($this->criticDb->fullDebug); | |
432 $this->assertTrue($return); | |
433 $this->assertEqual('fixture_tests', $this->criticDb->insertMulti['table']); | |
434 $this->assertEqual(array('name', 'created'), $this->criticDb->insertMulti['fields']); | |
435 $expected = array( | |
436 '(\'Gandalf\', \'2009-04-28 19:20:00\')', | |
437 '(\'Captain Picard\', \'2009-04-28 19:20:00\')', | |
438 '(\'Chewbacca\', \'2009-04-28 19:20:00\')' | |
439 ); | |
440 $this->assertEqual($expected, $this->criticDb->insertMulti['values']); | |
441 } | |
442 | |
443 /** | |
444 * test the insert method | |
445 * | |
446 * @access public | |
447 * @return void | |
448 */ | |
449 function testInsertStrings() { | |
450 $Fixture =& new StringsTestFixture(); | |
451 | |
452 $this->criticDb->insertMulti = array(); | |
453 $return = $Fixture->insert($this->criticDb); | |
454 $this->assertTrue(!empty($this->criticDb->insertMulti)); | |
455 $this->assertTrue($this->criticDb->fullDebug); | |
456 $this->assertTrue($return); | |
457 $this->assertEqual('strings', $this->criticDb->insertMulti['table']); | |
458 $this->assertEqual(array('name', 'email', 'age'), $this->criticDb->insertMulti['fields']); | |
459 $expected = array( | |
460 '(\'John Doe\', \'john.doe@email.com\', 20)', | |
461 '(\'Jane Doe\', \'jane.doe@email.com\', 30)', | |
462 '(\'Mark Doe\', \'mark.doe@email.com\', NULL)', | |
463 ); | |
464 $this->assertEqual($expected, $this->criticDb->insertMulti['values']); | |
465 } | |
466 | |
467 /** | |
468 * Test the drop method | |
469 * | |
470 * @access public | |
471 * @return void | |
472 */ | |
473 function testDrop() { | |
474 $Fixture =& new CakeTestFixtureTestFixture(); | |
475 $this->criticDb->setReturnValueAt(0, 'execute', true); | |
476 $this->criticDb->expectAtLeastOnce('execute'); | |
477 $this->criticDb->expectAtLeastOnce('dropSchema'); | |
478 | |
479 $return = $Fixture->drop($this->criticDb); | |
480 $this->assertTrue($this->criticDb->fullDebug); | |
481 $this->assertTrue($return); | |
482 | |
483 $this->criticDb->setReturnValueAt(1, 'execute', false); | |
484 $return = $Fixture->drop($this->criticDb); | |
485 $this->assertFalse($return); | |
486 | |
487 unset($Fixture->fields); | |
488 $return = $Fixture->drop($this->criticDb); | |
489 $this->assertFalse($return); | |
490 } | |
491 | |
492 /** | |
493 * Test the truncate method. | |
494 * | |
495 * @access public | |
496 * @return void | |
497 */ | |
498 function testTruncate() { | |
499 $Fixture =& new CakeTestFixtureTestFixture(); | |
500 $this->criticDb->expectAtLeastOnce('truncate'); | |
501 $Fixture->truncate($this->criticDb); | |
502 $this->assertTrue($this->criticDb->fullDebug); | |
503 } | |
504 } |