@@ -37,8 +37,8 @@ public function testFromDsn()
37
37
new Connection (['stream ' => 'queue ' , 'delete_after_ack ' => true ], [
38
38
'host ' => 'localhost ' ,
39
39
'port ' => 6379 ,
40
- ], [], $ this ->createMock (\Redis::class )),
41
- Connection::fromDsn ('redis://localhost/queue?delete_after_ack=1 ' , [], $ this ->createMock (\Redis::class ))
40
+ ], [], $ this ->createRedisMock ( )),
41
+ Connection::fromDsn ('redis://localhost/queue?delete_after_ack=1 ' , [], $ this ->createRedisMock ( ))
42
42
);
43
43
}
44
44
@@ -48,24 +48,24 @@ public function testFromDsnOnUnixSocket()
48
48
new Connection (['stream ' => 'queue ' , 'delete_after_ack ' => true ], [
49
49
'host ' => '/var/run/redis/redis.sock ' ,
50
50
'port ' => 0 ,
51
- ], [], $ redis = $ this ->createMock (\Redis::class )),
52
- Connection::fromDsn ('redis:///var/run/redis/redis.sock ' , ['stream ' => 'queue ' , 'delete_after_ack ' => true ], $ redis )
51
+ ], [], $ this ->createRedisMock ( )),
52
+ Connection::fromDsn ('redis:///var/run/redis/redis.sock ' , ['stream ' => 'queue ' , 'delete_after_ack ' => true ], $ this -> createRedisMock () )
53
53
);
54
54
}
55
55
56
56
public function testFromDsnWithOptions ()
57
57
{
58
58
$ this ->assertEquals (
59
- Connection::fromDsn ('redis://localhost ' , ['stream ' => 'queue ' , 'group ' => 'group1 ' , 'consumer ' => 'consumer1 ' , 'auto_setup ' => false , 'serializer ' => 2 , 'delete_after_ack ' => true ], $ this ->createMock (\Redis::class )),
60
- Connection::fromDsn ('redis://localhost/queue/group1/consumer1?serializer=2&auto_setup=0&delete_after_ack=1 ' , [], $ this ->createMock (\Redis::class ))
59
+ Connection::fromDsn ('redis://localhost ' , ['stream ' => 'queue ' , 'group ' => 'group1 ' , 'consumer ' => 'consumer1 ' , 'auto_setup ' => false , 'serializer ' => 2 , 'delete_after_ack ' => true ], $ this ->createRedisMock ( )),
60
+ Connection::fromDsn ('redis://localhost/queue/group1/consumer1?serializer=2&auto_setup=0&delete_after_ack=1 ' , [], $ this ->createRedisMock ( ))
61
61
);
62
62
}
63
63
64
64
public function testFromDsnWithOptionsAndTrailingSlash ()
65
65
{
66
66
$ this ->assertEquals (
67
- Connection::fromDsn ('redis://localhost/ ' , ['stream ' => 'queue ' , 'group ' => 'group1 ' , 'consumer ' => 'consumer1 ' , 'auto_setup ' => false , 'serializer ' => 2 , 'delete_after_ack ' => true ], $ this ->createMock (\Redis::class )),
68
- Connection::fromDsn ('redis://localhost/queue/group1/consumer1?serializer=2&auto_setup=0&delete_after_ack=1 ' , [], $ this ->createMock (\Redis::class ))
67
+ Connection::fromDsn ('redis://localhost/ ' , ['stream ' => 'queue ' , 'group ' => 'group1 ' , 'consumer ' => 'consumer1 ' , 'auto_setup ' => false , 'serializer ' => 2 , 'delete_after_ack ' => true ], $ this ->createRedisMock ( )),
68
+ Connection::fromDsn ('redis://localhost/queue/group1/consumer1?serializer=2&auto_setup=0&delete_after_ack=1 ' , [], $ this ->createRedisMock ( ))
69
69
);
70
70
}
71
71
@@ -79,6 +79,9 @@ public function testFromDsnWithTls()
79
79
->method ('connect ' )
80
80
->with ('tls://127.0.0.1 ' , 6379 )
81
81
->willReturn (true );
82
+ $ redis ->expects ($ this ->any ())
83
+ ->method ('isConnected ' )
84
+ ->willReturnOnConsecutiveCalls (false , true );
82
85
83
86
Connection::fromDsn ('redis://127.0.0.1?tls=1 ' , [], $ redis );
84
87
}
@@ -93,6 +96,9 @@ public function testFromDsnWithTlsOption()
93
96
->method ('connect ' )
94
97
->with ('tls://127.0.0.1 ' , 6379 )
95
98
->willReturn (true );
99
+ $ redis ->expects ($ this ->any ())
100
+ ->method ('isConnected ' )
101
+ ->willReturnOnConsecutiveCalls (false , true );
96
102
97
103
Connection::fromDsn ('redis://127.0.0.1 ' , ['tls ' => true ], $ redis );
98
104
}
@@ -104,6 +110,9 @@ public function testFromDsnWithRedissScheme()
104
110
->method ('connect ' )
105
111
->with ('tls://127.0.0.1 ' , 6379 )
106
112
->willReturn (true );
113
+ $ redis ->expects ($ this ->any ())
114
+ ->method ('isConnected ' )
115
+ ->willReturnOnConsecutiveCalls (false , true );
107
116
108
117
Connection::fromDsn ('rediss://127.0.0.1?delete_after_ack=true ' , [], $ redis );
109
118
}
@@ -116,21 +125,21 @@ public function testFromDsnWithQueryOptions()
116
125
'port ' => 6379 ,
117
126
], [
118
127
'serializer ' => 2 ,
119
- ], $ this ->createMock (\Redis::class )),
120
- Connection::fromDsn ('redis://localhost/queue/group1/consumer1?serializer=2&delete_after_ack=1 ' , [], $ this ->createMock (\Redis::class ))
128
+ ], $ this ->createRedisMock ( )),
129
+ Connection::fromDsn ('redis://localhost/queue/group1/consumer1?serializer=2&delete_after_ack=1 ' , [], $ this ->createRedisMock ( ))
121
130
);
122
131
}
123
132
124
133
public function testFromDsnWithMixDsnQueryOptions ()
125
134
{
126
135
$ this ->assertEquals (
127
- Connection::fromDsn ('redis://localhost/queue/group1?serializer=2 ' , ['consumer ' => 'specific-consumer ' , 'delete_after_ack ' => true ], $ this ->createMock (\Redis::class )),
128
- Connection::fromDsn ('redis://localhost/queue/group1/specific-consumer?serializer=2&delete_after_ack=1 ' , [], $ this ->createMock (\Redis::class ))
136
+ Connection::fromDsn ('redis://localhost/queue/group1?serializer=2 ' , ['consumer ' => 'specific-consumer ' , 'delete_after_ack ' => true ], $ this ->createRedisMock ( )),
137
+ Connection::fromDsn ('redis://localhost/queue/group1/specific-consumer?serializer=2&delete_after_ack=1 ' , [], $ this ->createRedisMock ( ))
129
138
);
130
139
131
140
$ this ->assertEquals (
132
- Connection::fromDsn ('redis://localhost/queue/group1/consumer1 ' , ['consumer ' => 'specific-consumer ' , 'delete_after_ack ' => true ], $ this ->createMock (\Redis::class )),
133
- Connection::fromDsn ('redis://localhost/queue/group1/consumer1 ' , ['delete_after_ack ' => true ], $ this ->createMock (\Redis::class ))
141
+ Connection::fromDsn ('redis://localhost/queue/group1/consumer1 ' , ['consumer ' => 'specific-consumer ' , 'delete_after_ack ' => true ], $ this ->createRedisMock ( )),
142
+ Connection::fromDsn ('redis://localhost/queue/group1/consumer1 ' , ['delete_after_ack ' => true ], $ this ->createRedisMock ( ))
134
143
);
135
144
}
136
145
@@ -140,7 +149,7 @@ public function testFromDsnWithMixDsnQueryOptions()
140
149
public function testDeprecationIfInvalidOptionIsPassedWithDsn ()
141
150
{
142
151
$ this ->expectDeprecation ('Since symfony/messenger 5.1: Invalid option(s) "foo" passed to the Redis Messenger transport. Passing invalid options is deprecated. ' );
143
- Connection::fromDsn ('redis://localhost/queue?foo=bar ' , [], $ this ->createMock (\Redis::class ));
152
+ Connection::fromDsn ('redis://localhost/queue?foo=bar ' , [], $ this ->createRedisMock ( ));
144
153
}
145
154
146
155
public function testRedisClusterInstanceIsSupported ()
@@ -151,7 +160,7 @@ public function testRedisClusterInstanceIsSupported()
151
160
152
161
public function testKeepGettingPendingMessages ()
153
162
{
154
- $ redis = $ this ->createMock (\Redis::class );
163
+ $ redis = $ this ->createRedisMock ( );
155
164
156
165
$ redis ->expects ($ this ->exactly (3 ))->method ('xreadgroup ' )
157
166
->with ('symfony ' , 'consumer ' , ['queue ' => 0 ], 1 , 1 )
@@ -170,7 +179,7 @@ public function testKeepGettingPendingMessages()
170
179
*/
171
180
public function testAuth ($ expected , string $ dsn )
172
181
{
173
- $ redis = $ this ->createMock (\Redis::class );
182
+ $ redis = $ this ->createRedisMock ( );
174
183
175
184
$ redis ->expects ($ this ->exactly (1 ))->method ('auth ' )
176
185
->with ($ expected )
@@ -190,7 +199,7 @@ public static function provideAuthDsn(): \Generator
190
199
191
200
public function testAuthFromOptions ()
192
201
{
193
- $ redis = $ this ->createMock (\Redis::class );
202
+ $ redis = $ this ->createRedisMock ( );
194
203
195
204
$ redis ->expects ($ this ->exactly (1 ))->method ('auth ' )
196
205
->with ('password ' )
@@ -201,7 +210,7 @@ public function testAuthFromOptions()
201
210
202
211
public function testAuthFromOptionsAndDsn ()
203
212
{
204
- $ redis = $ this ->createMock (\Redis::class );
213
+ $ redis = $ this ->createRedisMock ( );
205
214
206
215
$ redis ->expects ($ this ->exactly (1 ))->method ('auth ' )
207
216
->with ('password2 ' )
@@ -212,7 +221,7 @@ public function testAuthFromOptionsAndDsn()
212
221
213
222
public function testNoAuthWithEmptyPassword ()
214
223
{
215
- $ redis = $ this ->createMock (\Redis::class );
224
+ $ redis = $ this ->createRedisMock ( );
216
225
217
226
$ redis ->expects ($ this ->exactly (0 ))->method ('auth ' )
218
227
->with ('' )
@@ -223,7 +232,7 @@ public function testNoAuthWithEmptyPassword()
223
232
224
233
public function testAuthZeroPassword ()
225
234
{
226
- $ redis = $ this ->createMock (\Redis::class );
235
+ $ redis = $ this ->createRedisMock ( );
227
236
228
237
$ redis ->expects ($ this ->exactly (1 ))->method ('auth ' )
229
238
->with ('0 ' )
@@ -236,7 +245,7 @@ public function testFailedAuth()
236
245
{
237
246
$ this ->expectException (\InvalidArgumentException::class);
238
247
$ this ->expectExceptionMessage ('Redis connection ' );
239
- $ redis = $ this ->createMock (\Redis::class );
248
+ $ redis = $ this ->createRedisMock ( );
240
249
241
250
$ redis ->expects ($ this ->exactly (1 ))->method ('auth ' )
242
251
->with ('password ' )
@@ -247,7 +256,7 @@ public function testFailedAuth()
247
256
248
257
public function testGetPendingMessageFirst ()
249
258
{
250
- $ redis = $ this ->createMock (\Redis::class );
259
+ $ redis = $ this ->createRedisMock ( );
251
260
252
261
$ redis ->expects ($ this ->exactly (1 ))->method ('xreadgroup ' )
253
262
->with ('symfony ' , 'consumer ' , ['queue ' => '0 ' ], 1 , 1 )
@@ -269,7 +278,7 @@ public function testGetPendingMessageFirst()
269
278
270
279
public function testClaimAbandonedMessageWithRaceCondition ()
271
280
{
272
- $ redis = $ this ->createMock (\Redis::class );
281
+ $ redis = $ this ->createRedisMock ( );
273
282
274
283
$ redis ->expects ($ this ->exactly (3 ))->method ('xreadgroup ' )
275
284
->willReturnCallback (function (...$ args ) {
@@ -305,7 +314,7 @@ public function testClaimAbandonedMessageWithRaceCondition()
305
314
306
315
public function testClaimAbandonedMessage ()
307
316
{
308
- $ redis = $ this ->createMock (\Redis::class );
317
+ $ redis = $ this ->createRedisMock ( );
309
318
310
319
$ redis ->expects ($ this ->exactly (2 ))->method ('xreadgroup ' )
311
320
->willReturnCallback (function (...$ args ) {
@@ -341,7 +350,7 @@ public function testUnexpectedRedisError()
341
350
{
342
351
$ this ->expectException (TransportException::class);
343
352
$ this ->expectExceptionMessage ('Redis error happens ' );
344
- $ redis = $ this ->createMock (\Redis::class );
353
+ $ redis = $ this ->createRedisMock ( );
345
354
$ redis ->expects ($ this ->once ())->method ('xreadgroup ' )->willReturn (false );
346
355
$ redis ->expects ($ this ->once ())->method ('getLastError ' )->willReturn ('Redis error happens ' );
347
356
@@ -351,7 +360,7 @@ public function testUnexpectedRedisError()
351
360
352
361
public function testMaxEntries ()
353
362
{
354
- $ redis = $ this ->createMock (\Redis::class );
363
+ $ redis = $ this ->createRedisMock ( );
355
364
356
365
$ redis ->expects ($ this ->exactly (1 ))->method ('xadd ' )
357
366
->with ('queue ' , '* ' , ['message ' => '{"body":"1","headers":[]} ' ], 20000 , true )
@@ -363,7 +372,7 @@ public function testMaxEntries()
363
372
364
373
public function testDeleteAfterAck ()
365
374
{
366
- $ redis = $ this ->createMock (\Redis::class );
375
+ $ redis = $ this ->createRedisMock ( );
367
376
368
377
$ redis ->expects ($ this ->exactly (1 ))->method ('xack ' )
369
378
->with ('queue ' , 'symfony ' , ['1 ' ])
@@ -383,12 +392,12 @@ public function testLegacyOmitDeleteAfterAck()
383
392
{
384
393
$ this ->expectDeprecation ('Since symfony/redis-messenger 5.4: Not setting the "delete_after_ack" boolean option explicitly is deprecated, its default value will change to true in 6.0. ' );
385
394
386
- Connection::fromDsn ('redis://localhost/queue ' , [], $ this ->createMock (\Redis::class));
395
+ Connection::fromDsn ('redis://localhost/queue ' , [], $ this ->createRedisMock (\Redis::class));
387
396
}
388
397
389
398
public function testDeleteAfterReject ()
390
399
{
391
- $ redis = $ this ->createMock (\Redis::class );
400
+ $ redis = $ this ->createRedisMock ( );
392
401
393
402
$ redis ->expects ($ this ->exactly (1 ))->method ('xack ' )
394
403
->with ('queue ' , 'symfony ' , ['1 ' ])
@@ -403,7 +412,7 @@ public function testDeleteAfterReject()
403
412
404
413
public function testLastErrorGetsCleared ()
405
414
{
406
- $ redis = $ this ->createMock (\Redis::class );
415
+ $ redis = $ this ->createRedisMock ( );
407
416
408
417
$ redis ->expects ($ this ->once ())->method ('xadd ' )->willReturn ('0 ' );
409
418
$ redis ->expects ($ this ->once ())->method ('xack ' )->willReturn (0 );
@@ -427,4 +436,17 @@ public function testLastErrorGetsCleared()
427
436
428
437
$ this ->assertSame ('xack error ' , $ e ->getMessage ());
429
438
}
439
+
440
+ private function createRedisMock (): \Redis
441
+ {
442
+ $ redis = $ this ->createMock (\Redis::class);
443
+ $ redis ->expects ($ this ->any ())
444
+ ->method ('connect ' )
445
+ ->willReturn (true );
446
+ $ redis ->expects ($ this ->any ())
447
+ ->method ('isConnected ' )
448
+ ->willReturnOnConsecutiveCalls (false , true );
449
+
450
+ return $ redis ;
451
+ }
430
452
}