@@ -187,6 +187,43 @@ public function testUnsetExtend()
187187
188188 $ this ->assertSame ('foo ' , $ container ->make ('foo ' ));
189189 }
190+
191+ public function testExtendContextualBinding ()
192+ {
193+ $ container = new Container ();
194+ $ container ->when (ContainerExtendConsumesInterfaceStub::class)
195+ ->needs (ContainerExtendInterfaceStub::class)
196+ ->give (fn () => new ContainerExtendInterfaceImplementationStub ('foo ' ));
197+
198+ $ container ->extend (ContainerExtendInterfaceStub::class, function ($ instance ) {
199+ self ::assertInstanceOf (ContainerExtendInterfaceImplementationStub::class, $ instance );
200+ self ::assertSame ('foo ' , $ instance ->value );
201+
202+ return new ContainerExtendInterfaceImplementationStub ('bar ' );
203+ });
204+
205+ self ::assertSame ('bar ' , $ container ->make (ContainerExtendConsumesInterfaceStub::class)->stub ->value );
206+ }
207+
208+ // https://github.com/laravel/framework/issues/53501
209+ public function testExtendContextualBindingAfterResolution ()
210+ {
211+ $ container = new Container ();
212+ $ container ->when (ContainerExtendConsumesInterfaceStub::class)
213+ ->needs (ContainerExtendInterfaceStub::class)
214+ ->give (fn () => new ContainerExtendInterfaceImplementationStub ('foo ' ));
215+
216+ $ container ->make (ContainerExtendConsumesInterfaceStub::class);
217+
218+ $ container ->extend (ContainerExtendInterfaceStub::class, function ($ instance ) {
219+ self ::assertInstanceOf (ContainerExtendInterfaceImplementationStub::class, $ instance );
220+ self ::assertSame ('foo ' , $ instance ->value );
221+
222+ return new ContainerExtendInterfaceImplementationStub ('bar ' );
223+ });
224+
225+ self ::assertSame ('bar ' , $ container ->make (ContainerExtendConsumesInterfaceStub::class)->stub ->value );
226+ }
190227}
191228
192229class ContainerLazyExtendStub
@@ -198,3 +235,23 @@ public function init()
198235 static ::$ initialized = true ;
199236 }
200237}
238+
239+ interface ContainerExtendInterfaceStub
240+ {
241+ }
242+
243+ class ContainerExtendInterfaceImplementationStub implements ContainerExtendInterfaceStub
244+ {
245+ public function __construct (
246+ public string $ value ,
247+ ) {
248+ }
249+ }
250+
251+ class ContainerExtendConsumesInterfaceStub
252+ {
253+ public function __construct (
254+ public ContainerExtendInterfaceStub $ stub ,
255+ ) {
256+ }
257+ }
0 commit comments