Skip to content

Commit 838e284

Browse files
committed
Fix Race Condition in tests and prepare PHPUnit 10
1 parent 68458a7 commit 838e284

File tree

3 files changed

+45
-22
lines changed

3 files changed

+45
-22
lines changed

tests/FactoryStreamingClientTest.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function testWillWriteAuthCommandIfRedisUnixUriContainsUserInfo()
136136

137137
public function testWillResolveWhenAuthCommandReceivesOkResponseIfRedisUriContainsUserInfo()
138138
{
139-
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write'))->getMock();
139+
$stream = $this->createCallableMockWithOriginalConstructorDisabled(array('write'));
140140
$stream->expects($this->once())->method('write')->with("*2\r\n$4\r\nauth\r\n$5\r\nworld\r\n");
141141

142142
$this->connector->expects($this->once())->method('connect')->willReturn(Promise\resolve($stream));
@@ -149,7 +149,7 @@ public function testWillResolveWhenAuthCommandReceivesOkResponseIfRedisUriContai
149149

150150
public function testWillRejectAndCloseAutomaticallyWhenAuthCommandReceivesErrorResponseIfRedisUriContainsUserInfo()
151151
{
152-
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write', 'close'))->getMock();
152+
$stream = $this->createCallableMockWithOriginalConstructorDisabled(array('write', 'close'));
153153
$stream->expects($this->once())->method('write')->with("*2\r\n$4\r\nauth\r\n$5\r\nworld\r\n");
154154
$stream->expects($this->once())->method('close');
155155

@@ -182,7 +182,7 @@ public function testWillWriteSelectCommandIfRedisUnixUriContainsDbQueryParameter
182182

183183
public function testWillResolveWhenSelectCommandReceivesOkResponseIfRedisUriContainsPath()
184184
{
185-
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write'))->getMock();
185+
$stream = $this->createCallableMockWithOriginalConstructorDisabled(array('write'));
186186
$stream->expects($this->once())->method('write')->with("*2\r\n$6\r\nselect\r\n$3\r\n123\r\n");
187187

188188
$this->connector->expects($this->once())->method('connect')->willReturn(Promise\resolve($stream));
@@ -195,7 +195,7 @@ public function testWillResolveWhenSelectCommandReceivesOkResponseIfRedisUriCont
195195

196196
public function testWillRejectAndCloseAutomaticallyWhenSelectCommandReceivesErrorResponseIfRedisUriContainsPath()
197197
{
198-
$stream = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('write', 'close'))->getMock();
198+
$stream = $this->createCallableMockWithOriginalConstructorDisabled(array('write', 'close'));
199199
$stream->expects($this->once())->method('write')->with("*2\r\n$6\r\nselect\r\n$3\r\n123\r\n");
200200
$stream->expects($this->once())->method('close');
201201

@@ -337,4 +337,15 @@ public function testCreateClientWithoutTimeoutParameterWillStartTimerWithDefault
337337
$this->factory->createClient('redis://127.0.0.1:2');
338338
ini_set('default_socket_timeout', $old);
339339
}
340+
341+
public function createCallableMockWithOriginalConstructorDisabled($array)
342+
{
343+
if (method_exists('PHPUnit\Framework\MockObject\MockBuilder', 'addMethods')) {
344+
// PHPUnit 9+
345+
return $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->onlyMethods($array)->getMock();
346+
} else {
347+
// legacy PHPUnit 4 - PHPUnit 8
348+
return $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods($array)->getMock();
349+
}
350+
}
340351
}

tests/FunctionalTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,11 @@ public function testPubSub()
149149
$deferred = new Deferred();
150150
$consumer->on('message', $this->expectCallableOnce());
151151
$consumer->on('message', array($deferred, 'resolve'));
152-
$consumer->subscribe($channel)->then($this->expectCallableOnce());
153-
154-
// producer sends a single message
155-
$producer->publish($channel, 'hello world')->then($this->expectCallableOnceWith(1));
152+
$once = $this->expectCallableOnceWith(1);
153+
$consumer->subscribe($channel)->then(function() use ($producer, $channel, $once){
154+
// producer sends a single message
155+
$producer->publish($channel, 'hello world')->then($once);
156+
})->then($this->expectCallableOnce());
156157

157158
// expect "message" event to take no longer than 0.1s
158159
Block\await($deferred->promise(), $this->loop, 0.1);

tests/LazyClientTest.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function testPingAfterPreviousFactoryRejectsUnderlyingClientWillCreateNew
156156

157157
public function testPingAfterPreviousUnderlyingClientAlreadyClosedWillCreateNewUnderlyingConnection()
158158
{
159-
$client = $this->getMockBuilder('Clue\React\Redis\StreamingClient')->disableOriginalConstructor()->setMethods(array('__call'))->getMock();
159+
$client = $this->createCallableMockWithOriginalConstructorDisabled(array('__call'));
160160
$client->expects($this->once())->method('__call')->with('ping')->willReturn(\React\Promise\resolve('PONG'));
161161

162162
$this->factory->expects($this->exactly(2))->method('createClient')->willReturnOnConsecutiveCalls(
@@ -183,7 +183,7 @@ public function testPingAfterCloseWillRejectWithoutCreatingUnderlyingConnection(
183183
public function testPingAfterPingWillNotStartIdleTimerWhenFirstPingResolves()
184184
{
185185
$deferred = new Deferred();
186-
$client = $this->getMockBuilder('Clue\React\Redis\StreamingClient')->disableOriginalConstructor()->setMethods(array('__call'))->getMock();
186+
$client = $this->createCallableMockWithOriginalConstructorDisabled(array('__call'));
187187
$client->expects($this->exactly(2))->method('__call')->willReturnOnConsecutiveCalls(
188188
$deferred->promise(),
189189
new Promise(function () { })
@@ -201,7 +201,7 @@ public function testPingAfterPingWillNotStartIdleTimerWhenFirstPingResolves()
201201
public function testPingAfterPingWillStartAndCancelIdleTimerWhenSecondPingStartsAfterFirstResolves()
202202
{
203203
$deferred = new Deferred();
204-
$client = $this->getMockBuilder('Clue\React\Redis\StreamingClient')->disableOriginalConstructor()->setMethods(array('__call'))->getMock();
204+
$client = $this->createCallableMockWithOriginalConstructorDisabled(array('__call'));
205205
$client->expects($this->exactly(2))->method('__call')->willReturnOnConsecutiveCalls(
206206
$deferred->promise(),
207207
new Promise(function () { })
@@ -220,7 +220,7 @@ public function testPingAfterPingWillStartAndCancelIdleTimerWhenSecondPingStarts
220220

221221
public function testPingFollowedByIdleTimerWillCloseUnderlyingConnectionWithoutCloseEvent()
222222
{
223-
$client = $this->getMockBuilder('Clue\React\Redis\StreamingClient')->disableOriginalConstructor()->setMethods(array('__call', 'close'))->getMock();
223+
$client = $this->createCallableMockWithOriginalConstructorDisabled(array('__call', 'close'));
224224
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve());
225225
$client->expects($this->once())->method('close')->willReturn(\React\Promise\resolve());
226226

@@ -298,7 +298,7 @@ public function testCloseAfterPingWillCloseUnderlyingClientConnectionWhenAlready
298298
public function testCloseAfterPingWillCancelIdleTimerWhenPingIsAlreadyResolved()
299299
{
300300
$deferred = new Deferred();
301-
$client = $this->getMockBuilder('Clue\React\Redis\StreamingClient')->disableOriginalConstructor()->setMethods(array('__call', 'close'))->getMock();
301+
$client = $this->createCallableMockWithOriginalConstructorDisabled(array('__call', 'close'));
302302
$client->expects($this->once())->method('__call')->willReturn($deferred->promise());
303303
$client->expects($this->once())->method('close');
304304

@@ -316,7 +316,7 @@ public function testCloseAfterPingWillCancelIdleTimerWhenPingIsAlreadyResolved()
316316
public function testCloseAfterPingRejectsWillEmitClose()
317317
{
318318
$deferred = new Deferred();
319-
$client = $this->getMockBuilder('Clue\React\Redis\StreamingClient')->disableOriginalConstructor()->setMethods(array('__call', 'close'))->getMock();
319+
$client = $this->createCallableMockWithOriginalConstructorDisabled(array('__call', 'close'));
320320
$client->expects($this->once())->method('__call')->willReturn($deferred->promise());
321321
$client->expects($this->once())->method('close')->willReturnCallback(function () use ($client) {
322322
$client->emit('close');
@@ -358,7 +358,7 @@ public function testEndAfterPingWillEndUnderlyingClient()
358358

359359
public function testEndAfterPingWillCloseClientWhenUnderlyingClientEmitsClose()
360360
{
361-
$client = $this->getMockBuilder('Clue\React\Redis\StreamingClient')->disableOriginalConstructor()->setMethods(array('__call', 'end'))->getMock();
361+
$client = $this->createCallableMockWithOriginalConstructorDisabled(array('__call', 'end'));
362362
$client->expects($this->once())->method('__call')->with('ping')->willReturn(\React\Promise\resolve('PONG'));
363363
$client->expects($this->once())->method('end');
364364

@@ -378,7 +378,7 @@ public function testEmitsNoErrorEventWhenUnderlyingClientEmitsError()
378378
{
379379
$error = new \RuntimeException();
380380

381-
$client = $this->getMockBuilder('Clue\React\Redis\StreamingClient')->disableOriginalConstructor()->setMethods(array('__call'))->getMock();
381+
$client = $this->createCallableMockWithOriginalConstructorDisabled(array('__call'));
382382
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve());
383383

384384
$deferred = new Deferred();
@@ -393,7 +393,7 @@ public function testEmitsNoErrorEventWhenUnderlyingClientEmitsError()
393393

394394
public function testEmitsNoCloseEventWhenUnderlyingClientEmitsClose()
395395
{
396-
$client = $this->getMockBuilder('Clue\React\Redis\StreamingClient')->disableOriginalConstructor()->setMethods(array('__call'))->getMock();
396+
$client = $this->createCallableMockWithOriginalConstructorDisabled(array('__call'));
397397
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve());
398398

399399
$deferred = new Deferred();
@@ -409,7 +409,7 @@ public function testEmitsNoCloseEventWhenUnderlyingClientEmitsClose()
409409
public function testEmitsNoCloseEventButWillCancelIdleTimerWhenUnderlyingConnectionEmitsCloseAfterPingIsAlreadyResolved()
410410
{
411411
$deferred = new Deferred();
412-
$client = $this->getMockBuilder('Clue\React\Redis\StreamingClient')->disableOriginalConstructor()->setMethods(array('__call'))->getMock();
412+
$client = $this->createCallableMockWithOriginalConstructorDisabled(array('__call'));
413413
$client->expects($this->once())->method('__call')->willReturn($deferred->promise());
414414

415415
$this->factory->expects($this->once())->method('createClient')->willReturn(\React\Promise\resolve($client));
@@ -428,7 +428,7 @@ public function testEmitsNoCloseEventButWillCancelIdleTimerWhenUnderlyingConnect
428428

429429
public function testEmitsMessageEventWhenUnderlyingClientEmitsMessageForPubSubChannel()
430430
{
431-
$client = $this->getMockBuilder('Clue\React\Redis\StreamingClient')->disableOriginalConstructor()->setMethods(array('__call'))->getMock();
431+
$client = $this->createCallableMockWithOriginalConstructorDisabled(array('__call'));
432432
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve());
433433

434434
$deferred = new Deferred();
@@ -443,7 +443,7 @@ public function testEmitsMessageEventWhenUnderlyingClientEmitsMessageForPubSubCh
443443

444444
public function testEmitsUnsubscribeAndPunsubscribeEventsWhenUnderlyingClientClosesWhileUsingPubSubChannel()
445445
{
446-
$client = $this->getMockBuilder('Clue\React\Redis\StreamingClient')->disableOriginalConstructor()->setMethods(array('__call'))->getMock();
446+
$client = $this->createCallableMockWithOriginalConstructorDisabled(array('__call'));
447447
$client->expects($this->exactly(6))->method('__call')->willReturn(\React\Promise\resolve());
448448

449449
$this->factory->expects($this->once())->method('createClient')->willReturn(\React\Promise\resolve($client));
@@ -474,7 +474,7 @@ public function testEmitsUnsubscribeAndPunsubscribeEventsWhenUnderlyingClientClo
474474
public function testSubscribeWillResolveWhenUnderlyingClientResolvesSubscribeAndNotStartIdleTimerWithIdleDueToSubscription()
475475
{
476476
$deferred = new Deferred();
477-
$client = $this->getMockBuilder('Clue\React\Redis\StreamingClient')->disableOriginalConstructor()->setMethods(array('__call'))->getMock();
477+
$client = $this->createCallableMockWithOriginalConstructorDisabled(array('__call'));
478478
$client->expects($this->once())->method('__call')->with('subscribe')->willReturn($deferred->promise());
479479

480480
$this->factory->expects($this->once())->method('createClient')->willReturn(\React\Promise\resolve($client));
@@ -492,7 +492,7 @@ public function testUnsubscribeAfterSubscribeWillResolveWhenUnderlyingClientReso
492492
{
493493
$deferredSubscribe = new Deferred();
494494
$deferredUnsubscribe = new Deferred();
495-
$client = $this->getMockBuilder('Clue\React\Redis\StreamingClient')->disableOriginalConstructor()->setMethods(array('__call'))->getMock();
495+
$client = $this->createCallableMockWithOriginalConstructorDisabled(array('__call'));
496496
$client->expects($this->exactly(2))->method('__call')->willReturnOnConsecutiveCalls($deferredSubscribe->promise(), $deferredUnsubscribe->promise());
497497

498498
$this->factory->expects($this->once())->method('createClient')->willReturn(\React\Promise\resolve($client));
@@ -509,4 +509,15 @@ public function testUnsubscribeAfterSubscribeWillResolveWhenUnderlyingClientReso
509509
$deferredUnsubscribe->resolve(array('unsubscribe', 'foo', 0));
510510
$promise->then($this->expectCallableOnceWith(array('unsubscribe', 'foo', 0)));
511511
}
512+
513+
public function createCallableMockWithOriginalConstructorDisabled($array)
514+
{
515+
if (method_exists('PHPUnit\Framework\MockObject\MockBuilder', 'addMethods')) {
516+
// PHPUnit 9+
517+
return $this->getMockBuilder('Clue\React\Redis\StreamingClient')->disableOriginalConstructor()->onlyMethods($array)->getMock();
518+
} else {
519+
// legacy PHPUnit 4 - PHPUnit 8
520+
return $this->getMockBuilder('Clue\React\Redis\StreamingClient')->disableOriginalConstructor()->setMethods($array)->getMock();
521+
}
522+
}
512523
}

0 commit comments

Comments
 (0)