fake_twitter /lib/Cake/Test/Case/TestSuite/CakeTestFixtureTest.php

Language PHP Lines 493
MD5 Hash 1538f3ac7ae460616277eb2423f96ec7 Estimated Cost $6,249 (why?)
Repository https://bitbucket.org/udeshika/fake_twitter View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<?php
/**
 * CakeTestFixture file
 *
 * PHP 5
 *
 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
 * @package       Cake.Test.Case.TestSuite
 * @since         CakePHP(tm) v 1.2.0.4667
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('DboSource', 'Model/Datasource');
App::uses('Model', 'Model');
App::uses('CakeTestFixture', 'TestSuite/Fixture');

/**
 * CakeTestFixtureTestFixture class
 *
 * @package       Cake.Test.Case.TestSuite
 */
class CakeTestFixtureTestFixture extends CakeTestFixture {

/**
 * Name property
 *
 * @var string
 */
	public $name = 'FixtureTest';

/**
 * Table property
 *
 * @var string
 */
	public $table = 'fixture_tests';

/**
 * Fields array
 *
 * @var array
 */
	public $fields = array(
		'id' => array('type' => 'integer',  'key' => 'primary'),
		'name' => array('type' => 'string', 'length' => '255'),
		'created' => array('type' => 'datetime')
	);

/**
 * Records property
 *
 * @var array
 */
	public $records = array(
		array('name' => 'Gandalf', 'created' => '2009-04-28 19:20:00'),
		array('name' => 'Captain Picard', 'created' => '2009-04-28 19:20:00'),
		array('name' => 'Chewbacca', 'created' => '2009-04-28 19:20:00')
	);
}

/**
 * StringFieldsTestFixture class
 *
 * @package       Cake.Test.Case.TestSuite
 * @subpackage    cake.cake.tests.cases.libs
 */
class StringsTestFixture extends CakeTestFixture {

/**
 * Name property
 *
 * @var string
 */
	public $name = 'Strings';

/**
 * Table property
 *
 * @var string
 */
	public $table = 'strings';

/**
 * Fields array
 *
 * @var array
 */
	public $fields = array(
		'id' => array('type' => 'integer',  'key' => 'primary'),
		'name' => array('type' => 'string', 'length' => '255'),
		'email' => array('type' => 'string', 'length' => '255'),
		'age' => array('type' => 'integer', 'default' => 10)
	);

/**
 * Records property
 *
 * @var array
 */
	public $records = array(
		array('name' => 'Mark Doe', 'email' => 'mark.doe@email.com'),
		array('name' => 'John Doe', 'email' => 'john.doe@email.com', 'age' => 20),
		array('email' => 'jane.doe@email.com', 'name' => 'Jane Doe', 'age' => 30)
	);
}


/**
 * CakeTestFixtureImportFixture class
 *
 * @package       Cake.Test.Case.TestSuite
 */
class CakeTestFixtureImportFixture extends CakeTestFixture {

/**
 * Name property
 *
 * @var string
 */
	public $name = 'ImportFixture';

/**
 * Import property
 *
 * @var mixed
 */
	public $import = array('table' => 'fixture_tests', 'connection' => 'fixture_test_suite');
}

/**
 * CakeTestFixtureDefaultImportFixture class
 *
 * @package       Cake.Test.Case.TestSuite
 */
class CakeTestFixtureDefaultImportFixture extends CakeTestFixture {

/**
 * Name property
 *
 * @var string
 */
	public $name = 'ImportFixture';
}

/**
 * FixtureImportTestModel class
 *
 * @package       Cake.Test.Case.TestSuite
 * @package       Cake.Test.Case.TestSuite
 */
class FixtureImportTestModel extends Model {
	public $name = 'FixtureImport';
	public $useTable = 'fixture_tests';
	public $useDbConfig = 'test';
}

class FixturePrefixTest extends Model {
	public $name = 'FixturePrefix';
	public $useTable = '_tests';
	public $tablePrefix = 'fixture';
	public $useDbConfig = 'test';
}


/**
 * Test case for CakeTestFixture
 *
 * @package       Cake.Test.Case.TestSuite
 */
class CakeTestFixtureTest extends CakeTestCase {

/**
 * setUp method
 *
 * @return void
 */
	public function setUp() {
		$methods = array_diff(get_class_methods('DboSource'), array('enabled'));
		$methods[] = 'connect';

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

/**
 * tearDown
 *
 * @return void
 */
	public function tearDown() {
		unset($this->criticDb);
		$this->db->config = $this->_backupConfig;
	}

/**
 * testInit
 *
 * @return void
 */
	public function testInit() {
		$Fixture = new CakeTestFixtureTestFixture();
		unset($Fixture->table);
		$Fixture->init();
		$this->assertEquals($Fixture->table, 'fixture_tests');
		$this->assertEquals($Fixture->primaryKey, 'id');

		$Fixture = new CakeTestFixtureTestFixture();
		$Fixture->primaryKey = 'my_random_key';
		$Fixture->init();
		$this->assertEquals($Fixture->primaryKey, 'my_random_key');
	}


/**
 * test that init() correctly sets the fixture table when the connection or model have prefixes defined.
 *
 * @return void
 */
	public function testInitDbPrefix() {
		$this->skipIf($this->db instanceof Sqlite, 'Cannot open 2 connections to Sqlite');
		$db = ConnectionManager::getDataSource('test');
		$Source = new CakeTestFixtureTestFixture();
		$Source->drop($db);
		$Source->create($db);
		$Source->insert($db);

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

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

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

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

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

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

/**
 * test that fixtures don't duplicate the test db prefix.
 *
 * @return void
 */
	public function testInitDbPrefixDuplication() {
		$this->skipIf($this->db instanceof Sqlite, 'Cannot open 2 connections to Sqlite');
		$db = ConnectionManager::getDataSource('test');
		$backPrefix = $db->config['prefix'];
		$db->config['prefix'] = 'cake_fixture_test_';
		ConnectionManager::create('fixture_test_suite', $db->config);
		$newDb = ConnectionManager::getDataSource('fixture_test_suite');
		$newDb->config['prefix'] = 'cake_fixture_test_';

		$Source = new CakeTestFixtureTestFixture();
		$Source->create($db);
		$Source->insert($db);

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

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

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

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

		$Source = new CakeTestFixtureTestFixture();
		$Source->create($this->db);
		$Source->insert($this->db);

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

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

		$Source->drop($this->db);
	}

/**
 * testImport
 *
 * @return void
 */
	public function testImport() {
		$testSuiteDb = ConnectionManager::getDataSource('test');
		$testSuiteConfig = $testSuiteDb->config;
		ConnectionManager::create('new_test_suite', array_merge($testSuiteConfig, array('prefix' => 'new_' . $testSuiteConfig['prefix'])));
		$newTestSuiteDb = ConnectionManager::getDataSource('new_test_suite');

		$Source = new CakeTestFixtureTestFixture();
		$Source->create($newTestSuiteDb);
		$Source->insert($newTestSuiteDb);

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

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

		$Source->drop($newTestSuiteDb);
	}

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

		$Source = new CakeTestFixtureTestFixture();
		$Source->create($newTestSuiteDb);
		$Source->insert($newTestSuiteDb);


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

		$Source->drop($newTestSuiteDb);
	}

/**
 * test create method
 *
 * @return void
 */
	public function testCreate() {
		$Fixture = new CakeTestFixtureTestFixture();
		$this->criticDb->expects($this->atLeastOnce())->method('execute');
		$this->criticDb->expects($this->atLeastOnce())->method('createSchema');
		$return = $Fixture->create($this->criticDb);
		$this->assertTrue($this->criticDb->fullDebug);
		$this->assertTrue($return);

		unset($Fixture->fields);
		$return = $Fixture->create($this->criticDb);
		$this->assertFalse($return);
	}

/**
 * test the insert method
 *
 * @return void
 */
	public function testInsert() {
		$Fixture = new CakeTestFixtureTestFixture();
		$this->criticDb->expects($this->atLeastOnce())
			->method('insertMulti')
			->will($this->returnCallback(array($this, '_insertCallback')));

		$return = $Fixture->insert($this->criticDb);
		$this->assertTrue(!empty($this->insertMulti));
		$this->assertTrue($this->criticDb->fullDebug);
		$this->assertTrue($return);
		$this->assertEquals('fixture_tests', $this->insertMulti['table']);
		$this->assertEquals(array('name', 'created'), $this->insertMulti['fields']);
		$expected = array(
			array('Gandalf', '2009-04-28 19:20:00'),
			array('Captain Picard', '2009-04-28 19:20:00'),
			array('Chewbacca', '2009-04-28 19:20:00')
		);
		$this->assertEquals($expected, $this->insertMulti['values']);
	}

/**
 * Helper function to be used as callback and store the parameters of an insertMulti call
 *
 * @param string $table
 * @param string $fields
 * @param string $values
 * @return boolean true
 */
	function _insertCallback($table, $fields, $values) {
		$this->insertMulti['table'] = $table;
		$this->insertMulti['fields'] = $fields;
		$this->insertMulti['values'] = $values;
		return true;
	}

/**
 * test the insert method
 *
 * @return void
 */
	public function testInsertStrings() {
		$Fixture = new StringsTestFixture();
		$this->criticDb->expects($this->atLeastOnce())
			->method('insertMulti')
			->will($this->returnCallback(array($this, '_insertCallback')));

		$return = $Fixture->insert($this->criticDb);
		$this->assertTrue($this->criticDb->fullDebug);
		$this->assertTrue($return);
		$this->assertEquals('strings', $this->insertMulti['table']);
		$this->assertEquals(array('email', 'name', 'age'), $this->insertMulti['fields']);
		$expected = array(
			array('Mark Doe', 'mark.doe@email.com', null),
			array('John Doe', 'john.doe@email.com', 20),
			array('Jane Doe', 'jane.doe@email.com', 30),
		);
		$this->assertEquals($expected, $this->insertMulti['values']);
	}

/**
 * Test the drop method
 *
 * @return void
 */
	public function testDrop() {
		$Fixture = new CakeTestFixtureTestFixture();
		$this->criticDb->expects($this->at(1))->method('execute')->will($this->returnValue(true));
		$this->criticDb->expects($this->at(3))->method('execute')->will($this->returnValue(false));
		$this->criticDb->expects($this->exactly(2))->method('dropSchema');

		$return = $Fixture->drop($this->criticDb);
		$this->assertTrue($this->criticDb->fullDebug);
		$this->assertTrue($return);

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

		unset($Fixture->fields);
		$return = $Fixture->drop($this->criticDb);
		$this->assertFalse($return);
	}

/**
 * Test the truncate method.
 *
 * @return void
 */
	public function testTruncate() {
		$Fixture = new CakeTestFixtureTestFixture();
		$this->criticDb->expects($this->atLeastOnce())->method('truncate');
		$Fixture->truncate($this->criticDb);
		$this->assertTrue($this->criticDb->fullDebug);
	}
}
Back to Top