Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ac5a275

Browse files
adding backward compatibility support
1 parent 9dd7770 commit ac5a275

File tree

6 files changed

+98
-12
lines changed

6 files changed

+98
-12
lines changed

‎pkg/gps/GpsConnectionFactory.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ public function createContext(): Context
8080
});
8181
}
8282

83-
return new GpsContext($this->establishConnection());
83+
return new GpsContext($this->establishConnection(), [
84+
'serilalizeToJson' => $this->config['serilalizeToJson'],
85+
]);
8486
}
8587

8688
private function parseDsn(string $dsn): array
@@ -105,6 +107,7 @@ private function parseDsn(string $dsn): array
105107
'emulatorHost' => $emulatorHost,
106108
'hasEmulator' => $hasEmulator,
107109
'lazy' => $dsn->getBool('lazy'),
110+
'serilalizeToJson' => $dsn->getBool('serilalizeToJson'),
108111
]), function ($value) { return null !== $value; });
109112
}
110113

@@ -121,6 +124,7 @@ private function defaultConfig(): array
121124
{
122125
return [
123126
'lazy' => true,
127+
'serilalizeToJson' => true
124128
];
125129
}
126130
}

‎pkg/gps/GpsConsumer.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,14 @@ private function getSubscription(): Subscription
110110

111111
private function convertMessage(GoogleMessage $message): GpsMessage
112112
{
113-
$gpsMessage = new GpsMessage($message->data(), $message->attributes(),[]);
113+
$options = $this->context->getOptions();
114+
115+
if ($options['serilalizeToJson']) {
116+
$gpsMessage = GpsMessage::jsonUnserialize($message->data());
117+
} else {
118+
$gpsMessage = new GpsMessage($message->data(), $message->attributes());
119+
}
120+
114121
$gpsMessage->setNativeMessage($message);
115122

116123
return $gpsMessage;

‎pkg/gps/GpsContext.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,9 @@ public function getClient(): PubSubClient
160160

161161
return $this->client;
162162
}
163+
164+
public function getOptions(): array
165+
{
166+
return $this->options;
167+
}
163168
}

‎pkg/gps/Tests/GpsConnectionFactoryConfigTest.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,23 @@ public static function provideConfigs()
5858
null,
5959
[
6060
'lazy' => true,
61+
'serilalizeToJson' => true,
6162
],
6263
];
6364

6465
yield [
6566
'gps:',
6667
[
6768
'lazy' => true,
69+
'serilalizeToJson' => true,
6870
],
6971
];
7072

7173
yield [
7274
[],
7375
[
7476
'lazy' => true,
77+
'serilalizeToJson' => true,
7578
],
7679
];
7780

@@ -83,6 +86,7 @@ public static function provideConfigs()
8386
'emulatorHost' => 'http://google-pubsub:8085',
8487
'hasEmulator' => true,
8588
'lazy' => true,
89+
'serilalizeToJson' => true,
8690
],
8791
];
8892

@@ -94,6 +98,7 @@ public static function provideConfigs()
9498
'emulatorHost' => 'http://google-pubsub:8085',
9599
'hasEmulator' => true,
96100
'lazy' => true,
101+
'serilalizeToJson' => true,
97102
],
98103
];
99104

@@ -104,6 +109,19 @@ public static function provideConfigs()
104109
'projectId' => 'mqdev',
105110
'emulatorHost' => 'http://Fgoogle-pubsub:8085',
106111
'lazy' => false,
112+
'serilalizeToJson' => true,
113+
],
114+
];
115+
116+
yield [
117+
['dsn' => 'gps:?foo=fooVal&projectId=mqdev&emulatorHost=http%3A%2F%2Fgoogle-pubsub%3A8085&serilalizeToJson=false'],
118+
[
119+
'foo' => 'fooVal',
120+
'projectId' => 'mqdev',
121+
'emulatorHost' => 'http://google-pubsub:8085',
122+
'hasEmulator' => true,
123+
'lazy' => true,
124+
'serilalizeToJson' => false,
107125
],
108126
];
109127
}

‎pkg/gps/Tests/GpsConsumerTest.php‎

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,11 @@ public function testShouldReceiveMessage()
179179
$this->assertSame('the body', $message->getBody());
180180
}
181181

182-
public function testShouldReceiveMessageProtobufFormat()
182+
public function testShouldReceiveMessageUnSerialize()
183183
{
184-
$body = '2test+customer_6115118118117248@example.com"4test+customer_611511118118117248@example.com*&App\Tests\Entity\Entity497709';
185-
$attributes = [
186-
'ce-datacontenttype' => 'application/protobuf',
187-
];
188-
184+
$message = new GpsMessage('the body');
189185
$nativeMessage = new Message([
190-
'data' => $body,
191-
'attributes' => $attributes,
186+
'data' => json_encode($message),
192187
], []);
193188

194189
$subscription = $this->createSubscriptionMock();
@@ -212,14 +207,64 @@ public function testShouldReceiveMessageProtobufFormat()
212207
->expects($this->once())
213208
->method('getClient')
214209
->willReturn($client);
210+
$context
211+
->expects($this->once())
212+
->method('getOptions')
213+
->willReturn(['serilalizeToJson' => false]);
214+
215+
$consumer = new GpsConsumer($context, new GpsQueue('queue-name'));
216+
217+
$message = $consumer->receive(12345);
218+
219+
$this->assertInstanceOf(GpsMessage::class, $message);
220+
$this->assertSame($nativeMessage->data(), $message->getBody());
221+
$this->assertSame($nativeMessage->attributes(), $message->getProperties());
222+
}
223+
224+
public function testShouldReceiveMessageUnSerializeWithAttributes()
225+
{
226+
$message = new GpsMessage('the body');
227+
$nativeMessage = new Message([
228+
'data' => json_encode($message),
229+
'attributes' => [
230+
'foo' => 'fooVal',
231+
'bar' => 'barVal',
232+
],
233+
], []);
234+
235+
$subscription = $this->createSubscriptionMock();
236+
$subscription
237+
->expects($this->once())
238+
->method('pull')
239+
->with($this->identicalTo([
240+
'maxMessages' => 1,
241+
'requestTimeout' => 12.345,
242+
]))
243+
->willReturn([$nativeMessage]);
244+
245+
$client = $this->createPubSubClientMock();
246+
$client
247+
->expects($this->once())
248+
->method('subscription')
249+
->willReturn($subscription);
250+
251+
$context = $this->createContextMock();
252+
$context
253+
->expects($this->once())
254+
->method('getClient')
255+
->willReturn($client);
256+
$context
257+
->expects($this->once())
258+
->method('getOptions')
259+
->willReturn(['serilalizeToJson' => false]);
215260

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

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

220265
$this->assertInstanceOf(GpsMessage::class, $message);
221-
$this->assertSame($body, $message->getBody());
222-
$this->assertSame($attributes, $message->getProperties());
266+
$this->assertSame($nativeMessage->data(), $message->getBody());
267+
$this->assertSame($nativeMessage->attributes(), $message->getProperties());
223268
}
224269

225270
/**

‎pkg/gps/Tests/GpsContextTest.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ public function testCreateConsumerShouldThrowExceptionIfInvalidDestination()
8686
$context->createConsumer(new GpsTopic(''));
8787
}
8888

89+
public function testShouldReturnOptions()
90+
{
91+
$context = new GpsContext($this->createPubSubClientMock(), ['foo' => 'fooVal']);
92+
93+
$this->assertSame(['ackDeadlineSeconds' => 10, 'foo' => 'fooVal'], $context->getOptions());
94+
}
95+
8996
/**
9097
* @return PubSubClient|\PHPUnit\Framework\MockObject\MockObject|PubSubClient
9198
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /