Skip to content

adding protobuf support #1

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

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# the different stages of this Dockerfile are meant to be built into separate images
# https://docs.docker.com/develop/develop-images/multistage-build/#stop-at-a-specific-build-stage
# https://docs.docker.com/compose/compose-file/#target


# https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact
ARG PHP_VERSION=7.3
ARG CADDY_VERSION=2.5.1
# ARG GITHUB_TOKEN

# "php" stage
FROM php:${PHP_VERSION}-fpm-alpine AS api_platform_php

COPY --from=composer:2.5 /usr/bin/composer /usr/bin/composer

WORKDIR /app

COPY . .
2 changes: 1 addition & 1 deletion GpsConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private function getSubscription(): Subscription

private function convertMessage(GoogleMessage $message): GpsMessage
{
$gpsMessage = GpsMessage::jsonUnserialize($message->data());
$gpsMessage = GpsMessage::messageUnserialize($message->data(), $message->attributes());
$gpsMessage->setNativeMessage($message);

return $gpsMessage;
Expand Down
19 changes: 17 additions & 2 deletions GpsMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,30 @@ public function jsonSerialize(): array
];
}

public static function jsonUnserialize(string $json): self
public static function messageUnserialize(string $message, array $attributes) : self
{
if($attributes['ce-datacontenttype'] === 'application/protobuf') {
return self::protobufUnserialize($message, $attributes);
}

return self::jsonUnserialize($message, $attributes);
}

private static function jsonUnserialize(string $json, array $attributes): self
{
$data = json_decode($json, true);
if (\JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException(sprintf('The malformed json given. Error %s and message %s', json_last_error(), json_last_error_msg()));
}

return new self($data['body'] ?? $json, $data['properties'] ?? [], $data['headers'] ?? []);
return new self($data['body'] ?? $json, $data['properties'] ?? $attributes, $data['headers'] ?? []);
}

private static function protobufUnserialize(string $protobuf, array $attributes): self
{
return new self($protobuf, $data['properties'] ?? $attributes, $data['headers'] ?? []);
}


public function getNativeMessage(): ?GoogleMessage
{
Expand Down
58 changes: 58 additions & 0 deletions Tests/Fixtures/Test/AddTest.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions Tests/Fixtures/Test/Metadata/Test.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Tests/Fixtures/TestMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Enqueue\Gps\Tests\Fixtures;

class TestMessage extends \Google\Protobuf\Internal\Message {

}
45 changes: 45 additions & 0 deletions Tests/GpsConsumerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Enqueue\Gps\Tests;

use Enqueue\Gps\Tests\Fixtures\TestMessage;
use Enqueue\Gps\GpsConsumer;
use Enqueue\Gps\GpsContext;
use Enqueue\Gps\GpsMessage;
use Enqueue\Gps\GpsQueue;
use Enqueue\Gps\Tests\Test\AddTest;
use Google\Cloud\PubSub\Message;
use Google\Cloud\PubSub\PubSubClient;
use Google\Cloud\PubSub\Subscription;
Expand Down Expand Up @@ -179,6 +181,49 @@ public function testShouldReceiveMessage()
$this->assertSame('the body', $message->getBody());
}

public function testShouldReceiveMessagProtobuf()
{
$body = '[email protected]"[email protected]*&App\Tests\Entity\Entity497709';
$attributes = [
'ce-datacontenttype' => 'application/protobuf',
];

$nativeMessage = new Message([
'data' => $body,
'attributes' => $attributes,
], []);

$subscription = $this->createSubscriptionMock();
$subscription
->expects($this->once())
->method('pull')
->with($this->identicalTo([
'maxMessages' => 1,
'requestTimeout' => 12.345,
]))
->willReturn([$nativeMessage]);

$client = $this->createPubSubClientMock();
$client
->expects($this->once())
->method('subscription')
->willReturn($subscription);

$context = $this->createContextMock();
$context
->expects($this->once())
->method('getClient')
->willReturn($client);

$consumer = new GpsConsumer($context, new GpsQueue('queue-name'));

$message = $consumer->receive(12345);

$this->assertInstanceOf(GpsMessage::class, $message);
$this->assertSame($body, $message->getBody());
$this->assertSame($attributes, $message->getProperties());
}

/**
* @return \PHPUnit\Framework\MockObject\MockObject|GpsContext
*/
Expand Down
27 changes: 23 additions & 4 deletions Tests/GpsMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testCouldBeUnserializedFromJson()
//guard
$this->assertNotEmpty($json);

$unserializedMessage = GpsMessage::jsonUnserialize($json);
$unserializedMessage = GpsMessage::messageUnserialize($json, []);

$this->assertInstanceOf(GpsMessage::class, $unserializedMessage);
$this->assertEquals($message, $unserializedMessage);
Expand All @@ -42,7 +42,7 @@ public function testMessageEntityCouldBeUnserializedFromJson()
{
$json = '{"body":"theBody","properties":{"thePropFoo":"thePropFooVal"},"headers":{"theHeaderFoo":"theHeaderFooVal"}}';

$unserializedMessage = GpsMessage::jsonUnserialize($json);
$unserializedMessage = GpsMessage::messageUnserialize($json, []);

$this->assertInstanceOf(GpsMessage::class, $unserializedMessage);
$decoded = json_decode($json, true);
Expand All @@ -55,7 +55,7 @@ public function testMessagePayloadCouldBeUnserializedFromJson()
{
$json = '{"theBodyPropFoo":"theBodyPropVal"}';

$unserializedMessage = GpsMessage::jsonUnserialize($json);
$unserializedMessage = GpsMessage::messageUnserialize($json, []);

$this->assertInstanceOf(GpsMessage::class, $unserializedMessage);
$this->assertEquals($json, $unserializedMessage->getBody());
Expand All @@ -68,6 +68,25 @@ public function testThrowIfMalformedJsonGivenOnUnsterilizedFromJson()
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The malformed json given.');

GpsMessage::jsonUnserialize('{]');
GpsMessage::messageUnserialize('{]', []);
}

public function testCouldBeUnserializedFromProtobuf()
{
$protobufMessage = '[email protected]"[email protected]*&App\Tests\Entity\Entity497709';

$message = new GpsMessage(
$protobufMessage,
[
'ce-datacontenttype' => 'application/protobuf',
]
);

$unserializedMessage = GpsMessage::messageUnserialize($protobufMessage, [
'ce-datacontenttype' => 'application/protobuf',
]);

$this->assertInstanceOf(GpsMessage::class, $unserializedMessage);
$this->assertEquals($message, $unserializedMessage);
}
}
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
"homepage": "https://enqueue.forma-pro.com/",
"license": "MIT",
"require": {
"php": "^7.4|^8.0",
"php": "^7.3|^7.4|^8.0",
"queue-interop/queue-interop": "^0.8",
"google/cloud-pubsub": "^1.4.3",
"enqueue/dsn": "^0.10"
"enqueue/dsn": "^0.10",
"google/protobuf": "^3.25",
"grpc/grpc": "^1.57"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
Expand Down
16 changes: 16 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: "3.4"

services:
php:
build:
context: .
dockerfile: Dockerfile
restart: unless-stopped
volumes:
- .:/app
command: sh -c "composer install && php-fpm"
healthcheck:
interval: 10s
timeout: 3s
retries: 3
start_period: 30s