@@ -138,4 +138,63 @@ public function testGetWillReturnFromCacheWhenSericeAlreadyResolved(): void
138138
139139 self ::assertSame ($ service , $ this ->container ->get ('foo ' ));
140140 }
141+
142+ public function testApplyServiceExtensionByClassName (): void
143+ {
144+ $ service = new class () {
145+ public bool $ extended = false ;
146+ };
147+
148+ $ extension = static function (ContainerInterface $ c , object $ service ): void {
149+ $ service ->extended = true ;
150+ };
151+
152+ $ this ->provider ->getFactories ()->willReturn (['service.id ' => static fn () => $ service ]);
153+ $ this ->provider ->getExtensions ()->willReturn ([get_class ($ service ) => $ extension ]);
154+
155+ $ resolved = $ this ->container ->get ('service.id ' );
156+
157+ self ::assertTrue ($ resolved ->extended );
158+ }
159+
160+ public function testApplyServiceExtensionByIdAndClass (): void
161+ {
162+ $ service = new class () {
163+ public array $ calls = [];
164+ };
165+
166+ $ byIdExtension = static function (ContainerInterface $ c , object $ service ): void {
167+ $ service ->calls [] = 'id ' ;
168+ };
169+
170+ $ byClassExtension = static function (ContainerInterface $ c , object $ service ): void {
171+ $ service ->calls [] = 'class ' ;
172+ };
173+
174+ $ this ->provider ->getFactories ()->willReturn (['dual ' => static fn () => $ service ]);
175+ $ this ->provider ->getExtensions ()->willReturn ([
176+ 'dual ' => $ byIdExtension ,
177+ get_class ($ service ) => $ byClassExtension ,
178+ ]);
179+
180+ $ resolved = $ this ->container ->get ('dual ' );
181+
182+ self ::assertSame (['id ' , 'class ' ], $ resolved ->calls );
183+ }
184+
185+ public function testApplyServiceIgnoresNonCallableExtensions (): void
186+ {
187+ $ service = new \stdClass ();
188+
189+ $ this ->provider ->getFactories ()->willReturn (['not.callable ' => static fn () => $ service ]);
190+ $ this ->provider ->getExtensions ()->willReturn ([
191+ 'not.callable ' => 'not_a_function ' ,
192+ get_class ($ service ) => 123 ,
193+ ]);
194+
195+ $ resolved = $ this ->container ->get ('not.callable ' );
196+
197+ self ::assertSame ($ service , $ resolved ); // Should still return the service
198+ self ::assertObjectNotHasProperty ('extended ' , $ resolved ); // No extension applied
199+ }
141200}
0 commit comments