Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes tests for TestService #3

Merged
merged 3 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
"spiral/roadrunner": "^2.0"
},
"require-dev": {
"jetbrains/phpstorm-attributes": "^1.0",
"mockery/mockery": "^1.4",
"phpunit/phpunit": "^9.5",
"spiral/code-style": "^1.0",
"jetbrains/phpstorm-attributes": "^1.0",
"symfony/var-dumper": ">=4.4",
"vimeo/psalm": "^4.6"
},
Expand Down
1 change: 0 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
errorLevel="1"
hoistConstants="true"
resolveFromConfigFile="true"
allowPhpStormGenerics="true"
findUnusedPsalmSuppress="true"
findUnusedVariablesAndParams="true"
ensureArrayStringOffsetsExist="true"
Expand Down
2 changes: 1 addition & 1 deletion tests/InvokerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testInvoke(): void
$m = new Message();
$m->mergeFromString($out);

$this->assertSame('hello', $m->getMsg());
$this->assertSame('pong', $m->getMsg());
}

public function testInvokeError(): void
Expand Down
147 changes: 90 additions & 57 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,101 +11,108 @@

namespace Spiral\RoadRunner\GRPC\Tests;

use Mockery as m;
use PHPUnit\Framework\TestCase;
use Service\Message;
use Service\TestInterface;
use Spiral\Goridge\Frame;
use Spiral\Goridge\RelayInterface;
use Spiral\GRPC\Server;
use Spiral\RoadRunner\GRPC\Tests\Stub\TestWorker;
use Spiral\RoadRunner\GRPC\Tests\Stub\TestService;
use Spiral\RoadRunner\Worker;

class ServerTest extends TestCase
{
public function testInvoke(): void
use m\Adapter\Phpunit\MockeryPHPUnitIntegration;

private Server $server;

protected function setUp(): void
{
$s = new Server();
$s->registerService(TestInterface::class, new TestService());
parent::setUp();

$this->server = new Server();
$this->server->registerService(TestInterface::class, new TestService());
}

$w = new TestWorker($this, [
public function testInvoke(): void
{
$relay = $this->createRelay(
'ping',
[
'ctx' => [
'service' => 'service.Test',
'method' => 'Echo',
'context' => [],
],
'send' => $this->packMessage('hello world'),
'receive' => $this->packMessage('hello world')
'service' => 'service.Test',
'method' => 'Echo',
'context' => [],
]
]);
);

$s->serve($w);
$relay->shouldReceive('send')->once()->withArgs(function (Frame $frame) {
return $frame->payload === '{}'.$this->packMessage('pong');
});

$this->assertTrue($w->done());
$this->server->serve(
new Worker($relay)
);
}

public function testNotFound(): void
{
$s = new Server();
$s->registerService(TestInterface::class, new TestService());

$w = new TestWorker($this, [
$relay = $this->createRelay(
'ping',
[
'ctx' => [
'service' => 'service.Test2',
'method' => 'Echo',
'context' => [],
],
'send' => $this->packMessage('hello world'),
'error' => '5|:|Service `service.Test2` not found.'
'service' => 'service.Test2',
'method' => 'Echo',
'context' => [],
]
]);
);

$s->serve($w);
$relay->shouldReceive('send')->once()->withArgs(function (Frame $frame) {
return $frame->payload === '5|:|Service `service.Test2` not found.';
});

$this->assertTrue($w->done());
$this->server->serve(
new Worker($relay)
);
}

public function testNotFound2(): void
{
$s = new Server();
$s->registerService(TestInterface::class, new TestService());

$w = new TestWorker($this, [
$relay = $this->createRelay(
'ping',
[
'ctx' => [
'service' => 'service.Test',
'method' => 'Echo2',
'context' => [],
],
'send' => $this->packMessage('hello world'),
'error' => '5|:|Method `Echo2` not found in service `service.Test`.'
'service' => 'service.Test',
'method' => 'Echo2',
'context' => [],
]
]);
);

$s->serve($w);
$relay->shouldReceive('send')->once()->withArgs(function (Frame $frame) {
return $frame->payload === '5|:|Method `Echo2` not found in service `service.Test`.';
});

$this->assertTrue($w->done());
$this->server->serve(
new Worker($relay)
);
}

public function testServerDebugModeNotEnabled(): void
{
$s = new Server();
$s->registerService(TestInterface::class, new TestService());

$w = new TestWorker($this, [
$relay = $this->createRelay(
'regularException',
[
'ctx' => [
'service' => 'service.Test',
'method' => 'Throw',
'context' => [],
],
'send' => $this->packMessage('regularException'),
'error' => 'Just another exception'
'service' => 'service.Test',
'method' => 'Throw',
'context' => [],
]
]);
);

$s->serve($w);
$relay->shouldReceive('send')->once()->withArgs(function (Frame $frame) {
return $frame->payload === 'Just another exception';
});

$this->assertTrue($w->done());
$this->server->serve(
new Worker($relay)
);
}

private function packMessage(string $message): string
Expand All @@ -115,4 +122,30 @@ private function packMessage(string $message): string

return $m->serializeToString();
}

protected function tearDown(): void
{
parent::tearDown();
ob_end_clean();

m::close();
}

protected function createRelay(string $body, array $header): RelayInterface
{
$body = $this->packMessage($body);
$header = json_encode($header);

$relay = m::mock(RelayInterface::class);
$relay->shouldReceive('waitFrame')->once()->andReturn(
new Frame($header.$body, [mb_strlen($header)])
);

$header = json_encode(['stop' => true]);
$relay->shouldReceive('waitFrame')->once()->andReturn(
new Frame($header, [mb_strlen($header)], Frame::CONTROL)
);

return $relay;
}
}
2 changes: 1 addition & 1 deletion tests/ServiceWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function testInvoke(): void
$m = new Message();
$m->mergeFromString($out);

$this->assertSame('hello world', $m->getMsg());
$this->assertSame('pong', $m->getMsg());
}

public function testNotImplemented(): void
Expand Down
5 changes: 3 additions & 2 deletions tests/Stub/TestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TestService implements TestInterface
{
public function Echo(ContextInterface $ctx, Message $in): Message
{
return $in;
return $in->setMsg('pong');
}

public function Throw(ContextInterface $ctx, Message $in): Message
Expand All @@ -33,7 +33,8 @@ public function Throw(ContextInterface $ctx, Message $in): Message
$grpcException = new GRPCException("main exception message", 3, [$detailsMessage]);

throw $grpcException;
case "regularException": {
case "regularException":
{
throw new \Exception("Just another exception");
}
}
Expand Down
78 changes: 0 additions & 78 deletions tests/Stub/TestWorker.php

This file was deleted.