@@ -289,6 +289,40 @@ public function __invoke(ServerRequestInterface $request): Response
289289 $ this ->assertEquals ('{"name":"Alice"} ' , (string ) $ response ->getBody ());
290290 }
291291
292+ public function testCallableReturnsCallableForClassNameViaAutowiringWithFactoryFunctionWithContainerDependency (): void
293+ {
294+ $ request = new ServerRequest ('GET ' , 'http://example.com/ ' );
295+
296+ $ controller = new class (new \stdClass ()) {
297+ /** @var \stdClass */
298+ private $ data ;
299+
300+ public function __construct (\stdClass $ data )
301+ {
302+ $ this ->data = $ data ;
303+ }
304+
305+ public function __invoke (ServerRequestInterface $ request ): Response
306+ {
307+ return new Response (200 , [], (string ) json_encode ($ this ->data ));
308+ }
309+ };
310+
311+ $ container = new Container ([
312+ \stdClass::class => function (Container $ container ) {
313+ return (object )['container ' => spl_object_hash ($ container )];
314+ }
315+ ]);
316+
317+ $ callable = $ container ->callable (get_class ($ controller ));
318+ $ this ->assertInstanceOf (\Closure::class, $ callable );
319+
320+ $ response = $ callable ($ request );
321+ $ this ->assertInstanceOf (ResponseInterface::class, $ response );
322+ $ this ->assertEquals (200 , $ response ->getStatusCode ());
323+ $ this ->assertEquals ('{"container":" ' . spl_object_hash ($ container ) . '"} ' , (string ) $ response ->getBody ());
324+ }
325+
292326 public function testCallableTwiceReturnsCallableForClassNameViaAutowiringWithFactoryFunctionForDependencyWillCallFactoryOnlyOnce (): void
293327 {
294328 $ request = new ServerRequest ('GET ' , 'http://example.com/ ' );
@@ -2555,6 +2589,43 @@ public function testGetObjectReturnsAccessLogHandlerInstanceFromConfig(): void
25552589 $ this ->assertSame ($ accessLogHandler , $ ret );
25562590 }
25572591
2592+ public function testGetObjectReturnsSelfContainerByDefault (): void
2593+ {
2594+ $ container = new Container ([]);
2595+
2596+ $ ret = $ container ->getObject (Container::class);
2597+
2598+ $ this ->assertSame ($ container , $ ret );
2599+ }
2600+
2601+ public function testGetObjectReturnsOtherContainerFromConfig (): void
2602+ {
2603+ $ other = new Container ();
2604+
2605+ $ container = new Container ([
2606+ Container::class => $ other
2607+ ]);
2608+
2609+ $ ret = $ container ->getObject (Container::class);
2610+
2611+ $ this ->assertSame ($ other , $ ret );
2612+ }
2613+
2614+ public function testGetObjectReturnsOtherContainerFromFactoryFunction (): void
2615+ {
2616+ $ other = new Container ();
2617+
2618+ $ container = new Container ([
2619+ Container::class => function () use ($ other ) {
2620+ return $ other ;
2621+ }
2622+ ]);
2623+
2624+ $ ret = $ container ->getObject (Container::class);
2625+
2626+ $ this ->assertSame ($ other , $ ret );
2627+ }
2628+
25582629 public function testGetObjectReturnsAccessLogHandlerInstanceFromPsrContainer (): void
25592630 {
25602631 $ accessLogHandler = new AccessLogHandler ();
@@ -2585,6 +2656,20 @@ public function testGetObjectReturnsDefaultAccessLogHandlerInstanceIfPsrContaine
25852656 $ this ->assertInstanceOf (AccessLogHandler::class, $ accessLogHandler );
25862657 }
25872658
2659+ public function testGetObjectReturnsSelfContainerIfPsrContainerHasNoEntry (): void
2660+ {
2661+ $ psr = $ this ->createMock (ContainerInterface::class);
2662+ $ psr ->expects ($ this ->once ())->method ('has ' )->with (Container::class)->willReturn (false );
2663+ $ psr ->expects ($ this ->never ())->method ('get ' );
2664+
2665+ assert ($ psr instanceof ContainerInterface);
2666+ $ container = new Container ($ psr );
2667+
2668+ $ ret = $ container ->getObject (Container::class);
2669+
2670+ $ this ->assertSame ($ container , $ ret );
2671+ }
2672+
25882673 public function testGetObjectThrowsIfFactoryFunctionThrows (): void
25892674 {
25902675 $ container = new Container ([
@@ -2636,6 +2721,20 @@ public function testGetObjectThrowsIfFactoryFunctionIsRecursive(): void
26362721 $ container ->getObject (AccessLogHandler::class);
26372722 }
26382723
2724+ public function testGetObjectThrowsIfFactoryFunctionHasRecursiveContainerArgument (): void
2725+ {
2726+ $ line = __LINE__ + 2 ;
2727+ $ container = new Container ([
2728+ Container::class => function (Container $ container ): Container {
2729+ return $ container ;
2730+ }
2731+ ]);
2732+
2733+ $ this ->expectException (\Error::class);
2734+ $ this ->expectExceptionMessage ('Argument #1 ($container) of {closure: ' . __FILE__ . ': ' . $ line .'}() for FrameworkX\Container is recursive ' );
2735+ $ container ->getObject (Container::class);
2736+ }
2737+
26392738 public function testGetObjectThrowsIfConfigReferencesInterface (): void
26402739 {
26412740 $ container = new Container ([
0 commit comments