From 501437baa0fe7fc71382b088dc40d84932edc7c2 Mon Sep 17 00:00:00 2001 From: DarkAngelBGE Date: Wed, 30 Jul 2008 15:37:26 +0000 Subject: [PATCH] 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 --- cake/libs/class_registry.php | 37 +++++++++++++++++++++++++++- cake/tests/lib/cake_test_case.php | 2 ++ cake/tests/lib/cake_test_fixture.php | 6 ++++- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/cake/libs/class_registry.php b/cake/libs/class_registry.php index f299a7084..6350d327e 100644 --- a/cake/libs/class_registry.php +++ b/cake/libs/class_registry.php @@ -51,6 +51,13 @@ class ClassRegistry { * @access private */ var $__map = array(); +/** + * Default constructor parameter settings, indexed by type + * + * @var array + * @access private + */ + var $__params = array(); /** * Return a singleton instance of the ClassRegistry. * @@ -99,11 +106,16 @@ class ClassRegistry { $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); foreach ($objects as $key => $settings) { if (is_array($settings)) { $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); @@ -237,6 +249,29 @@ class ClassRegistry { $return = false; 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 * diff --git a/cake/tests/lib/cake_test_case.php b/cake/tests/lib/cake_test_case.php index 30b74e2f4..ebf6c493d 100644 --- a/cake/tests/lib/cake_test_case.php +++ b/cake/tests/lib/cake_test_case.php @@ -654,6 +654,8 @@ class CakeTestCase extends UnitTestCase { $this->db =& ConnectionManager::getDataSource('test_suite'); $this->db->cacheSources = false; $this->db->fullDebug = false; + + ClassRegistry::params(array('ds' => 'test_suite')); } /** * Load fixtures specified in var $fixtures. diff --git a/cake/tests/lib/cake_test_fixture.php b/cake/tests/lib/cake_test_fixture.php index a5db62d2f..630d4d4a9 100644 --- a/cake/tests/lib/cake_test_fixture.php +++ b/cake/tests/lib/cake_test_fixture.php @@ -78,7 +78,11 @@ class CakeTestFixture extends Object { } 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->cacheSources = false;