Commit
Because getMockForModel() does not go through the test datasource injection in ClassRegistry::init() we need to duplicate the basics of that logic here. Thankfully, we already have a mock so we can do that datasource switching without reflection. Of course this means there will be limitations to how/when this will work, but I feel those scenarios can probably be solved by not using mocks, or by mocking out the problematic methods. This set of changes makes getMockForModel() work with secondary datasources, as one would expect it to do, but I'm not sure it ever did. Refs #4694
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -722,13 +722,23 @@ public function getMockForModel($model, $methods = array(), $config = array()) { | |
|
||
list($plugin, $name) = pluginSplit($model, true); | ||
App::uses($name, $plugin . 'Model'); | ||
|
||
$config = array_merge((array)$config, array('name' => $name)); | ||
unset($config['ds']); | ||
|
||
if (!class_exists($name)) { | ||
throw new MissingModelException(array($model)); | ||
} | ||
|
||
$mock = $this->getMock($name, $methods, array($config)); | ||
|
||
$availableDs = array_keys(ConnectionManager::enumConnectionObjects()); | ||
if ($mock->useDbConfig === 'default') { | ||
$mock->setDataSource('test'); | ||
This comment has been minimized.
Sorry, something went wrong.
DIDoS
Contributor
|
||
} | ||
if ($mock->useDbConfig !== 'test' && in_array('test_' . $mock->useDbConfig, $availableDs)) { | ||
$mock->setDataSource('test_' . $mock->useDbConfig); | ||
} | ||
|
||
ClassRegistry::removeObject($name); | ||
ClassRegistry::addObject($name, $mock); | ||
return $mock; | ||
|
The latter was correct actually
But nitpicking