|
4 | 4 | use DateTime; |
5 | 5 | use GT\ServiceContainer\Container; |
6 | 6 | use GT\ServiceContainer\Injector; |
| 7 | +use GT\ServiceContainer\ServiceNotFoundException; |
7 | 8 | use GT\ServiceContainer\Test\Example\Greeter; |
8 | 9 | use PHPUnit\Framework\TestCase; |
9 | 10 |
|
@@ -77,4 +78,87 @@ public function testInvoke_usesExtraArgByParameterName():void { |
77 | 78 | $sut->invoke(null, $function, ["name" => "Cron"]) |
78 | 79 | ); |
79 | 80 | } |
| 81 | + |
| 82 | + public function testInvoke_usesExtraArgByParameterType():void { |
| 83 | + $dateTime = new DateTime("2026-06-22"); |
| 84 | + $function = function(DateTime $dateTime):string { |
| 85 | + return $dateTime->format("Y-m-d"); |
| 86 | + }; |
| 87 | + |
| 88 | + $sut = new Injector(new Container()); |
| 89 | + self::assertSame( |
| 90 | + "2026-06-22", |
| 91 | + $sut->invoke(null, $function, [DateTime::class => $dateTime]) |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + public function testInvoke_usesExtraArgForUntypedParameter():void { |
| 96 | + $function = function($name = "World"):string { |
| 97 | + return "Hello, $name!"; |
| 98 | + }; |
| 99 | + |
| 100 | + $sut = new Injector(new Container()); |
| 101 | + self::assertSame( |
| 102 | + "Hello, Cron!", |
| 103 | + $sut->invoke(null, $function, ["name" => "Cron"]) |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + public function testInvoke_usesDefaultUntypedParameter():void { |
| 108 | + $function = function($name = "World"):string { |
| 109 | + return "Hello, $name!"; |
| 110 | + }; |
| 111 | + |
| 112 | + $sut = new Injector(new Container()); |
| 113 | + self::assertSame("Hello, World!", $sut->invoke(null, $function)); |
| 114 | + } |
| 115 | + |
| 116 | + public function testInvoke_setsMissingUntypedParameterToNull():void { |
| 117 | + $function = function($name):mixed { |
| 118 | + return $name; |
| 119 | + }; |
| 120 | + |
| 121 | + $sut = new Injector(new Container()); |
| 122 | + self::assertNull($sut->invoke(null, $function)); |
| 123 | + } |
| 124 | + |
| 125 | + public function testInvoke_setsMissingNullableBuiltinParameterToNull():void { |
| 126 | + $function = function(?string $name):mixed { |
| 127 | + return $name; |
| 128 | + }; |
| 129 | + |
| 130 | + $sut = new Injector(new Container()); |
| 131 | + self::assertNull($sut->invoke(null, $function)); |
| 132 | + } |
| 133 | + |
| 134 | + public function testInvoke_throwsWhenRequiredBuiltinParameterIsMissing():void { |
| 135 | + $function = function(string $name):string { |
| 136 | + return $name; |
| 137 | + }; |
| 138 | + |
| 139 | + $sut = new Injector(new Container()); |
| 140 | + |
| 141 | + self::expectException(ServiceNotFoundException::class); |
| 142 | + $sut->invoke(null, $function); |
| 143 | + } |
| 144 | + |
| 145 | + public function testInvoke_setsMissingNullableServiceParameterToNull():void { |
| 146 | + $function = function(?Greeter $greeter):mixed { |
| 147 | + return $greeter; |
| 148 | + }; |
| 149 | + |
| 150 | + $sut = new Injector(new Container()); |
| 151 | + self::assertNull($sut->invoke(null, $function)); |
| 152 | + } |
| 153 | + |
| 154 | + public function testInvoke_throwsWhenRequiredServiceParameterIsMissing():void { |
| 155 | + $function = function(Greeter $greeter):Greeter { |
| 156 | + return $greeter; |
| 157 | + }; |
| 158 | + |
| 159 | + $sut = new Injector(new Container()); |
| 160 | + |
| 161 | + self::expectException(ServiceNotFoundException::class); |
| 162 | + $sut->invoke(null, $function); |
| 163 | + } |
80 | 164 | } |
0 commit comments