diff --git a/tests/src/Container/ConstructorTest.php b/tests/src/Container/ConstructorTest.php index dbaa392..4ad8847 100644 --- a/tests/src/Container/ConstructorTest.php +++ b/tests/src/Container/ConstructorTest.php @@ -41,6 +41,23 @@ public function testClass() $this->assertInstanceOf(Foo::class, $entry->getFoo()); } + public function testAlias() + { + // arrange + $container = new Container([ + 'foo' => ['class' => Foo::class], + ClassDependency::class => [ + 'args' => ['$foo'] + ], + ]); + // act + /** @var ClassDependency $entry */ + $entry = $container->get(ClassDependency::class); + // assert + $this->assertInstanceOf(ClassDependency::class, $entry); + $this->assertInstanceOf(Foo::class, $entry->getFoo()); + } + public function testInterface() { // arrange diff --git a/tests/src/Container/DependencyTest.php b/tests/src/Container/DependencyTest.php index be7c192..6070b31 100644 --- a/tests/src/Container/DependencyTest.php +++ b/tests/src/Container/DependencyTest.php @@ -23,6 +23,21 @@ public function testClassDependency() $entry = $container->get(ClassDependency::class); // assert $this->assertInstanceOf(ClassDependency::class, $entry); + $this->assertNull($entry->getFoo()); + } + + public function testAliasDependency() + { + // arrange + $container = new Container([ + ClassDependency::class => ['args' => ['$foo']], + 'foo' => ['class' => Foo::class], + ]); + // act + $entry = $container->get(ClassDependency::class); + // assert + $this->assertInstanceOf(ClassDependency::class, $entry); + $this->assertInstanceOf(Foo::class, $entry->getFoo()); } public function testInterfaceDependency() @@ -36,6 +51,7 @@ public function testInterfaceDependency() $entry = $container->get(InterfaceDependency::class); // assert $this->assertInstanceOf(InterfaceDependency::class, $entry); + $this->assertInstanceOf(Foo::class, $entry->getFoo()); } /** @@ -49,8 +65,20 @@ public function testUnresolvedDependencies() ScalarDependency::class => [], ]); // act - $entry = $container->get(ScalarDependency::class); - // assert - $this->assertInstanceOf(ScalarDependency::class, $entry); + $container->get(ScalarDependency::class); + } + + /** + * @expectedException \Psr\Container\ContainerExceptionInterface + * @expectedExceptionMessage Unable to create instance of entry `Zp\PHPWire\Tests\Fixtures\ClassDependency`: Unable to invoke arguments to constructor: Requested a non-existent container entry `foo` + */ + public function testRequestNonExistent() + { + // arrange + $container = new Container([ + ClassDependency::class => ['args' => ['$foo']], + ]); + // act + $container->get(ClassDependency::class); } }