Skip to content

Commit

Permalink
GPS: revert the attributes and use the headers instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
p-pichet committed Aug 13, 2024
1 parent 2d41b6e commit c8c4993
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 40 deletions.
21 changes: 2 additions & 19 deletions pkg/gps/GpsMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ class GpsMessage implements Message, \JsonSerializable
*/
private $headers;

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

/**
* @var bool
*/
Expand All @@ -39,12 +34,11 @@ class GpsMessage implements Message, \JsonSerializable
*/
private $nativeMessage;

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

$this->redelivered = false;
}
Expand Down Expand Up @@ -157,7 +151,6 @@ public function jsonSerialize(): array
'body' => $this->getBody(),
'properties' => $this->getProperties(),
'headers' => $this->getHeaders(),
'attributes' => $this->getAttributes(),
];
}

Expand All @@ -168,7 +161,7 @@ public static function jsonUnserialize(string $json): self
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'] ?? [], $data['attributes'] ?? []);
return new self($data['body'] ?? $json, $data['properties'] ?? [], $data['headers'] ?? []);
}

public function getNativeMessage(): ?GoogleMessage
Expand All @@ -180,14 +173,4 @@ public function setNativeMessage(?GoogleMessage $message = null): void
{
$this->nativeMessage = $message;
}

public function setAttributes(array $attributes): void
{
$this->attributes = $attributes;
}

public function getAttributes(): array
{
return $this->attributes;
}
}
4 changes: 2 additions & 2 deletions pkg/gps/GpsProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public function send(Destination $destination, Message $message): void

$params = ['data' => json_encode($message)];

if (count($message->getAttributes()) > 0) {
$params['attributes'] = $message->getAttributes();
if (count($message->getHeaders()) > 0) {
$params['attributes'] = $message->getHeaders();
}

$topic->publish($params);
Expand Down
19 changes: 4 additions & 15 deletions pkg/gps/Tests/GpsMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public function testCouldSetGetNativeMessage()

public function testColdBeSerializedToJson()
{
$message = new GpsMessage('theBody', ['thePropFoo' => 'thePropFooVal'], ['theHeaderFoo' => 'theHeaderFooVal'], ['theAttributeFoo' => 'theAttributeFooVal']);
$message = new GpsMessage('theBody', ['thePropFoo' => 'thePropFooVal'], ['theHeaderFoo' => 'theHeaderFooVal']);

$this->assertEquals('{"body":"theBody","properties":{"thePropFoo":"thePropFooVal"},"headers":{"theHeaderFoo":"theHeaderFooVal"},"attributes":{"theAttributeFoo":"theAttributeFooVal"}}', json_encode($message));
$this->assertEquals('{"body":"theBody","properties":{"thePropFoo":"thePropFooVal"},"headers":{"theHeaderFoo":"theHeaderFooVal"}}', json_encode($message));
}

public function testCouldBeUnserializedFromJson()
{
$message = new GpsMessage('theBody', ['thePropFoo' => 'thePropFooVal'], ['theHeaderFoo' => 'theHeaderFooVal'], ['theAttributeFoo' => 'theAttributeFooVal']);
$message = new GpsMessage('theBody', ['thePropFoo' => 'thePropFooVal'], ['theHeaderFoo' => 'theHeaderFooVal']);

$json = json_encode($message);

Expand All @@ -40,7 +40,7 @@ public function testCouldBeUnserializedFromJson()

public function testMessageEntityCouldBeUnserializedFromJson()
{
$json = '{"body":"theBody","properties":{"thePropFoo":"thePropFooVal"},"headers":{"theHeaderFoo":"theHeaderFooVal"},"attributes":{"theAttributeFoo":"theAttributeFooVal"}}';
$json = '{"body":"theBody","properties":{"thePropFoo":"thePropFooVal"},"headers":{"theHeaderFoo":"theHeaderFooVal"}}';

$unserializedMessage = GpsMessage::jsonUnserialize($json);

Expand All @@ -49,7 +49,6 @@ public function testMessageEntityCouldBeUnserializedFromJson()
$this->assertEquals($decoded['body'], $unserializedMessage->getBody());
$this->assertEquals($decoded['properties'], $unserializedMessage->getProperties());
$this->assertEquals($decoded['headers'], $unserializedMessage->getHeaders());
$this->assertEquals($decoded['attributes'], $unserializedMessage->getAttributes());
}

public function testMessagePayloadCouldBeUnserializedFromJson()
Expand All @@ -62,7 +61,6 @@ public function testMessagePayloadCouldBeUnserializedFromJson()
$this->assertEquals($json, $unserializedMessage->getBody());
$this->assertEquals([], $unserializedMessage->getProperties());
$this->assertEquals([], $unserializedMessage->getHeaders());
$this->assertEquals([], $unserializedMessage->getAttributes());
}

public function testThrowIfMalformedJsonGivenOnUnsterilizedFromJson()
Expand All @@ -72,13 +70,4 @@ public function testThrowIfMalformedJsonGivenOnUnsterilizedFromJson()

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

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

$attributes = $message->getAttributes();

$this->assertSame(['key1' => 'value1'], $attributes);
}
}
8 changes: 4 additions & 4 deletions pkg/gps/Tests/GpsProducerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function testShouldSendMessage()
->expects($this->once())
->method('publish')
->with($this->identicalTo([
'data' => '{"body":"","properties":[],"headers":[],"attributes":[]}',
'data' => '{"body":"","properties":[],"headers":[]}',
]));

$client = $this->createPubSubClientMock();
Expand All @@ -56,16 +56,16 @@ public function testShouldSendMessage()
$producer->send($topic, $message);
}

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

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

$client = $this->createPubSubClientMock();
Expand Down

0 comments on commit c8c4993

Please sign in to comment.