From 073d04931a1f0d631716bba730dd100c094fca76 Mon Sep 17 00:00:00 2001 From: Rachman Chavik Date: Thu, 27 Oct 2011 18:37:46 +0700 Subject: [PATCH] Adding tests for loading multischema fixtures --- .../Test/Case/Model/ModelIntegrationTest.php | 78 +++++++++++++++++++ lib/Cake/Test/Case/Model/ModelTestBase.php | 3 +- lib/Cake/Test/Case/Model/models.php | 68 ++++++++++++++++ lib/Cake/Test/Fixture/ArmorFixture.php | 66 ++++++++++++++++ lib/Cake/Test/Fixture/ArmorsPlayerFixture.php | 67 ++++++++++++++++ lib/Cake/Test/Fixture/GuildFixture.php | 54 +++++++++++++ lib/Cake/Test/Fixture/GuildsPlayerFixture.php | 57 ++++++++++++++ lib/Cake/Test/Fixture/PlayerFixture.php | 57 ++++++++++++++ 8 files changed, 449 insertions(+), 1 deletion(-) create mode 100644 lib/Cake/Test/Fixture/ArmorFixture.php create mode 100644 lib/Cake/Test/Fixture/ArmorsPlayerFixture.php create mode 100644 lib/Cake/Test/Fixture/GuildFixture.php create mode 100644 lib/Cake/Test/Fixture/GuildsPlayerFixture.php create mode 100644 lib/Cake/Test/Fixture/PlayerFixture.php diff --git a/lib/Cake/Test/Case/Model/ModelIntegrationTest.php b/lib/Cake/Test/Case/Model/ModelIntegrationTest.php index 38c9ce71b..cf2cfbe25 100644 --- a/lib/Cake/Test/Case/Model/ModelIntegrationTest.php +++ b/lib/Cake/Test/Case/Model/ModelIntegrationTest.php @@ -2072,4 +2072,82 @@ class ModelIntegrationTest extends BaseModelTest { $this->assertTrue($Article->hasMethod('pass')); $this->assertFalse($Article->hasMethod('fail')); } + +/** + * testMultischemaFixture + * + * @return void + */ + public function testMultischemaFixture() { + + $config = new DATABASE_CONFIG(); + $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with Sqlite.'); + $this->skipIf(!isset($config->test) || !isset($config->test2), + 'Primary and secondary test databases not configured, skipping cross-database join tests. To run these tests define $test and $test2 in your database configuration.' + ); + + $this->loadFixtures('Player', 'Guild', 'GuildsPlayer'); + + $Player = ClassRegistry::init('Player'); + $this->assertEqual($Player->useDbConfig, 'test'); + $this->assertEqual($Player->Guild->useDbConfig, 'test'); + $this->assertEqual($Player->Guild->GuildsPlayer->useDbConfig, 'test2'); + $this->assertEqual($Player->GuildsPlayer->useDbConfig, 'test2'); + + $players = $Player->find('all', array('recursive' => -1)); + $guilds = $Player->Guild->find('all', array('recursive' => -1)); + $guildsPlayers = $Player->GuildsPlayer->find('all', array('recursive' => -1)); + + $this->assertEqual(true, count($players) > 1); + $this->assertEqual(true, count($guilds) > 1); + $this->assertEqual(true, count($guildsPlayers) > 1); + } + +/** + * testMultischemaFixtureWithThreeDatabases, three databases + * + * @return void + */ + public function testMultischemaFixtureWithThreeDatabases() { + + $config = new DATABASE_CONFIG(); + $this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with Sqlite.'); + $this->skipIf( + !isset($config->test) || !isset($config->test2) || !isset($config->test_database_three), + 'Primary, secondary, and tertiary test databases not configured, skipping test. To run this test define $test, $test2, and $test_database_three in your database configuration.' + ); + + $this->loadFixtures('Player', 'Guild', 'GuildsPlayer', 'Armor', 'ArmorsPlayer'); + + $Player = ClassRegistry::init('Player'); + $Player->bindModel(array( + 'hasAndBelongsToMany' => array( + 'Armor' => array( + 'with' => 'ArmorsPlayer', + ), + ), + ), false); + $this->assertEqual('test', $Player->useDbConfig); + $this->assertEqual('test', $Player->Guild->useDbConfig); + $this->assertEqual('test2', $Player->Guild->GuildsPlayer->useDbConfig); + $this->assertEqual('test2', $Player->GuildsPlayer->useDbConfig); + $this->assertEqual('test2', $Player->Armor->useDbConfig); + $this->assertEqual('test_database_three', $Player->Armor->ArmorsPlayer->useDbConfig); + $this->assertEqual('test', $Player->getDataSource()->configKeyName); + $this->assertEqual('test', $Player->Guild->getDataSource()->configKeyName); + $this->assertEqual('test2', $Player->GuildsPlayer->getDataSource()->configKeyName); + $this->assertEqual('test2', $Player->Armor->getDataSource()->configKeyName); + $this->assertEqual('test_database_three', $Player->Armor->ArmorsPlayer->getDataSource()->configKeyName); + + $players = $Player->find('all', array('recursive' => -1)); + $guilds = $Player->Guild->find('all', array('recursive' => -1)); + $guildsPlayers = $Player->GuildsPlayer->find('all', array('recursive' => -1)); + $armorsPlayers = $Player->ArmorsPlayer->find('all', array('recursive' => -1)); + + $this->assertEqual(true, count($players) > 1); + $this->assertEqual(true, count($guilds) > 1); + $this->assertEqual(true, count($guildsPlayers) > 1); + $this->assertEqual(true, count($armorsPlayers) > 1); + } + } diff --git a/lib/Cake/Test/Case/Model/ModelTestBase.php b/lib/Cake/Test/Case/Model/ModelTestBase.php index 7124118a5..0dd9d0940 100644 --- a/lib/Cake/Test/Case/Model/ModelTestBase.php +++ b/lib/Cake/Test/Case/Model/ModelTestBase.php @@ -67,7 +67,8 @@ abstract class BaseModelTest extends CakeTestCase { 'core.counter_cache_user_nonstandard_primary_key', 'core.counter_cache_post_nonstandard_primary_key', 'core.uuidportfolio', 'core.uuiditems_uuidportfolio', 'core.uuiditems_uuidportfolio_numericid', 'core.fruit', - 'core.fruits_uuid_tag', 'core.uuid_tag', 'core.product_update_all', 'core.group_update_all' + 'core.fruits_uuid_tag', 'core.uuid_tag', 'core.product_update_all', 'core.group_update_all', + 'core.player', 'core.guild', 'core.guilds_player', 'core.armor', 'core.armors_player', ); /** diff --git a/lib/Cake/Test/Case/Model/models.php b/lib/Cake/Test/Case/Model/models.php index d747196a3..f18f5f677 100644 --- a/lib/Cake/Test/Case/Model/models.php +++ b/lib/Cake/Test/Case/Model/models.php @@ -4532,3 +4532,71 @@ class ScaffoldTag extends CakeTestModel { */ public $useTable = 'tags'; } + +/** + * Player class + * + * @package Cake.Test.Case.Model + */ +class Player extends CakeTestModel { + public $hasAndBelongsToMany = array( + 'Guild' => array( + 'with' => 'GuildsPlayer', + 'unique' => true, + ), + ); +} + +/** + * Guild class + * + * @package Cake.Test.Case.Model + */ +class Guild extends CakeTestModel { + public $hasAndBelongsToMany = array( + 'Player' => array( + 'with' => 'GuildsPlayer', + 'unique' => true, + ), + ); +} + +/** + * GuildsPlayer class + * + * @package Cake.Test.Case.Model + */ +class GuildsPlayer extends CakeTestModel { + + public $useDbConfig = 'test2'; + + public $belongsTo = array( + 'Player', + 'Guild', + ); +} + +/** + * Armor class + * + * @package Cake.Test.Case.Model + */ +class Armor extends CakeTestModel { + + public $useDbConfig = 'test2'; + + public $hasAndBelongsToMany = array( + 'Player' => array('with' => 'ArmorsPlayer'), + ); +} + +/** + * ArmorsPlayer class + * + * @package Cake.Test.Case.Model + */ +class ArmorsPlayer extends CakeTestModel { + + public $useDbConfig = 'test_database_three'; + +} diff --git a/lib/Cake/Test/Fixture/ArmorFixture.php b/lib/Cake/Test/Fixture/ArmorFixture.php new file mode 100644 index 000000000..99c9457d0 --- /dev/null +++ b/lib/Cake/Test/Fixture/ArmorFixture.php @@ -0,0 +1,66 @@ + + * 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.Fixture + * @since CakePHP(tm) v 1.2.0.4667 + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) + */ + +/** + * Short description for class. + * + * @package Cake.Test.Fixture + */ +class ArmorFixture extends CakeTestFixture { + +/** + * name property + * + * @var string 'Armor' + */ + public $name = 'Armor'; + +/** + * Datasource + * + * Used for Multi database fixture test + * + * @var string 'test2' + */ + public $useDbConfig = 'test2'; + +/** + * fields property + * + * @var array + */ + public $fields = array( + 'id' => array('type' => 'integer', 'key' => 'primary'), + 'name' => array('type' => 'string', 'null' => false), + 'created' => 'datetime', + 'updated' => 'datetime' + ); + +/** + * records property + * + * @var array + */ + public $records = array( + array('id' => 1, 'name' => 'Leather', 'created' => '2007-03-17 01:16:23'), + array('id' => 2, 'name' => 'Chainmail', 'created' => '2007-03-17 01:18:23'), + array('id' => 3, 'name' => 'Cloak', 'created' => '2007-03-17 01:20:23'), + array('id' => 4, 'name' => 'Bikini', 'created' => '2007-03-17 01:22:23'), + ); +} diff --git a/lib/Cake/Test/Fixture/ArmorsPlayerFixture.php b/lib/Cake/Test/Fixture/ArmorsPlayerFixture.php new file mode 100644 index 000000000..8e65bfb50 --- /dev/null +++ b/lib/Cake/Test/Fixture/ArmorsPlayerFixture.php @@ -0,0 +1,67 @@ + + * 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.Fixture + * @since CakePHP(tm) v 1.2.0.4667 + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) + */ + +/** + * Short description for class. + * + * @package Cake.Test.Fixture + */ +class ArmorsPlayerFixture extends CakeTestFixture { + +/** + * name property + * + * @var string 'ArmorsPlayer' + */ + public $name = 'ArmorsPlayer'; + +/** + * Datasource + * + * Used for Multi database fixture test + * + * @var string 'test_database_three' + */ + public $useDbConfig = 'test_database_three'; + +/** + * fields property + * + * @var array + */ + public $fields = array( + 'id' => array('type' => 'integer', 'key' => 'primary'), + 'player_id' => array('type' => 'integer', 'null' => false), + 'armor_id' => array('type' => 'integer', 'null' => false), + 'broken' => array('type' => 'boolean', 'null' => false, 'default' => false), + 'created' => 'datetime', + 'updated' => 'datetime' + ); + +/** + * records property + * + * @var array + */ + public $records = array( + array('id' => 1, 'player_id' => 1, 'armor_id' => 1, 'broken' => false), + array('id' => 2, 'player_id' => 2, 'armor_id' => 2, 'broken' => false), + array('id' => 3, 'player_id' => 3, 'armor_id' => 3, 'broken' => false), + ); +} diff --git a/lib/Cake/Test/Fixture/GuildFixture.php b/lib/Cake/Test/Fixture/GuildFixture.php new file mode 100644 index 000000000..6a5edd414 --- /dev/null +++ b/lib/Cake/Test/Fixture/GuildFixture.php @@ -0,0 +1,54 @@ + + * 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.Fixture + * @since CakePHP(tm) v 1.2.0.4667 + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) + */ + +/** + * Short description for class. + * + * @package Cake.Test.Fixture + */ +class GuildFixture extends CakeTestFixture { + +/** + * name property + * + * @var string 'Guild' + */ + public $name = 'Guild'; + +/** + * fields property + * + * @var array + */ + public $fields = array( + 'id' => array('type' => 'integer', 'key' => 'primary'), + 'name' => array('type' => 'string', 'null' => false), + ); + +/** + * records property + * + * @var array + */ + public $records = array( + array('id' => 1, 'name' => 'Warriors'), + array('id' => 2, 'name' => 'Rangers'), + array('id' => 3, 'name' => 'Wizards'), + ); +} diff --git a/lib/Cake/Test/Fixture/GuildsPlayerFixture.php b/lib/Cake/Test/Fixture/GuildsPlayerFixture.php new file mode 100644 index 000000000..5e7876a47 --- /dev/null +++ b/lib/Cake/Test/Fixture/GuildsPlayerFixture.php @@ -0,0 +1,57 @@ + + * 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.Fixture + * @since CakePHP(tm) v 1.2.0.4667 + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) + */ + +/** + * Short description for class. + * + * @package Cake.Test.Fixture + */ +class GuildsPlayerFixture extends CakeTestFixture { + +/** + * name property + * + * @var string 'GuildsPlayer' + */ + public $name = 'GuildsPlayer'; + + public $useDbConfig = 'test2'; + +/** + * fields property + * + * @var array + */ + public $fields = array( + 'id' => array('type' => 'integer', 'key' => 'primary'), + 'player_id' => array('type' => 'integer', 'null' => false), + 'guild_id' => array('type' => 'integer', 'null' => false), + ); + +/** + * records property + * + * @var array + */ + public $records = array( + array('id' => 1, 'player_id' => 1, 'guild_id' => 1), + array('id' => 2, 'player_id' => 1, 'guild_id' => 2), + array('id' => 3, 'player_id' => 4, 'guild_id' => 3), + ); +} diff --git a/lib/Cake/Test/Fixture/PlayerFixture.php b/lib/Cake/Test/Fixture/PlayerFixture.php new file mode 100644 index 000000000..fb8b58201 --- /dev/null +++ b/lib/Cake/Test/Fixture/PlayerFixture.php @@ -0,0 +1,57 @@ + + * 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.Fixture + * @since CakePHP(tm) v 1.2.0.4667 + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) + */ + +/** + * Short description for class. + * + * @package Cake.Test.Fixture + */ +class PlayerFixture extends CakeTestFixture { + +/** + * name property + * + * @var string 'Player' + */ + public $name = 'Player'; + +/** + * fields property + * + * @var array + */ + public $fields = array( + 'id' => array('type' => 'integer', 'key' => 'primary'), + 'name' => array('type' => 'string', 'null' => false), + 'created' => 'datetime', + 'updated' => 'datetime' + ); + +/** + * records property + * + * @var array + */ + public $records = array( + array('id' => 1, 'name' => 'mark', 'created' => '2007-03-17 01:16:23'), + array('id' => 2, 'name' => 'jack', 'created' => '2007-03-17 01:18:23'), + array('id' => 3, 'name' => 'larry', 'created' => '2007-03-17 01:20:23'), + array('id' => 4, 'name' => 'jose', 'created' => '2007-03-17 01:22:23'), + ); +}