Skip to content
Permalink
Browse files

Cleaning up ModelCrossSchemaHabtm test case

  • Loading branch information...
shama committed Feb 17, 2012
1 parent a9c279a commit 95b8675637ad1a5898c91dd511e402bc71c73eaa
Showing with 89 additions and 40 deletions.
  1. +89 −40 lib/Cake/Test/Case/Model/ModelCrossSchemaHabtmTest.php
@@ -1,45 +1,81 @@
<?php
require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
/**
* Tests cross database HABTM. Requires $test and $test2 to both be set in DATABASE_CONFIG
* Tests cross database HABTM. Requires $test and $test2 to both be set in DATABASE_CONFIG
* NOTE: When testing on MySQL, you must set 'persistent' => false on *both* database connections,
* or one connection will step on the other.
*
* @package cake
* @subpackage cake.tests.cases.libs.model.operations
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright 2005-2011, Cake Software Foundation, Inc.
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc.
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
* @package Cake.Test.Case.Model
* @since CakePHP(tm) v 2.1
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
class ModelCrossSchemaHabtmTest extends BaseModelTest {
var $fixtures = array(
/**
* Fixtures to be used
*
* @var array
*/
public $fixtures = array(
'core.player', 'core.guild', 'core.guilds_player',
'core.armor', 'core.armors_player',
);
);
var $dropTables = false;
/**
* Don't drop tables if they exist
*
* @var boolean
*/
public $dropTables = false;
/**
* Don't auto load fixtures
*
* @var boolean
*/
public $autoFixtures = false;
function setUp() {
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->_checkConfigs();
}
function tearDown() {
parent::tearDown();
}
function _checkConfigs() {
/**
* Check if primary and secondary test databases are configured.
*
* @return void
*/
protected 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.'
. ' To run these tests, you must define $test and $test2 in your database configuration.'
);
}
function testModelDatasources() {
/**
* testModelDatasources method
*
* @return void
*/
public function testModelDatasources() {
$this->loadFixtures('Player', 'Guild', 'GuildsPlayer');
$Player = ClassRegistry::init('Player');
@@ -52,7 +88,12 @@ function testModelDatasources() {
$this->assertEquals('test2', $Player->GuildsPlayer->getDataSource()->configKeyName);
}
function testHabtmFind() {
/**
* testHabtmFind method
*
* @return void
*/
public function testHabtmFind() {
$this->loadFixtures('Player', 'Guild', 'GuildsPlayer');
$Player = ClassRegistry::init('Player');
@@ -63,10 +104,10 @@ function testHabtmFind() {
'Guild' => array(
'conditions' => array(
'Guild.name' => 'Wizards',
),
),
),
));
),
));
$this->assertEquals(4, count($players));
$wizards = Set::extract('/Guild[name=Wizards]', $players);
$this->assertEquals(1, count($wizards));
@@ -75,24 +116,28 @@ function testHabtmFind() {
'fields' => array('id', 'name'),
'conditions' => array(
'Player.id' => 1,
),
));
),
));
$this->assertEquals(1, count($players));
$wizards = Set::extract('/Guild', $players);
$this->assertEquals(2, count($wizards));
}
function testHabtmSave() {
/**
* testHabtmSave method
*
* @return void
*/
public function testHabtmSave() {
$this->loadFixtures('Player', 'Guild', 'GuildsPlayer');
$Player =& ClassRegistry::init('Player');
$Player = ClassRegistry::init('Player');
$players = $Player->find('count');
$this->assertEquals(4, $players);
$player = $Player->create(array(
'name' => 'rchavik',
));
));
$results = $Player->saveAll($player, array('validate' => 'first'));
$this->assertNotEqual(false, $results);
@@ -115,20 +160,25 @@ function testHabtmSave() {
'contain' => array(
'conditions' => array(
'Guild.name' => 'Rangers',
),
),
));
),
));
$rangers = Set::extract('/Guild[name=Rangers]', $players);
$this->assertEquals(2, count($rangers));
}
function testHabtmWithThreeDatabases() {
/**
* testHabtmWithThreeDatabases method
*
* @return void
*/
public 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.'
. ' To run these tests, you must define $test, $test2, and $test_database_three in your database configuration.'
);
$this->loadFixtures('Player', 'Guild', 'GuildsPlayer', 'Armor', 'ArmorsPlayer');
@@ -139,9 +189,9 @@ function testHabtmWithThreeDatabases() {
'Armor' => array(
'with' => 'ArmorsPlayer',
'unique' => true,
),
),
), false);
),
), false);
$this->assertEquals('test', $Player->useDbConfig);
$this->assertEquals('test2', $Player->Armor->useDbConfig);
$this->assertEquals('test_database_three', $Player->ArmorsPlayer->useDbConfig);
@@ -151,33 +201,32 @@ function testHabtmWithThreeDatabases() {
$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->assertEquals($expected, $result);
$spongebob = $Player->find('all', array(
'conditions' => array(
'Player.id' => 10,
)
));
)
));
$spongeBobsArmors = Set::extract('/Armor', $spongebob);
$this->assertEquals(4, count($spongeBobsArmors));
}
}

0 comments on commit 95b8675

Please sign in to comment.
You can’t perform that action at this time.