From 6bd8682219080bff99f82bcfb0529e5df9b42c63 Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Fri, 19 May 2023 20:44:03 +0200 Subject: [PATCH 1/3] test: Add test for additional provided parameters (#5) --- tests/DispatcherTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index 8b54d06..49699c0 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -72,5 +72,17 @@ public function testCallMethodWithTypeHintWithNamedArgsOnNestedTarget() $this->assertEquals($this->callsOfNestedTarget, [new MethodCall('someMethodWithTypeHint', [new Argument('whatever')])]); } + public function testCallMethodWithAdditionalProvidedParamsOnSomeMethodWithoutArgs() + { + $result = $this->dispatcher->dispatch((string) new Request(1, 'someMethodWithoutArgs', ['arg' => new Argument('whatever')])); + $this->assertEquals('Hello World', $result); + $this->assertEquals($this->calls, [new MethodCall('someMethodWithoutArgs', [])]); + } + public function testCallMethodWithAdditionalProvidedParamsOnSomeMethodWithTypeHint() + { + $result = $this->dispatcher->dispatch((string) new Request(1, 'someMethodWithTypeHint', ['arg' => new Argument('whatever'), 'arg2' => new Argument('anything')])); + $this->assertEquals('Hello World', $result); + $this->assertEquals($this->calls, [new MethodCall('someMethodWithTypeHint', [new Argument('whatever')])]); + } } From b39a137774da917be45b4f9b79b9db9c33dab67b Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Fri, 19 May 2023 20:45:46 +0200 Subject: [PATCH 2/3] test: Add test to proof docblock is being used when type hint is array (#6) --- lib/Dispatcher.php | 2 +- tests/DispatcherTest.php | 8 +++++++- tests/Target.php | 9 +++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/Dispatcher.php b/lib/Dispatcher.php index 5f045df..cb2116a 100644 --- a/lib/Dispatcher.php +++ b/lib/Dispatcher.php @@ -152,7 +152,7 @@ public function dispatch($msg) } } } else if ($type instanceof Types\Array_) { - $class = (string)$type->getValueType()->getFqsen(); + $class = (string) $type->getValueType()->getFqsen(); $value = $this->mapper->mapArray($value, [], $class); } else { throw new Error('Type is not matching @param tag', ErrorCode::INVALID_PARAMS); diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index 49699c0..1b2eaa4 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -63,7 +63,6 @@ public function testCallMethodWithUnionTypeParamTag() $this->assertEquals('Hello World', $result); $this->assertEquals($this->calls, [new MethodCall('someMethodWithUnionTypeParamTag', [[new Argument('whatever')]])]); } - public function testCallMethodWithTypeHintWithNamedArgsOnNestedTarget() { $result = $this->dispatcher->dispatch((string)new Request(1, 'nestedTarget->someMethodWithTypeHint', ['arg' => new Argument('whatever')])); @@ -72,6 +71,13 @@ public function testCallMethodWithTypeHintWithNamedArgsOnNestedTarget() $this->assertEquals($this->callsOfNestedTarget, [new MethodCall('someMethodWithTypeHint', [new Argument('whatever')])]); } + public function testCallMethodWithArrayTypeHintAndDocblock(): void + { + $result = $this->dispatcher->dispatch((string)new Request(1, 'someMethodWithArrayTypeHint', ['args' => [new Argument('1'), new Argument('2')]])); + $this->assertEquals('Hello World', $result); + $this->assertEquals($this->calls, [new MethodCall('someMethodWithArrayTypeHint', [[new Argument('1'), new Argument('2')]])]); + } + public function testCallMethodWithAdditionalProvidedParamsOnSomeMethodWithoutArgs() { $result = $this->dispatcher->dispatch((string) new Request(1, 'someMethodWithoutArgs', ['arg' => new Argument('whatever')])); diff --git a/tests/Target.php b/tests/Target.php index 84ece52..e9651ce 100644 --- a/tests/Target.php +++ b/tests/Target.php @@ -49,4 +49,13 @@ public function someMethodWithDifferentlyTypedArgs(string $arg1 = null, int $arg $this->calls[] = new MethodCall('someMethodWithDifferentlyTypedArgs', func_get_args()); return 'Hello World'; } + + /** + * @param Argument[] $args + */ + public function someMethodWithArrayTypeHint(array $args): string + { + $this->calls[] = new MethodCall('someMethodWithArrayTypeHint', func_get_args()); + return 'Hello World'; + } } From 80b949f20eb62f6e37991877c2f6ab9fcb70a874 Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Mon, 11 Jan 2021 21:44:12 +0100 Subject: [PATCH 3/3] test: Proof @param ?string is supported in current setup --- tests/DispatcherTest.php | 9 ++++++++- tests/Target.php | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index 1b2eaa4..f98ba5b 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -77,7 +77,7 @@ public function testCallMethodWithArrayTypeHintAndDocblock(): void $this->assertEquals('Hello World', $result); $this->assertEquals($this->calls, [new MethodCall('someMethodWithArrayTypeHint', [[new Argument('1'), new Argument('2')]])]); } - + public function testCallMethodWithAdditionalProvidedParamsOnSomeMethodWithoutArgs() { $result = $this->dispatcher->dispatch((string) new Request(1, 'someMethodWithoutArgs', ['arg' => new Argument('whatever')])); @@ -91,4 +91,11 @@ public function testCallMethodWithAdditionalProvidedParamsOnSomeMethodWithTypeHi $this->assertEquals('Hello World', $result); $this->assertEquals($this->calls, [new MethodCall('someMethodWithTypeHint', [new Argument('whatever')])]); } + + public function testSomeMethodWithNullableTypeParamTag(): void + { + $result = $this->dispatcher->dispatch((string)new Request(1, 'someMethodWithNullableTypeParamTag', ['arg' => null])); + $this->assertEquals('Hello World', $result); + $this->assertEquals($this->calls, [new MethodCall('someMethodWithNullableTypeParamTag', [null])]); + } } diff --git a/tests/Target.php b/tests/Target.php index e9651ce..fd7afd9 100644 --- a/tests/Target.php +++ b/tests/Target.php @@ -58,4 +58,13 @@ public function someMethodWithArrayTypeHint(array $args): string $this->calls[] = new MethodCall('someMethodWithArrayTypeHint', func_get_args()); return 'Hello World'; } + + /** + * @param ?string $arg + */ + public function someMethodWithNullableTypeParamTag($arg): string + { + $this->calls[] = new MethodCall('someMethodWithNullableTypeParamTag', func_get_args()); + return 'Hello World'; + } }