統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Test / Case / TestSuite / CakeTestFixtureTest.php @ 635eef61

履歴 | 表示 | アノテート | ダウンロード (15.222 KB)

1
<?php
2
/**
3
 * CakeTestFixture file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.TestSuite
15
 * @since         CakePHP(tm) v 1.2.0.4667
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18

    
19
App::uses('DboSource', 'Model/Datasource');
20
App::uses('Model', 'Model');
21
App::uses('CakeTestFixture', 'TestSuite/Fixture');
22

    
23
/**
24
 * CakeTestFixtureTestFixture class
25
 *
26
 * @package       Cake.Test.Case.TestSuite
27
 */
28
class CakeTestFixtureTestFixture extends CakeTestFixture {
29

    
30
/**
31
 * Name property
32
 *
33
 * @var string
34
 */
35
        public $name = 'FixtureTest';
36

    
37
/**
38
 * Table property
39
 *
40
 * @var string
41
 */
42
        public $table = 'fixture_tests';
43

    
44
/**
45
 * Fields array
46
 *
47
 * @var array
48
 */
49
        public $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
        public $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
 * StringTestFixture class
69
 *
70
 * @package       Cake.Test.Case.TestSuite
71
 */
72
class StringsTestFixture extends CakeTestFixture {
73

    
74
/**
75
 * Name property
76
 *
77
 * @var string
78
 */
79
        public $name = 'Strings';
80

    
81
/**
82
 * Table property
83
 *
84
 * @var string
85
 */
86
        public $table = 'strings';
87

    
88
/**
89
 * Fields array
90
 *
91
 * @var array
92
 */
93
        public $fields = array(
94
                'id' => array('type' => 'integer', 'key' => 'primary'),
95
                'name' => array('type' => 'string', 'length' => '255'),
96
                'email' => array('type' => 'string', 'length' => '255'),
97
                'age' => array('type' => 'integer', 'default' => 10)
98
        );
99

    
100
/**
101
 * Records property
102
 *
103
 * @var array
104
 */
105
        public $records = array(
106
                array('name' => 'Mark Doe', 'email' => 'mark.doe@email.com'),
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
        );
110
}
111

    
112
/**
113
 * InvalidTestFixture class
114
 *
115
 * @package       Cake.Test.Case.TestSuite
116
 */
117
class InvalidTestFixture extends CakeTestFixture {
118

    
119
/**
120
 * Name property
121
 *
122
 * @var string
123
 */
124
        public $name = 'Invalid';
125

    
126
/**
127
 * Table property
128
 *
129
 * @var string
130
 */
131
        public $table = 'invalid';
132

    
133
/**
134
 * Fields array - missing "email" row
135
 *
136
 * @var array
137
 */
138
        public $fields = array(
139
                'id' => array('type' => 'integer', 'key' => 'primary'),
140
                'name' => array('type' => 'string', 'length' => '255'),
141
                'age' => array('type' => 'integer', 'default' => 10)
142
        );
143

    
144
/**
145
 * Records property
146
 *
147
 * @var array
148
 */
149
        public $records = array(
150
                array('name' => 'Mark Doe', 'email' => 'mark.doe@email.com'),
151
                array('name' => 'John Doe', 'email' => 'john.doe@email.com', 'age' => 20),
152
                array('email' => 'jane.doe@email.com', 'name' => 'Jane Doe', 'age' => 30)
153
        );
154
}
155

    
156
/**
157
 * CakeTestFixtureImportFixture class
158
 *
159
 * @package       Cake.Test.Case.TestSuite
160
 */
161
class CakeTestFixtureImportFixture extends CakeTestFixture {
162

    
163
/**
164
 * Name property
165
 *
166
 * @var string
167
 */
168
        public $name = 'ImportFixture';
169

    
170
/**
171
 * Import property
172
 *
173
 * @var mixed
174
 */
175
        public $import = array('table' => 'fixture_tests', 'connection' => 'fixture_test_suite');
176
}
177

    
178
/**
179
 * CakeTestFixtureDefaultImportFixture class
180
 *
181
 * @package       Cake.Test.Case.TestSuite
182
 */
183
class CakeTestFixtureDefaultImportFixture extends CakeTestFixture {
184

    
185
/**
186
 * Name property
187
 *
188
 * @var string
189
 */
190
        public $name = 'ImportFixture';
191
}
192

    
193
/**
194
 * FixtureImportTestModel class
195
 *
196
 * @package       Cake.Test.Case.TestSuite
197
 */
198
class FixtureImportTestModel extends Model {
199

    
200
        public $name = 'FixtureImport';
201

    
202
        public $useTable = 'fixture_tests';
203

    
204
        public $useDbConfig = 'test';
205

    
206
}
207

    
208
class FixturePrefixTest extends Model {
209

    
210
        public $name = 'FixturePrefix';
211

    
212
        public $useTable = '_tests';
213

    
214
        public $tablePrefix = 'fixture';
215

    
216
        public $useDbConfig = 'test';
217
}
218

    
219
/**
220
 * Test case for CakeTestFixture
221
 *
222
 * @package       Cake.Test.Case.TestSuite
223
 */
224
class CakeTestFixtureTest extends CakeTestCase {
225

    
226
/**
227
 * setUp method
228
 *
229
 * @return void
230
 */
231
        public function setUp() {
232
                parent::setUp();
233
                $methods = array_diff(get_class_methods('DboSource'), array('enabled'));
234
                $methods[] = 'connect';
235

    
236
                $this->criticDb = $this->getMock('DboSource', $methods);
237
                $this->criticDb->fullDebug = true;
238
                $this->db = ConnectionManager::getDataSource('test');
239
                $this->_backupConfig = $this->db->config;
240
        }
241

    
242
/**
243
 * tearDown
244
 *
245
 * @return void
246
 */
247
        public function tearDown() {
248
                parent::tearDown();
249
                unset($this->criticDb);
250
                $this->db->config = $this->_backupConfig;
251
        }
252

    
253
/**
254
 * testInit
255
 *
256
 * @return void
257
 */
258
        public function testInit() {
259
                $Fixture = new CakeTestFixtureTestFixture();
260
                unset($Fixture->table);
261
                $Fixture->init();
262
                $this->assertEquals('fixture_tests', $Fixture->table);
263
                $this->assertEquals('id', $Fixture->primaryKey);
264

    
265
                $Fixture = new CakeTestFixtureTestFixture();
266
                $Fixture->primaryKey = 'my_random_key';
267
                $Fixture->init();
268
                $this->assertEquals('my_random_key', $Fixture->primaryKey);
269
        }
270

    
271
/**
272
 * test that init() correctly sets the fixture table when the connection
273
 * or model have prefixes defined.
274
 *
275
 * @return void
276
 */
277
        public function testInitDbPrefix() {
278
                $this->skipIf($this->db instanceof Sqlite, 'Cannot open 2 connections to Sqlite');
279
                $db = ConnectionManager::getDataSource('test');
280
                $Source = new CakeTestFixtureTestFixture();
281
                $Source->drop($db);
282
                $Source->create($db);
283
                $Source->insert($db);
284

    
285
                $Fixture = new CakeTestFixtureTestFixture();
286
                $expected = array('id', 'name', 'created');
287
                $this->assertEquals($expected, array_keys($Fixture->fields));
288

    
289
                $config = $db->config;
290
                $config['prefix'] = 'fixture_test_suite_';
291
                ConnectionManager::create('fixture_test_suite', $config);
292

    
293
                $Fixture->fields = $Fixture->records = null;
294
                $Fixture->import = array('table' => 'fixture_tests', 'connection' => 'test', 'records' => true);
295
                $Fixture->init();
296
                $this->assertEquals(count($Fixture->records), count($Source->records));
297
                $Fixture->create(ConnectionManager::getDataSource('fixture_test_suite'));
298

    
299
                $Fixture = new CakeTestFixtureImportFixture();
300
                $Fixture->fields = $Fixture->records = $Fixture->table = null;
301
                $Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'test');
302
                $Fixture->init();
303
                $this->assertEquals(array('id', 'name', 'created'), array_keys($Fixture->fields));
304
                $this->assertEquals('fixture_tests', $Fixture->table);
305

    
306
                $keys = array_flip(ClassRegistry::keys());
307
                $this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys));
308

    
309
                $Fixture->drop(ConnectionManager::getDataSource('fixture_test_suite'));
310
                $Source->drop($db);
311
        }
312

    
313
/**
314
 * test that fixtures don't duplicate the test db prefix.
315
 *
316
 * @return void
317
 */
318
        public function testInitDbPrefixDuplication() {
319
                $this->skipIf($this->db instanceof Sqlite, 'Cannot open 2 connections to Sqlite');
320
                $db = ConnectionManager::getDataSource('test');
321
                $backPrefix = $db->config['prefix'];
322
                $db->config['prefix'] = 'cake_fixture_test_';
323
                ConnectionManager::create('fixture_test_suite', $db->config);
324
                $newDb = ConnectionManager::getDataSource('fixture_test_suite');
325
                $newDb->config['prefix'] = 'cake_fixture_test_';
326

    
327
                $Source = new CakeTestFixtureTestFixture();
328
                $Source->create($db);
329
                $Source->insert($db);
330

    
331
                $Fixture = new CakeTestFixtureImportFixture();
332
                $Fixture->fields = $Fixture->records = $Fixture->table = null;
333
                $Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'test');
334

    
335
                $Fixture->init();
336
                $this->assertEquals(array('id', 'name', 'created'), array_keys($Fixture->fields));
337
                $this->assertEquals('fixture_tests', $Fixture->table);
338

    
339
                $Source->drop($db);
340
                $db->config['prefix'] = $backPrefix;
341
        }
342

    
343
/**
344
 * test init with a model that has a tablePrefix declared.
345
 *
346
 * @return void
347
 */
348
        public function testInitModelTablePrefix() {
349
                $this->skipIf($this->db instanceof Sqlite, 'Cannot open 2 connections to Sqlite');
350
                $this->skipIf(!empty($this->db->config['prefix']), 'Cannot run this test, you have a database connection prefix.');
351

    
352
                $Source = new CakeTestFixtureTestFixture();
353
                $Source->create($this->db);
354
                $Source->insert($this->db);
355

    
356
                $Fixture = new CakeTestFixtureTestFixture();
357
                unset($Fixture->table);
358
                $Fixture->fields = $Fixture->records = null;
359
                $Fixture->import = array('model' => 'FixturePrefixTest', 'connection' => 'test', 'records' => false);
360
                $Fixture->init();
361
                $this->assertEquals('fixture_tests', $Fixture->table);
362

    
363
                $keys = array_flip(ClassRegistry::keys());
364
                $this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys));
365

    
366
                $Source->drop($this->db);
367
        }
368

    
369
/**
370
 * testImport
371
 *
372
 * @return void
373
 */
374
        public function testImport() {
375
                $testSuiteDb = ConnectionManager::getDataSource('test');
376
                $testSuiteConfig = $testSuiteDb->config;
377
                ConnectionManager::create('new_test_suite', array_merge($testSuiteConfig, array('prefix' => 'new_' . $testSuiteConfig['prefix'])));
378
                $newTestSuiteDb = ConnectionManager::getDataSource('new_test_suite');
379

    
380
                $Source = new CakeTestFixtureTestFixture();
381
                $Source->create($newTestSuiteDb);
382
                $Source->insert($newTestSuiteDb);
383

    
384
                $Fixture = new CakeTestFixtureDefaultImportFixture();
385
                $Fixture->fields = $Fixture->records = null;
386
                $Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'new_test_suite');
387
                $Fixture->init();
388
                $this->assertEquals(array('id', 'name', 'created'), array_keys($Fixture->fields));
389

    
390
                $keys = array_flip(ClassRegistry::keys());
391
                $this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys));
392

    
393
                $Source->drop($newTestSuiteDb);
394
        }
395

    
396
/**
397
 * test that importing with records works. Make sure to try with postgres as its
398
 * handling of aliases is a workaround at best.
399
 *
400
 * @return void
401
 */
402
        public function testImportWithRecords() {
403
                $testSuiteDb = ConnectionManager::getDataSource('test');
404
                $testSuiteConfig = $testSuiteDb->config;
405
                ConnectionManager::create('new_test_suite', array_merge($testSuiteConfig, array('prefix' => 'new_' . $testSuiteConfig['prefix'])));
406
                $newTestSuiteDb = ConnectionManager::getDataSource('new_test_suite');
407

    
408
                $Source = new CakeTestFixtureTestFixture();
409
                $Source->create($newTestSuiteDb);
410
                $Source->insert($newTestSuiteDb);
411

    
412
                $Fixture = new CakeTestFixtureDefaultImportFixture();
413
                $Fixture->fields = $Fixture->records = null;
414
                $Fixture->import = array(
415
                        'model' => 'FixtureImportTestModel', 'connection' => 'new_test_suite', 'records' => true
416
                );
417
                $Fixture->init();
418
                $this->assertEquals(array('id', 'name', 'created'), array_keys($Fixture->fields));
419
                $this->assertFalse(empty($Fixture->records[0]), 'No records loaded on importing fixture.');
420
                $this->assertTrue(isset($Fixture->records[0]['name']), 'No name loaded for first record');
421

    
422
                $Source->drop($newTestSuiteDb);
423
        }
424

    
425
/**
426
 * test create method
427
 *
428
 * @return void
429
 */
430
        public function testCreate() {
431
                $Fixture = new CakeTestFixtureTestFixture();
432
                $this->criticDb->expects($this->atLeastOnce())->method('execute');
433
                $this->criticDb->expects($this->atLeastOnce())->method('createSchema');
434
                $return = $Fixture->create($this->criticDb);
435
                $this->assertTrue($this->criticDb->fullDebug);
436
                $this->assertTrue($return);
437

    
438
                unset($Fixture->fields);
439
                $return = $Fixture->create($this->criticDb);
440
                $this->assertFalse($return);
441
        }
442

    
443
/**
444
 * test the insert method
445
 *
446
 * @return void
447
 */
448
        public function testInsert() {
449
                $Fixture = new CakeTestFixtureTestFixture();
450
                $this->criticDb->expects($this->atLeastOnce())
451
                        ->method('insertMulti')
452
                        ->will($this->returnCallback(array($this, 'insertCallback')));
453

    
454
                $return = $Fixture->insert($this->criticDb);
455
                $this->assertTrue(!empty($this->insertMulti));
456
                $this->assertTrue($this->criticDb->fullDebug);
457
                $this->assertTrue($return);
458
                $this->assertEquals('fixture_tests', $this->insertMulti['table']);
459
                $this->assertEquals(array('name', 'created'), $this->insertMulti['fields']);
460
                $expected = array(
461
                        array('Gandalf', '2009-04-28 19:20:00'),
462
                        array('Captain Picard', '2009-04-28 19:20:00'),
463
                        array('Chewbacca', '2009-04-28 19:20:00')
464
                );
465
                $this->assertEquals($expected, $this->insertMulti['values']);
466
        }
467

    
468
/**
469
 * Helper function to be used as callback and store the parameters of an insertMulti call
470
 *
471
 * @param string $table
472
 * @param string $fields
473
 * @param string $values
474
 * @return bool true
475
 */
476
        public function insertCallback($table, $fields, $values) {
477
                $this->insertMulti['table'] = $table;
478
                $this->insertMulti['fields'] = $fields;
479
                $this->insertMulti['values'] = $values;
480
                $this->insertMulti['fields_values'] = array();
481
                foreach ($values as $record) {
482
                        $this->insertMulti['fields_values'][] = array_combine($fields, $record);
483
                }
484
                return true;
485
        }
486

    
487
/**
488
 * test the insert method
489
 *
490
 * @return void
491
 */
492
        public function testInsertStrings() {
493
                $Fixture = new StringsTestFixture();
494
                $this->criticDb->expects($this->atLeastOnce())
495
                        ->method('insertMulti')
496
                        ->will($this->returnCallback(array($this, 'insertCallback')));
497

    
498
                $return = $Fixture->insert($this->criticDb);
499
                $this->assertTrue($this->criticDb->fullDebug);
500
                $this->assertTrue($return);
501
                $this->assertEquals('strings', $this->insertMulti['table']);
502
                $this->assertEquals(array('name', 'email', 'age'), array_values($this->insertMulti['fields']));
503
                $expected = array(
504
                        array('Mark Doe', 'mark.doe@email.com', null),
505
                        array('John Doe', 'john.doe@email.com', 20),
506
                        array('Jane Doe', 'jane.doe@email.com', 30),
507
                );
508
                $this->assertEquals($expected, $this->insertMulti['values']);
509
                $expected = array(
510
                        array(
511
                                'name' => 'Mark Doe',
512
                                'email' => 'mark.doe@email.com',
513
                                'age' => null
514
                        ),
515
                        array(
516
                                'name' => 'John Doe',
517
                                'email' => 'john.doe@email.com',
518
                                'age' => 20
519
                        ),
520
                        array(
521
                                'name' => 'Jane Doe',
522
                                'email' => 'jane.doe@email.com',
523
                                'age' => 30
524
                        ),
525
                );
526
                $this->assertEquals($expected, $this->insertMulti['fields_values']);
527
        }
528

    
529
/**
530
 * test the insert method with invalid fixture
531
 *
532
 * @expectedException CakeException
533
 * @return void
534
 */
535
        public function testInsertInvalid() {
536
                $Fixture = new InvalidTestFixture();
537
                $Fixture->insert($this->criticDb);
538
        }
539

    
540
/**
541
 * Test the drop method
542
 *
543
 * @return void
544
 */
545
        public function testDrop() {
546
                $Fixture = new CakeTestFixtureTestFixture();
547
                $this->criticDb->expects($this->at(1))->method('execute')->will($this->returnValue(true));
548
                $this->criticDb->expects($this->at(3))->method('execute')->will($this->returnValue(false));
549
                $this->criticDb->expects($this->exactly(2))->method('dropSchema');
550

    
551
                $return = $Fixture->drop($this->criticDb);
552
                $this->assertTrue($this->criticDb->fullDebug);
553
                $this->assertTrue($return);
554

    
555
                $return = $Fixture->drop($this->criticDb);
556
                $this->assertTrue($return);
557

    
558
                unset($Fixture->fields);
559
                $return = $Fixture->drop($this->criticDb);
560
                $this->assertFalse($return);
561
        }
562

    
563
/**
564
 * Test the truncate method.
565
 *
566
 * @return void
567
 */
568
        public function testTruncate() {
569
                $Fixture = new CakeTestFixtureTestFixture();
570
                $this->criticDb->expects($this->atLeastOnce())->method('truncate');
571
                $Fixture->truncate($this->criticDb);
572
                $this->assertTrue($this->criticDb->fullDebug);
573
        }
574
}