44
55use Bunny \Channel ;
66use Bunny \Message ;
7+ use Enqueue \AmqpBunny \AmqpContext ;
78use Enqueue \AmqpBunny \AmqpProducer ;
9+ use Enqueue \AmqpTools \DelayStrategy ;
810use Enqueue \Test \ClassExtensionTrait ;
911use Interop \Amqp \AmqpMessage as InteropAmqpMessage ;
1012use Interop \Amqp \Impl \AmqpMessage ;
1113use Interop \Amqp \Impl \AmqpQueue ;
1214use Interop \Amqp \Impl \AmqpTopic ;
15+ use Interop \Queue \DeliveryDelayNotSupportedException ;
1316use Interop \Queue \InvalidDestinationException ;
1417use Interop \Queue \InvalidMessageException ;
1518use Interop \Queue \PsrDestination ;
@@ -23,7 +26,7 @@ class AmqpProducerTest extends TestCase
2326
2427 public function testCouldBeConstructedWithRequiredArguments ()
2528 {
26- new AmqpProducer ($ this ->createBunnyChannelMock ());
29+ new AmqpProducer ($ this ->createBunnyChannelMock (), $ this -> createContextMock () );
2730 }
2831
2932 public function testShouldImplementPsrProducerInterface ()
@@ -33,7 +36,7 @@ public function testShouldImplementPsrProducerInterface()
3336
3437 public function testShouldThrowExceptionWhenDestinationTypeIsInvalid ()
3538 {
36- $ producer = new AmqpProducer ($ this ->createBunnyChannelMock ());
39+ $ producer = new AmqpProducer ($ this ->createBunnyChannelMock (), $ this -> createContextMock () );
3740
3841 $ this ->expectException (InvalidDestinationException::class);
3942 $ this ->expectExceptionMessage ('The destination must be an instance of Interop\Amqp\AmqpQueue but got ' );
@@ -43,7 +46,7 @@ public function testShouldThrowExceptionWhenDestinationTypeIsInvalid()
4346
4447 public function testShouldThrowExceptionWhenMessageTypeIsInvalid ()
4548 {
46- $ producer = new AmqpProducer ($ this ->createBunnyChannelMock ());
49+ $ producer = new AmqpProducer ($ this ->createBunnyChannelMock (), $ this -> createContextMock () );
4750
4851 $ this ->expectException (InvalidMessageException::class);
4952 $ this ->expectExceptionMessage ('The message must be an instance of Interop\Amqp\AmqpMessage but it is ' );
@@ -65,7 +68,7 @@ public function testShouldPublishMessageToTopic()
6568 $ message = new AmqpMessage ('body ' );
6669 $ message ->setRoutingKey ('routing-key ' );
6770
68- $ producer = new AmqpProducer ($ channel );
71+ $ producer = new AmqpProducer ($ channel, $ this -> createContextMock () );
6972 $ producer ->send ($ topic , $ message );
7073 }
7174
@@ -80,10 +83,52 @@ public function testShouldPublishMessageToQueue()
8083
8184 $ queue = new AmqpQueue ('queue ' );
8285
83- $ producer = new AmqpProducer ($ channel );
86+ $ producer = new AmqpProducer ($ channel, $ this -> createContextMock () );
8487 $ producer ->send ($ queue , new AmqpMessage ('body ' ));
8588 }
8689
90+ public function testShouldDelayMessage ()
91+ {
92+ $ channel = $ this ->createBunnyChannelMock ();
93+ $ channel
94+ ->expects ($ this ->never ())
95+ ->method ('publish ' )
96+ ;
97+
98+ $ message = new AmqpMessage ('body ' );
99+ $ context = $ this ->createContextMock ();
100+ $ queue = new AmqpQueue ('queue ' );
101+
102+ $ delayStrategy = $ this ->createDelayStrategyMock ();
103+ $ delayStrategy
104+ ->expects ($ this ->once ())
105+ ->method ('delayMessage ' )
106+ ->with ($ this ->identicalTo ($ context ), $ this ->identicalTo ($ queue ), $ this ->identicalTo ($ message ), 10000 )
107+ ;
108+
109+ $ producer = new AmqpProducer ($ channel , $ context );
110+ $ producer ->setDelayStrategy ($ delayStrategy );
111+ $ producer ->setDeliveryDelay (10000 );
112+
113+ $ producer ->send ($ queue , $ message );
114+ }
115+
116+ public function testShouldThrowExceptionOnSetDeliveryDelayWhenDeliveryStrategyIsNotSet ()
117+ {
118+ $ channel = $ this ->createBunnyChannelMock ();
119+ $ channel
120+ ->expects ($ this ->never ())
121+ ->method ('publish ' )
122+ ;
123+
124+ $ producer = new AmqpProducer ($ channel , $ this ->createContextMock ());
125+
126+ $ this ->expectException (DeliveryDelayNotSupportedException::class);
127+ $ this ->expectExceptionMessage ('The provider does not support delivery delay feature ' );
128+
129+ $ producer ->setDeliveryDelay (10000 );
130+ }
131+
87132 public function testShouldSetMessageHeaders ()
88133 {
89134 $ channel = $ this ->createBunnyChannelMock ();
@@ -93,7 +138,7 @@ public function testShouldSetMessageHeaders()
93138 ->with ($ this ->anything (), ['content_type ' => 'text/plain ' ])
94139 ;
95140
96- $ producer = new AmqpProducer ($ channel );
141+ $ producer = new AmqpProducer ($ channel, $ this -> createContextMock () );
97142 $ producer ->send (new AmqpTopic ('name ' ), new AmqpMessage ('body ' , [], ['content_type ' => 'text/plain ' ]));
98143 }
99144
@@ -106,7 +151,7 @@ public function testShouldSetMessageProperties()
106151 ->with ($ this ->anything (), ['application_headers ' => ['key ' => 'value ' ]])
107152 ;
108153
109- $ producer = new AmqpProducer ($ channel );
154+ $ producer = new AmqpProducer ($ channel, $ this -> createContextMock () );
110155 $ producer ->send (new AmqpTopic ('name ' ), new AmqpMessage ('body ' , ['key ' => 'value ' ]));
111156 }
112157
@@ -123,7 +168,7 @@ public function testShouldPropagateFlags()
123168 $ message ->addFlag (InteropAmqpMessage::FLAG_IMMEDIATE );
124169 $ message ->addFlag (InteropAmqpMessage::FLAG_MANDATORY );
125170
126- $ producer = new AmqpProducer ($ channel );
171+ $ producer = new AmqpProducer ($ channel, $ this -> createContextMock () );
127172 $ producer ->send (new AmqpTopic ('name ' ), $ message );
128173 }
129174
@@ -150,4 +195,20 @@ private function createBunnyChannelMock()
150195 {
151196 return $ this ->createMock (Channel::class);
152197 }
198+
199+ /**
200+ * @return \PHPUnit_Framework_MockObject_MockObject|AmqpContext
201+ */
202+ private function createContextMock ()
203+ {
204+ return $ this ->createMock (AmqpContext::class);
205+ }
206+
207+ /**
208+ * @return \PHPUnit_Framework_MockObject_MockObject|DelayStrategy
209+ */
210+ private function createDelayStrategyMock ()
211+ {
212+ return $ this ->createMock (DelayStrategy::class);
213+ }
153214}
0 commit comments