mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
Configure ClassRegistry in test mode
CR::config accepts 'testing' key to indicate a test run. When this flag is set, CR::init() will use 'test' datasource, or append 'test_' prefix to a model's useDbConfig value if the datasource configured.
This commit is contained in:
parent
408e785d5e
commit
aab8e98531
3 changed files with 61 additions and 4 deletions
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue