Skip to content

Commit

Permalink
fix Module::setComponent() unable to override component resolved fr…
Browse files Browse the repository at this point in the history
…om DI container
  • Loading branch information
klimov-paul committed Oct 13, 2023
1 parent 4daed7e commit 4e1be01
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Yii1 Dependency Injection extension
===================================

1.0.4 Under Development
-----------------------

- Bug: Fixed `Module::setComponent()` unable to override component resolved from DI container (klimov-paul)


1.0.3, August 30, 2023
----------------------

Expand Down
4 changes: 4 additions & 0 deletions src/base/ResolvesComponentViaDI.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ public function setComponent($id, $component, $merge = true): void
return;
}

if (isset($this->_diComponents[$id])) {
unset($this->_diComponents[$id]);
}

parent::setComponent($id, $component, $merge);
}
}
27 changes: 27 additions & 0 deletions tests/base/ModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,31 @@ public function testSetComponentByString(): void
$component = $module->getComponent('cache', true);
$this->assertSame($cache, $component);
}

/**
* @depends testGetComponent
*/
public function testOverrideResolvedComponent(): void
{
$container = new Container();
$cache = new CDummyCache();
$container->instance(ICache::class, $cache);

DI::setContainer($container);

$module = new Module('test', Yii::app(), [
'components' => [
'cache' => [
'class' => ICache::class,
],
],
]);

$resolvedComponent = $module->getComponent('cache');

$newComponent = new CDummyCache();
$module->setComponent('cache', $newComponent, false);

$this->assertSame($newComponent, $module->getComponent('cache'));
}
}

0 comments on commit 4e1be01

Please sign in to comment.