Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Aug 2, 2024
1 parent 8acf38e commit d1cb4ee
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Console/Command/GRPC/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function perform(
$this->sprintf('<error>Proto file `%s` not found.</error>', $protoFile);
continue;
}
$protoFile = \realpath($protoFile);
$protoFile = \realpath($protoFile) ?: $protoFile;

$this->sprintf("<info>Compiling <fg=cyan>`%s`</fg=cyan>:</info>\n", $protoFile);

Expand Down
4 changes: 2 additions & 2 deletions src/GRPC/ProtoCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/GRPC/ProtocCommandBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
));
}
Expand Down
3 changes: 2 additions & 1 deletion tests/src/Console/Command/GRPC/GenerateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand Down
6 changes: 3 additions & 3 deletions tests/src/GRPC/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
});

Expand Down
35 changes: 19 additions & 16 deletions tests/src/GRPC/Interceptor/InvokerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/src/GRPC/ProtocCommandBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
);
}

Expand Down

0 comments on commit d1cb4ee

Please sign in to comment.