@@ -226,6 +226,77 @@ public function testAutowireWithoutContainerLazyLoadsParams(): void
226226 $ this ->assertEquals ('lazy_loaded ' , $ instance ->dep ->value );
227227 }
228228
229+ public function testRefResolvesClassDependencyByStringKey (): void
230+ {
231+ $ container = new Container ();
232+ $ dep = new AutowireDependency ('via_ref ' );
233+ $ container ['my.custom.dep ' ] = $ dep ;
234+
235+ $ autowire = new Autowire (AutowireTypedConsumer::class);
236+ $ autowire ->setContainer ($ container );
237+ $ autowire ->setParam ('dep ' , new Ref ('my.custom.dep ' ));
238+
239+ $ instance = $ autowire ->getInstance ();
240+ $ this ->assertSame ($ dep , $ instance ->dep );
241+ }
242+
243+ public function testRefResolvesNonClassDependency (): void
244+ {
245+ $ container = new Container ();
246+ $ container ['app.name ' ] = 'MyApp ' ;
247+
248+ $ autowire = new Autowire (AutowireWithBuiltin::class);
249+ $ autowire ->setContainer ($ container );
250+ $ autowire ->setParam ('name ' , new Ref ('app.name ' ));
251+
252+ $ instance = $ autowire ->getInstance ();
253+ $ this ->assertEquals ('MyApp ' , $ instance ->name );
254+ }
255+
256+ public function testRefCoexistsWithTypeBasedAutowiring (): void
257+ {
258+ $ container = new Container ();
259+ $ container ['DateTime ' ] = new DateTime ('2024-01-15 ' );
260+ $ container ['custom.dep ' ] = new AutowireDependency ('from_ref ' );
261+
262+ $ autowire = new Autowire (AutowireMultiParam::class);
263+ $ autowire ->setContainer ($ container );
264+ // Only bind 'dep' via Ref; 'date' should be autowired by type
265+ $ autowire ->setParam ('dep ' , new Ref ('custom.dep ' ));
266+
267+ $ instance = $ autowire ->getInstance ();
268+ $ this ->assertInstanceOf (DateTime::class, $ instance ->date );
269+ $ this ->assertEquals ('from_ref ' , $ instance ->dep ->value );
270+ }
271+
272+ public function testRefTakesPrecedenceOverTypeBasedAutowiring (): void
273+ {
274+ $ container = new Container ();
275+ $ container ['DateTime ' ] = new DateTime ('2024-01-15 ' );
276+ $ container [AutowireDependency::class] = new AutowireDependency ('from_type ' );
277+ $ container ['override.dep ' ] = new AutowireDependency ('from_ref ' );
278+
279+ $ autowire = new Autowire (AutowireTypedConsumer::class);
280+ $ autowire ->setContainer ($ container );
281+ $ autowire ->setParam ('dep ' , new Ref ('override.dep ' ));
282+
283+ $ instance = $ autowire ->getInstance ();
284+ $ this ->assertEquals ('from_ref ' , $ instance ->dep ->value );
285+ }
286+
287+ public function testRefResolvesArrayDependency (): void
288+ {
289+ $ container = new Container ();
290+ $ container ['app.paths ' ] = ['/path/one ' , '/path/two ' ];
291+
292+ $ autowire = new Autowire (AutowireWithArray::class);
293+ $ autowire ->setContainer ($ container );
294+ $ autowire ->setParam ('paths ' , new Ref ('app.paths ' ));
295+
296+ $ instance = $ autowire ->getInstance ();
297+ $ this ->assertEquals (['/path/one ' , '/path/two ' ], $ instance ->paths );
298+ }
299+
229300 /** @return array<string, mixed> */
230301 private static function parseIni (string $ ini ): array
231302 {
@@ -284,3 +355,11 @@ public function __construct(public DateTime|null $a = null, public DateTime|null
284355 {
285356 }
286357}
358+
359+ class AutowireWithArray
360+ {
361+ /** @param array<string> $paths */
362+ public function __construct (public array $ paths )
363+ {
364+ }
365+ }
0 commit comments