|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Thesis\Grpc; |
| 6 | + |
| 7 | +use Amp\Cancellation; |
| 8 | +use BcMath\Number; |
| 9 | +use Google\Protobuf\Timestamp; |
| 10 | +use Google\Rpc\Code; |
| 11 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | +use Thesis\Grpc\Server\ServerStreamHandler; |
| 14 | +use Topic\Api\V1\Event; |
| 15 | +use Topic\Api\V1\SubscribeRequest; |
| 16 | +use Topic\Api\V1\TopicServiceClient; |
| 17 | +use Topic\Api\V1\TopicServiceServer; |
| 18 | +use Topic\Api\V1\TopicServiceServerRegistry; |
| 19 | + |
| 20 | +/** |
| 21 | + * @api |
| 22 | + */ |
| 23 | +#[CoversClass(Server::class)] |
| 24 | +#[CoversClass(Client::class)] |
| 25 | +#[CoversClass(ServerStreamHandler::class)] |
| 26 | +final class ServerStreamTest extends TestCase |
| 27 | +{ |
| 28 | + private Server $server; |
| 29 | + |
| 30 | + protected function setUp(): void |
| 31 | + { |
| 32 | + parent::setUp(); |
| 33 | + |
| 34 | + $this->server = new Server\Builder() |
| 35 | + ->withServices(new TopicServiceServerRegistry(new TopicServer())) |
| 36 | + ->build(); |
| 37 | + |
| 38 | + $this->server->start(); |
| 39 | + } |
| 40 | + |
| 41 | + protected function tearDown(): void |
| 42 | + { |
| 43 | + parent::tearDown(); |
| 44 | + |
| 45 | + $this->server->stop(); |
| 46 | + } |
| 47 | + |
| 48 | + public function testEventReceived(): void |
| 49 | + { |
| 50 | + $client = new TopicServiceClient(new Client\Builder()->build()); |
| 51 | + |
| 52 | + $stream = $client->subscribe(new SubscribeRequest('payments')); |
| 53 | + |
| 54 | + $paymentEvents = []; |
| 55 | + foreach ($stream as $event) { |
| 56 | + $paymentEvents[] = $event; |
| 57 | + } |
| 58 | + |
| 59 | + self::assertCount(2, $paymentEvents); |
| 60 | + self::assertEquals( |
| 61 | + [ |
| 62 | + new Event('payment_finished', '{"id": 1}', new Timestamp(new Number(1_771_782_096))), |
| 63 | + new Event('payment_rejected', '{"id": 2}', new Timestamp(new Number(1_771_782_097))), |
| 64 | + ], |
| 65 | + $paymentEvents, |
| 66 | + ); |
| 67 | + |
| 68 | + $stream = $client->subscribe(new SubscribeRequest('subscriptions')); |
| 69 | + |
| 70 | + $subscriptionEvents = []; |
| 71 | + foreach ($stream as $event) { |
| 72 | + $subscriptionEvents[] = $event; |
| 73 | + } |
| 74 | + |
| 75 | + self::assertCount(1, $subscriptionEvents); |
| 76 | + self::assertEquals( |
| 77 | + [ |
| 78 | + new Event('subscription_terminated', '{"id": 1}', new Timestamp(new Number(1_771_782_099))), |
| 79 | + ], |
| 80 | + $subscriptionEvents, |
| 81 | + ); |
| 82 | + |
| 83 | + $this->expectExceptionObject(new InvokeError(Code::FAILED_PRECONDITION, 'Unknown topic "users"')); |
| 84 | + $stream = $client->subscribe(new SubscribeRequest('users')); |
| 85 | + iterator_to_array($stream); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +final readonly class TopicServer implements TopicServiceServer |
| 90 | +{ |
| 91 | + #[\Override] |
| 92 | + public function subscribe( |
| 93 | + SubscribeRequest $request, |
| 94 | + Server\ServerStreamChannel $stream, |
| 95 | + Metadata $md, |
| 96 | + Cancellation $cancellation, |
| 97 | + ): void { |
| 98 | + $events = [ |
| 99 | + 'payments' => [ |
| 100 | + new Event('payment_finished', '{"id": 1}', new Timestamp(new Number(1_771_782_096))), |
| 101 | + new Event('payment_rejected', '{"id": 2}', new Timestamp(new Number(1_771_782_097))), |
| 102 | + ], |
| 103 | + 'subscriptions' => [ |
| 104 | + new Event('subscription_terminated', '{"id": 1}', new Timestamp(new Number(1_771_782_099))), |
| 105 | + ], |
| 106 | + ]; |
| 107 | + |
| 108 | + foreach ($events[$request->topic] ?? throw new InvokeError(Code::FAILED_PRECONDITION, 'Unknown topic "' . $request->topic . '"') as $it) { |
| 109 | + $stream->send($it); |
| 110 | + } |
| 111 | + |
| 112 | + $stream->close(); |
| 113 | + } |
| 114 | +} |
0 commit comments