1818use Interop \Queue \PsrProcessor ;
1919use Interop \Queue \PsrQueue ;
2020use Interop \Queue \PsrSubscriptionConsumer ;
21+ use Interop \Queue \SubscriptionConsumerNotSupportedException ;
2122use PHPUnit \Framework \TestCase ;
2223use Psr \Log \NullLogger ;
2324
@@ -127,7 +128,7 @@ public function testShouldAllowBindCallbackToQueueName()
127128 $ queueName = 'theQueueName ' ;
128129 $ queue = new NullQueue ($ queueName );
129130
130- $ context = $ this ->createMock (PsrContext::class );
131+ $ context = $ this ->createContextWithoutSubscriptionConsumerMock ( );
131132 $ context
132133 ->expects ($ this ->once ())
133134 ->method ('createQueue ' )
@@ -160,6 +161,90 @@ public function testShouldReturnSelfOnBind()
160161 $ this ->assertSame ($ consumer , $ consumer ->bind (new NullQueue ('foo_queue ' ), $ processorMock ));
161162 }
162163
164+ public function testShouldUseContextSubscriptionConsumerIfSupport ()
165+ {
166+ $ expectedQueue = new NullQueue ('theQueueName ' );
167+
168+ $ contextSubscriptionConsumer = $ this ->createSubscriptionConsumerMock ();
169+ $ contextSubscriptionConsumer
170+ ->expects ($ this ->once ())
171+ ->method ('consume ' )
172+ ->willReturn (null )
173+ ;
174+
175+ $ fallbackSubscriptionConsumer = $ this ->createSubscriptionConsumerMock ();
176+ $ fallbackSubscriptionConsumer
177+ ->expects ($ this ->never ())
178+ ->method ('consume ' )
179+ ;
180+
181+ $ contextMock = $ this ->createMock (PsrContext::class);
182+ $ contextMock
183+ ->expects ($ this ->once ())
184+ ->method ('createConsumer ' )
185+ ->with ($ this ->identicalTo ($ expectedQueue ))
186+ ->willReturn ($ this ->createConsumerStub ())
187+ ;
188+ $ contextMock
189+ ->expects ($ this ->once ())
190+ ->method ('createSubscriptionConsumer ' )
191+ ->willReturn ($ contextSubscriptionConsumer )
192+ ;
193+
194+ $ processorMock = $ this ->createProcessorMock ();
195+ $ processorMock
196+ ->expects ($ this ->never ())
197+ ->method ('process ' )
198+ ;
199+
200+ $ queueConsumer = new QueueConsumer ($ contextMock , new BreakCycleExtension (1 ));
201+ $ queueConsumer ->setFallbackSubscriptionConsumer ($ fallbackSubscriptionConsumer );
202+ $ queueConsumer ->bind ($ expectedQueue , $ processorMock );
203+ $ queueConsumer ->consume ();
204+ }
205+
206+ public function testShouldUseFallbackSubscriptionConsumerIfNotSupported ()
207+ {
208+ $ expectedQueue = new NullQueue ('theQueueName ' );
209+
210+ $ contextSubscriptionConsumer = $ this ->createSubscriptionConsumerMock ();
211+ $ contextSubscriptionConsumer
212+ ->expects ($ this ->never ())
213+ ->method ('consume ' )
214+ ;
215+
216+ $ fallbackSubscriptionConsumer = $ this ->createSubscriptionConsumerMock ();
217+ $ fallbackSubscriptionConsumer
218+ ->expects ($ this ->once ())
219+ ->method ('consume ' )
220+ ->willReturn (null )
221+ ;
222+
223+ $ contextMock = $ this ->createContextWithoutSubscriptionConsumerMock ();
224+ $ contextMock
225+ ->expects ($ this ->once ())
226+ ->method ('createConsumer ' )
227+ ->with ($ this ->identicalTo ($ expectedQueue ))
228+ ->willReturn ($ this ->createConsumerStub ())
229+ ;
230+ $ contextMock
231+ ->expects ($ this ->once ())
232+ ->method ('createSubscriptionConsumer ' )
233+ ->willThrowException (SubscriptionConsumerNotSupportedException ::providerDoestNotSupportIt ())
234+ ;
235+
236+ $ processorMock = $ this ->createProcessorMock ();
237+ $ processorMock
238+ ->expects ($ this ->never ())
239+ ->method ('process ' )
240+ ;
241+
242+ $ queueConsumer = new QueueConsumer ($ contextMock , new BreakCycleExtension (1 ));
243+ $ queueConsumer ->setFallbackSubscriptionConsumer ($ fallbackSubscriptionConsumer );
244+ $ queueConsumer ->bind ($ expectedQueue , $ processorMock );
245+ $ queueConsumer ->consume ();
246+ }
247+
163248 public function testShouldSubscribeToGivenQueueWithExpectedTimeout ()
164249 {
165250 $ expectedQueue = new NullQueue ('theQueueName ' );
@@ -172,7 +257,7 @@ public function testShouldSubscribeToGivenQueueWithExpectedTimeout()
172257 ->willReturn (null )
173258 ;
174259
175- $ contextMock = $ this ->createMock (PsrContext::class );
260+ $ contextMock = $ this ->createContextWithoutSubscriptionConsumerMock ( );
176261 $ contextMock
177262 ->expects ($ this ->once ())
178263 ->method ('createConsumer ' )
@@ -203,7 +288,7 @@ public function testShouldSubscribeToGivenQueueAndQuitAfterFifthIdleCycle()
203288 ->willReturn (null )
204289 ;
205290
206- $ contextMock = $ this ->createMock (PsrContext::class );
291+ $ contextMock = $ this ->createContextWithoutSubscriptionConsumerMock ( );
207292 $ contextMock
208293 ->expects ($ this ->once ())
209294 ->method ('createConsumer ' )
@@ -1173,7 +1258,7 @@ public function testShouldCallProcessorAsMessageComeAlong()
11731258 'bar_queue ' => $ barConsumerStub ,
11741259 ];
11751260
1176- $ contextStub = $ this ->createMock (PsrContext::class );
1261+ $ contextStub = $ this ->createContextWithoutSubscriptionConsumerMock ( );
11771262 $ contextStub
11781263 ->expects ($ this ->any ())
11791264 ->method ('createQueue ' )
@@ -1225,12 +1310,27 @@ public function testShouldCallProcessorAsMessageComeAlong()
12251310 $ this ->assertSame ($ fooConsumerStub , $ actualContexts [2 ]->getPsrConsumer ());
12261311 }
12271312
1313+ /**
1314+ * @return \PHPUnit_Framework_MockObject_MockObject
1315+ */
1316+ private function createContextWithoutSubscriptionConsumerMock (): PsrContext
1317+ {
1318+ $ contextMock = $ this ->createMock (PsrContext::class);
1319+ $ contextMock
1320+ ->expects ($ this ->any ())
1321+ ->method ('createSubscriptionConsumer ' )
1322+ ->willThrowException (SubscriptionConsumerNotSupportedException ::providerDoestNotSupportIt ())
1323+ ;
1324+
1325+ return $ contextMock ;
1326+ }
1327+
12281328 /**
12291329 * @return \PHPUnit_Framework_MockObject_MockObject|PsrContext
12301330 */
1231- protected function createPsrContextStub (PsrConsumer $ consumer = null )
1331+ private function createPsrContextStub (PsrConsumer $ consumer = null )
12321332 {
1233- $ context = $ this ->createMock (PsrContext::class );
1333+ $ context = $ this ->createContextWithoutSubscriptionConsumerMock ( );
12341334 $ context
12351335 ->expects ($ this ->any ())
12361336 ->method ('createQueue ' )
@@ -1257,15 +1357,15 @@ protected function createPsrContextStub(PsrConsumer $consumer = null)
12571357 /**
12581358 * @return \PHPUnit_Framework_MockObject_MockObject|PsrProcessor
12591359 */
1260- protected function createProcessorMock ()
1360+ private function createProcessorMock ()
12611361 {
12621362 return $ this ->createMock (PsrProcessor::class);
12631363 }
12641364
12651365 /**
12661366 * @return \PHPUnit_Framework_MockObject_MockObject|PsrProcessor
12671367 */
1268- protected function createProcessorStub ()
1368+ private function createProcessorStub ()
12691369 {
12701370 $ processorMock = $ this ->createProcessorMock ();
12711371 $ processorMock
@@ -1280,15 +1380,15 @@ protected function createProcessorStub()
12801380 /**
12811381 * @return \PHPUnit_Framework_MockObject_MockObject|PsrMessage
12821382 */
1283- protected function createMessageMock ()
1383+ private function createMessageMock ()
12841384 {
12851385 return $ this ->createMock (PsrMessage::class);
12861386 }
12871387
12881388 /**
12891389 * @return \PHPUnit_Framework_MockObject_MockObject|ExtensionInterface
12901390 */
1291- protected function createExtension ()
1391+ private function createExtension ()
12921392 {
12931393 return $ this ->createMock (ExtensionInterface::class);
12941394 }
@@ -1298,7 +1398,7 @@ protected function createExtension()
12981398 *
12991399 * @return \PHPUnit_Framework_MockObject_MockObject|PsrConsumer
13001400 */
1301- protected function createConsumerStub ($ queue = null )
1401+ private function createConsumerStub ($ queue = null )
13021402 {
13031403 if (is_string ($ queue )) {
13041404 $ queue = new NullQueue ($ queue );
0 commit comments