Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy-j committed Oct 20, 2018
1 parent c7dbd54 commit 7e24dc8
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 16 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -14,7 +14,8 @@
"spiral/roadrunner": "^1.2"
},
"require-dev": {
"phpunit/phpunit": "~7.0"
"phpunit/phpunit": "~7.0",
"mockery/mockery": "^1.1"
},
"autoload": {
"psr-4": {
Expand Down
15 changes: 8 additions & 7 deletions src/Server.php
Expand Up @@ -58,13 +58,14 @@ public function serve(Worker $worker)
while ($body = $worker->receive($ctx)) {
try {
$ctx = json_decode($ctx, true);
$worker->send(
$this->invoke(
$ctx['service'],
$ctx['method'],
$ctx['context'] ?? [],
$body
));
$resp = $this->invoke(
$ctx['service'],
$ctx['method'],
$ctx['context'] ?? [],
$body
);

$worker->send($resp);
} catch (GRPCException $e) {
$worker->error($this->packError($e));
} catch (\Throwable $e) {
Expand Down
8 changes: 0 additions & 8 deletions src/ServiceWrapper.php
Expand Up @@ -69,14 +69,6 @@ public function getMethods(): array
return array_values($this->methods);
}

/**
* @return array
*/
public function getMethods(): array
{
return array_values($this->methods);
}

/**
* @param string $method
* @param ContextInterface $context
Expand Down
134 changes: 134 additions & 0 deletions tests/GRPC/ServerTest.php
@@ -0,0 +1,134 @@
<?php
/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

namespace Spiral\GRPC\Tests;

use PHPUnit\Framework\TestCase;
use Service\Message;
use Service\TestInterface;
use Spiral\GRPC\Server;
use Spiral\RoadRunner\Worker;
use Test\TestService;

class ServerTest extends TestCase
{
public function testInvoke()
{
$s = new Server();
$s->registerService(TestInterface::class, new TestService());

$w = new TestWorker($this, [
[
'ctx' => [
'service' => 'service.Test',
'method' => 'Echo',
'context' => [],
],
'send' => $this->packMessage('hello world'),
'receive' => $this->packMessage('hello world')
]
]);

$s->serve($w);

$this->assertTrue($w->done());
}

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

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

$s->serve($w);

$this->assertTrue($w->done());
}

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

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

$s->serve($w);

$this->assertTrue($w->done());
}

private function packMessage(string $message): string
{
$m = new Message();
$m->setMsg($message);

return $m->serializeToString();
}
}

class TestWorker extends Worker
{
private $t;
private $sequence = [];
private $pos = 0;

public function __construct(TestCase $t, array $sequence)
{
$this->t = $t;
$this->sequence = $sequence;
}

public function done()
{
return $this->pos == count($this->sequence);
}

public function receive(&$header)
{
if (!isset($this->sequence[$this->pos])) {
return null;
}

$header = json_encode($this->sequence[$this->pos]['ctx']);

return $this->sequence[$this->pos]['send'];
}

public function send(string $payload = null, string $header = null)
{
$this->t->assertSame($this->sequence[$this->pos]['receive'], $payload);
$this->pos++;
}

public function error(string $message)
{
$this->t->assertSame($this->sequence[$this->pos]['error'], $message);
$this->pos++;
}
}

0 comments on commit 7e24dc8

Please sign in to comment.