Skip to content
Merged
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
10 changes: 3 additions & 7 deletions pkg/gps/GpsMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,11 @@ public function jsonSerialize(): array
public static function jsonUnserialize(string $json): 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()
));
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'], $data['properties'], $data['headers']);
return new self($data['body'] ?? $json, $data['properties'] ?? [], $data['headers'] ?? []);
}

public function getNativeMessage(): ?GoogleMessage
Expand Down
25 changes: 25 additions & 0 deletions pkg/gps/Tests/GpsMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,31 @@ public function testCouldBeUnserializedFromJson()
$this->assertEquals($message, $unserializedMessage);
}

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

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

$this->assertInstanceOf(GpsMessage::class, $unserializedMessage);
$decoded = json_decode($json, true);
$this->assertEquals($decoded['body'], $unserializedMessage->getBody());
$this->assertEquals($decoded['properties'], $unserializedMessage->getProperties());
$this->assertEquals($decoded['headers'], $unserializedMessage->getHeaders());
}

public function testMessagePayloadCouldBeUnserializedFromJson()
{
$json = '{"theBodyPropFoo":"theBodyPropVal"}';

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

$this->assertInstanceOf(GpsMessage::class, $unserializedMessage);
$this->assertEquals($json, $unserializedMessage->getBody());
$this->assertEquals([], $unserializedMessage->getProperties());
$this->assertEquals([], $unserializedMessage->getHeaders());
}

public function testThrowIfMalformedJsonGivenOnUnsterilizedFromJson()
{
$this->expectException(\InvalidArgumentException::class);
Expand Down