4
4
5
5
use Bunny \Channel ;
6
6
use Bunny \Message ;
7
+ use Enqueue \AmqpBunny \AmqpContext ;
7
8
use Enqueue \AmqpBunny \AmqpProducer ;
9
+ use Enqueue \AmqpTools \DelayStrategy ;
8
10
use Enqueue \Test \ClassExtensionTrait ;
9
11
use Interop \Amqp \AmqpMessage as InteropAmqpMessage ;
10
12
use Interop \Amqp \Impl \AmqpMessage ;
11
13
use Interop \Amqp \Impl \AmqpQueue ;
12
14
use Interop \Amqp \Impl \AmqpTopic ;
15
+ use Interop \Queue \DeliveryDelayNotSupportedException ;
13
16
use Interop \Queue \InvalidDestinationException ;
14
17
use Interop \Queue \InvalidMessageException ;
15
18
use Interop \Queue \PsrDestination ;
@@ -23,7 +26,7 @@ class AmqpProducerTest extends TestCase
23
26
24
27
public function testCouldBeConstructedWithRequiredArguments ()
25
28
{
26
- new AmqpProducer ($ this ->createBunnyChannelMock ());
29
+ new AmqpProducer ($ this ->createBunnyChannelMock (), $ this -> createContextMock () );
27
30
}
28
31
29
32
public function testShouldImplementPsrProducerInterface ()
@@ -33,7 +36,7 @@ public function testShouldImplementPsrProducerInterface()
33
36
34
37
public function testShouldThrowExceptionWhenDestinationTypeIsInvalid ()
35
38
{
36
- $ producer = new AmqpProducer ($ this ->createBunnyChannelMock ());
39
+ $ producer = new AmqpProducer ($ this ->createBunnyChannelMock (), $ this -> createContextMock () );
37
40
38
41
$ this ->expectException (InvalidDestinationException::class);
39
42
$ this ->expectExceptionMessage ('The destination must be an instance of Interop\Amqp\AmqpQueue but got ' );
@@ -43,7 +46,7 @@ public function testShouldThrowExceptionWhenDestinationTypeIsInvalid()
43
46
44
47
public function testShouldThrowExceptionWhenMessageTypeIsInvalid ()
45
48
{
46
- $ producer = new AmqpProducer ($ this ->createBunnyChannelMock ());
49
+ $ producer = new AmqpProducer ($ this ->createBunnyChannelMock (), $ this -> createContextMock () );
47
50
48
51
$ this ->expectException (InvalidMessageException::class);
49
52
$ this ->expectExceptionMessage ('The message must be an instance of Interop\Amqp\AmqpMessage but it is ' );
@@ -65,7 +68,7 @@ public function testShouldPublishMessageToTopic()
65
68
$ message = new AmqpMessage ('body ' );
66
69
$ message ->setRoutingKey ('routing-key ' );
67
70
68
- $ producer = new AmqpProducer ($ channel );
71
+ $ producer = new AmqpProducer ($ channel, $ this -> createContextMock () );
69
72
$ producer ->send ($ topic , $ message );
70
73
}
71
74
@@ -80,10 +83,52 @@ public function testShouldPublishMessageToQueue()
80
83
81
84
$ queue = new AmqpQueue ('queue ' );
82
85
83
- $ producer = new AmqpProducer ($ channel );
86
+ $ producer = new AmqpProducer ($ channel, $ this -> createContextMock () );
84
87
$ producer ->send ($ queue , new AmqpMessage ('body ' ));
85
88
}
86
89
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
+
87
132
public function testShouldSetMessageHeaders ()
88
133
{
89
134
$ channel = $ this ->createBunnyChannelMock ();
@@ -93,7 +138,7 @@ public function testShouldSetMessageHeaders()
93
138
->with ($ this ->anything (), ['content_type ' => 'text/plain ' ])
94
139
;
95
140
96
- $ producer = new AmqpProducer ($ channel );
141
+ $ producer = new AmqpProducer ($ channel, $ this -> createContextMock () );
97
142
$ producer ->send (new AmqpTopic ('name ' ), new AmqpMessage ('body ' , [], ['content_type ' => 'text/plain ' ]));
98
143
}
99
144
@@ -106,7 +151,7 @@ public function testShouldSetMessageProperties()
106
151
->with ($ this ->anything (), ['application_headers ' => ['key ' => 'value ' ]])
107
152
;
108
153
109
- $ producer = new AmqpProducer ($ channel );
154
+ $ producer = new AmqpProducer ($ channel, $ this -> createContextMock () );
110
155
$ producer ->send (new AmqpTopic ('name ' ), new AmqpMessage ('body ' , ['key ' => 'value ' ]));
111
156
}
112
157
@@ -123,7 +168,7 @@ public function testShouldPropagateFlags()
123
168
$ message ->addFlag (InteropAmqpMessage::FLAG_IMMEDIATE );
124
169
$ message ->addFlag (InteropAmqpMessage::FLAG_MANDATORY );
125
170
126
- $ producer = new AmqpProducer ($ channel );
171
+ $ producer = new AmqpProducer ($ channel, $ this -> createContextMock () );
127
172
$ producer ->send (new AmqpTopic ('name ' ), $ message );
128
173
}
129
174
@@ -150,4 +195,20 @@ private function createBunnyChannelMock()
150
195
{
151
196
return $ this ->createMock (Channel::class);
152
197
}
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
+ }
153
214
}
0 commit comments