|
1 | 1 | <?php namespace lang\reflection\unittest; |
2 | 2 |
|
| 3 | +use ReflectionFunction; |
3 | 4 | use lang\reflection\{Annotation, CannotInstantiate, InvocationFailed}; |
4 | 5 | use lang\{IllegalStateException, Reflection, XPClass}; |
5 | 6 | use test\{Assert, Expect, Test, Values}; |
@@ -170,6 +171,48 @@ public function with_array_lambda() { |
170 | 171 | Assert::equals(6100, $f[0]()); |
171 | 172 | } |
172 | 173 |
|
| 174 | + #[Test, Values(['function(\$param= null) { }', 'fn(\$param= null) => null'])] |
| 175 | + public function with_optional_param($function) { |
| 176 | + $t= $this->declare('{}', '#[Annotated(eval: "'.$function.'")]'); |
| 177 | + $f= $t->annotation(Annotated::class)->argument(0); |
| 178 | + |
| 179 | + Assert::true((new ReflectionFunction($f))->getParameters()[0]->isOptional()); |
| 180 | + } |
| 181 | + |
| 182 | + #[Test, Values(['function(... \$param) { }', 'fn(... \$param) => null'])] |
| 183 | + public function with_variadic_param($function) { |
| 184 | + $t= $this->declare('{}', '#[Annotated(eval: "'.$function.'")]'); |
| 185 | + $f= $t->annotation(Annotated::class)->argument(0); |
| 186 | + |
| 187 | + Assert::true((new ReflectionFunction($f))->getParameters()[0]->isVariadic()); |
| 188 | + } |
| 189 | + |
| 190 | + #[Test, Values(['function(&\$param) { }', 'fn(&\$param) => null'])] |
| 191 | + public function with_reference_param($function) { |
| 192 | + $t= $this->declare('{}', '#[Annotated(eval: "'.$function.'")]'); |
| 193 | + $f= $t->annotation(Annotated::class)->argument(0); |
| 194 | + |
| 195 | + Assert::true((new ReflectionFunction($f))->getParameters()[0]->isPassedByReference()); |
| 196 | + } |
| 197 | + |
| 198 | + #[Test, Values(['function(int \$param) { }', 'fn(int \$param) => null'])] |
| 199 | + public function with_param_type($function) { |
| 200 | + $t= $this->declare('{}', '#[Annotated(eval: "'.$function.'")]'); |
| 201 | + $f= $t->annotation(Annotated::class)->argument(0); |
| 202 | + |
| 203 | + $type= (new ReflectionFunction($f))->getParameters()[0]->getType(); |
| 204 | + Assert::equals('int', PHP_VERSION_ID >= 70100 ? $type->getName() : (string)$type); |
| 205 | + } |
| 206 | + |
| 207 | + #[Test, Values(['function(): int { return 6100; }', 'fn(): int => 6100'])] |
| 208 | + public function with_return_type($function) { |
| 209 | + $t= $this->declare('{}', '#[Annotated(eval: "'.$function.'")]'); |
| 210 | + $f= $t->annotation(Annotated::class)->argument(0); |
| 211 | + |
| 212 | + $type= (new ReflectionFunction($f))->getReturnType(); |
| 213 | + Assert::equals('int', PHP_VERSION_ID >= 70100 ? $type->getName() : (string)$type); |
| 214 | + } |
| 215 | + |
173 | 216 | #[Test] |
174 | 217 | public function multiple() { |
175 | 218 | $t= $this->declare('{}', '#[Annotated, Enumeration([])]'); |
|
0 commit comments