Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 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
 * StringFieldsTestFixture 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
 * CakeTestFixtureImportFixture class
114
 *
115
 * @package       Cake.Test.Case.TestSuite
116
 */
117
class CakeTestFixtureImportFixture extends CakeTestFixture {
118
 
119
/**
120
 * Name property
121
 *
122
 * @var string
123
 */
124
	public $name = 'ImportFixture';
125
 
126
/**
127
 * Import property
128
 *
129
 * @var mixed
130
 */
131
	public $import = array('table' => 'fixture_tests', 'connection' => 'fixture_test_suite');
132
}
133
 
134
/**
135
 * CakeTestFixtureDefaultImportFixture class
136
 *
137
 * @package       Cake.Test.Case.TestSuite
138
 */
139
class CakeTestFixtureDefaultImportFixture extends CakeTestFixture {
140
 
141
/**
142
 * Name property
143
 *
144
 * @var string
145
 */
146
	public $name = 'ImportFixture';
147
}
148
 
149
/**
150
 * FixtureImportTestModel class
151
 *
152
 * @package       Cake.Test.Case.TestSuite
153
 */
154
class FixtureImportTestModel extends Model {
155
 
156
	public $name = 'FixtureImport';
157
 
158
	public $useTable = 'fixture_tests';
159
 
160
	public $useDbConfig = 'test';
161
 
162
}
163
 
164
class FixturePrefixTest extends Model {
165
 
166
	public $name = 'FixturePrefix';
167
 
168
	public $useTable = '_tests';
169
 
170
	public $tablePrefix = 'fixture';
171
 
172
	public $useDbConfig = 'test';
173
}
174
 
175
/**
176
 * Test case for CakeTestFixture
177
 *
178
 * @package       Cake.Test.Case.TestSuite
179
 */
180
class CakeTestFixtureTest extends CakeTestCase {
181
 
182
/**
183
 * setUp method
184
 *
185
 * @return void
186
 */
187
	public function setUp() {
188
		parent::setUp();
189
		$methods = array_diff(get_class_methods('DboSource'), array('enabled'));
190
		$methods[] = 'connect';
191
 
192
		$this->criticDb = $this->getMock('DboSource', $methods);
193
		$this->criticDb->fullDebug = true;
194
		$this->db = ConnectionManager::getDataSource('test');
195
		$this->_backupConfig = $this->db->config;
196
	}
197
 
198
/**
199
 * tearDown
200
 *
201
 * @return void
202
 */
203
	public function tearDown() {
204
		parent::tearDown();
205
		unset($this->criticDb);
206
		$this->db->config = $this->_backupConfig;
207
	}
208
 
209
/**
210
 * testInit
211
 *
212
 * @return void
213
 */
214
	public function testInit() {
215
		$Fixture = new CakeTestFixtureTestFixture();
216
		unset($Fixture->table);
217
		$Fixture->init();
218
		$this->assertEquals('fixture_tests', $Fixture->table);
219
		$this->assertEquals('id', $Fixture->primaryKey);
220
 
221
		$Fixture = new CakeTestFixtureTestFixture();
222
		$Fixture->primaryKey = 'my_random_key';
223
		$Fixture->init();
224
		$this->assertEquals('my_random_key', $Fixture->primaryKey);
225
	}
226
 
227
/**
228
 * test that init() correctly sets the fixture table when the connection
229
 * or model have prefixes defined.
230
 *
231
 * @return void
232
 */
233
	public function testInitDbPrefix() {
234
		$this->skipIf($this->db instanceof Sqlite, 'Cannot open 2 connections to Sqlite');
235
		$db = ConnectionManager::getDataSource('test');
236
		$Source = new CakeTestFixtureTestFixture();
237
		$Source->drop($db);
238
		$Source->create($db);
239
		$Source->insert($db);
240
 
241
		$Fixture = new CakeTestFixtureTestFixture();
242
		$expected = array('id', 'name', 'created');
243
		$this->assertEquals($expected, array_keys($Fixture->fields));
244
 
245
		$config = $db->config;
246
		$config['prefix'] = 'fixture_test_suite_';
247
		ConnectionManager::create('fixture_test_suite', $config);
248
 
249
		$Fixture->fields = $Fixture->records = null;
250
		$Fixture->import = array('table' => 'fixture_tests', 'connection' => 'test', 'records' => true);
251
		$Fixture->init();
252
		$this->assertEquals(count($Fixture->records), count($Source->records));
253
		$Fixture->create(ConnectionManager::getDataSource('fixture_test_suite'));
254
 
255
		$Fixture = new CakeTestFixtureImportFixture();
256
		$Fixture->fields = $Fixture->records = $Fixture->table = null;
257
		$Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'test');
258
		$Fixture->init();
259
		$this->assertEquals(array('id', 'name', 'created'), array_keys($Fixture->fields));
260
		$this->assertEquals('fixture_tests', $Fixture->table);
261
 
262
		$keys = array_flip(ClassRegistry::keys());
263
		$this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys));
264
 
265
		$Fixture->drop(ConnectionManager::getDataSource('fixture_test_suite'));
266
		$Source->drop($db);
267
	}
268
 
269
/**
270
 * test that fixtures don't duplicate the test db prefix.
271
 *
272
 * @return void
273
 */
274
	public function testInitDbPrefixDuplication() {
275
		$this->skipIf($this->db instanceof Sqlite, 'Cannot open 2 connections to Sqlite');
276
		$db = ConnectionManager::getDataSource('test');
277
		$backPrefix = $db->config['prefix'];
278
		$db->config['prefix'] = 'cake_fixture_test_';
279
		ConnectionManager::create('fixture_test_suite', $db->config);
280
		$newDb = ConnectionManager::getDataSource('fixture_test_suite');
281
		$newDb->config['prefix'] = 'cake_fixture_test_';
282
 
283
		$Source = new CakeTestFixtureTestFixture();
284
		$Source->create($db);
285
		$Source->insert($db);
286
 
287
		$Fixture = new CakeTestFixtureImportFixture();
288
		$Fixture->fields = $Fixture->records = $Fixture->table = null;
289
		$Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'test');
290
 
291
		$Fixture->init();
292
		$this->assertEquals(array('id', 'name', 'created'), array_keys($Fixture->fields));
293
		$this->assertEquals('fixture_tests', $Fixture->table);
294
 
295
		$Source->drop($db);
296
		$db->config['prefix'] = $backPrefix;
297
	}
298
 
299
/**
300
 * test init with a model that has a tablePrefix declared.
301
 *
302
 * @return void
303
 */
304
	public function testInitModelTablePrefix() {
305
		$this->skipIf($this->db instanceof Sqlite, 'Cannot open 2 connections to Sqlite');
306
		$this->skipIf(!empty($this->db->config['prefix']), 'Cannot run this test, you have a database connection prefix.');
307
 
308
		$Source = new CakeTestFixtureTestFixture();
309
		$Source->create($this->db);
310
		$Source->insert($this->db);
311
 
312
		$Fixture = new CakeTestFixtureTestFixture();
313
		unset($Fixture->table);
314
		$Fixture->fields = $Fixture->records = null;
315
		$Fixture->import = array('model' => 'FixturePrefixTest', 'connection' => 'test', 'records' => false);
316
		$Fixture->init();
317
		$this->assertEquals('fixture_tests', $Fixture->table);
318
 
319
		$keys = array_flip(ClassRegistry::keys());
320
		$this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys));
321
 
322
		$Source->drop($this->db);
323
	}
324
 
325
/**
326
 * testImport
327
 *
328
 * @return void
329
 */
330
	public function testImport() {
331
		$testSuiteDb = ConnectionManager::getDataSource('test');
332
		$testSuiteConfig = $testSuiteDb->config;
333
		ConnectionManager::create('new_test_suite', array_merge($testSuiteConfig, array('prefix' => 'new_' . $testSuiteConfig['prefix'])));
334
		$newTestSuiteDb = ConnectionManager::getDataSource('new_test_suite');
335
 
336
		$Source = new CakeTestFixtureTestFixture();
337
		$Source->create($newTestSuiteDb);
338
		$Source->insert($newTestSuiteDb);
339
 
340
		$Fixture = new CakeTestFixtureDefaultImportFixture();
341
		$Fixture->fields = $Fixture->records = null;
342
		$Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'new_test_suite');
343
		$Fixture->init();
344
		$this->assertEquals(array('id', 'name', 'created'), array_keys($Fixture->fields));
345
 
346
		$keys = array_flip(ClassRegistry::keys());
347
		$this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys));
348
 
349
		$Source->drop($newTestSuiteDb);
350
	}
351
 
352
/**
353
 * test that importing with records works. Make sure to try with postgres as its
354
 * handling of aliases is a workaround at best.
355
 *
356
 * @return void
357
 */
358
	public function testImportWithRecords() {
359
		$testSuiteDb = ConnectionManager::getDataSource('test');
360
		$testSuiteConfig = $testSuiteDb->config;
361
		ConnectionManager::create('new_test_suite', array_merge($testSuiteConfig, array('prefix' => 'new_' . $testSuiteConfig['prefix'])));
362
		$newTestSuiteDb = ConnectionManager::getDataSource('new_test_suite');
363
 
364
		$Source = new CakeTestFixtureTestFixture();
365
		$Source->create($newTestSuiteDb);
366
		$Source->insert($newTestSuiteDb);
367
 
368
		$Fixture = new CakeTestFixtureDefaultImportFixture();
369
		$Fixture->fields = $Fixture->records = null;
370
		$Fixture->import = array(
371
			'model' => 'FixtureImportTestModel', 'connection' => 'new_test_suite', 'records' => true
372
		);
373
		$Fixture->init();
374
		$this->assertEquals(array('id', 'name', 'created'), array_keys($Fixture->fields));
375
		$this->assertFalse(empty($Fixture->records[0]), 'No records loaded on importing fixture.');
376
		$this->assertTrue(isset($Fixture->records[0]['name']), 'No name loaded for first record');
377
 
378
		$Source->drop($newTestSuiteDb);
379
	}
380
 
381
/**
382
 * test create method
383
 *
384
 * @return void
385
 */
386
	public function testCreate() {
387
		$Fixture = new CakeTestFixtureTestFixture();
388
		$this->criticDb->expects($this->atLeastOnce())->method('execute');
389
		$this->criticDb->expects($this->atLeastOnce())->method('createSchema');
390
		$return = $Fixture->create($this->criticDb);
391
		$this->assertTrue($this->criticDb->fullDebug);
392
		$this->assertTrue($return);
393
 
394
		unset($Fixture->fields);
395
		$return = $Fixture->create($this->criticDb);
396
		$this->assertFalse($return);
397
	}
398
 
399
/**
400
 * test the insert method
401
 *
402
 * @return void
403
 */
404
	public function testInsert() {
405
		$Fixture = new CakeTestFixtureTestFixture();
406
		$this->criticDb->expects($this->atLeastOnce())
407
			->method('insertMulti')
408
			->will($this->returnCallback(array($this, 'insertCallback')));
409
 
410
		$return = $Fixture->insert($this->criticDb);
411
		$this->assertTrue(!empty($this->insertMulti));
412
		$this->assertTrue($this->criticDb->fullDebug);
413
		$this->assertTrue($return);
414
		$this->assertEquals('fixture_tests', $this->insertMulti['table']);
415
		$this->assertEquals(array('name', 'created'), $this->insertMulti['fields']);
416
		$expected = array(
417
			array('Gandalf', '2009-04-28 19:20:00'),
418
			array('Captain Picard', '2009-04-28 19:20:00'),
419
			array('Chewbacca', '2009-04-28 19:20:00')
420
		);
421
		$this->assertEquals($expected, $this->insertMulti['values']);
422
	}
423
 
424
/**
425
 * Helper function to be used as callback and store the parameters of an insertMulti call
426
 *
427
 * @param string $table
428
 * @param string $fields
429
 * @param string $values
430
 * @return boolean true
431
 */
432
	public function insertCallback($table, $fields, $values) {
433
		$this->insertMulti['table'] = $table;
434
		$this->insertMulti['fields'] = $fields;
435
		$this->insertMulti['values'] = $values;
436
		$this->insertMulti['fields_values'] = array();
437
		foreach ($values as $record) {
438
			$this->insertMulti['fields_values'][] = array_combine($fields, $record);
439
		}
440
		return true;
441
	}
442
 
443
/**
444
 * test the insert method
445
 *
446
 * @return void
447
 */
448
	public function testInsertStrings() {
449
		$Fixture = new StringsTestFixture();
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($this->criticDb->fullDebug);
456
		$this->assertTrue($return);
457
		$this->assertEquals('strings', $this->insertMulti['table']);
458
		$this->assertEquals(array('name', 'email', 'age'), array_values($this->insertMulti['fields']));
459
		$expected = array(
460
			array('Mark Doe', 'mark.doe@email.com', null),
461
			array('John Doe', 'john.doe@email.com', 20),
462
			array('Jane Doe', 'jane.doe@email.com', 30),
463
		);
464
		$this->assertEquals($expected, $this->insertMulti['values']);
465
		$expected = array(
466
			array(
467
				'name' => 'Mark Doe',
468
				'email' => 'mark.doe@email.com',
469
				'age' => null
470
			),
471
			array(
472
				'name' => 'John Doe',
473
				'email' => 'john.doe@email.com',
474
				'age' => 20
475
			),
476
			array(
477
				'name' => 'Jane Doe',
478
				'email' => 'jane.doe@email.com',
479
				'age' => 30
480
			),
481
		);
482
		$this->assertEquals($expected, $this->insertMulti['fields_values']);
483
	}
484
 
485
/**
486
 * Test the drop method
487
 *
488
 * @return void
489
 */
490
	public function testDrop() {
491
		$Fixture = new CakeTestFixtureTestFixture();
492
		$this->criticDb->expects($this->at(1))->method('execute')->will($this->returnValue(true));
493
		$this->criticDb->expects($this->at(3))->method('execute')->will($this->returnValue(false));
494
		$this->criticDb->expects($this->exactly(2))->method('dropSchema');
495
 
496
		$return = $Fixture->drop($this->criticDb);
497
		$this->assertTrue($this->criticDb->fullDebug);
498
		$this->assertTrue($return);
499
 
500
		$return = $Fixture->drop($this->criticDb);
501
		$this->assertTrue($return);
502
 
503
		unset($Fixture->fields);
504
		$return = $Fixture->drop($this->criticDb);
505
		$this->assertFalse($return);
506
	}
507
 
508
/**
509
 * Test the truncate method.
510
 *
511
 * @return void
512
 */
513
	public function testTruncate() {
514
		$Fixture = new CakeTestFixtureTestFixture();
515
		$this->criticDb->expects($this->atLeastOnce())->method('truncate');
516
		$Fixture->truncate($this->criticDb);
517
		$this->assertTrue($this->criticDb->fullDebug);
518
	}
519
}