Adding multidb fixture support

CakeTestFixture now has a $useDbConfig property, that is similar
to Model::useDbConfig.  CakeFixtureManager now uses this property
to decide which connection to use.  CakeTestFixture::$created
records datasources it was created on.
This commit is contained in:
Rachman Chavik 2011-10-20 11:24:10 +07:00
parent fa0ec44dfd
commit a5ac328a71
2 changed files with 44 additions and 12 deletions

View file

@ -149,9 +149,13 @@ class CakeFixtureManager {
*/
protected function _setupTable($fixture, $db = null, $drop = true) {
if (!$db) {
$db = $this->_db;
if (!empty($fixture->useDbConfig)) {
$db = ClassRegistry::getDataSource($fixture->useDbConfig);
} else {
$db = $this->_db;
}
}
if (!empty($fixture->created) && $fixture->created == $db->configKeyName) {
if (!empty($fixture->created) && in_array($db->configKeyName, $fixture->created)) {
return;
}
@ -161,10 +165,8 @@ class CakeFixtureManager {
if ($drop && in_array($table, $sources)) {
$fixture->drop($db);
$fixture->create($db);
$fixture->created = $db->configKeyName;
} elseif (!in_array($table, $sources)) {
$fixture->create($db);
$fixture->created = $db->configKeyName;
}
}
@ -187,8 +189,9 @@ class CakeFixtureManager {
foreach ($fixtures as $f) {
if (!empty($this->_loaded[$f])) {
$fixture = $this->_loaded[$f];
$this->_setupTable($fixture, $test->db, $test->dropTables);
$fixture->insert($test->db);
$db = ConnectionManager::getDataSource($fixture->useDbConfig);
$this->_setupTable($fixture, $db, $test->dropTables);
$fixture->insert($db);
}
}
$test->db->commit();
@ -206,7 +209,10 @@ class CakeFixtureManager {
if (isset($this->_loaded[$f])) {
$fixture = $this->_loaded[$f];
if (!empty($fixture->created)) {
$fixture->truncate($test->db);
foreach ($fixture->created as $ds) {
$db = ConnectionManager::getDataSource($ds);
$fixture->truncate($db);
}
}
}
}
@ -222,10 +228,10 @@ class CakeFixtureManager {
public function loadSingle($name, $db = null) {
$name .= 'Fixture';
if (isset($this->_fixtureMap[$name])) {
if (!$db) {
$db = $this->_db;
}
$fixture = $this->_fixtureMap[$name];
if (!$db) {
$db = ConnectionManager::getDataSource($fixture->useDbConfig);
}
$this->_setupTable($fixture, $db);
$fixture->truncate($db);
$fixture->insert($db);
@ -242,8 +248,12 @@ class CakeFixtureManager {
public function shutDown() {
foreach ($this->_loaded as $fixture) {
if (!empty($fixture->created)) {
$fixture->drop($this->_db);
foreach ($fixture->created as $ds) {
$db = ConnectionManager::getDataSource($ds);
$fixture->drop($db);
}
}
}
}
}

View file

@ -38,12 +38,24 @@ class CakeTestFixture {
*/
public $db = null;
/**
* Fixture Datasource
*
*/
public $useDbConfig = 'test';
/**
* Full Table Name
*
*/
public $table = null;
/**
* List of datasources where this fixture has been created
*
*/
public $created = array();
/**
* Instantiate the fixture.
*
@ -56,7 +68,14 @@ class CakeTestFixture {
$this->name = get_class($this);
}
}
$this->Schema = new CakeSchema(array('name' => 'TestSuite', 'connection' => 'test'));
$connection = 'test';
if (!empty($this->useDbConfig)) {
$connection = $this->useDbConfig;
if (strpos($connection, 'test') !== 0) {
throw new CakeException(__d('cake_dev', 'Invalid datasource %s for object %s', $connection, $this->name));
}
}
$this->Schema = new CakeSchema(array('name' => 'TestSuite', 'connection' => $connection));
$this->init();
}
@ -71,6 +90,7 @@ class CakeTestFixture {
is_array($this->import) ? $this->import : array('model' => $this->import)
);
$this->Schema->connection = $import['connection'];
if (isset($import['model'])) {
list($plugin, $modelClass) = pluginSplit($import['model'], true);
App::uses($modelClass, $plugin . 'Model');
@ -167,6 +187,7 @@ class CakeTestFixture {
$this->Schema->build(array($this->table => $this->fields));
try {
$db->execute($db->createSchema($this->Schema), array('log' => false));
$this->created[] = $db->configKeyName;
} catch (Exception $e) {
return false;
}
@ -187,6 +208,7 @@ class CakeTestFixture {
try {
$db->execute($db->dropSchema($this->Schema), array('log' => false));
$this->created = array_diff($this->created, array($db->configKeyName));;
} catch (Exception $e) {
return false;
}