22
33namespace Enqueue \Redis \Tests ;
44
5+ use Enqueue \Redis \JsonSerializer ;
56use Enqueue \Redis \Redis ;
67use Enqueue \Redis \RedisConsumer ;
78use Enqueue \Redis \RedisContext ;
@@ -35,41 +36,85 @@ public function testShouldReturnDestinationSetInConstructorOnGetQueue()
3536 $ this ->assertSame ($ destination , $ consumer ->getQueue ());
3637 }
3738
38- public function testShouldDoNothingOnAcknowledge ()
39+ public function testShouldAcknowledgeMessage ()
3940 {
40- $ consumer = new RedisConsumer ($ this ->createContextMock (), new RedisDestination ('aQueue ' ));
41+ $ redisMock = $ this ->createRedisMock ();
42+ $ redisMock
43+ ->expects ($ this ->once ())
44+ ->method ('zrem ' )
45+ ->with ('aQueue:reserved ' , 'reserved-key ' )
46+ ->willReturn (1 )
47+ ;
4148
42- $ consumer ->acknowledge (new RedisMessage ());
43- }
49+ $ contextMock = $ this ->createContextMock ();
50+ $ contextMock
51+ ->expects ($ this ->once ())
52+ ->method ('getRedis ' )
53+ ->willReturn ($ redisMock )
54+ ;
4455
45- public function testShouldDoNothingOnReject ()
46- {
47- $ consumer = new RedisConsumer ($ this ->createContextMock (), new RedisDestination ('aQueue ' ));
56+ $ message = new RedisMessage ();
57+ $ message ->setReservedKey ('reserved-key ' );
58+
59+ $ consumer = new RedisConsumer ($ contextMock , new RedisDestination ('aQueue ' ));
4860
49- $ consumer ->reject ( new RedisMessage () );
61+ $ consumer ->acknowledge ( $ message );
5062 }
5163
52- public function testShouldSendSameMessageToDestinationOnReQueue ()
64+ public function testShouldRejectMessage ()
5365 {
66+ $ redisMock = $ this ->createRedisMock ();
67+ $ redisMock
68+ ->expects ($ this ->once ())
69+ ->method ('zrem ' )
70+ ->with ('aQueue:reserved ' , 'reserved-key ' )
71+ ->willReturn (1 )
72+ ;
73+
74+ $ contextMock = $ this ->createContextMock ();
75+ $ contextMock
76+ ->expects ($ this ->once ())
77+ ->method ('getRedis ' )
78+ ->willReturn ($ redisMock )
79+ ;
80+
5481 $ message = new RedisMessage ();
82+ $ message ->setReservedKey ('reserved-key ' );
5583
56- $ destination = new RedisDestination ('aQueue ' );
84+ $ consumer = new RedisConsumer ( $ contextMock , new RedisDestination ('aQueue ' ) );
5785
58- $ producerMock = $ this ->createProducerMock ();
59- $ producerMock
86+ $ consumer ->reject ($ message );
87+ }
88+
89+ public function testShouldSendSameMessageToDestinationOnReQueue ()
90+ {
91+ $ redisMock = $ this ->createRedisMock ();
92+ $ redisMock
6093 ->expects ($ this ->once ())
61- ->method ('send ' )
62- ->with ($ this ->identicalTo ($ destination ), $ this ->identicalTo ($ message ))
94+ ->method ('lpush ' )
95+ ->with ('aQueue ' , '{"body":"text","properties":[],"headers":{"attempts":0}} ' )
96+ ->willReturn (1 )
6397 ;
6498
99+ $ serializer = new JsonSerializer ();
100+
65101 $ contextMock = $ this ->createContextMock ();
66102 $ contextMock
67- ->expects ($ this ->once ())
68- ->method ('createProducer ' )
69- ->willReturn ($ producerMock )
103+ ->expects ($ this ->any ())
104+ ->method ('getRedis ' )
105+ ->willReturn ($ redisMock )
106+ ;
107+ $ contextMock
108+ ->expects ($ this ->any ())
109+ ->method ('getSerializer ' )
110+ ->willReturn ($ serializer )
70111 ;
71112
72- $ consumer = new RedisConsumer ($ contextMock , $ destination );
113+ $ message = new RedisMessage ();
114+ $ message ->setBody ('text ' );
115+ $ message ->setReservedKey ($ serializer ->toString ($ message ));
116+
117+ $ consumer = new RedisConsumer ($ contextMock , new RedisDestination ('aQueue ' ));
73118
74119 $ consumer ->reject ($ message , true );
75120 }
@@ -88,7 +133,7 @@ public function testShouldCallRedisBRPopAndReturnNullIfNothingInQueueOnReceive()
88133
89134 $ contextMock = $ this ->createContextMock ();
90135 $ contextMock
91- ->expects ($ this ->once ())
136+ ->expects ($ this ->any ())
92137 ->method ('getRedis ' )
93138 ->willReturn ($ redisMock )
94139 ;
@@ -102,20 +147,27 @@ public function testShouldCallRedisBRPopAndReturnMessageIfOneInQueueOnReceive()
102147 {
103148 $ destination = new RedisDestination ('aQueue ' );
104149
150+ $ serializer = new JsonSerializer ();
151+
105152 $ redisMock = $ this ->createRedisMock ();
106153 $ redisMock
107154 ->expects ($ this ->once ())
108155 ->method ('brpop ' )
109156 ->with (['aQueue ' ], 2 )
110- ->willReturn (new RedisResult ('aQueue ' , json_encode (new RedisMessage ('aBody ' ))))
157+ ->willReturn (new RedisResult ('aQueue ' , $ serializer -> toString (new RedisMessage ('aBody ' ))))
111158 ;
112159
113160 $ contextMock = $ this ->createContextMock ();
114161 $ contextMock
115- ->expects ($ this ->once ())
162+ ->expects ($ this ->any ())
116163 ->method ('getRedis ' )
117164 ->willReturn ($ redisMock )
118165 ;
166+ $ contextMock
167+ ->expects ($ this ->any ())
168+ ->method ('getSerializer ' )
169+ ->willReturn ($ serializer )
170+ ;
119171
120172 $ consumer = new RedisConsumer ($ contextMock , $ destination );
121173
@@ -131,24 +183,26 @@ public function testShouldCallRedisBRPopSeveralTimesWithFiveSecondTimeoutIfZeroT
131183
132184 $ expectedTimeout = 5 ;
133185
186+ $ serializer = new JsonSerializer ();
187+
134188 $ redisMock = $ this ->createRedisMock ();
135189 $ redisMock
136- ->expects ($ this ->at (0 ))
190+ ->expects ($ this ->at (2 ))
137191 ->method ('brpop ' )
138192 ->with (['aQueue ' ], $ expectedTimeout )
139193 ->willReturn (null )
140194 ;
141195 $ redisMock
142- ->expects ($ this ->at (1 ))
196+ ->expects ($ this ->at (5 ))
143197 ->method ('brpop ' )
144198 ->with (['aQueue ' ], $ expectedTimeout )
145199 ->willReturn (null )
146200 ;
147201 $ redisMock
148- ->expects ($ this ->at (2 ))
202+ ->expects ($ this ->at (8 ))
149203 ->method ('brpop ' )
150204 ->with (['aQueue ' ], $ expectedTimeout )
151- ->willReturn (new RedisResult ('aQueue ' , json_encode (new RedisMessage ('aBody ' ))))
205+ ->willReturn (new RedisResult ('aQueue ' , $ serializer -> toString (new RedisMessage ('aBody ' ))))
152206 ;
153207
154208 $ contextMock = $ this ->createContextMock ();
@@ -157,6 +211,11 @@ public function testShouldCallRedisBRPopSeveralTimesWithFiveSecondTimeoutIfZeroT
157211 ->method ('getRedis ' )
158212 ->willReturn ($ redisMock )
159213 ;
214+ $ contextMock
215+ ->expects ($ this ->atLeastOnce ())
216+ ->method ('getSerializer ' )
217+ ->willReturn ($ serializer )
218+ ;
160219
161220 $ consumer = new RedisConsumer ($ contextMock , $ destination );
162221
@@ -170,6 +229,8 @@ public function testShouldCallRedisRPopAndReturnNullIfNothingInQueueOnReceiveNoW
170229 {
171230 $ destination = new RedisDestination ('aQueue ' );
172231
232+ $ serializer = new JsonSerializer ();
233+
173234 $ redisMock = $ this ->createRedisMock ();
174235 $ redisMock
175236 ->expects ($ this ->once ())
@@ -180,10 +241,15 @@ public function testShouldCallRedisRPopAndReturnNullIfNothingInQueueOnReceiveNoW
180241
181242 $ contextMock = $ this ->createContextMock ();
182243 $ contextMock
183- ->expects ($ this ->once ())
244+ ->expects ($ this ->any ())
184245 ->method ('getRedis ' )
185246 ->willReturn ($ redisMock )
186247 ;
248+ $ contextMock
249+ ->expects ($ this ->any ())
250+ ->method ('getSerializer ' )
251+ ->willReturn ($ serializer )
252+ ;
187253
188254 $ consumer = new RedisConsumer ($ contextMock , $ destination );
189255
@@ -194,20 +260,27 @@ public function testShouldCallRedisRPopAndReturnMessageIfOneInQueueOnReceiveNoWa
194260 {
195261 $ destination = new RedisDestination ('aQueue ' );
196262
263+ $ serializer = new JsonSerializer ();
264+
197265 $ redisMock = $ this ->createRedisMock ();
198266 $ redisMock
199267 ->expects ($ this ->once ())
200268 ->method ('rpop ' )
201269 ->with ('aQueue ' )
202- ->willReturn (new RedisResult ('aQueue ' , json_encode (new RedisMessage ('aBody ' ))))
270+ ->willReturn (new RedisResult ('aQueue ' , $ serializer -> toString (new RedisMessage ('aBody ' ))))
203271 ;
204272
205273 $ contextMock = $ this ->createContextMock ();
206274 $ contextMock
207- ->expects ($ this ->once ())
275+ ->expects ($ this ->atLeastOnce ())
208276 ->method ('getRedis ' )
209277 ->willReturn ($ redisMock )
210278 ;
279+ $ contextMock
280+ ->expects ($ this ->any ())
281+ ->method ('getSerializer ' )
282+ ->willReturn ($ serializer )
283+ ;
211284
212285 $ consumer = new RedisConsumer ($ contextMock , $ destination );
213286
0 commit comments