diff --git a/src/Invoker.php b/src/Invoker.php index ab2fbc0..d234dd2 100644 --- a/src/Invoker.php +++ b/src/Invoker.php @@ -17,13 +17,19 @@ final class Invoker implements InvokerInterface 'Method %s input type must be an instance of %s, ' . 'but the input is type of %s'; - public function invoke(ServiceInterface $service, Method $method, ContextInterface $ctx, ?string $input): string - { + public function invoke( + ServiceInterface $service, + Method $method, + ContextInterface $ctx, + string|Message|null $input, + ): string { /** @var callable $callable */ $callable = [$service, $method->name]; + $input = $input instanceof Message ? $input : $this->makeInput($method, $input); + /** @var Message $message */ - $message = $callable($ctx, $this->makeInput($method, $input)); + $message = $callable($ctx, $input); \assert($this->assertResultType($method, $message)); @@ -45,7 +51,7 @@ private function assertResultType(Method $method, mixed $result): bool $type = \get_debug_type($result); throw new \BadFunctionCallException( - \sprintf(self::ERROR_METHOD_RETURN, $method->name, Message::class, $type) + \sprintf(self::ERROR_METHOD_RETURN, $method->name, Message::class, $type), ); } @@ -86,7 +92,7 @@ private function assertInputType(Method $method, string $class): bool { if (!\is_subclass_of($class, Message::class)) { throw new \InvalidArgumentException( - \sprintf(self::ERROR_METHOD_IN_TYPE, $method->name, Message::class, $class) + \sprintf(self::ERROR_METHOD_IN_TYPE, $method->name, Message::class, $class), ); } diff --git a/tests/InvokerTest.php b/tests/InvokerTest.php index 429fee3..9e64f6a 100644 --- a/tests/InvokerTest.php +++ b/tests/InvokerTest.php @@ -28,6 +28,21 @@ public function testInvoke(): void $this->assertSame('pong', $m->getMsg()); } + public function testInvokeWithInputMessage(): void + { + $s = new TestService(); + $m = Method::parse(new \ReflectionMethod($s, 'Echo')); + + $i = new Invoker(); + + $out = $i->invoke($s, $m, new Context([]), $this->createMessage('hello')); + + $m = new Message(); + $m->mergeFromString($out); + + $this->assertSame('pong', $m->getMsg()); + } + public function testInvokeError(): void { $this->expectException(\Spiral\RoadRunner\GRPC\Exception\InvokeException::class); @@ -41,10 +56,15 @@ public function testInvokeError(): void } private function packMessage(string $message): string + { + return $this->createMessage($message)->serializeToString(); + } + + private function createMessage(string $message): Message { $m = new Message(); $m->setMsg($message); - return $m->serializeToString(); + return $m; } }