Skip to content

Commit

Permalink
Add better assertions to extends tests
Browse files Browse the repository at this point in the history
  • Loading branch information
philipobenito committed Sep 30, 2020
1 parent 002a94c commit 132f614
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/Definition/Definition.php
Expand Up @@ -11,6 +11,8 @@
LiteralArgumentInterface
};
use League\Container\ContainerAwareTrait;
use League\Container\Exception\ContainerException;
use Psr\Container\ContainerInterface;
use ReflectionClass;

class Definition implements ArgumentResolverInterface, DefinitionInterface
Expand Down Expand Up @@ -177,10 +179,16 @@ public function resolveNew()
$concrete = $this->invokeMethods($concrete);
}

try {
$container = $this->getContainer();
} catch (ContainerException $e) {
$container = null;
}

// if we still have a string, try to pull it from the container
// this allows for `alias -> alias -> ... -> concrete
if (is_string($concrete) && $this->getContainer()->has($concrete)) {
$concrete = $this->getContainer()->get($concrete);
if (is_string($concrete) && $container instanceof ContainerInterface && $container->has($concrete)) {
$concrete = $container->get($concrete);
}

$this->resolved = $concrete;
Expand Down
8 changes: 6 additions & 2 deletions tests/ContainerTest.php
Expand Up @@ -142,7 +142,9 @@ public function testContainerCanExtendDefinition(): void
{
$container = new Container();
$container->add(Foo::class);
$container->extend(Foo::class);
$definition = $container->extend(Foo::class);
self::assertSame(Foo::class, $definition->getAlias());
self::assertSame(Foo::class, $definition->getConcrete());
}

public function testContainerCanExtendDefinitionFromServiceProvider(): void
Expand All @@ -161,7 +163,9 @@ public function register(): void

$container = new Container();
$container->addServiceProvider($provider);
$container->extend(Foo::class);
$definition = $container->extend(Foo::class);
self::assertSame(Foo::class, $definition->getAlias());
self::assertSame(Foo::class, $definition->getConcrete());
}

public function testContainerThrowsWhenCannotGetDefinitionToExtend(): void
Expand Down

0 comments on commit 132f614

Please sign in to comment.