From c4cbf1c6353f905f57afc46e6f59f6819782d296 Mon Sep 17 00:00:00 2001 From: Rachman Chavik Date: Sat, 5 Nov 2011 18:21:53 +0700 Subject: [PATCH] Adding cross schema test --- .../Case/Model/ModelCrossSchemaHabtmTest.php | 183 ++++++++++++++++++ lib/Cake/Test/Case/Model/ModelTest.php | 1 + 2 files changed, 184 insertions(+) create mode 100644 lib/Cake/Test/Case/Model/ModelCrossSchemaHabtmTest.php diff --git a/lib/Cake/Test/Case/Model/ModelCrossSchemaHabtmTest.php b/lib/Cake/Test/Case/Model/ModelCrossSchemaHabtmTest.php new file mode 100644 index 00000000000..8968ba3372b --- /dev/null +++ b/lib/Cake/Test/Case/Model/ModelCrossSchemaHabtmTest.php @@ -0,0 +1,183 @@ + false on *both* database connections, + * or one connection will step on the other. + * + * @package cake + * @subpackage cake.tests.cases.libs.model.operations + * @since CakePHP(tm) v 2.1 + */ +class ModelCrossSchemaHabtmTest extends BaseModelTest { + + var $fixtures = array( + 'core.player', 'core.guild', 'core.guilds_player', + 'core.armor', 'core.armors_player', + ); + + var $dropTables = false; + + function setUp() { + parent::setUp(); + $this->_checkConfigs(); + } + + function tearDown() { + parent::tearDown(); + } + + function _checkConfigs() { + $config = ConnectionManager::enumConnectionObjects(); + + $this->skipIf( + !isset($config['test']) || !isset($config['test2']), + 'Primary and secondary test databases not configured, skipping cross-database join tests.' + .' To run these tests, you must define $test and $test2 in your database configuration.' + ); + } + + function testModelDatasources() { + $this->loadFixtures('Player', 'Guild', 'GuildsPlayer'); + + $Player = ClassRegistry::init('Player'); + $this->assertEqual('test', $Player->useDbConfig); + $this->assertEqual('test', $Player->Guild->useDbConfig); + $this->assertEqual('test2', $Player->GuildsPlayer->useDbConfig); + + $this->assertEqual('test', $Player->getDataSource()->configKeyName); + $this->assertEqual('test', $Player->Guild->getDataSource()->configKeyName); + $this->assertEqual('test2', $Player->GuildsPlayer->getDataSource()->configKeyName); + } + + function testHabtmFind() { + $this->loadFixtures('Player', 'Guild', 'GuildsPlayer'); + + $Player = ClassRegistry::init('Player'); + + $players = $Player->find('all', array( + 'fields' => array('id', 'name'), + 'contain' => array( + 'Guild' => array( + 'conditions' => array( + 'Guild.name' => 'Wizards', + ), + ), + ), + )); + $this->assertEqual(4, count($players)); + $wizards = Set::extract('/Guild[name=Wizards]', $players); + $this->assertEqual(1, count($wizards)); + + $players = $Player->find('all', array( + 'fields' => array('id', 'name'), + 'conditions' => array( + 'Player.id' => 1, + ), + )); + $this->assertEqual(1, count($players)); + $wizards = Set::extract('/Guild', $players); + $this->assertEqual(2, count($wizards)); + } + + + function testHabtmSave() { + $this->loadFixtures('Player', 'Guild', 'GuildsPlayer'); + + $Player =& ClassRegistry::init('Player'); + $players = $Player->find('count'); + $this->assertEqual(4, $players); + + $player = $Player->create(array( + 'name' => 'rchavik', + )); + + $results = $Player->saveAll($player, array('validate' => 'first')); + $this->assertNotEqual(false, $results); + $count = $Player->find('count'); + $this->assertEqual(5, $count); + + $count = $Player->GuildsPlayer->find('count'); + $this->assertEqual(3, $count); + + $player = $Player->findByName('rchavik'); + $this->assertEmpty($player['Guild']); + + $player['Guild']['Guild'] = array(1, 2, 3); + $Player->save($player); + + $player = $Player->findByName('rchavik'); + $this->assertEqual(3, count($player['Guild'])); + + $players = $Player->find('all', array( + 'contain' => array( + 'conditions' => array( + 'Guild.name' => 'Rangers', + ), + ), + )); + + $rangers = Set::extract('/Guild[name=Rangers]', $players); + $this->assertEqual(2, count($rangers)); + } + + function testHabtmWithThreeDatabases() { + $config = ConnectionManager::enumConnectionObjects(); + $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 these tests, you must 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', + 'unique' => true, + ), + ), + ), false); + $this->assertEqual('test', $Player->useDbConfig); + $this->assertEqual('test2', $Player->Armor->useDbConfig); + $this->assertEqual('test_database_three', $Player->ArmorsPlayer->useDbConfig); + $players = $Player->find('count'); + $this->assertEqual(4, $players); + + $spongebob = $Player->create(array( + 'id' => 10, + 'name' => 'spongebob', + )); + $spongebob['Armor'] = array('Armor' => array(1, 2, 3, 4)); + $result = $Player->save($spongebob); + + $expected = array( + 'Player' => array( + 'id' => 10, + 'name' => 'spongebob', + ), + 'Armor' => array( + 'Armor' => array( + 1, 2, 3, 4, + 1, 2, 3, 4, + ), + ), + ); + unset($result['Player']['created']); + unset($result['Player']['updated']); + $this->assertEqual($expected, $result); + + $spongebob = $Player->find('all', array( + 'conditions' => array( + 'Player.id' => 10, + ) + )); + $spongeBobsArmors = Set::extract('/Armor', $spongebob); + $this->assertEqual(4, count($spongeBobsArmors)); + } + +} diff --git a/lib/Cake/Test/Case/Model/ModelTest.php b/lib/Cake/Test/Case/Model/ModelTest.php index 32461a10557..4ce2d988c37 100644 --- a/lib/Cake/Test/Case/Model/ModelTest.php +++ b/lib/Cake/Test/Case/Model/ModelTest.php @@ -39,6 +39,7 @@ public static function suite() { $suite->addTestFile(CORE_TEST_CASES . DS . 'Model' . DS . 'ModelDeleteTest.php'); $suite->addTestFile(CORE_TEST_CASES . DS . 'Model' . DS . 'ModelValidationTest.php'); $suite->addTestFile(CORE_TEST_CASES . DS . 'Model' . DS . 'ModelIntegrationTest.php'); + $suite->addTestFile(CORE_TEST_CASES . DS . 'Model' . DS . 'ModelCrossSchemaHabtmTest.php'); return $suite; } }