@@ -156,16 +156,24 @@ public function testPingAfterPreviousFactoryRejectsUnderlyingClientWillCreateNew
156
156
157
157
public function testPingAfterPreviousUnderlyingClientAlreadyClosedWillCreateNewUnderlyingConnection ()
158
158
{
159
- $ client = $ this ->createCallableMockWithOriginalConstructorDisabled (array ('__call ' ));
159
+ $ closeHandler = null ;
160
+ $ client = $ this ->getMockBuilder ('Clue\React\Redis\Client ' )->getMock ();
160
161
$ client ->expects ($ this ->once ())->method ('__call ' )->with ('ping ' )->willReturn (\React \Promise \resolve ('PONG ' ));
162
+ $ client ->expects ($ this ->any ())->method ('on ' )->withConsecutive (
163
+ array ('close ' , $ this ->callback (function ($ arg ) use (&$ closeHandler ) {
164
+ $ closeHandler = $ arg ;
165
+ return true ;
166
+ }))
167
+ );
161
168
162
169
$ this ->factory ->expects ($ this ->exactly (2 ))->method ('createClient ' )->willReturnOnConsecutiveCalls (
163
170
\React \Promise \resolve ($ client ),
164
171
new Promise (function () { })
165
172
);
166
173
167
174
$ this ->client ->ping ();
168
- $ client ->emit ('close ' );
175
+ $ this ->assertTrue (is_callable ($ closeHandler ));
176
+ $ closeHandler ();
169
177
170
178
$ this ->client ->ping ();
171
179
}
@@ -183,7 +191,7 @@ public function testPingAfterCloseWillRejectWithoutCreatingUnderlyingConnection(
183
191
public function testPingAfterPingWillNotStartIdleTimerWhenFirstPingResolves ()
184
192
{
185
193
$ deferred = new Deferred ();
186
- $ client = $ this ->createCallableMockWithOriginalConstructorDisabled ( array ( ' __call ' ) );
194
+ $ client = $ this ->getMockBuilder ( ' Clue\React\Redis\Client ' )-> getMock ( );
187
195
$ client ->expects ($ this ->exactly (2 ))->method ('__call ' )->willReturnOnConsecutiveCalls (
188
196
$ deferred ->promise (),
189
197
new Promise (function () { })
@@ -201,7 +209,7 @@ public function testPingAfterPingWillNotStartIdleTimerWhenFirstPingResolves()
201
209
public function testPingAfterPingWillStartAndCancelIdleTimerWhenSecondPingStartsAfterFirstResolves ()
202
210
{
203
211
$ deferred = new Deferred ();
204
- $ client = $ this ->createCallableMockWithOriginalConstructorDisabled ( array ( ' __call ' ) );
212
+ $ client = $ this ->getMockBuilder ( ' Clue\React\Redis\Client ' )-> getMock ( );
205
213
$ client ->expects ($ this ->exactly (2 ))->method ('__call ' )->willReturnOnConsecutiveCalls (
206
214
$ deferred ->promise (),
207
215
new Promise (function () { })
@@ -220,7 +228,7 @@ public function testPingAfterPingWillStartAndCancelIdleTimerWhenSecondPingStarts
220
228
221
229
public function testPingFollowedByIdleTimerWillCloseUnderlyingConnectionWithoutCloseEvent ()
222
230
{
223
- $ client = $ this ->createCallableMockWithOriginalConstructorDisabled ( array ( ' __call ' , ' close ' ) );
231
+ $ client = $ this ->getMockBuilder ( ' Clue\React\Redis\Client ' )-> getMock ( );
224
232
$ client ->expects ($ this ->once ())->method ('__call ' )->willReturn (\React \Promise \resolve ());
225
233
$ client ->expects ($ this ->once ())->method ('close ' )->willReturn (\React \Promise \resolve ());
226
234
@@ -298,7 +306,7 @@ public function testCloseAfterPingWillCloseUnderlyingClientConnectionWhenAlready
298
306
public function testCloseAfterPingWillCancelIdleTimerWhenPingIsAlreadyResolved ()
299
307
{
300
308
$ deferred = new Deferred ();
301
- $ client = $ this ->createCallableMockWithOriginalConstructorDisabled ( array ( ' __call ' , ' close ' ) );
309
+ $ client = $ this ->getMockBuilder ( ' Clue\React\Redis\Client ' )-> getMock ( );
302
310
$ client ->expects ($ this ->once ())->method ('__call ' )->willReturn ($ deferred ->promise ());
303
311
$ client ->expects ($ this ->once ())->method ('close ' );
304
312
@@ -316,7 +324,7 @@ public function testCloseAfterPingWillCancelIdleTimerWhenPingIsAlreadyResolved()
316
324
public function testCloseAfterPingRejectsWillEmitClose ()
317
325
{
318
326
$ deferred = new Deferred ();
319
- $ client = $ this ->createCallableMockWithOriginalConstructorDisabled ( array ( ' __call ' , ' close ' ) );
327
+ $ client = $ this ->getMockBuilder ( ' Clue\React\Redis\Client ' )-> getMock ( );
320
328
$ client ->expects ($ this ->once ())->method ('__call ' )->willReturn ($ deferred ->promise ());
321
329
$ client ->expects ($ this ->once ())->method ('close ' )->willReturnCallback (function () use ($ client ) {
322
330
$ client ->emit ('close ' );
@@ -358,9 +366,15 @@ public function testEndAfterPingWillEndUnderlyingClient()
358
366
359
367
public function testEndAfterPingWillCloseClientWhenUnderlyingClientEmitsClose ()
360
368
{
361
- $ client = $ this ->createCallableMockWithOriginalConstructorDisabled (array ('__call ' , 'end ' ));
369
+ $ closeHandler = null ;
370
+ $ client = $ this ->getMockBuilder ('Clue\React\Redis\Client ' )->getMock ();
362
371
$ client ->expects ($ this ->once ())->method ('__call ' )->with ('ping ' )->willReturn (\React \Promise \resolve ('PONG ' ));
363
372
$ client ->expects ($ this ->once ())->method ('end ' );
373
+ $ client ->expects ($ this ->any ())->method ('on ' )->willReturnCallback (function ($ event , $ callback ) use (&$ closeHandler ) {
374
+ if ($ event === 'close ' ) {
375
+ $ closeHandler = $ callback ;
376
+ }
377
+ });
364
378
365
379
$ deferred = new Deferred ();
366
380
$ this ->factory ->expects ($ this ->once ())->method ('createClient ' )->willReturn ($ deferred ->promise ());
@@ -371,14 +385,15 @@ public function testEndAfterPingWillCloseClientWhenUnderlyingClientEmitsClose()
371
385
$ this ->client ->on ('close ' , $ this ->expectCallableOnce ());
372
386
$ this ->client ->end ();
373
387
374
- $ client ->emit ('close ' );
388
+ $ this ->assertTrue (is_callable ($ closeHandler ));
389
+ $ closeHandler ();
375
390
}
376
391
377
392
public function testEmitsNoErrorEventWhenUnderlyingClientEmitsError ()
378
393
{
379
394
$ error = new \RuntimeException ();
380
395
381
- $ client = $ this ->createCallableMockWithOriginalConstructorDisabled ( array ( ' __call ' ) );
396
+ $ client = $ this ->getMockBuilder ( ' Clue\React\Redis\Client ' )-> getMock ( );
382
397
$ client ->expects ($ this ->once ())->method ('__call ' )->willReturn (\React \Promise \resolve ());
383
398
384
399
$ deferred = new Deferred ();
@@ -393,7 +408,7 @@ public function testEmitsNoErrorEventWhenUnderlyingClientEmitsError()
393
408
394
409
public function testEmitsNoCloseEventWhenUnderlyingClientEmitsClose ()
395
410
{
396
- $ client = $ this ->createCallableMockWithOriginalConstructorDisabled ( array ( ' __call ' ) );
411
+ $ client = $ this ->getMockBuilder ( ' Clue\React\Redis\Client ' )-> getMock ( );
397
412
$ client ->expects ($ this ->once ())->method ('__call ' )->willReturn (\React \Promise \resolve ());
398
413
399
414
$ deferred = new Deferred ();
@@ -408,9 +423,16 @@ public function testEmitsNoCloseEventWhenUnderlyingClientEmitsClose()
408
423
409
424
public function testEmitsNoCloseEventButWillCancelIdleTimerWhenUnderlyingConnectionEmitsCloseAfterPingIsAlreadyResolved ()
410
425
{
426
+ $ closeHandler = null ;
427
+ $ client = $ this ->getMockBuilder ('Clue\React\Redis\Client ' )->getMock ();
411
428
$ deferred = new Deferred ();
412
- $ client = $ this ->createCallableMockWithOriginalConstructorDisabled (array ('__call ' ));
413
429
$ client ->expects ($ this ->once ())->method ('__call ' )->willReturn ($ deferred ->promise ());
430
+ $ client ->expects ($ this ->any ())->method ('on ' )->withConsecutive (
431
+ array ('close ' , $ this ->callback (function ($ arg ) use (&$ closeHandler ) {
432
+ $ closeHandler = $ arg ;
433
+ return true ;
434
+ }))
435
+ );
414
436
415
437
$ this ->factory ->expects ($ this ->once ())->method ('createClient ' )->willReturn (\React \Promise \resolve ($ client ));
416
438
@@ -423,13 +445,20 @@ public function testEmitsNoCloseEventButWillCancelIdleTimerWhenUnderlyingConnect
423
445
$ this ->client ->ping ();
424
446
$ deferred ->resolve ();
425
447
426
- $ client ->emit ('close ' );
448
+ $ this ->assertTrue (is_callable ($ closeHandler ));
449
+ $ closeHandler ();
427
450
}
428
451
429
452
public function testEmitsMessageEventWhenUnderlyingClientEmitsMessageForPubSubChannel ()
430
453
{
431
- $ client = $ this ->createCallableMockWithOriginalConstructorDisabled (array ('__call ' ));
454
+ $ messageHandler = null ;
455
+ $ client = $ this ->getMockBuilder ('Clue\React\Redis\Client ' )->getMock ();
432
456
$ client ->expects ($ this ->once ())->method ('__call ' )->willReturn (\React \Promise \resolve ());
457
+ $ client ->expects ($ this ->any ())->method ('on ' )->willReturnCallback (function ($ event , $ callback ) use (&$ messageHandler ) {
458
+ if ($ event === 'message ' ) {
459
+ $ messageHandler = $ callback ;
460
+ }
461
+ });
433
462
434
463
$ deferred = new Deferred ();
435
464
$ this ->factory ->expects ($ this ->once ())->method ('createClient ' )->willReturn ($ deferred ->promise ());
@@ -438,74 +467,108 @@ public function testEmitsMessageEventWhenUnderlyingClientEmitsMessageForPubSubCh
438
467
$ deferred ->resolve ($ client );
439
468
440
469
$ this ->client ->on ('message ' , $ this ->expectCallableOnce ());
441
- $ client ->emit ('message ' , array ('foo ' , 'bar ' ));
470
+ $ this ->assertTrue (is_callable ($ messageHandler ));
471
+ $ messageHandler ('foo ' , 'bar ' );
442
472
}
443
473
444
474
public function testEmitsUnsubscribeAndPunsubscribeEventsWhenUnderlyingClientClosesWhileUsingPubSubChannel ()
445
475
{
446
- $ client = $ this ->createCallableMockWithOriginalConstructorDisabled (array ('__call ' ));
476
+ $ allHandler = null ;
477
+ $ client = $ this ->getMockBuilder ('Clue\React\Redis\Client ' )->getMock ();
447
478
$ client ->expects ($ this ->exactly (6 ))->method ('__call ' )->willReturn (\React \Promise \resolve ());
479
+ $ client ->expects ($ this ->any ())->method ('on ' )->willReturnCallback (function ($ event , $ callback ) use (&$ allHandler ) {
480
+ if (!isset ($ allHandler [$ event ])) {
481
+ $ allHandler [$ event ] = $ callback ;
482
+ }
483
+ });
448
484
449
485
$ this ->factory ->expects ($ this ->once ())->method ('createClient ' )->willReturn (\React \Promise \resolve ($ client ));
450
486
451
487
$ this ->client ->subscribe ('foo ' );
452
- $ client ->emit ('subscribe ' , array ('foo ' , 1 ));
488
+ $ this ->assertTrue (is_callable ($ allHandler ['subscribe ' ]));
489
+ $ allHandler ['subscribe ' ]('foo ' , 1 );
453
490
454
491
$ this ->client ->subscribe ('bar ' );
455
- $ client ->emit ('subscribe ' , array ('bar ' , 2 ));
492
+ $ this ->assertTrue (is_callable ($ allHandler ['subscribe ' ]));
493
+ $ allHandler ['subscribe ' ]('bar ' , 2 );
456
494
457
495
$ this ->client ->unsubscribe ('bar ' );
458
- $ client ->emit ('unsubscribe ' , array ('bar ' , 1 ));
496
+ $ this ->assertTrue (is_callable ($ allHandler ['unsubscribe ' ]));
497
+ $ allHandler ['unsubscribe ' ]('bar ' , 1 );
459
498
460
499
$ this ->client ->psubscribe ('foo* ' );
461
- $ client ->emit ('psubscribe ' , array ('foo* ' , 1 ));
500
+ $ this ->assertTrue (is_callable ($ allHandler ['psubscribe ' ]));
501
+ $ allHandler ['psubscribe ' ]('foo* ' , 1 );
462
502
463
503
$ this ->client ->psubscribe ('bar* ' );
464
- $ client ->emit ('psubscribe ' , array ('bar* ' , 2 ));
504
+ $ this ->assertTrue (is_callable ($ allHandler ['psubscribe ' ]));
505
+ $ allHandler ['psubscribe ' ]('bar* ' , 2 );
465
506
466
507
$ this ->client ->punsubscribe ('bar* ' );
467
- $ client ->emit ('punsubscribe ' , array ('bar* ' , 1 ));
508
+ $ this ->assertTrue (is_callable ($ allHandler ['punsubscribe ' ]));
509
+ $ allHandler ['punsubscribe ' ]('bar* ' , 1 );
468
510
469
511
$ this ->client ->on ('unsubscribe ' , $ this ->expectCallableOnce ());
470
512
$ this ->client ->on ('punsubscribe ' , $ this ->expectCallableOnce ());
471
- $ client ->emit ('close ' );
513
+
514
+ $ this ->assertTrue (is_callable ($ allHandler ['close ' ]));
515
+ $ allHandler ['close ' ]();
472
516
}
473
517
474
518
public function testSubscribeWillResolveWhenUnderlyingClientResolvesSubscribeAndNotStartIdleTimerWithIdleDueToSubscription ()
475
519
{
520
+ $ subscribeHandler = null ;
476
521
$ deferred = new Deferred ();
477
- $ client = $ this ->createCallableMockWithOriginalConstructorDisabled ( array ( ' __call ' ) );
522
+ $ client = $ this ->getMockBuilder ( ' Clue\React\Redis\Client ' )-> getMock ( );
478
523
$ client ->expects ($ this ->once ())->method ('__call ' )->with ('subscribe ' )->willReturn ($ deferred ->promise ());
524
+ $ client ->expects ($ this ->any ())->method ('on ' )->willReturnCallback (function ($ event , $ callback ) use (&$ subscribeHandler ) {
525
+ if ($ event === 'subscribe ' && $ subscribeHandler === null ) {
526
+ $ subscribeHandler = $ callback ;
527
+ }
528
+ });
479
529
480
530
$ this ->factory ->expects ($ this ->once ())->method ('createClient ' )->willReturn (\React \Promise \resolve ($ client ));
481
531
482
532
$ this ->loop ->expects ($ this ->never ())->method ('addTimer ' );
483
533
484
534
$ promise = $ this ->client ->subscribe ('foo ' );
485
- $ client ->emit ('subscribe ' , array ('foo ' , 1 ));
535
+ $ this ->assertTrue (is_callable ($ subscribeHandler ));
536
+ $ subscribeHandler ('foo ' , 1 );
486
537
$ deferred ->resolve (array ('subscribe ' , 'foo ' , 1 ));
487
538
488
539
$ promise ->then ($ this ->expectCallableOnceWith (array ('subscribe ' , 'foo ' , 1 )));
489
540
}
490
541
491
542
public function testUnsubscribeAfterSubscribeWillResolveWhenUnderlyingClientResolvesUnsubscribeAndStartIdleTimerWhenSubscriptionStopped ()
492
543
{
544
+ $ subscribeHandler = null ;
545
+ $ unsubscribeHandler = null ;
493
546
$ deferredSubscribe = new Deferred ();
494
547
$ deferredUnsubscribe = new Deferred ();
495
- $ client = $ this ->createCallableMockWithOriginalConstructorDisabled ( array ( ' __call ' ) );
548
+ $ client = $ this ->getMockBuilder ( ' Clue\React\Redis\Client ' )-> getMock ( );
496
549
$ client ->expects ($ this ->exactly (2 ))->method ('__call ' )->willReturnOnConsecutiveCalls ($ deferredSubscribe ->promise (), $ deferredUnsubscribe ->promise ());
550
+ $ client ->expects ($ this ->any ())->method ('on ' )->willReturnCallback (function ($ event , $ callback ) use (&$ subscribeHandler , &$ unsubscribeHandler ) {
551
+ if ($ event === 'subscribe ' && $ subscribeHandler === null ) {
552
+ $ subscribeHandler = $ callback ;
553
+ }
554
+ if ($ event === 'unsubscribe ' && $ unsubscribeHandler === null ) {
555
+ $ unsubscribeHandler = $ callback ;
556
+ }
557
+ });
497
558
498
559
$ this ->factory ->expects ($ this ->once ())->method ('createClient ' )->willReturn (\React \Promise \resolve ($ client ));
499
560
500
561
$ this ->loop ->expects ($ this ->once ())->method ('addTimer ' );
501
562
502
563
$ promise = $ this ->client ->subscribe ('foo ' );
503
- $ client ->emit ('subscribe ' , array ('foo ' , 1 ));
564
+ $ this ->assertTrue (is_callable ($ subscribeHandler ));
565
+ $ subscribeHandler ('foo ' , 1 );
504
566
$ deferredSubscribe ->resolve (array ('subscribe ' , 'foo ' , 1 ));
505
567
$ promise ->then ($ this ->expectCallableOnceWith (array ('subscribe ' , 'foo ' , 1 )));
506
568
507
569
$ promise = $ this ->client ->unsubscribe ('foo ' );
508
- $ client ->emit ('unsubscribe ' , array ('foo ' , 0 ));
570
+ $ this ->assertTrue (is_callable ($ unsubscribeHandler ));
571
+ $ unsubscribeHandler ('foo ' , 0 );
509
572
$ deferredUnsubscribe ->resolve (array ('unsubscribe ' , 'foo ' , 0 ));
510
573
$ promise ->then ($ this ->expectCallableOnceWith (array ('unsubscribe ' , 'foo ' , 0 )));
511
574
}
0 commit comments