Skip to content

Commit

Permalink
Fix #102: Reference should not fallback to container (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Jul 28, 2021
1 parent 87a17cf commit 6d03850
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
5 changes: 1 addition & 4 deletions src/DependencyResolver.php
Expand Up @@ -81,10 +81,7 @@ public function has($id): bool
*/
public function resolve(string $id)
{
if (isset($this->definitions[$id])) {
return $this->getFromFactory($id);
}
return $this->get($id);
return $this->getFromFactory($id);
}

public function shouldCloneOnResolve(): bool
Expand Down
9 changes: 7 additions & 2 deletions tests/Unit/Definition/DynamicReferenceTest.php
Expand Up @@ -10,6 +10,7 @@
use Yiisoft\Factory\DependencyResolver;
use Yiisoft\Factory\Tests\Support\EngineInterface;
use Yiisoft\Factory\Tests\Support\EngineMarkOne;
use Yiisoft\Factory\Tests\Support\EngineMarkTwo;
use Yiisoft\Factory\Tests\TestHelper;
use Yiisoft\Injector\Injector;
use Yiisoft\Test\Support\Container\SimpleContainer;
Expand All @@ -24,13 +25,17 @@ public function createDependencyResolver(): DependencyResolver
Injector::class => &$injector,
]);
$injector = new Injector($container);
return TestHelper::createDependencyResolver($container);

$dependencyResolver = TestHelper::createDependencyResolver($container);
$dependencyResolver->setFactoryDefinition(EngineInterface::class, EngineMarkTwo::class);

return $dependencyResolver;
}

public function testString(): void
{
$ref = DynamicReference::to(EngineInterface::class);
$this->assertInstanceOf(EngineMarkOne::class, $ref->resolve($this->createDependencyResolver()));
$this->assertInstanceOf(EngineMarkTwo::class, $ref->resolve($this->createDependencyResolver()));
}

public function testClosure(): void
Expand Down
16 changes: 16 additions & 0 deletions tests/Unit/FactoryTest.php
Expand Up @@ -302,6 +302,22 @@ public function testDoNotFallbackToContainer(): void
$this->assertNotSame($engine, $instance);
}

public function testDoNotFallbackToContainerForReference(): void
{
$factory = new Factory(
new SimpleContainer([
EngineInterface::class => new EngineMarkOne(),
]),
[
EngineInterface::class => new EngineMarkTwo(),
'engine' => Reference::to(EngineInterface::class),
]
);

$engine = $factory->create('engine');
$this->assertInstanceOf(EngineMarkTwo::class, $engine);
}

/**
* When resolving dependencies, factory should rely on container only
*/
Expand Down

0 comments on commit 6d03850

Please sign in to comment.