diff --git a/lib/Cake/Test/Case/Utility/ClassRegistryTest.php b/lib/Cake/Test/Case/Utility/ClassRegistryTest.php index 30de00514..30563dc71 100644 --- a/lib/Cake/Test/Case/Utility/ClassRegistryTest.php +++ b/lib/Cake/Test/Case/Utility/ClassRegistryTest.php @@ -123,6 +123,21 @@ class RegisterCategory extends ClassRegisterModel { public $name = 'RegisterCategory'; } +/** + * RegisterPrefixedDs class + * + * @package Cake.Test.Case.Utility + */ +class RegisterPrefixedDs extends ClassRegisterModel { + +/** + * useDbConfig property + * + * @var string 'doesnotexist' + */ + public $useDbConfig = 'doesnotexist'; +} + /** * ClassRegistryTest class * @@ -270,6 +285,24 @@ class ClassRegistryTest extends CakeTestCase { CakePlugin::unload(); } +/** + * Tests prefixed datasource names for test purposes + * + */ + public function testPrefixedTestDatasource() { + $Model = ClassRegistry::init('RegisterPrefixedDs'); + $this->assertEqual($Model->useDbConfig, 'test'); + ClassRegistry::removeObject('RegisterPrefixedDs'); + + $testConfig = ConnectionManager::getDataSource('test')->config; + ConnectionManager::create('test_doesnotexist', $testConfig); + + $Model = ClassRegistry::init('RegisterArticle'); + $this->assertEqual($Model->useDbConfig, 'test'); + $Model = ClassRegistry::init('RegisterPrefixedDs'); + $this->assertEqual($Model->useDbConfig, 'test_doesnotexist'); + } + /** * Tests that passing the string parameter to init() will return false if the model does not exists * diff --git a/lib/Cake/TestSuite/Fixture/CakeFixtureManager.php b/lib/Cake/TestSuite/Fixture/CakeFixtureManager.php index 3fc4ecaac..dd0ef5fef 100644 --- a/lib/Cake/TestSuite/Fixture/CakeFixtureManager.php +++ b/lib/Cake/TestSuite/Fixture/CakeFixtureManager.php @@ -84,7 +84,7 @@ class CakeFixtureManager { $db = ConnectionManager::getDataSource('test'); $db->cacheSources = false; $this->_db = $db; - ClassRegistry::config(array('ds' => 'test')); + ClassRegistry::config(array('ds' => 'test', 'testing' => true)); $this->_initialized = true; } @@ -131,7 +131,7 @@ class CakeFixtureManager { $fixtureFile = $path . DS . $className . 'Fixture.php'; require_once($fixtureFile); $fixtureClass = $className . 'Fixture'; - $this->_loaded[$fixtureIndex] = new $fixtureClass($this->_db); + $this->_loaded[$fixtureIndex] = new $fixtureClass(); $this->_fixtureMap[$fixtureClass] = $this->_loaded[$fixtureIndex]; break; } diff --git a/lib/Cake/Utility/ClassRegistry.php b/lib/Cake/Utility/ClassRegistry.php index 27fb0a823..3d5775fd6 100644 --- a/lib/Cake/Utility/ClassRegistry.php +++ b/lib/Cake/Utility/ClassRegistry.php @@ -19,6 +19,13 @@ * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ +/** + * Included libraries. + */ +App::uses('Model', 'Model'); +App::uses('AppModel', 'Model'); +App::uses('ConnectionManager', 'Model'); + /** * Class Collections. * @@ -105,6 +112,7 @@ class ClassRegistry { } $defaults = isset($_this->_config['Model']) ? $_this->_config['Model'] : array(); $count = count($objects); + $availableDs = array_keys(ConnectionManager::enumConnectionObjects()); foreach ($objects as $key => $settings) { if (is_array($settings)) { @@ -127,12 +135,25 @@ class ClassRegistry { return $model; } - App::uses('Model', 'Model'); - App::uses('AppModel', 'Model'); App::uses($plugin . 'AppModel', $pluginPath . 'Model'); App::uses($class, $pluginPath . 'Model'); if (class_exists($class)) { + $testing = isset($settings['testing']) ? $settings['testing'] : false; + if ($testing) { + $settings['ds'] = 'test'; + $reflected = new ReflectionClass($class); + $defaultProperties = $reflected->getDefaultProperties(); + if (isset($defaultProperties['useDbConfig'])) { + $useDbConfig = $defaultProperties['useDbConfig']; + if (in_array('test_' . $useDbConfig, $availableDs)) { + $useDbConfig = 'test_' . $useDbConfig; + } + if (strpos($useDbConfig, 'test') === 0) { + $settings['ds'] = $useDbConfig; + } + } + } $instance = new $class($settings); if ($strict) { $instance = ($instance instanceof Model) ? $instance : null; @@ -268,6 +289,9 @@ class ClassRegistry { } elseif (empty($param) && is_string($type)) { return isset($_this->_config[$type]) ? $_this->_config[$type] : null; } + if (isset($_this->_config[$type]['testing'])) { + $param['testing'] = true; + } $_this->_config[$type] = $param; }