From ac5a27599885d0b4dd4a02d69a81d550abde9c1c Mon Sep 17 00:00:00 2001 From: Ahmed Khan Date: Tue, 26 Dec 2023 15:01:38 +0100 Subject: [PATCH] adding backward compatibility support --- pkg/gps/GpsConnectionFactory.php | 6 +- pkg/gps/GpsConsumer.php | 9 ++- pkg/gps/GpsContext.php | 5 ++ .../Tests/GpsConnectionFactoryConfigTest.php | 18 +++++ pkg/gps/Tests/GpsConsumerTest.php | 65 ++++++++++++++++--- pkg/gps/Tests/GpsContextTest.php | 7 ++ 6 files changed, 98 insertions(+), 12 deletions(-) diff --git a/pkg/gps/GpsConnectionFactory.php b/pkg/gps/GpsConnectionFactory.php index c15854763..c803c9de5 100644 --- a/pkg/gps/GpsConnectionFactory.php +++ b/pkg/gps/GpsConnectionFactory.php @@ -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 @@ -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; }); } @@ -121,6 +124,7 @@ private function defaultConfig(): array { return [ 'lazy' => true, + 'serilalizeToJson' => true ]; } } diff --git a/pkg/gps/GpsConsumer.php b/pkg/gps/GpsConsumer.php index aa745c456..de67ca085 100644 --- a/pkg/gps/GpsConsumer.php +++ b/pkg/gps/GpsConsumer.php @@ -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; diff --git a/pkg/gps/GpsContext.php b/pkg/gps/GpsContext.php index 77e6200cf..fad96cfe6 100644 --- a/pkg/gps/GpsContext.php +++ b/pkg/gps/GpsContext.php @@ -160,4 +160,9 @@ public function getClient(): PubSubClient return $this->client; } + + public function getOptions(): array + { + return $this->options; + } } diff --git a/pkg/gps/Tests/GpsConnectionFactoryConfigTest.php b/pkg/gps/Tests/GpsConnectionFactoryConfigTest.php index 039e86cd4..3329e562b 100644 --- a/pkg/gps/Tests/GpsConnectionFactoryConfigTest.php +++ b/pkg/gps/Tests/GpsConnectionFactoryConfigTest.php @@ -58,6 +58,7 @@ public static function provideConfigs() null, [ 'lazy' => true, + 'serilalizeToJson' => true, ], ]; @@ -65,6 +66,7 @@ public static function provideConfigs() 'gps:', [ 'lazy' => true, + 'serilalizeToJson' => true, ], ]; @@ -72,6 +74,7 @@ public static function provideConfigs() [], [ 'lazy' => true, + 'serilalizeToJson' => true, ], ]; @@ -83,6 +86,7 @@ public static function provideConfigs() 'emulatorHost' => 'http://google-pubsub:8085', 'hasEmulator' => true, 'lazy' => true, + 'serilalizeToJson' => true, ], ]; @@ -94,6 +98,7 @@ public static function provideConfigs() 'emulatorHost' => 'http://google-pubsub:8085', 'hasEmulator' => true, 'lazy' => true, + 'serilalizeToJson' => true, ], ]; @@ -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, ], ]; } diff --git a/pkg/gps/Tests/GpsConsumerTest.php b/pkg/gps/Tests/GpsConsumerTest.php index 27259dcec..4c7fce163 100644 --- a/pkg/gps/Tests/GpsConsumerTest.php +++ b/pkg/gps/Tests/GpsConsumerTest.php @@ -179,16 +179,11 @@ public function testShouldReceiveMessage() $this->assertSame('the body', $message->getBody()); } - public function testShouldReceiveMessageProtobufFormat() + public function testShouldReceiveMessageUnSerialize() { - $body = '2test+customer_6115118118117248@example.com"4test+customer_611511118118117248@example.com*&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(); @@ -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()); } /** diff --git a/pkg/gps/Tests/GpsContextTest.php b/pkg/gps/Tests/GpsContextTest.php index 658e2659e..d7640bff8 100644 --- a/pkg/gps/Tests/GpsContextTest.php +++ b/pkg/gps/Tests/GpsContextTest.php @@ -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 */