From d1cb4eee7cb7f196b276cf95ca38acfdfed9bd19 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Sat, 3 Aug 2024 01:23:06 +0400 Subject: [PATCH] Fix tests --- src/Console/Command/GRPC/GenerateCommand.php | 2 +- src/GRPC/ProtoCompiler.php | 4 +-- src/GRPC/ProtocCommandBuilder.php | 4 +-- .../Command/GRPC/GenerateCommandTest.php | 3 +- tests/src/GRPC/DispatcherTest.php | 6 ++-- tests/src/GRPC/Interceptor/InvokerTest.php | 35 ++++++++++--------- tests/src/GRPC/ProtocCommandBuilderTest.php | 2 +- 7 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/Console/Command/GRPC/GenerateCommand.php b/src/Console/Command/GRPC/GenerateCommand.php index 1b0bb9b..98d8a28 100644 --- a/src/Console/Command/GRPC/GenerateCommand.php +++ b/src/Console/Command/GRPC/GenerateCommand.php @@ -62,7 +62,7 @@ public function perform( $this->sprintf('Proto file `%s` not found.', $protoFile); continue; } - $protoFile = \realpath($protoFile); + $protoFile = \realpath($protoFile) ?: $protoFile; $this->sprintf("Compiling `%s`:\n", $protoFile); diff --git a/src/GRPC/ProtoCompiler.php b/src/GRPC/ProtoCompiler.php index 732b14e..9f56949 100644 --- a/src/GRPC/ProtoCompiler.php +++ b/src/GRPC/ProtoCompiler.php @@ -31,8 +31,8 @@ public function __construct( */ public function compile(string $protoFile): array { - $protoFile = \realpath($protoFile); - $tmpDir = \realpath($this->tmpDir()); + $protoFile = \realpath($protoFile) ?: $protoFile; + $tmpDir = \realpath($this->tmpDir()) ?: $this->tmpDir(); $output = $this->executor->execute( $this->commandBuilder->build(\dirname($protoFile), $tmpDir) diff --git a/src/GRPC/ProtocCommandBuilder.php b/src/GRPC/ProtocCommandBuilder.php index ff017d7..0307e7d 100644 --- a/src/GRPC/ProtocCommandBuilder.php +++ b/src/GRPC/ProtocCommandBuilder.php @@ -40,7 +40,7 @@ private function getProtoFiles(string $protoDir): array $this->files->getFiles($protoDir), static fn (string $file) => \str_ends_with($file, '.proto') ); - return \array_map(\realpath(...), $filtered); + return \array_map(static fn(string $path): string => \realpath($path) ?: $path, $filtered); } private function buildDirs(string $protoDir): string @@ -56,7 +56,7 @@ private function buildDirs(string $protoDir): string } return ' -I=' . \implode(' -I=', \array_map( - static fn(string$dir): string => \escapeshellarg(\realpath($dir)), + static fn(string$dir): string => \escapeshellarg(\realpath($dir) ?: $dir), $dirs, )); } diff --git a/tests/src/Console/Command/GRPC/GenerateCommandTest.php b/tests/src/Console/Command/GRPC/GenerateCommandTest.php index a15fdac..c5dd74c 100644 --- a/tests/src/Console/Command/GRPC/GenerateCommandTest.php +++ b/tests/src/Console/Command/GRPC/GenerateCommandTest.php @@ -19,8 +19,9 @@ public function testGenerateServices() 'GRPC/Ping/PingResponse.php', ]; + $path = $this->getDirectoryByAlias('app') . 'proto/service.proto'; $this->assertStringContainsString( - \sprintf('Compiling `%s`:', \realpath($this->getDirectoryByAlias('app') . 'proto/service.proto')), + \sprintf('Compiling `%s`:', \realpath($path) ?: $path), $result ); diff --git a/tests/src/GRPC/DispatcherTest.php b/tests/src/GRPC/DispatcherTest.php index fcf4daa..a1631d3 100644 --- a/tests/src/GRPC/DispatcherTest.php +++ b/tests/src/GRPC/DispatcherTest.php @@ -4,7 +4,7 @@ namespace Spiral\Tests\GRPC; -use Spiral\App\GRPC\EchoService\Message; +use Spiral\App\GRPC\Ping\PingResponse; use Spiral\Boot\FinalizerInterface; use Spiral\RoadRunner\Payload; use Spiral\RoadRunner\Worker; @@ -42,13 +42,13 @@ public function testServe(): void $worker->shouldReceive('waitPayload')->once()->andReturn( new Payload( - (new Message())->setMsg('PING')->serializeToString(), + (new PingResponse())->serializeToString(), json_encode(['service' => 'service.Echo', 'method' => 'Ping', 'context' => []]) ) ); $worker->shouldReceive('respond')->once()->withArgs(function (Payload $payload) { - $this->assertSame($payload->body, (new Message())->setMsg('PONG')->serializeToString()); + $this->assertSame($payload->body, (new PingResponse())->serializeToString()); return true; }); diff --git a/tests/src/GRPC/Interceptor/InvokerTest.php b/tests/src/GRPC/Interceptor/InvokerTest.php index fe22b36..d550e3d 100644 --- a/tests/src/GRPC/Interceptor/InvokerTest.php +++ b/tests/src/GRPC/Interceptor/InvokerTest.php @@ -6,8 +6,10 @@ use Mockery as m; use Service\PingService; -use Spiral\Core\CoreInterface; -use Service\Message; +use Spiral\App\GRPC\Ping\PingRequest; +use Spiral\App\GRPC\Ping\PingResponse; +use Spiral\Interceptors\Context\CallContextInterface; +use Spiral\Interceptors\HandlerInterface; use Spiral\RoadRunner\GRPC\ContextInterface; use Spiral\RoadRunner\GRPC\Exception\InvokeException; use Spiral\RoadRunner\GRPC\Method; @@ -19,30 +21,31 @@ final class InvokerTest extends TestCase { public function testInvoke(): void { - $invoker = new Invoker($core = m::mock(CoreInterface::class), $this->getContainer()); + $invoker = new Invoker($core = m::mock(HandlerInterface::class), $this->getContainer()); - $service = m::mock(ServiceInterface::class); - $method = Method::parse(new \ReflectionMethod(PingService::class, 'Ping')); + $service = m::mock(\Spiral\App\GRPC\Ping\PingServiceInterface::class); + $method = Method::parse(new \ReflectionMethod(\Spiral\App\GRPC\Ping\PingService::class, 'Ping')); - $input = (new Message(['msg' => 'hello']))->serializeToString(); - $output = (new Message(['msg' => 'world']))->serializeToString(); + $input = new PingRequest(); + $output = new PingResponse(); $ctx = m::mock(ContextInterface::class); $core - ->shouldReceive('callAction') + ->shouldReceive('handle') ->once() - ->withArgs(function (string $class, string $method, array $params) use ($service, $input) { - $this->assertSame($class, $service::class); - $this->assertSame('Ping', $method); - $this->assertInstanceOf(ContextInterface::class, $params['ctx']); - $this->assertSame($input, $params['input']); - $this->assertInstanceOf(Message::class, $params['message']); - $this->assertSame('hello', $params['message']->getMsg()); + ->withArgs(function (CallContextInterface $context) use ($service, $input) { + $this->assertSame($context->getTarget()->getPath()[0], $service::class); + $this->assertSame('Ping', $context->getTarget()->getPath()[1]); + $this->assertInstanceOf(ContextInterface::class, $context->getArguments()[0]); + $this->assertInstanceOf(PingRequest::class, $context->getArguments()[1]); return true; })->andReturn($output); - $this->assertSame($output, $invoker->invoke($service, $method, $ctx, $input)); + $this->assertSame( + $output->serializeToString(), + $invoker->invoke($service, $method, $ctx, $input->serializeToString()), + ); } public function testInvokeWithBrokenText(): void diff --git a/tests/src/GRPC/ProtocCommandBuilderTest.php b/tests/src/GRPC/ProtocCommandBuilderTest.php index 3b0ef2a..fe67f64 100644 --- a/tests/src/GRPC/ProtocCommandBuilderTest.php +++ b/tests/src/GRPC/ProtocCommandBuilderTest.php @@ -40,7 +40,7 @@ public function testBuild(): void $this->assertSame( "protoc --plugin=path3 --php_out='path2' --php-grpc_out='path2' -I='path1' -I='path4' 'message.proto' 'service.proto' 2>&1", - $builder->build('path1', 'path2') + \str_replace('"', "'", $builder->build('path1', 'path2')), ); }