Skip to content

feat(GPS): allow send attributes in Google PubSub message. #1352

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
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
15 changes: 15 additions & 0 deletions docs/transport/gps.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ $context->declareTopic($fooTopic);
$context->createProducer()->send($fooTopic, $message);
```

You can send attributes using headers :

```php
<?php
/** @var \Enqueue\Gps\GpsContext $context */

$fooTopic = $context->createTopic('foo');
$attributes = ['key1' => 'value1'];
$message = $context->createMessage('Hello world!', [], ['attributes' => $attributes]);

$context->declareTopic($fooTopic);

$context->createProducer()->send($fooTopic, $message);
```

## Consume message:

Before you can consume message you have to subscribe a queue to the topic.
Expand Down
22 changes: 17 additions & 5 deletions pkg/gps/GpsMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@ class GpsMessage implements Message, \JsonSerializable
*/
private $nativeMessage;

/**
* @var array
*/
private $attributes;

public function __construct(string $body = '', array $properties = [], array $headers = [])
{
$this->body = $body;
$this->properties = $properties;
$this->attributes = $headers['attributes'] ?? [];
unset($headers['attributes']);
$this->headers = $headers;

$this->redelivered = false;
Expand Down Expand Up @@ -103,7 +110,7 @@ public function isRedelivered(): bool
return $this->redelivered;
}

public function setCorrelationId(string $correlationId = null): void
public function setCorrelationId(?string $correlationId = null): void
{
$this->setHeader('correlation_id', $correlationId);
}
Expand All @@ -113,7 +120,7 @@ public function getCorrelationId(): ?string
return $this->getHeader('correlation_id');
}

public function setMessageId(string $messageId = null): void
public function setMessageId(?string $messageId = null): void
{
$this->setHeader('message_id', $messageId);
}
Expand All @@ -130,12 +137,12 @@ public function getTimestamp(): ?int
return null === $value ? null : (int) $value;
}

public function setTimestamp(int $timestamp = null): void
public function setTimestamp(?int $timestamp = null): void
{
$this->setHeader('timestamp', $timestamp);
}

public function setReplyTo(string $replyTo = null): void
public function setReplyTo(?string $replyTo = null): void
{
$this->setHeader('reply_to', $replyTo);
}
Expand Down Expand Up @@ -169,8 +176,13 @@ public function getNativeMessage(): ?GoogleMessage
return $this->nativeMessage;
}

public function setNativeMessage(GoogleMessage $message = null): void
public function setNativeMessage(?GoogleMessage $message = null): void
{
$this->nativeMessage = $message;
}

public function getAttributes(): array
{
return $this->attributes;
}
}
7 changes: 4 additions & 3 deletions pkg/gps/GpsProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ public function send(Destination $destination, Message $message): void
$topic = $this->context->getClient()->topic($destination->getTopicName());
$topic->publish([
'data' => json_encode($message),
'attributes' => $message->getAttributes(),
]);
}

public function setDeliveryDelay(int $deliveryDelay = null): Producer
public function setDeliveryDelay(?int $deliveryDelay = null): Producer
{
if (null === $deliveryDelay) {
return $this;
Expand All @@ -56,7 +57,7 @@ public function getDeliveryDelay(): ?int
return null;
}

public function setPriority(int $priority = null): Producer
public function setPriority(?int $priority = null): Producer
{
if (null === $priority) {
return $this;
Expand All @@ -70,7 +71,7 @@ public function getPriority(): ?int
return null;
}

public function setTimeToLive(int $timeToLive = null): Producer
public function setTimeToLive(?int $timeToLive = null): Producer
{
if (null === $timeToLive) {
return $this;
Expand Down
11 changes: 10 additions & 1 deletion pkg/gps/Tests/GpsMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testCouldBeUnserializedFromJson()

$json = json_encode($message);

//guard
// guard
$this->assertNotEmpty($json);

$unserializedMessage = GpsMessage::jsonUnserialize($json);
Expand Down Expand Up @@ -70,4 +70,13 @@ public function testThrowIfMalformedJsonGivenOnUnsterilizedFromJson()

GpsMessage::jsonUnserialize('{]');
}

public function testGetAttributes()
{
$message = new GpsMessage('the body', [], ['attributes' => ['key1' => 'value1']]);

$attributes = $message->getAttributes();

$this->assertSame(['key1' => 'value1'], $attributes);
}
}
31 changes: 31 additions & 0 deletions pkg/gps/Tests/GpsProducerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,37 @@ public function testShouldSendMessage()
$producer->send($topic, $message);
}

public function testShouldSendMessageWithAttributes()
{
$topic = new GpsTopic('topic-name');
$message = new GpsMessage('', [], ['attributes' => ['key1' => 'value1']]);

$gtopic = $this->createGTopicMock();
$gtopic
->expects($this->once())
->method('publish')
->with($this->identicalTo(['data' => '{"body":"","properties":[],"headers":[]}', 'attributes' => ['key1' => 'value1']]))
;

$client = $this->createPubSubClientMock();
$client
->expects($this->once())
->method('topic')
->with('topic-name')
->willReturn($gtopic)
;

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

$producer = new GpsProducer($context);
$producer->send($topic, $message);
}

/**
* @return GpsContext|\PHPUnit\Framework\MockObject\MockObject|GpsContext
*/
Expand Down
Loading