Components can have settings assigned in the components declaration, tests added. Closes #4055

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7112 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
mark_story 2008-06-04 02:39:05 +00:00
parent 7c6cdb6354
commit 55bcf1e25e
4 changed files with 129 additions and 15 deletions

View file

@ -44,6 +44,13 @@ class Component extends Object {
* @access private
*/
var $__loaded = array();
/**
* Settings for loaded components.
*
* @var array
* @access private
**/
var $__settings = array();
/**
* Used to initialize the components for current controller
*
@ -71,7 +78,11 @@ class Component extends Object {
function initialize(&$controller) {
foreach ($this->__loaded as $name => $component) {
if (method_exists($component,'initialize') && $component->enabled === true) {
$component->initialize($controller);
$settings = array();
if (isset($this->__settings[$name])) {
$settings = $this->__settings[$name];
}
$component->initialize($controller, $settings);
}
}
}
@ -146,7 +157,8 @@ class Component extends Object {
$base = $this->__controllerVars['base'];
if (is_array($object->components)) {
foreach ($object->components as $component) {
$normal = Set::normalize($object->components);
foreach ($normal as $component => $config) {
$parts = preg_split('/\/|\./', $component);
if (count($parts) === 1) {
@ -185,6 +197,12 @@ class Component extends Object {
if (isset($this->__loaded[$component])) {
$object->{$component} =& $this->__loaded[$component];
if (!empty($config) && isset($this->__settings[$component])) {
$this->__settings[$component] = array_merge($this->__settings[$component], $config);
} elseif (!empty($config)) {
$this->__settings[$component] = $config;
}
} else {
if ($componentCn == 'SessionComponent') {
$object->{$component} =& new $componentCn($base);
@ -193,6 +211,9 @@ class Component extends Object {
}
$object->{$component}->enabled = true;
$this->__loaded[$component] =& $object->{$component};
if (!empty($config)) {
$this->__settings[$component] = $config;
}
}
if (isset($object->{$component}->components) && is_array($object->{$component}->components)) {

View file

@ -339,10 +339,16 @@ class Controller extends Object {
foreach ($merge as $var) {
if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) {
if ($var == 'components') {
$normal = Set::normalize($this->{$var});
$app = Set::normalize($appVars[$var]);
$this->{$var} = Set::merge($normal, $app);
} else {
$this->{$var} = Set::merge($this->{$var}, array_diff($appVars[$var], $this->{$var}));
}
}
}
}
if ($pluginController) {
$appVars = get_class_vars($pluginController);
@ -355,11 +361,17 @@ class Controller extends Object {
foreach ($merge as $var) {
if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) {
if ($var == 'components') {
$normal = Set::normalize($this->{$var});
$app = Set::normalize($appVars[$var]);
$this->{$var} = Set::merge($normal, array_diff_assoc($app, $normal));
} else {
$this->{$var} = Set::merge($this->{$var}, array_diff($appVars[$var], $this->{$var}));
}
}
}
}
}
/**
* Loads Model classes based on the the uses property
* see Controller::loadModel(); for more info

View file

@ -26,7 +26,42 @@
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
App::import('Core', array('Component', 'AppController'));
App::import('Core', array('Component', 'Controller'));
if (!class_exists('AppController')) {
class AppController extends Controller {
var $name = 'App';
var $uses = array();
var $helpers = array();
var $components = array('Orange' => array('colour' => 'blood orange'));
}
} else {
define('AppControllerExists', true);
}
/**
* ParamTestComponent
*
* @package cake.tests.cases.libs.controller
*/
class ParamTestComponent extends Object {
var $name = 'ParamTest';
var $components = array('Banana' => array('config' => 'value'));
function initialize(&$controller, $settings) {
foreach ($settings as $key => $value) {
if (is_numeric($key)) {
$this->{$value} = true;
} else {
$this->{$key} = $value;
}
}
}
}
/**
* Short description for class.
*
@ -103,8 +138,9 @@ class OrangeComponent extends Object {
* @access public
* @return void
*/
function initialize(&$controller) {
function initialize(&$controller, $settings) {
$this->Banana->testField = 'OrangeField';
$this->settings = $settings;
}
}
/**
@ -186,7 +222,11 @@ class ComponentTest extends CakeTestCase {
$this->assertTrue(is_a($Controller->RequestHandler, 'RequestHandlerComponent'));
$this->assertTrue(is_a($Controller->Cookie, 'CookieComponent'));
}
/**
* test component loading
*
* @return void
*/
function testNestedComponentLoading() {
$Controller =& new ComponentTestController();
$Controller->components = array('Apple');
@ -196,7 +236,11 @@ class ComponentTest extends CakeTestCase {
$this->assertTrue(is_a($Controller->Apple->Orange, 'OrangeComponent'));
$this->assertTrue(is_a($Controller->Apple->Orange->Banana, 'BananaComponent'));
}
/**
* test component::startup and running all built components startup()
*
* @return void
*/
function testComponentStartup() {
$Controller =& new ComponentTestController();
$Controller->components = array('Apple');
@ -209,7 +253,11 @@ class ComponentTest extends CakeTestCase {
$this->assertEqual($Controller->Apple->testName, 'ComponentTest');
}
/**
* test a component being used more than once.
*
* @return void
*/
function testMultipleComponentInitialize() {
$Controller =& new ComponentTestController();
$Controller->components = array('Orange', 'Banana');
@ -220,5 +268,37 @@ class ComponentTest extends CakeTestCase {
$this->assertEqual($Controller->Banana->testField, 'OrangeField');
$this->assertEqual($Controller->Orange->Banana->testField, 'OrangeField');
}
/**
* Test Component declarations with Parameters
* tests merging of component parameters and merging / construction of components.
*
* @return void
*/
function testComponentsWithParams() {
$this->skipIf(defined('AppControllerExists'), 'Components with Params test will be skipped as it needs a non-existent AppController. As the an AppController class exists, this cannot be run.');
$Controller =& new ComponentTestController();
$Controller->components = array('ParamTest' => array('test' => 'value', 'flag'), 'Apple');
$Controller->constructClasses();
$Controller->Component->initialize($Controller);
$this->assertTrue(is_a($Controller->ParamTest, 'ParamTestComponent'));
$this->assertTrue(is_a($Controller->ParamTest->Banana, 'BananaComponent'));
$this->assertTrue(is_a($Controller->Orange, 'OrangeComponent'));
$this->assertTrue(is_a($Controller->Session, 'SessionComponent'));
$this->assertEqual($Controller->Orange->settings, array('colour' => 'blood orange'));
$this->assertEqual($Controller->ParamTest->test, 'value');
$this->assertEqual($Controller->ParamTest->flag, true);
//Settings are merged from app controller and current controller.
$Controller =& new ComponentTestController();
$Controller->components = array('ParamTest' => array('test' => 'value'), 'Orange' => array('ripeness' => 'perfect'));
$Controller->constructClasses();
$Controller->Component->initialize($Controller);
$this->assertEqual($Controller->Orange->settings, array('colour' => 'blood orange', 'ripeness' => 'perfect'));
$this->assertEqual($Controller->ParamTest->test, 'value');
}
}
?>

View file

@ -573,6 +573,7 @@ class ControllerTest extends CakeTestCase {
$testVars = get_class_vars('TestController');
$appVars = get_class_vars('AppController');
$components = is_array($appVars['components'])
? array_merge($appVars['components'], $testVars['components'])
: $testVars['components'];
@ -588,7 +589,7 @@ class ControllerTest extends CakeTestCase {
$this->assertEqual(count(array_diff($TestController->helpers, $helpers)), 0);
$this->assertEqual(count(array_diff($TestController->uses, $uses)), 0);
$this->assertEqual(count(array_diff($TestController->components, $components)), 0);
$this->assertEqual(count(array_diff_assoc(Set::normalize($TestController->components), Set::normalize($components))), 0);
}
function testReferer() {