auraphp / Aura.Di

Dependency Injection System
MIT License
349 stars 63 forks source link

Question: What is the return value when invoking lazyNew with args? #204

Closed kenjis closed 4 months ago

kenjis commented 3 years ago

In 4.x.

The following test code is correct or not?

--- a/tests/ContainerTest.php
+++ b/tests/ContainerTest.php
@@ -190,6 +190,16 @@ class ContainerTest extends TestCase
         $this->assertSame($items, $instance->getItems());
     }

+    public function testLazyNewInvokable()
+    {
+        $lazy = $this->container->lazyNew(
+            'Aura\Di\Fake\FakeInvokableClass',
+            ['foo' => 'construct']
+        );
+        $invoke = $lazy('invoke');
+        $this->assertSame('constructinvoke', $invoke);
+    }
+
     public function testLockedMagicGet()
     {
         $this->container->lock();
kenjis commented 3 years ago

The return type is object: https://github.com/auraphp/Aura.Di/blob/d1908d3170a453682447052d091a4e1a81974ace/src/Injection/Factory.php#L93

And the Doc comment says: https://github.com/auraphp/Aura.Di/blob/d1908d3170a453682447052d091a4e1a81974ace/src/Injection/Factory.php#L82-L84

The test should be:

--- a/tests/ContainerTest.php
+++ b/tests/ContainerTest.php
@@ -190,6 +190,19 @@ class ContainerTest extends TestCase
         $this->assertSame($items, $instance->getItems());
     }

+    public function testLazyNewInvokable()
+    {
+        $lazy = $this->container->lazyNew(
+            'Aura\Di\Fake\FakeInvokableClass',
+            ['foo' => 'construct']
+        );
+        $invoke = $lazy('LazyNew');
+        $this->assertInstanceOf('Aura\Di\Fake\FakeInvokableClass', $invoke);
+
+        $output = $invoke('FakeInvokableClass');
+        $this->assertSame('LazyNewFakeInvokableClass', $output);
+    }
+
     public function testLockedMagicGet()
     {
         $this->container->lock();
harikt commented 2 years ago

Tha lazyNew will return an object. I am not sure what you are trying to get from these tests. I did tried your test and looks ok. Is there any specific question or answer you want to address here ?

frederikbosch commented 4 months ago

The return type is a LazyNew object, which is an implementation of the LazyInterface.