mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
introducing ClassRegistry::params() to fix testing of models associations; also removes the need for TestModels that extend the model under test to change the useDbConfig, fixes #5158, #5076 (kudos to nate)
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7383 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
eddf2ee5f3
commit
501437baa0
3 changed files with 43 additions and 2 deletions
|
@ -51,6 +51,13 @@ class ClassRegistry {
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
var $__map = array();
|
var $__map = array();
|
||||||
|
/**
|
||||||
|
* Default constructor parameter settings, indexed by type
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
* @access private
|
||||||
|
*/
|
||||||
|
var $__params = array();
|
||||||
/**
|
/**
|
||||||
* Return a singleton instance of the ClassRegistry.
|
* Return a singleton instance of the ClassRegistry.
|
||||||
*
|
*
|
||||||
|
@ -99,11 +106,16 @@ class ClassRegistry {
|
||||||
$objects = array(array('class' => $class));
|
$objects = array(array('class' => $class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$defaults = array_merge(
|
||||||
|
array('id' => false, 'table' => null, 'ds' => null, 'alias' => null, 'name' => null),
|
||||||
|
isset($_this->__params[$type]) ? $_this->__params[$type] : array()
|
||||||
|
);
|
||||||
|
|
||||||
$count = count($objects);
|
$count = count($objects);
|
||||||
foreach ($objects as $key => $settings) {
|
foreach ($objects as $key => $settings) {
|
||||||
if (is_array($settings)) {
|
if (is_array($settings)) {
|
||||||
$plugin = null;
|
$plugin = null;
|
||||||
$settings = array_merge(array('id' => false, 'table' => null, 'ds' => null, 'alias' => null, 'name' => null), $settings);
|
$settings = array_merge($defaults, $settings);
|
||||||
|
|
||||||
extract($settings, EXTR_OVERWRITE);
|
extract($settings, EXTR_OVERWRITE);
|
||||||
|
|
||||||
|
@ -237,6 +249,29 @@ class ClassRegistry {
|
||||||
$return = false;
|
$return = false;
|
||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
* Sets the default constructor parameter for an object type
|
||||||
|
*
|
||||||
|
* @param string $type Type of object. If this parameter is omitted, defaults to "Model"
|
||||||
|
* @param array $param The parameter that will be passed to object constructors when objects
|
||||||
|
* of $type are created
|
||||||
|
* @return mixed Void if $param is being set. Otherwise, if only $type is passed, returns
|
||||||
|
* the previously-set value of $param, or null if not set.
|
||||||
|
*/
|
||||||
|
function params($type, $param = array()) {
|
||||||
|
$_this =& ClassRegistry::getInstance();
|
||||||
|
|
||||||
|
if (empty($param) && is_array($type)) {
|
||||||
|
$param = $type;
|
||||||
|
$type = 'Model';
|
||||||
|
} elseif (is_null($param)) {
|
||||||
|
unset($_this->__params[$type]);
|
||||||
|
} elseif (empty($param) && is_string($type)) {
|
||||||
|
return isset($_this->__params[$type]) ? $_this->__params[$type] : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$_this->__params[$type] = $param;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Checks to see if $alias is a duplicate $class Object
|
* Checks to see if $alias is a duplicate $class Object
|
||||||
*
|
*
|
||||||
|
|
|
@ -654,6 +654,8 @@ class CakeTestCase extends UnitTestCase {
|
||||||
$this->db =& ConnectionManager::getDataSource('test_suite');
|
$this->db =& ConnectionManager::getDataSource('test_suite');
|
||||||
$this->db->cacheSources = false;
|
$this->db->cacheSources = false;
|
||||||
$this->db->fullDebug = false;
|
$this->db->fullDebug = false;
|
||||||
|
|
||||||
|
ClassRegistry::params(array('ds' => 'test_suite'));
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Load fixtures specified in var $fixtures.
|
* Load fixtures specified in var $fixtures.
|
||||||
|
|
|
@ -78,7 +78,11 @@ class CakeTestFixture extends Object {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($import['model']) && (class_exists($import['model']) || App::import('Model', $import['model']))) {
|
if (isset($import['model']) && (class_exists($import['model']) || App::import('Model', $import['model']))) {
|
||||||
$model =& new $import['model'];
|
$connection = isset($import['connection'])
|
||||||
|
? $import['connection']
|
||||||
|
: 'test_suite';
|
||||||
|
ClassRegistry::params(array('ds' => $connection));
|
||||||
|
$model =& ClassRegistry::init($import['model']);
|
||||||
|
|
||||||
$db =& ConnectionManager::getDataSource($model->useDbConfig);
|
$db =& ConnectionManager::getDataSource($model->useDbConfig);
|
||||||
$db->cacheSources = false;
|
$db->cacheSources = false;
|
||||||
|
|
Loading…
Reference in a new issue