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

Commit 0823505

Browse files
add functionality to enable/disable auto topic & subscription creation
1 parent 7201f7e commit 0823505

File tree

4 files changed

+182
-9
lines changed

4 files changed

+182
-9
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ $client = new \Google\Cloud\PubSub\PubSubClient([
2626

2727
$adapter = new \Superbalist\PubSub\GoogleCloud\GoogleCloudPubSubAdapter($client);
2828

29+
2930
// consume messages
3031
// note: this is a blocking call
3132
$adapter->subscribe('my_channel', function ($message) {
@@ -37,6 +38,10 @@ $adapter->publish('my_channel', 'HELLO WORLD');
3738
$adapter->publish('my_channel', json_encode(['hello' => 'world']));
3839
$adapter->publish('my_channel', 1);
3940
$adapter->publish('my_channel', false);
41+
42+
// disable auto topic & subscription creation
43+
$adapter->setAutoCreateTopics(false); // this is true by default
44+
$adapter->setAutoCreateSubscriptions(false); // this is true by default
4045
```
4146

4247
## Examples

changelog.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
## 1.0.1 - ?
44

55
* Ack messages individually after callable returns successfully
6-
7-
* Initial release
6+
* Add functionality to enable/disable auto topic & subscription creation
87

98
## 1.0.0 - 2016-09-05
109

src/GoogleCloudPubSubAdapter.php

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,26 @@ class GoogleCloudPubSubAdapter implements PubSubAdapterInterface
1313
*/
1414
protected $client;
1515

16+
/**
17+
* @var bool
18+
*/
19+
protected $autoCreateTopics;
20+
21+
/**
22+
* @var bool
23+
*/
24+
protected $autoCreateSubscriptions;
25+
1626
/**
1727
* @param PubSubClient $client
28+
* @param bool $autoCreateTopics
29+
* @param bool $autoCreateSubscriptions
1830
*/
19-
public function __construct(PubSubClient $client)
31+
public function __construct(PubSubClient $client, $autoCreateTopics = true, $autoCreateSubscriptions = true)
2032
{
2133
$this->client = $client;
34+
$this->autoCreateTopics = $autoCreateTopics;
35+
$this->autoCreateSubscriptions = $autoCreateSubscriptions;
2236
}
2337

2438
/**
@@ -31,6 +45,46 @@ public function getClient()
3145
return $this->client;
3246
}
3347

48+
/**
49+
* Set whether or not topics will be auto created.
50+
*
51+
* @param bool $autoCreateTopics
52+
*/
53+
public function setAutoCreateTopics($autoCreateTopics)
54+
{
55+
$this->autoCreateTopics = $autoCreateTopics;
56+
}
57+
58+
/**
59+
* Check whether or not topics will be auto created.
60+
*
61+
* @return bool
62+
*/
63+
public function areTopicsAutoCreated()
64+
{
65+
return $this->autoCreateTopics;
66+
}
67+
68+
/**
69+
* Set whether or not subscriptions will be auto created.
70+
*
71+
* @param bool $autoCreateSubscriptions
72+
*/
73+
public function setAutoCreateSubscriptions($autoCreateSubscriptions)
74+
{
75+
$this->autoCreateSubscriptions = $autoCreateSubscriptions;
76+
}
77+
78+
/**
79+
* Check whether or not subscriptions will be auto created.
80+
*
81+
* @return bool
82+
*/
83+
public function areSubscriptionsAutoCreated()
84+
{
85+
return $this->autoCreateSubscriptions;
86+
}
87+
3488
/**
3589
* Subscribe a handler to a channel.
3690
*
@@ -85,7 +139,7 @@ public function publish($channel, $message)
85139
protected function getTopicForChannel($channel)
86140
{
87141
$topic = $this->client->topic($channel);
88-
if (!$topic->exists()) {
142+
if ($this->autoCreateTopics && !$topic->exists()) {
89143
$topic->create();
90144
}
91145
return $topic;
@@ -103,7 +157,7 @@ protected function getSubscriptionForChannel($channel)
103157
{
104158
$topic = $this->getTopicForChannel($channel);
105159
$subscription = $topic->subscription($channel);
106-
if (!$subscription->exists()) {
160+
if ($this->autoCreateSubscriptions && !$subscription->exists()) {
107161
$subscription->create();
108162
}
109163
return $subscription;

tests/GoogleCloudPubSubAdapterTest.php

Lines changed: 119 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ public function testGetClient()
1818
$this->assertSame($client, $adapter->getClient());
1919
}
2020

21+
public function testGetSetAutoCreateTopics()
22+
{
23+
$client = Mockery::mock(PubSubClient::class);
24+
$adapter = new GoogleCloudPubSubAdapter($client);
25+
$this->assertTrue($adapter->areTopicsAutoCreated());
26+
27+
$adapter->setAutoCreateTopics(false);
28+
$this->assertFalse($adapter->areTopicsAutoCreated());
29+
}
30+
31+
public function testGetSetAutoCreateSubscriptions()
32+
{
33+
$client = Mockery::mock(PubSubClient::class);
34+
$adapter = new GoogleCloudPubSubAdapter($client);
35+
$this->assertTrue($adapter->areSubscriptionsAutoCreated());
36+
37+
$adapter->setAutoCreateSubscriptions(false);
38+
$this->assertFalse($adapter->areSubscriptionsAutoCreated());
39+
}
40+
2141
public function testPublishWhenTopicMustBeCreated()
2242
{
2343
$topic = Mockery::mock(Topic::class);
@@ -49,7 +69,7 @@ public function testPublishWhenTopicExists()
4969
$topic->shouldReceive('exists')
5070
->once()
5171
->andReturn(true);
52-
$topic->shouldNotReceive('create');
72+
$topic->shouldNotHaveReceived('create');
5373
$topic->shouldReceive('publish')
5474
->with([
5575
'data' => 'a:1:{s:5:"hello";s:5:"world";}',
@@ -67,6 +87,28 @@ public function testPublishWhenTopicExists()
6787
$adapter->publish('channel_name', ['hello' => 'world']);
6888
}
6989

90+
public function testPublishWhenAutoTopicCreationIsDisabled()
91+
{
92+
$topic = Mockery::mock(Topic::class);
93+
$topic->shouldNotHaveReceived('exists');
94+
$topic->shouldNotHaveReceived('create');
95+
$topic->shouldReceive('publish')
96+
->with([
97+
'data' => 'a:1:{s:5:"hello";s:5:"world";}',
98+
])
99+
->once();
100+
101+
$client = Mockery::mock(PubSubClient::class);
102+
$client->shouldReceive('topic')
103+
->with('channel_name')
104+
->once()
105+
->andReturn($topic);
106+
107+
$adapter = new GoogleCloudPubSubAdapter($client, false);
108+
109+
$adapter->publish('channel_name', ['hello' => 'world']);
110+
}
111+
70112
public function testSubscribeWhenSubscriptionMustBeCreated()
71113
{
72114
$messageBatch1 = [
@@ -118,7 +160,7 @@ public function testSubscribeWhenSubscriptionMustBeCreated()
118160
$topic->shouldReceive('exists')
119161
->once()
120162
->andReturn(true);
121-
$topic->shouldNotReceive('create');
163+
$topic->shouldNotHaveReceived('create');
122164
$topic->shouldReceive('subscription')
123165
->with('channel_name')
124166
->once()
@@ -172,7 +214,7 @@ public function testSubscribeWhenSubscriptionExists()
172214
$subscription->shouldReceive('exists')
173215
->once()
174216
->andReturn(true);
175-
$subscription->shouldNotReceive('create');
217+
$subscription->shouldNotHaveReceived('create');
176218
$subscription->shouldReceive('pull')
177219
->once()
178220
->andReturn($messageBatch1);
@@ -193,7 +235,7 @@ public function testSubscribeWhenSubscriptionExists()
193235
$topic->shouldReceive('exists')
194236
->once()
195237
->andReturn(true);
196-
$topic->shouldNotReceive('create');
238+
$topic->shouldNotHaveReceived('create');
197239
$topic->shouldReceive('subscription')
198240
->with('channel_name')
199241
->once()
@@ -217,4 +259,77 @@ public function testSubscribeWhenSubscriptionExists()
217259

218260
$adapter->subscribe('channel_name', [$handler1, 'handle']);
219261
}
262+
263+
public function testSubscribeWhenAutoTopicCreationIsDisabled()
264+
{
265+
$messageBatch1 = [
266+
[
267+
'ackId' => 1,
268+
'message' => [
269+
'data' => base64_encode('a:1:{s:5:"hello";s:5:"world";}')
270+
],
271+
],
272+
[
273+
'ackId' => 2,
274+
'message' => [
275+
'data' => base64_encode('this is a string')
276+
],
277+
],
278+
];
279+
$messageBatch2 = [
280+
[
281+
'ackId' => 3,
282+
'message' => [
283+
'data' => base64_encode('unsubscribe')
284+
],
285+
],
286+
];
287+
288+
$subscription = Mockery::mock(Subscription::class);
289+
$subscription->shouldNotHaveReceived('exists');
290+
$subscription->shouldNotHaveReceived('create');
291+
$subscription->shouldReceive('pull')
292+
->once()
293+
->andReturn($messageBatch1);
294+
$subscription->shouldReceive('acknowledge')
295+
->with(1)
296+
->once();
297+
$subscription->shouldReceive('acknowledge')
298+
->with(2)
299+
->once();
300+
$subscription->shouldReceive('pull')
301+
->once()
302+
->andReturn($messageBatch2);
303+
$subscription->shouldReceive('acknowledge')
304+
->with(3)
305+
->once();
306+
307+
$topic = Mockery::mock(Topic::class);
308+
$topic->shouldReceive('exists')
309+
->once()
310+
->andReturn(true);
311+
$topic->shouldNotHaveReceived('create');
312+
$topic->shouldReceive('subscription')
313+
->with('channel_name')
314+
->once()
315+
->andReturn($subscription);
316+
317+
$client = Mockery::mock(PubSubClient::class);
318+
$client->shouldReceive('topic')
319+
->with('channel_name')
320+
->once()
321+
->andReturn($topic);
322+
323+
$adapter = new GoogleCloudPubSubAdapter($client, true, false);
324+
325+
$handler1 = Mockery::mock(\stdClass::class);
326+
$handler1->shouldReceive('handle')
327+
->with(['hello' => 'world'])
328+
->once();
329+
$handler1->shouldReceive('handle')
330+
->with('this is a string')
331+
->once();
332+
333+
$adapter->subscribe('channel_name', [$handler1, 'handle']);
334+
}
220335
}

0 commit comments

Comments
 (0)