Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Unit test + improved fix

  • Loading branch information...
commit 3e3deacbf112efa4b87e912a6b207df137f48108 1 parent 95b27a7
@creocoder creocoder authored
View
49 framework/base/CModule.php
@@ -459,14 +459,51 @@ public function getComponents($loadedOnly=true)
*/
public function setComponents($components,$merge=true)
{
- foreach($components as $id=>$component)
+ foreach($components as $id=>$config)
{
- if($component instanceof IApplicationComponent)
- $this->setComponent($id,$component);
- else if(isset($this->_componentConfig[$id]) && $merge)
- $this->_componentConfig[$id]=CMap::mergeArray($this->_componentConfig[$id],$component);
+ if($config instanceof IApplicationComponent)
+ $this->setComponent($id,$config);
else
- $this->_componentConfig[$id]=$component;
+ {
+ if(isset($this->_components[$id]))
+ {
+ if(isset($config['class']))
+ {
+ if(get_class($this->_components[$id])!==$config['class'])
+ {
+ unset($this->_components[$id]);
+ $this->_componentConfig[$id]=$config; //we should ignore merge here
+ continue;
+ }
+ else
+ {
+ $class=$config['class'];
+ unset($config['class']);
+
+ foreach($config as $key=>$value)
+ $this->_components[$id]->$key=$value;
+
+ $config['class']=$class;
+ }
+ }
+ else
+ {
+ foreach($config as $key=>$value)
+ $this->_components[$id]->$key=$value;
+ }
+ }
+ elseif(isset($this->_componentConfig[$id]['class'],$config['class'])
+ && $this->_componentConfig[$id]['class']!==$config['class'])
+ {
+ $this->_componentConfig[$id]=$config; //we should ignore merge here
+ continue;
+ }
+
+ if(isset($this->_componentConfig[$id]) && $merge)
+ $this->_componentConfig[$id]=CMap::mergeArray($this->_componentConfig[$id],$config);
+ else
+ $this->_componentConfig[$id]=$config;
+ }
}
}
View
14 tests/framework/base/AnotherNewApplicationComponent.php
@@ -0,0 +1,14 @@
+<?php
+class AnotherNewApplicationComponent extends CApplicationComponent {
+ private $_text='new';
+
+ public function getText()
+ {
+ return $this->_text;
+ }
+
+ public function setText($value)
+ {
+ return $this->_text=$value;
+ }
+}
View
32 tests/framework/base/CModuleTest.php
@@ -1,6 +1,7 @@
<?php
require_once dirname(__FILE__) . '/NewModule.php';
require_once dirname(__FILE__) . '/NewApplicationComponent.php';
+require_once dirname(__FILE__) . '/AnotherNewApplicationComponent.php';
class CModuleTest extends CTestCase {
protected $parent;
@@ -83,10 +84,37 @@ public function testSetComponents() {
public function testSetComponentsViaConfig() {
$this->mod = new NewModule('foo',$this->parent,array(
'components' => array(
- 'bar' => array('class' => 'NewApplicationComponent')
- )
+ 'bar' => array('class' => 'NewApplicationComponent'),
+ ),
));
$this->assertEquals('hello world',$this->mod->bar->getText('hello world'));
+ $this->mod->setComponents(array(
+ 'bar' => array('text' => 'test'),
+ ));
+ $this->assertEquals('test',$this->mod->bar->getText());
+ $this->mod->setComponent('bar',null);
+ $this->assertEquals('test',$this->mod->bar->getText());
+ $this->mod->setComponents(array(
+ 'bar' => array('class' => 'NewApplicationComponent'),
+ ));
+ $this->assertEquals('test',$this->mod->bar->getText());
+ $this->mod->setComponents(array(
+ 'bar' => array('class' => 'AnotherNewApplicationComponent'),
+ ));
+ $this->assertEquals('new',$this->mod->bar->getText());
+ $this->mod->setComponent('bar',null);
+ $this->assertEquals('new',$this->mod->bar->getText());
+ $this->mod->setComponent('bar',null);
+ $this->mod->setComponents(array(
+ 'bar' => array(
+ 'class' => 'NewApplicationComponent',
+ 'text' => 'test',
+ ),
+ ));
+ $this->mod->setComponents(array(
+ 'bar' => array('class' => 'AnotherNewApplicationComponent'),
+ ));
+ $this->assertEquals('new',$this->mod->bar->getText());
}
public function testSetAliases() {
$this->mod->setAliases(array('modules' => $this->d));
Please sign in to comment.
Something went wrong with that request. Please try again.