Skip to content
This repository
Browse code

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.
  • Loading branch information...
commit a5ac328a71867a0c7634b884bcf107d91e0b5933 1 parent fa0ec44
Rachman Chavik authored October 20, 2011
30  lib/Cake/TestSuite/Fixture/CakeFixtureManager.php
@@ -149,9 +149,13 @@ protected function _loadFixtures($fixtures) {
149 149
  */
150 150
 	protected function _setupTable($fixture, $db = null, $drop = true) {
151 151
 		if (!$db) {
152  
-			$db = $this->_db;
  152
+			if (!empty($fixture->useDbConfig)) {
  153
+				$db = ClassRegistry::getDataSource($fixture->useDbConfig);
  154
+			} else {
  155
+				$db = $this->_db;
  156
+			}
153 157
 		}
154  
-		if (!empty($fixture->created) && $fixture->created == $db->configKeyName) {
  158
+		if (!empty($fixture->created) && in_array($db->configKeyName, $fixture->created)) {
155 159
 			return;
156 160
 		}
157 161
 
@@ -161,10 +165,8 @@ protected function _setupTable($fixture, $db = null, $drop = true) {
161 165
 		if ($drop && in_array($table, $sources)) {
162 166
 			$fixture->drop($db);
163 167
 			$fixture->create($db);
164  
-			$fixture->created = $db->configKeyName;
165 168
 		} elseif (!in_array($table, $sources)) {
166 169
 			$fixture->create($db);
167  
-			$fixture->created = $db->configKeyName;
168 170
 		}
169 171
 	}
170 172
 
@@ -187,8 +189,9 @@ public function load(CakeTestCase $test) {
187 189
 		foreach ($fixtures as $f) {
188 190
 			if (!empty($this->_loaded[$f])) {
189 191
 				$fixture = $this->_loaded[$f];
190  
-				$this->_setupTable($fixture, $test->db, $test->dropTables);
191  
-				$fixture->insert($test->db);
  192
+				$db = ConnectionManager::getDataSource($fixture->useDbConfig);
  193
+				$this->_setupTable($fixture, $db, $test->dropTables);
  194
+				$fixture->insert($db);
192 195
 			}
193 196
 		}
194 197
 		$test->db->commit();
@@ -206,7 +209,10 @@ public function unload(CakeTestCase $test) {
206 209
 			if (isset($this->_loaded[$f])) {
207 210
 				$fixture = $this->_loaded[$f];
208 211
 				if (!empty($fixture->created)) {
209  
-					$fixture->truncate($test->db);
  212
+					foreach ($fixture->created as $ds) {
  213
+						$db = ConnectionManager::getDataSource($ds);
  214
+						$fixture->truncate($db);
  215
+					}
210 216
 				}
211 217
 			}
212 218
 		}
@@ -222,10 +228,10 @@ public function unload(CakeTestCase $test) {
222 228
 	public function loadSingle($name, $db = null) {
223 229
 		$name .= 'Fixture';
224 230
 		if (isset($this->_fixtureMap[$name])) {
  231
+			$fixture = $this->_fixtureMap[$name];
225 232
 			if (!$db) {
226  
-				$db = $this->_db;
  233
+				$db = ConnectionManager::getDataSource($fixture->useDbConfig);
227 234
 			}
228  
-			$fixture = $this->_fixtureMap[$name];
229 235
 			$this->_setupTable($fixture, $db);
230 236
 			$fixture->truncate($db);
231 237
 			$fixture->insert($db);
@@ -242,8 +248,12 @@ public function loadSingle($name, $db = null) {
242 248
 	public function shutDown() {
243 249
 		foreach ($this->_loaded as $fixture) {
244 250
 			if (!empty($fixture->created)) {
245  
-				$fixture->drop($this->_db);
  251
+				foreach ($fixture->created as $ds) {
  252
+					$db = ConnectionManager::getDataSource($ds);
  253
+					$fixture->drop($db);
  254
+				}
246 255
 			}
247 256
 		}
248 257
 	}
  258
+
249 259
 }
24  lib/Cake/TestSuite/Fixture/CakeTestFixture.php
@@ -39,12 +39,24 @@ class CakeTestFixture {
39 39
 	public $db = null;
40 40
 
41 41
 /**
  42
+ * Fixture Datasource
  43
+ *
  44
+ */
  45
+	public $useDbConfig = 'test';
  46
+
  47
+/**
42 48
  * Full Table Name
43 49
  *
44 50
  */
45 51
 	public $table = null;
46 52
 
47 53
 /**
  54
+ * List of datasources where this fixture has been created
  55
+ *
  56
+ */
  57
+	public $created = array();
  58
+
  59
+/**
48 60
  * Instantiate the fixture.
49 61
  *
50 62
  */
@@ -56,7 +68,14 @@ public function __construct() {
56 68
 				$this->name = get_class($this);
57 69
 			}
58 70
 		}
59  
-		$this->Schema = new CakeSchema(array('name' => 'TestSuite', 'connection' => 'test'));
  71
+		$connection = 'test';
  72
+		if (!empty($this->useDbConfig)) {
  73
+			$connection = $this->useDbConfig;
  74
+			if (strpos($connection, 'test') !== 0) {
  75
+				throw new CakeException(__d('cake_dev', 'Invalid datasource %s for object %s', $connection, $this->name));
  76
+			}
  77
+		}
  78
+		$this->Schema = new CakeSchema(array('name' => 'TestSuite', 'connection' => $connection));
60 79
 		$this->init();
61 80
 	}
62 81
 
@@ -71,6 +90,7 @@ public function init() {
71 90
 				is_array($this->import) ? $this->import : array('model' => $this->import)
72 91
 			);
73 92
 
  93
+			$this->Schema->connection = $import['connection'];
74 94
 			if (isset($import['model'])) {
75 95
 				list($plugin, $modelClass) = pluginSplit($import['model'], true);
76 96
 				App::uses($modelClass, $plugin . 'Model');
@@ -167,6 +187,7 @@ public function create($db) {
167 187
 		$this->Schema->build(array($this->table => $this->fields));
168 188
 		try {
169 189
 			$db->execute($db->createSchema($this->Schema), array('log' => false));
  190
+			$this->created[] = $db->configKeyName;
170 191
 		} catch (Exception $e) {
171 192
 			return false;
172 193
 		}
@@ -187,6 +208,7 @@ public function drop($db) {
187 208
 		try {
188 209
 
189 210
 			$db->execute($db->dropSchema($this->Schema), array('log' => false));
  211
+			$this->created = array_diff($this->created, array($db->configKeyName));;
190 212
 		} catch (Exception $e) {
191 213
 			return false;
192 214
 		}

0 notes on commit a5ac328

Please sign in to comment.
Something went wrong with that request. Please try again.