From a5ac328a71867a0c7634b884bcf107d91e0b5933 Mon Sep 17 00:00:00 2001 From: Rachman Chavik Date: Thu, 20 Oct 2011 11:24:10 +0700 Subject: [PATCH] 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. --- .../TestSuite/Fixture/CakeFixtureManager.php | 30 ++++++++++++------- .../TestSuite/Fixture/CakeTestFixture.php | 24 ++++++++++++++- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/lib/Cake/TestSuite/Fixture/CakeFixtureManager.php b/lib/Cake/TestSuite/Fixture/CakeFixtureManager.php index 59638986ba8..3fc4ecaacc3 100644 --- a/lib/Cake/TestSuite/Fixture/CakeFixtureManager.php +++ b/lib/Cake/TestSuite/Fixture/CakeFixtureManager.php @@ -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; } @@ -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; } } @@ -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(); @@ -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); + } } } } @@ -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); @@ -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); + } } } } + } diff --git a/lib/Cake/TestSuite/Fixture/CakeTestFixture.php b/lib/Cake/TestSuite/Fixture/CakeTestFixture.php index 11fe4b23669..c11eef6e205 100644 --- a/lib/Cake/TestSuite/Fixture/CakeTestFixture.php +++ b/lib/Cake/TestSuite/Fixture/CakeTestFixture.php @@ -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 @@ 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(); } @@ -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'); @@ -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; } @@ -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; }