@@ -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