Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.

Commit f469e8b

Browse files
bump up to superbalist/php-pubsub ^2.0 & add publishBatch method (#4)
1 parent 37b5957 commit f469e8b

File tree

4 files changed

+63
-14
lines changed

4 files changed

+63
-14
lines changed

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 4.0.0 - 2017-05-16
4+
5+
* Allow for google/cloud ^0.26.0|^0.27.0|^0.27.0
6+
* Bump up to superbalist/php-pubsub ^2.0
7+
* Add new publishBatch method to GoogleCloudPubSubAdapter
8+
39
## 3.0.1 - 2017-04-03
410

511
* Fix to gRPC timeouts

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
],
1111
"require": {
1212
"php": ">=5.6.0",
13-
"superbalist/php-pubsub": "^1.0",
14-
"google/cloud": "^0.11.0|^0.12.0|^0.13.0|^0.20.0|^0.21.0|^0.22.0|^0.23.0|^0.24.0|^0.25.0"
13+
"superbalist/php-pubsub": "^2.0",
14+
"google/cloud": "^0.11.0|^0.12.0|^0.13.0|^0.20.0|^0.21.0|^0.22.0|^0.23.0|^0.24.0|^0.25.0|^0.26.0|^0.27.0|^0.28.0"
1515
},
1616
"autoload": {
1717
"psr-4": {

src/GoogleCloudPubSubAdapter.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,21 @@ public function publish($channel, $message)
169169
$topic->publish(['data' => Utils::serializeMessage($message)]);
170170
}
171171

172+
/**
173+
* Publish multiple messages to a channel.
174+
*
175+
* @param string $channel
176+
* @param array $messages
177+
*/
178+
public function publishBatch($channel, array $messages)
179+
{
180+
$topic = $this->getTopicForChannel($channel);
181+
$messages = array_map(function ($message) {
182+
return ['data' => Utils::serializeMessage($message)];
183+
}, $messages);
184+
$topic->publishBatch($messages);
185+
}
186+
172187
/**
173188
* Return a `Topic` instance from a channel name.
174189
*

tests/GoogleCloudPubSubAdapterTest.php

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function testPublishWhenTopicMustBeCreated()
5959
->once();
6060
$topic->shouldReceive('publish')
6161
->with([
62-
'data' => 'a:1:{s:5:"hello";s:5:"world";}',
62+
'data' => '{"hello":"world"}',
6363
])
6464
->once();
6565

@@ -83,7 +83,7 @@ public function testPublishWhenTopicExists()
8383
$topic->shouldNotHaveReceived('create');
8484
$topic->shouldReceive('publish')
8585
->with([
86-
'data' => 'a:1:{s:5:"hello";s:5:"world";}',
86+
'data' => '{"hello":"world"}',
8787
])
8888
->once();
8989

@@ -105,7 +105,7 @@ public function testPublishWhenAutoTopicCreationIsDisabled()
105105
$topic->shouldNotHaveReceived('create');
106106
$topic->shouldReceive('publish')
107107
->with([
108-
'data' => 'a:1:{s:5:"hello";s:5:"world";}',
108+
'data' => '{"hello":"world"}',
109109
])
110110
->once();
111111

@@ -120,11 +120,39 @@ public function testPublishWhenAutoTopicCreationIsDisabled()
120120
$adapter->publish('channel_name', ['hello' => 'world']);
121121
}
122122

123+
public function testPublishBatch()
124+
{
125+
$topic = Mockery::mock(Topic::class);
126+
$topic->shouldReceive('exists')
127+
->once()
128+
->andReturn(true);
129+
$topic->shouldReceive('publishBatch')
130+
->with([
131+
['data' => '{"hello":"world"}'],
132+
['data' => '"booo!"'],
133+
])
134+
->once();
135+
136+
$client = Mockery::mock(PubSubClient::class);
137+
$client->shouldReceive('topic')
138+
->with('channel_name')
139+
->once()
140+
->andReturn($topic);
141+
142+
$adapter = new GoogleCloudPubSubAdapter($client);
143+
144+
$messages = [
145+
['hello' => 'world'],
146+
'booo!',
147+
];
148+
$adapter->publishBatch('channel_name', $messages);
149+
}
150+
123151
public function testSubscribeWhenSubscriptionMustBeCreated()
124152
{
125-
$message1 = new Message(['data' => 'a:1:{s:5:"hello";s:5:"world";}'], ['ackId' => 1]);
126-
$message2 = new Message(['data' => 'this is a string'], ['ackId' => 2]);
127-
$message3 = new Message(['data' => 'unsubscribe'], ['ackId' => 3]);
153+
$message1 = new Message(['data' => '{"hello":"world"}'], ['ackId' => 1]);
154+
$message2 = new Message(['data' => '"this is a string"'], ['ackId' => 2]);
155+
$message3 = new Message(['data' => '"unsubscribe"'], ['ackId' => 3]);
128156

129157
$messageBatch1 = [
130158
$message1,
@@ -198,9 +226,9 @@ public function testSubscribeWhenSubscriptionMustBeCreated()
198226

199227
public function testSubscribeWhenSubscriptionExists()
200228
{
201-
$message1 = new Message(['data' => 'a:1:{s:5:"hello";s:5:"world";}'], ['ackId' => 1]);
202-
$message2 = new Message(['data' => 'this is a string'], ['ackId' => 2]);
203-
$message3 = new Message(['data' => 'unsubscribe'], ['ackId' => 3]);
229+
$message1 = new Message(['data' => '{"hello":"world"}'], ['ackId' => 1]);
230+
$message2 = new Message(['data' => '"this is a string"'], ['ackId' => 2]);
231+
$message3 = new Message(['data' => '"unsubscribe"'], ['ackId' => 3]);
204232

205233
$messageBatch1 = [
206234
$message1,
@@ -272,9 +300,9 @@ public function testSubscribeWhenSubscriptionExists()
272300

273301
public function testSubscribeWhenAutoTopicCreationIsDisabled()
274302
{
275-
$message1 = new Message(['data' => 'a:1:{s:5:"hello";s:5:"world";}'], ['ackId' => 1]);
276-
$message2 = new Message(['data' => 'this is a string'], ['ackId' => 2]);
277-
$message3 = new Message(['data' => 'unsubscribe'], ['ackId' => 3]);
303+
$message1 = new Message(['data' => '{"hello":"world"}'], ['ackId' => 1]);
304+
$message2 = new Message(['data' => '"this is a string"'], ['ackId' => 2]);
305+
$message3 = new Message(['data' => '"unsubscribe"'], ['ackId' => 3]);
278306

279307
$messageBatch1 = [
280308
$message1,

0 commit comments

Comments
 (0)