Skip to content

Commit 5371af5

Browse files
authored
Merge pull request #152 from clue-labs/v2-promise-v3
[2.x] Forward compatibility with Promise v3
2 parents 39e156d + 0b9f524 commit 5371af5

File tree

6 files changed

+36
-22
lines changed

6 files changed

+36
-22
lines changed

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
"clue/redis-protocol": "0.3.*",
1616
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
1717
"react/event-loop": "^1.2",
18-
"react/promise": "^2.0 || ^1.1",
19-
"react/promise-timer": "^1.8",
20-
"react/socket": "^1.9"
18+
"react/promise": "^3 || ^2.0 || ^1.1",
19+
"react/promise-timer": "^1.9",
20+
"react/socket": "^1.12"
2121
},
2222
"require-dev": {
23-
"clue/block-react": "^1.1",
23+
"clue/block-react": "^1.5",
2424
"phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36"
2525
},
2626
"autoload": {

src/Factory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ public function createClient($uri)
8484
// either close successful connection or cancel pending connection attempt
8585
$connecting->then(function (ConnectionInterface $connection) {
8686
$connection->close();
87+
}, function () {
88+
// ignore to avoid reporting unhandled rejection
8789
});
8890
$connecting->cancel();
8991
});

src/LazyClient.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ public function close()
168168
if ($this->promise !== null) {
169169
$this->promise->then(function (Client $redis) {
170170
$redis->close();
171+
}, function () {
172+
// ignore to avoid reporting unhandled rejection
171173
});
172174
if ($this->promise !== null) {
173175
$this->promise->cancel();

tests/FactoryLazyClientTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ public function testConstructWithoutLoopAssignsLoopAutomatically()
3434

3535
public function testWillConnectWithDefaultPort()
3636
{
37-
$this->connector->expects($this->never())->method('connect')->with('redis.example.com:6379')->willReturn(Promise\reject(new \RuntimeException()));
37+
$this->connector->expects($this->never())->method('connect');
3838
$this->factory->createLazyClient('redis.example.com');
3939
}
4040

4141
public function testWillConnectToLocalhost()
4242
{
43-
$this->connector->expects($this->never())->method('connect')->with('localhost:1337')->willReturn(Promise\reject(new \RuntimeException()));
43+
$this->connector->expects($this->never())->method('connect');
4444
$this->factory->createLazyClient('localhost:1337');
4545
}
4646

@@ -147,7 +147,7 @@ public function testWillWriteSelectCommandIfRedisUnixUriContainsDbQueryParameter
147147

148148
public function testWillRejectIfConnectorRejects()
149149
{
150-
$this->connector->expects($this->never())->method('connect')->with('127.0.0.1:2')->willReturn(Promise\reject(new \RuntimeException()));
150+
$this->connector->expects($this->never())->method('connect');
151151
$redis = $this->factory->createLazyClient('redis://127.0.0.1:2');
152152

153153
$this->assertInstanceOf('Clue\React\Redis\Client', $redis);

tests/FactoryStreamingClientTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,17 @@ public function testCtor()
4444
public function testWillConnectWithDefaultPort()
4545
{
4646
$this->connector->expects($this->once())->method('connect')->with('redis.example.com:6379')->willReturn(Promise\reject(new \RuntimeException()));
47-
$this->factory->createClient('redis.example.com');
47+
$promise = $this->factory->createClient('redis.example.com');
48+
49+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
4850
}
4951

5052
public function testWillConnectToLocalhost()
5153
{
5254
$this->connector->expects($this->once())->method('connect')->with('localhost:1337')->willReturn(Promise\reject(new \RuntimeException()));
53-
$this->factory->createClient('localhost:1337');
55+
$promise = $this->factory->createClient('localhost:1337');
56+
57+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
5458
}
5559

5660
public function testWillResolveIfConnectorResolves()

tests/LazyClientTest.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,10 @@ public function testPingAfterPreviousFactoryRejectsUnderlyingClientWillCreateNew
148148
new Promise(function () { })
149149
);
150150

151-
$this->redis->ping();
151+
$promise = $this->redis->ping();
152+
153+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
154+
152155
$deferred->reject($error);
153156

154157
$this->redis->ping();
@@ -213,7 +216,7 @@ public function testPingAfterPingWillNotStartIdleTimerWhenFirstPingResolves()
213216

214217
$this->redis->ping();
215218
$this->redis->ping();
216-
$deferred->resolve();
219+
$deferred->resolve(null);
217220
}
218221

219222
public function testPingAfterPingWillStartAndCancelIdleTimerWhenSecondPingStartsAfterFirstResolves()
@@ -232,15 +235,15 @@ public function testPingAfterPingWillStartAndCancelIdleTimerWhenSecondPingStarts
232235
$this->loop->expects($this->once())->method('cancelTimer')->with($timer);
233236

234237
$this->redis->ping();
235-
$deferred->resolve();
238+
$deferred->resolve(null);
236239
$this->redis->ping();
237240
}
238241

239242
public function testPingFollowedByIdleTimerWillCloseUnderlyingConnectionWithoutCloseEvent()
240243
{
241244
$client = $this->getMockBuilder('Clue\React\Redis\Client')->getMock();
242-
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve());
243-
$client->expects($this->once())->method('close')->willReturn(\React\Promise\resolve());
245+
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve(null));
246+
$client->expects($this->once())->method('close');
244247

245248
$this->factory->expects($this->once())->method('createClient')->willReturn(\React\Promise\resolve($client));
246249

@@ -295,14 +298,17 @@ public function testCloseAfterPingWillEmitCloseWithoutErrorWhenUnderlyingClientC
295298
$this->redis->on('error', $this->expectCallableNever());
296299
$this->redis->on('close', $this->expectCallableOnce());
297300

298-
$this->redis->ping();
301+
$promise = $this->redis->ping();
302+
303+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
304+
299305
$this->redis->close();
300306
}
301307

302308
public function testCloseAfterPingWillCloseUnderlyingClientConnectionWhenAlreadyResolved()
303309
{
304310
$client = $this->getMockBuilder('Clue\React\Redis\Client')->getMock();
305-
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve());
311+
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve(null));
306312
$client->expects($this->once())->method('close');
307313

308314
$deferred = new Deferred();
@@ -327,7 +333,7 @@ public function testCloseAfterPingWillCancelIdleTimerWhenPingIsAlreadyResolved()
327333
$this->loop->expects($this->once())->method('cancelTimer')->with($timer);
328334

329335
$this->redis->ping();
330-
$deferred->resolve();
336+
$deferred->resolve(null);
331337
$this->redis->close();
332338
}
333339

@@ -404,7 +410,7 @@ public function testEmitsNoErrorEventWhenUnderlyingClientEmitsError()
404410
$error = new \RuntimeException();
405411

406412
$client = $this->getMockBuilder('Clue\React\Redis\Client')->getMock();
407-
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve());
413+
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve(null));
408414

409415
$deferred = new Deferred();
410416
$this->factory->expects($this->once())->method('createClient')->willReturn($deferred->promise());
@@ -419,7 +425,7 @@ public function testEmitsNoErrorEventWhenUnderlyingClientEmitsError()
419425
public function testEmitsNoCloseEventWhenUnderlyingClientEmitsClose()
420426
{
421427
$client = $this->getMockBuilder('Clue\React\Redis\Client')->getMock();
422-
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve());
428+
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve(null));
423429

424430
$deferred = new Deferred();
425431
$this->factory->expects($this->once())->method('createClient')->willReturn($deferred->promise());
@@ -453,7 +459,7 @@ public function testEmitsNoCloseEventButWillCancelIdleTimerWhenUnderlyingConnect
453459
$this->redis->on('close', $this->expectCallableNever());
454460

455461
$this->redis->ping();
456-
$deferred->resolve();
462+
$deferred->resolve(null);
457463

458464
$this->assertTrue(is_callable($closeHandler));
459465
$closeHandler();
@@ -463,7 +469,7 @@ public function testEmitsMessageEventWhenUnderlyingClientEmitsMessageForPubSubCh
463469
{
464470
$messageHandler = null;
465471
$client = $this->getMockBuilder('Clue\React\Redis\Client')->getMock();
466-
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve());
472+
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve(null));
467473
$client->expects($this->any())->method('on')->willReturnCallback(function ($event, $callback) use (&$messageHandler) {
468474
if ($event === 'message') {
469475
$messageHandler = $callback;
@@ -485,7 +491,7 @@ public function testEmitsUnsubscribeAndPunsubscribeEventsWhenUnderlyingClientClo
485491
{
486492
$allHandler = null;
487493
$client = $this->getMockBuilder('Clue\React\Redis\Client')->getMock();
488-
$client->expects($this->exactly(6))->method('__call')->willReturn(\React\Promise\resolve());
494+
$client->expects($this->exactly(6))->method('__call')->willReturn(\React\Promise\resolve(null));
489495
$client->expects($this->any())->method('on')->willReturnCallback(function ($event, $callback) use (&$allHandler) {
490496
if (!isset($allHandler[$event])) {
491497
$allHandler[$event] = $callback;

0 commit comments

Comments
 (0)