11
11
12
12
namespace Symfony \Component \Messenger \Bridge \Beanstalkd \Tests \Transport ;
13
13
14
- use Pheanstalk \Contract \PheanstalkInterface ;
14
+ use Pheanstalk \Contract \PheanstalkManagerInterface ;
15
+ use Pheanstalk \Contract \PheanstalkPublisherInterface ;
16
+ use Pheanstalk \Contract \PheanstalkSubscriberInterface ;
15
17
use Pheanstalk \Exception ;
16
18
use Pheanstalk \Exception \ClientException ;
17
19
use Pheanstalk \Exception \DeadlineSoonException ;
18
20
use Pheanstalk \Exception \ServerException ;
19
- use Pheanstalk \Job ;
20
- use Pheanstalk \JobId ;
21
21
use Pheanstalk \Pheanstalk ;
22
- use Pheanstalk \Response \ArrayResponse ;
22
+ use Pheanstalk \Values \Job ;
23
+ use Pheanstalk \Values \JobId ;
24
+ use Pheanstalk \Values \JobState ;
25
+ use Pheanstalk \Values \JobStats ;
26
+ use Pheanstalk \Values \TubeList ;
27
+ use Pheanstalk \Values \TubeName ;
28
+ use Pheanstalk \Values \TubeStats ;
23
29
use PHPUnit \Framework \TestCase ;
24
30
use Symfony \Component \Messenger \Bridge \Beanstalkd \Transport \Connection ;
25
31
use Symfony \Component \Messenger \Exception \InvalidArgumentException as MessengerInvalidArgumentException ;
@@ -124,7 +130,7 @@ public function testItThrowsAnExceptionIfAnExtraOptionIsDefinedInDSN()
124
130
125
131
public function testGet ()
126
132
{
127
- $ id = 1234 ;
133
+ $ id = ' 1234 ' ;
128
134
$ beanstalkdEnvelope = [
129
135
'body ' => 'foo ' ,
130
136
'headers ' => 'bar ' ,
@@ -133,17 +139,20 @@ public function testGet()
133
139
$ tube = 'baz ' ;
134
140
$ timeout = 44 ;
135
141
136
- $ job = new Job ($ id , json_encode ($ beanstalkdEnvelope ));
142
+ $ tubeList = new TubeList ($ tubeName = new TubeName ($ tube ), $ tubeNameDefault = new TubeName ('default ' ));
143
+ $ job = new Job (new JobId ($ id ), json_encode ($ beanstalkdEnvelope ));
137
144
138
145
$ client = $ this ->createMock (PheanstalkInterface::class);
139
- $ client ->expects ($ this ->once ())->method ('watchOnly ' )->with ($ tube )->willReturn ($ client );
146
+ $ client ->expects ($ this ->once ())->method ('watch ' )->with ($ tubeName )->willReturn (2 );
147
+ $ client ->expects ($ this ->once ())->method ('listTubesWatched ' )->willReturn ($ tubeList );
148
+ $ client ->expects ($ this ->once ())->method ('ignore ' )->with ($ tubeNameDefault )->willReturn (1 );
140
149
$ client ->expects ($ this ->once ())->method ('reserveWithTimeout ' )->with ($ timeout )->willReturn ($ job );
141
150
142
151
$ connection = new Connection (['tube_name ' => $ tube , 'timeout ' => $ timeout], $ client );
143
152
144
153
$ envelope = $ connection ->get ();
145
154
146
- $ this ->assertSame (( string ) $ id , $ envelope ['id ' ]);
155
+ $ this ->assertSame ($ id , $ envelope ['id ' ]);
147
156
$ this ->assertSame ($ beanstalkdEnvelope ['body ' ], $ envelope ['body ' ]);
148
157
$ this ->assertSame ($ beanstalkdEnvelope ['headers ' ], $ envelope ['headers ' ]);
149
158
}
@@ -154,7 +163,9 @@ public function testGetWhenThereIsNoJobInTheTube()
154
163
$ timeout = 44 ;
155
164
156
165
$ client = $ this ->createMock (PheanstalkInterface::class);
157
- $ client ->expects ($ this ->once ())->method ('watchOnly ' )->with ($ tube )->willReturn ($ client );
166
+ $ client ->expects ($ this ->once ())->method ('watch ' )->with (new TubeName ($ tube ))->willReturn (1 );
167
+ $ client ->expects ($ this ->never ())->method ('listTubesWatched ' );
168
+ $ client ->expects ($ this ->never ())->method ('ignore ' );
158
169
$ client ->expects ($ this ->once ())->method ('reserveWithTimeout ' )->with ($ timeout )->willReturn (null );
159
170
160
171
$ connection = new Connection (['tube_name ' => $ tube , 'timeout ' => $ timeout ], $ client );
@@ -170,7 +181,9 @@ public function testGetWhenABeanstalkdExceptionOccurs()
170
181
$ exception = new DeadlineSoonException ('foo error ' );
171
182
172
183
$ client = $ this ->createMock (PheanstalkInterface::class);
173
- $ client ->expects ($ this ->once ())->method ('watchOnly ' )->with ($ tube )->willReturn ($ client );
184
+ $ client ->expects ($ this ->once ())->method ('watch ' )->with (new TubeName ($ tube ))->willReturn (1 );
185
+ $ client ->expects ($ this ->never ())->method ('listTubesWatched ' );
186
+ $ client ->expects ($ this ->never ())->method ('ignore ' );
174
187
$ client ->expects ($ this ->once ())->method ('reserveWithTimeout ' )->with ($ timeout )->willThrowException ($ exception );
175
188
176
189
$ connection = new Connection (['tube_name ' => $ tube , 'timeout ' => $ timeout ], $ client );
@@ -181,35 +194,35 @@ public function testGetWhenABeanstalkdExceptionOccurs()
181
194
182
195
public function testAck ()
183
196
{
184
- $ id = 123456 ;
197
+ $ id = ' 123456 ' ;
185
198
186
199
$ tube = 'xyz ' ;
187
200
188
201
$ client = $ this ->createMock (PheanstalkInterface::class);
189
- $ client ->expects ($ this ->once ())->method ('useTube ' )->with ($ tube )-> willReturn ( $ client );
202
+ $ client ->expects ($ this ->once ())->method ('useTube ' )->with (new TubeName ( $ tube ));
190
203
$ client ->expects ($ this ->once ())->method ('delete ' )->with ($ this ->callback (fn (JobId $ jobId ): bool => $ jobId ->getId () === $ id ));
191
204
192
205
$ connection = new Connection (['tube_name ' => $ tube ], $ client );
193
206
194
- $ connection ->ack (( string ) $ id );
207
+ $ connection ->ack ($ id );
195
208
}
196
209
197
210
public function testAckWhenABeanstalkdExceptionOccurs ()
198
211
{
199
- $ id = 123456 ;
212
+ $ id = ' 123456 ' ;
200
213
201
214
$ tube = 'xyzw ' ;
202
215
203
216
$ exception = new ServerException ('baz error ' );
204
217
205
218
$ client = $ this ->createMock (PheanstalkInterface::class);
206
- $ client ->expects ($ this ->once ())->method ('useTube ' )->with ($ tube )-> willReturn ( $ client );
219
+ $ client ->expects ($ this ->once ())->method ('useTube ' )->with (new TubeName ( $ tube ));
207
220
$ client ->expects ($ this ->once ())->method ('delete ' )->with ($ this ->callback (fn (JobId $ jobId ): bool => $ jobId ->getId () === $ id ))->willThrowException ($ exception );
208
221
209
222
$ connection = new Connection (['tube_name ' => $ tube ], $ client );
210
223
211
224
$ this ->expectExceptionObject (new TransportException ($ exception ->getMessage (), 0 , $ exception ));
212
- $ connection ->ack (( string ) $ id );
225
+ $ connection ->ack ($ id );
213
226
}
214
227
215
228
/**
@@ -219,66 +232,66 @@ public function testAckWhenABeanstalkdExceptionOccurs()
219
232
*/
220
233
public function testReject (bool $ buryOnReject , bool $ forceDelete )
221
234
{
222
- $ id = 123456 ;
235
+ $ id = ' 123456 ' ;
223
236
224
237
$ tube = 'baz ' ;
225
238
226
239
$ client = $ this ->createMock (PheanstalkInterface::class);
227
- $ client ->expects ($ this ->once ())->method ('useTube ' )->with ($ tube )-> willReturn ( $ client );
240
+ $ client ->expects ($ this ->once ())->method ('useTube ' )->with (new TubeName ( $ tube ));
228
241
$ client ->expects ($ this ->once ())->method ('delete ' )->with ($ this ->callback (fn (JobId $ jobId ): bool => $ jobId ->getId () === $ id ));
229
242
230
243
$ connection = new Connection (['tube_name ' => $ tube , 'bury_on_reject ' => $ buryOnReject ], $ client );
231
244
232
- $ connection ->reject (( string ) $ id , null , $ forceDelete );
245
+ $ connection ->reject ($ id , null , $ forceDelete );
233
246
}
234
247
235
248
public function testRejectWithBury ()
236
249
{
237
- $ id = 123456 ;
250
+ $ id = ' 123456 ' ;
238
251
239
252
$ tube = 'baz ' ;
240
253
241
254
$ client = $ this ->createMock (PheanstalkInterface::class);
242
- $ client ->expects ($ this ->once ())->method ('useTube ' )->with ($ tube )-> willReturn ( $ client );
255
+ $ client ->expects ($ this ->once ())->method ('useTube ' )->with (new TubeName ( $ tube ));
243
256
$ client ->expects ($ this ->once ())->method ('bury ' )->with ($ this ->callback (fn (JobId $ jobId ): bool => $ jobId ->getId () === $ id ), 1024 );
244
257
245
258
$ connection = new Connection (['tube_name ' => $ tube , 'bury_on_reject ' => true ], $ client );
246
259
247
- $ connection ->reject (( string ) $ id );
260
+ $ connection ->reject ($ id );
248
261
}
249
262
250
263
public function testRejectWithBuryAndPriority ()
251
264
{
252
- $ id = 123456 ;
265
+ $ id = ' 123456 ' ;
253
266
$ priority = 2 ;
254
267
255
268
$ tube = 'baz ' ;
256
269
257
270
$ client = $ this ->createMock (PheanstalkInterface::class);
258
- $ client ->expects ($ this ->once ())->method ('useTube ' )->with ($ tube )-> willReturn ( $ client );
271
+ $ client ->expects ($ this ->once ())->method ('useTube ' )->with (new TubeName ( $ tube ));
259
272
$ client ->expects ($ this ->once ())->method ('bury ' )->with ($ this ->callback (fn (JobId $ jobId ): bool => $ jobId ->getId () === $ id ), $ priority );
260
273
261
274
$ connection = new Connection (['tube_name ' => $ tube , 'bury_on_reject ' => true ], $ client );
262
275
263
- $ connection ->reject (( string ) $ id , $ priority );
276
+ $ connection ->reject ($ id , $ priority );
264
277
}
265
278
266
279
public function testRejectWhenABeanstalkdExceptionOccurs ()
267
280
{
268
- $ id = 123456 ;
281
+ $ id = ' 123456 ' ;
269
282
270
283
$ tube = 'baz123 ' ;
271
284
272
285
$ exception = new ServerException ('baz error ' );
273
286
274
287
$ client = $ this ->createMock (PheanstalkInterface::class);
275
- $ client ->expects ($ this ->once ())->method ('useTube ' )->with ($ tube )-> willReturn ( $ client );
288
+ $ client ->expects ($ this ->once ())->method ('useTube ' )->with (new TubeName ( $ tube ));
276
289
$ client ->expects ($ this ->once ())->method ('delete ' )->with ($ this ->callback (fn (JobId $ jobId ): bool => $ jobId ->getId () === $ id ))->willThrowException ($ exception );
277
290
278
291
$ connection = new Connection (['tube_name ' => $ tube ], $ client );
279
292
280
293
$ this ->expectExceptionObject (new TransportException ($ exception ->getMessage (), 0 , $ exception ));
281
- $ connection ->reject (( string ) $ id );
294
+ $ connection ->reject ($ id );
282
295
}
283
296
284
297
public function testMessageCount ()
@@ -287,10 +300,11 @@ public function testMessageCount()
287
300
288
301
$ count = 51 ;
289
302
290
- $ response = new ArrayResponse ( ' OK ' , [ ' current-jobs-ready ' => $ count ] );
303
+ $ response = new TubeStats ( $ tubeName = new TubeName ( $ tube ), 0 , 51 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 );
291
304
292
305
$ client = $ this ->createMock (PheanstalkInterface::class);
293
- $ client ->expects ($ this ->once ())->method ('statsTube ' )->with ($ tube )->willReturn ($ response );
306
+ $ client ->expects ($ this ->once ())->method ('useTube ' )->with ($ tubeName );
307
+ $ client ->expects ($ this ->once ())->method ('statsTube ' )->with ($ tubeName )->willReturn ($ response );
294
308
295
309
$ connection = new Connection (['tube_name ' => $ tube ], $ client );
296
310
@@ -304,7 +318,7 @@ public function testMessageCountWhenABeanstalkdExceptionOccurs()
304
318
$ exception = new ClientException ('foobar error ' );
305
319
306
320
$ client = $ this ->createMock (PheanstalkInterface::class);
307
- $ client ->expects ($ this ->once ())->method ('statsTube ' )->with ($ tube )->willThrowException ($ exception );
321
+ $ client ->expects ($ this ->once ())->method ('statsTube ' )->with (new TubeName ( $ tube) )->willThrowException ($ exception );
308
322
309
323
$ connection = new Connection (['tube_name ' => $ tube ], $ client );
310
324
@@ -314,24 +328,24 @@ public function testMessageCountWhenABeanstalkdExceptionOccurs()
314
328
315
329
public function testMessagePriority ()
316
330
{
317
- $ id = 123456 ;
331
+ $ id = ' 123456 ' ;
318
332
$ priority = 51 ;
319
333
320
334
$ tube = 'baz ' ;
321
335
322
- $ response = new ArrayResponse ( ' OK ' , [ ' pri ' => $ priority] );
336
+ $ response = new JobStats ( new JobId ( $ id ), new TubeName ( $ tube ), JobState:: READY , $ priority, 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 );
323
337
324
338
$ client = $ this ->createMock (PheanstalkInterface::class);
325
339
$ client ->expects ($ this ->once ())->method ('statsJob ' )->with ($ this ->callback (fn (JobId $ jobId ): bool => $ jobId ->getId () === $ id ))->willReturn ($ response );
326
340
327
341
$ connection = new Connection (['tube_name ' => $ tube ], $ client );
328
342
329
- $ this ->assertSame ($ priority , $ connection ->getMessagePriority (( string ) $ id ));
343
+ $ this ->assertSame ($ priority , $ connection ->getMessagePriority ($ id ));
330
344
}
331
345
332
346
public function testMessagePriorityWhenABeanstalkdExceptionOccurs ()
333
347
{
334
- $ id = 123456 ;
348
+ $ id = ' 123456 ' ;
335
349
336
350
$ tube = 'baz1234 ' ;
337
351
@@ -343,7 +357,7 @@ public function testMessagePriorityWhenABeanstalkdExceptionOccurs()
343
357
$ connection = new Connection (['tube_name ' => $ tube ], $ client );
344
358
345
359
$ this ->expectExceptionObject (new TransportException ($ exception ->getMessage (), 0 , $ exception ));
346
- $ connection ->getMessagePriority (( string ) $ id );
360
+ $ connection ->getMessagePriority ($ id );
347
361
}
348
362
349
363
public function testSend ()
@@ -355,10 +369,10 @@ public function testSend()
355
369
$ delay = 1000 ;
356
370
$ expectedDelay = $ delay / 1000 ;
357
371
358
- $ id = 110 ;
372
+ $ id = ' 110 ' ;
359
373
360
374
$ client = $ this ->createMock (PheanstalkInterface::class);
361
- $ client ->expects ($ this ->once ())->method ('useTube ' )->with ($ tube )-> willReturn ( $ client );
375
+ $ client ->expects ($ this ->once ())->method ('useTube ' )->with (new TubeName ( $ tube ));
362
376
$ client ->expects ($ this ->once ())->method ('put ' )->with (
363
377
$ this ->callback (function (string $ data ) use ($ body , $ headers ): bool {
364
378
$ expectedMessage = json_encode ([
@@ -371,13 +385,13 @@ public function testSend()
371
385
1024 ,
372
386
$ expectedDelay ,
373
387
90
374
- )->willReturn (new Job ($ id , 'foobar ' ));
388
+ )->willReturn (new Job (new JobId ( $ id) , 'foobar ' ));
375
389
376
390
$ connection = new Connection (['tube_name ' => $ tube ], $ client );
377
391
378
392
$ returnedId = $ connection ->send ($ body , $ headers , $ delay );
379
393
380
- $ this ->assertSame ($ id , ( int ) $ returnedId );
394
+ $ this ->assertSame ($ id , $ returnedId );
381
395
}
382
396
383
397
public function testSendWithPriority ()
@@ -390,10 +404,10 @@ public function testSendWithPriority()
390
404
$ priority = 2 ;
391
405
$ expectedDelay = $ delay / 1000 ;
392
406
393
- $ id = 110 ;
407
+ $ id = ' 110 ' ;
394
408
395
409
$ client = $ this ->createMock (PheanstalkInterface::class);
396
- $ client ->expects ($ this ->once ())->method ('useTube ' )->with ($ tube )-> willReturn ( $ client );
410
+ $ client ->expects ($ this ->once ())->method ('useTube ' )->with (new TubeName ( $ tube ));
397
411
$ client ->expects ($ this ->once ())->method ('put ' )->with (
398
412
$ this ->callback (function (string $ data ) use ($ body , $ headers ): bool {
399
413
$ expectedMessage = json_encode ([
@@ -406,13 +420,13 @@ public function testSendWithPriority()
406
420
$ priority ,
407
421
$ expectedDelay ,
408
422
90
409
- )->willReturn (new Job ($ id , 'foobar ' ));
423
+ )->willReturn (new Job (new JobId ( $ id) , 'foobar ' ));
410
424
411
425
$ connection = new Connection (['tube_name ' => $ tube ], $ client );
412
426
413
427
$ returnedId = $ connection ->send ($ body , $ headers , $ delay , $ priority );
414
428
415
- $ this ->assertSame ($ id , ( int ) $ returnedId );
429
+ $ this ->assertSame ($ id , $ returnedId );
416
430
}
417
431
418
432
public function testSendWhenABeanstalkdExceptionOccurs ()
@@ -427,7 +441,7 @@ public function testSendWhenABeanstalkdExceptionOccurs()
427
441
$ exception = new Exception ('foo bar ' );
428
442
429
443
$ client = $ this ->createMock (PheanstalkInterface::class);
430
- $ client ->expects ($ this ->once ())->method ('useTube ' )->with ($ tube )-> willReturn ( $ client );
444
+ $ client ->expects ($ this ->once ())->method ('useTube ' )->with (new TubeName ( $ tube ));
431
445
$ client ->expects ($ this ->once ())->method ('put ' )->with (
432
446
$ this ->callback (function (string $ data ) use ($ body , $ headers ): bool {
433
447
$ expectedMessage = json_encode ([
@@ -451,35 +465,35 @@ public function testSendWhenABeanstalkdExceptionOccurs()
451
465
452
466
public function testKeepalive ()
453
467
{
454
- $ id = 123456 ;
468
+ $ id = ' 123456 ' ;
455
469
456
470
$ tube = 'baz ' ;
457
471
458
472
$ client = $ this ->createMock (PheanstalkInterface::class);
459
- $ client ->expects ($ this ->once ())->method ('useTube ' )->with ($ tube )-> willReturn ( $ client );
473
+ $ client ->expects ($ this ->once ())->method ('useTube ' )->with (new TubeName ( $ tube ));
460
474
$ client ->expects ($ this ->once ())->method ('touch ' )->with ($ this ->callback (fn (JobId $ jobId ): bool => $ jobId ->getId () === $ id ));
461
475
462
476
$ connection = new Connection (['tube_name ' => $ tube ], $ client );
463
477
464
- $ connection ->keepalive (( string ) $ id );
478
+ $ connection ->keepalive ($ id );
465
479
}
466
480
467
481
public function testKeepaliveWhenABeanstalkdExceptionOccurs ()
468
482
{
469
- $ id = 123456 ;
483
+ $ id = ' 123456 ' ;
470
484
471
485
$ tube = 'baz123 ' ;
472
486
473
487
$ exception = new ServerException ('baz error ' );
474
488
475
489
$ client = $ this ->createMock (PheanstalkInterface::class);
476
- $ client ->expects ($ this ->once ())->method ('useTube ' )->with ($ tube )-> willReturn ( $ client );
490
+ $ client ->expects ($ this ->once ())->method ('useTube ' )->with (new TubeName ( $ tube ));
477
491
$ client ->expects ($ this ->once ())->method ('touch ' )->with ($ this ->callback (fn (JobId $ jobId ): bool => $ jobId ->getId () === $ id ))->willThrowException ($ exception );
478
492
479
493
$ connection = new Connection (['tube_name ' => $ tube ], $ client );
480
494
481
495
$ this ->expectExceptionObject (new TransportException ($ exception ->getMessage (), 0 , $ exception ));
482
- $ connection ->keepalive (( string ) $ id );
496
+ $ connection ->keepalive ($ id );
483
497
}
484
498
485
499
public function testSendWithRoundedDelay ()
@@ -491,7 +505,7 @@ public function testSendWithRoundedDelay()
491
505
$ expectedDelay = 0 ;
492
506
493
507
$ client = $ this ->createMock (PheanstalkInterface::class);
494
- $ client ->expects ($ this ->once ())->method ('useTube ' )->with ($ tube )-> willReturn ( $ client );
508
+ $ client ->expects ($ this ->once ())->method ('useTube ' )->with (new TubeName ( $ tube ));
495
509
$ client ->expects ($ this ->once ())->method ('put ' )->with (
496
510
$ this ->anything (),
497
511
$ this ->anything (),
@@ -503,3 +517,7 @@ public function testSendWithRoundedDelay()
503
517
$ connection ->send ($ body , $ headers , $ delay );
504
518
}
505
519
}
520
+
521
+ interface PheanstalkInterface extends PheanstalkPublisherInterface, PheanstalkSubscriberInterface, PheanstalkManagerInterface
522
+ {
523
+ }
0 commit comments