Skip to content

Commit 482d875

Browse files
committed
add tests for named arguments
1 parent 6783b19 commit 482d875

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Output is `What a nice way to use PHP`
2727
- A native-like `\Error` will be thrown when trying to call a non-existent method.
2828
- A `skrtdev\Prototypes\Exception` will be thrown if class method already exists, is a prototype, class does not exist or isn't Prototypeable (when using `skrtdev\Prototypes\Prototypes::addMethod()`).
2929
- Ability to use any kind of `callable`s, not just `Closure`s.
30+
- Ability to use [named arguments](https://www.php.net/manual/en/functions.arguments.php#functions.named-arguments) in Prototypes methods.
3031

3132
### Check if a Class is Prototypeable
3233

@@ -47,7 +48,6 @@ The `Prototypes` class itself is `Prototypeable`, how strange.
4748
### Known issues
4849

4950
- This library does not have `Inheritance`: you won't be able to use Prototypes methods defined for a class in his child classes. (this is going to be added soon)
50-
- You won't be able to use [named arguments](https://www.php.net/manual/en/functions.arguments.php#functions.named-arguments) in Prototypes methods, as `array unpacking` doesn't work with string keys [yet](https://wiki.php.net/rfc/array_unpacking_string_keys).
5151
- You can't override already-prototyped methods, but this will be added soon.
5252
- Any kind of `Error/Exception`(s) look a bit strange in the Stack traces, and method name is hidden. Maybe there is a solution; if you find it, feel free to open an `Issue/Pull Request`.
5353

tests/PrototypesTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,13 @@ public function testCanUseCallable(): void
4242
$this->assertNull(DemoClassTest::addMethod('unexistentMethod', 'file_get_contents'));
4343
}
4444

45+
public function testCanUseNamedArguments(): void
46+
{
47+
DemoClassTest::addMethod('methodWithNamedArguments', function (int $named_argument){
48+
return $named_argument;
49+
});
50+
$this->assertEquals(12, (new DemoClassTest)->methodWithNamedArguments(named_argument: 12));
51+
$this->assertEquals(12, (new DemoClassTest)->methodWithNamedArguments(...['named_argument' => 12]));
52+
}
53+
4554
}

tests/StaticPrototypesTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,13 @@ public function testCantOverrideMethods(): void
3737
DemoClassTest::addStaticMethod('existentStaticMethod', fn() => self::$static_property);
3838
}
3939

40+
public function testCanUseNamedArguments(): void
41+
{
42+
DemoClassTest::addStaticMethod('staticMethodWithNamedArguments', function (int $named_argument){
43+
return $named_argument;
44+
});
45+
$this->assertEquals(12, DemoClassTest::staticMethodWithNamedArguments(named_argument: 12));
46+
$this->assertEquals(12, DemoClassTest::staticMethodWithNamedArguments(...['named_argument' => 12]));
47+
}
48+
4049
}

0 commit comments

Comments
 (0)