Skip to content

Commit

Permalink
adding backward compatibility support
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedkhan847 committed Dec 26, 2023
1 parent 9dd7770 commit ac5a275
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 12 deletions.
6 changes: 5 additions & 1 deletion pkg/gps/GpsConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public function createContext(): Context
});
}

return new GpsContext($this->establishConnection());
return new GpsContext($this->establishConnection(), [
'serilalizeToJson' => $this->config['serilalizeToJson'],
]);
}

private function parseDsn(string $dsn): array
Expand All @@ -105,6 +107,7 @@ private function parseDsn(string $dsn): array
'emulatorHost' => $emulatorHost,
'hasEmulator' => $hasEmulator,
'lazy' => $dsn->getBool('lazy'),
'serilalizeToJson' => $dsn->getBool('serilalizeToJson'),
]), function ($value) { return null !== $value; });
}

Expand All @@ -121,6 +124,7 @@ private function defaultConfig(): array
{
return [
'lazy' => true,
'serilalizeToJson' => true
];
}
}
9 changes: 8 additions & 1 deletion pkg/gps/GpsConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,14 @@ private function getSubscription(): Subscription

private function convertMessage(GoogleMessage $message): GpsMessage
{
$gpsMessage = new GpsMessage($message->data(), $message->attributes(),[]);
$options = $this->context->getOptions();

if ($options['serilalizeToJson']) {
$gpsMessage = GpsMessage::jsonUnserialize($message->data());
} else {
$gpsMessage = new GpsMessage($message->data(), $message->attributes());
}

$gpsMessage->setNativeMessage($message);

return $gpsMessage;
Expand Down
5 changes: 5 additions & 0 deletions pkg/gps/GpsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,9 @@ public function getClient(): PubSubClient

return $this->client;
}

public function getOptions(): array
{
return $this->options;
}
}
18 changes: 18 additions & 0 deletions pkg/gps/Tests/GpsConnectionFactoryConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,23 @@ public static function provideConfigs()
null,
[
'lazy' => true,
'serilalizeToJson' => true,
],
];

yield [
'gps:',
[
'lazy' => true,
'serilalizeToJson' => true,
],
];

yield [
[],
[
'lazy' => true,
'serilalizeToJson' => true,
],
];

Expand All @@ -83,6 +86,7 @@ public static function provideConfigs()
'emulatorHost' => 'http://google-pubsub:8085',
'hasEmulator' => true,
'lazy' => true,
'serilalizeToJson' => true,
],
];

Expand All @@ -94,6 +98,7 @@ public static function provideConfigs()
'emulatorHost' => 'http://google-pubsub:8085',
'hasEmulator' => true,
'lazy' => true,
'serilalizeToJson' => true,
],
];

Expand All @@ -104,6 +109,19 @@ public static function provideConfigs()
'projectId' => 'mqdev',
'emulatorHost' => 'http://Fgoogle-pubsub:8085',
'lazy' => false,
'serilalizeToJson' => true,
],
];

yield [
['dsn' => 'gps:?foo=fooVal&projectId=mqdev&emulatorHost=http%3A%2F%2Fgoogle-pubsub%3A8085&serilalizeToJson=false'],
[
'foo' => 'fooVal',
'projectId' => 'mqdev',
'emulatorHost' => 'http://google-pubsub:8085',
'hasEmulator' => true,
'lazy' => true,
'serilalizeToJson' => false,
],
];
}
Expand Down
65 changes: 55 additions & 10 deletions pkg/gps/Tests/GpsConsumerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,11 @@ public function testShouldReceiveMessage()
$this->assertSame('the body', $message->getBody());
}

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

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

$subscription = $this->createSubscriptionMock();
Expand All @@ -212,14 +207,64 @@ public function testShouldReceiveMessageProtobufFormat()
->expects($this->once())
->method('getClient')
->willReturn($client);
$context
->expects($this->once())
->method('getOptions')
->willReturn(['serilalizeToJson' => false]);

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

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

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

public function testShouldReceiveMessageUnSerializeWithAttributes()
{
$message = new GpsMessage('the body');
$nativeMessage = new Message([
'data' => json_encode($message),
'attributes' => [
'foo' => 'fooVal',
'bar' => 'barVal',
],
], []);

$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);
$context
->expects($this->once())
->method('getOptions')
->willReturn(['serilalizeToJson' => false]);

$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());
$this->assertSame($nativeMessage->data(), $message->getBody());
$this->assertSame($nativeMessage->attributes(), $message->getProperties());
}

/**
Expand Down
7 changes: 7 additions & 0 deletions pkg/gps/Tests/GpsContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ public function testCreateConsumerShouldThrowExceptionIfInvalidDestination()
$context->createConsumer(new GpsTopic(''));
}

public function testShouldReturnOptions()
{
$context = new GpsContext($this->createPubSubClientMock(), ['foo' => 'fooVal']);

$this->assertSame(['ackDeadlineSeconds' => 10, 'foo' => 'fooVal'], $context->getOptions());
}

/**
* @return PubSubClient|\PHPUnit\Framework\MockObject\MockObject|PubSubClient
*/
Expand Down

0 comments on commit ac5a275

Please sign in to comment.