Skip to content

Commit

Permalink
add tests for named arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
skrtdev committed Jun 28, 2021
1 parent 6783b19 commit 482d875
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Output is `What a nice way to use PHP`
- A native-like `\Error` will be thrown when trying to call a non-existent method.
- 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()`).
- Ability to use any kind of `callable`s, not just `Closure`s.
- Ability to use [named arguments](https://www.php.net/manual/en/functions.arguments.php#functions.named-arguments) in Prototypes methods.

### Check if a Class is Prototypeable

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

- 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)
- 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).
- You can't override already-prototyped methods, but this will be added soon.
- 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`.

Expand Down
9 changes: 9 additions & 0 deletions tests/PrototypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@ public function testCanUseCallable(): void
$this->assertNull(DemoClassTest::addMethod('unexistentMethod', 'file_get_contents'));
}

public function testCanUseNamedArguments(): void
{
DemoClassTest::addMethod('methodWithNamedArguments', function (int $named_argument){
return $named_argument;
});
$this->assertEquals(12, (new DemoClassTest)->methodWithNamedArguments(named_argument: 12));
$this->assertEquals(12, (new DemoClassTest)->methodWithNamedArguments(...['named_argument' => 12]));
}

}
9 changes: 9 additions & 0 deletions tests/StaticPrototypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,13 @@ public function testCantOverrideMethods(): void
DemoClassTest::addStaticMethod('existentStaticMethod', fn() => self::$static_property);
}

public function testCanUseNamedArguments(): void
{
DemoClassTest::addStaticMethod('staticMethodWithNamedArguments', function (int $named_argument){
return $named_argument;
});
$this->assertEquals(12, DemoClassTest::staticMethodWithNamedArguments(named_argument: 12));
$this->assertEquals(12, DemoClassTest::staticMethodWithNamedArguments(...['named_argument' => 12]));
}

}

0 comments on commit 482d875

Please sign in to comment.