Skip to content

Commit

Permalink
Adding multidb fixture support
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rchavik committed Nov 17, 2011
1 parent fa0ec44 commit a5ac328
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
30 changes: 20 additions & 10 deletions lib/Cake/TestSuite/Fixture/CakeFixtureManager.php
Expand Up @@ -149,9 +149,13 @@ protected function _loadFixtures($fixtures) {
*/
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;
}

Expand All @@ -161,10 +165,8 @@ protected function _setupTable($fixture, $db = null, $drop = true) {
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;
}
}

Expand All @@ -187,8 +189,9 @@ public function load(CakeTestCase $test) {
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();
Expand All @@ -206,7 +209,10 @@ public function unload(CakeTestCase $test) {
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);
}
}
}
}
Expand All @@ -222,10 +228,10 @@ public function unload(CakeTestCase $test) {
public function loadSingle($name, $db = null) {
$name .= 'Fixture';
if (isset($this->_fixtureMap[$name])) {
$fixture = $this->_fixtureMap[$name];
if (!$db) {
$db = $this->_db;
$db = ConnectionManager::getDataSource($fixture->useDbConfig);
}
$fixture = $this->_fixtureMap[$name];
$this->_setupTable($fixture, $db);
$fixture->truncate($db);
$fixture->insert($db);
Expand All @@ -242,8 +248,12 @@ public function loadSingle($name, $db = null) {
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);
}
}
}
}

}
24 changes: 23 additions & 1 deletion lib/Cake/TestSuite/Fixture/CakeTestFixture.php
Expand Up @@ -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.
*
Expand All @@ -56,7 +68,14 @@ public function __construct() {
$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();
}

Expand All @@ -71,6 +90,7 @@ public function init() {
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');
Expand Down Expand Up @@ -167,6 +187,7 @@ public function create($db) {
$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;
}
Expand All @@ -187,6 +208,7 @@ public function drop($db) {
try {

$db->execute($db->dropSchema($this->Schema), array('log' => false));
$this->created = array_diff($this->created, array($db->configKeyName));;
} catch (Exception $e) {
return false;
}
Expand Down

0 comments on commit a5ac328

Please sign in to comment.