From 0b9f5243b41823b68711ac4fb4cde5ce30c5ad42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Mon, 18 Dec 2023 10:23:23 +0100 Subject: [PATCH] Forward compatibility with Promise v3 --- composer.json | 8 +++---- src/Factory.php | 2 ++ src/LazyClient.php | 2 ++ tests/FactoryLazyClientTest.php | 6 +++--- tests/FactoryStreamingClientTest.php | 8 +++++-- tests/LazyClientTest.php | 32 +++++++++++++++++----------- 6 files changed, 36 insertions(+), 22 deletions(-) diff --git a/composer.json b/composer.json index 35743e7..82dba29 100644 --- a/composer.json +++ b/composer.json @@ -15,12 +15,12 @@ "clue/redis-protocol": "0.3.*", "evenement/evenement": "^3.0 || ^2.0 || ^1.0", "react/event-loop": "^1.2", - "react/promise": "^2.0 || ^1.1", - "react/promise-timer": "^1.8", - "react/socket": "^1.9" + "react/promise": "^3 || ^2.0 || ^1.1", + "react/promise-timer": "^1.9", + "react/socket": "^1.12" }, "require-dev": { - "clue/block-react": "^1.1", + "clue/block-react": "^1.5", "phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36" }, "autoload": { diff --git a/src/Factory.php b/src/Factory.php index 4e94905..1493057 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -84,6 +84,8 @@ public function createClient($uri) // either close successful connection or cancel pending connection attempt $connecting->then(function (ConnectionInterface $connection) { $connection->close(); + }, function () { + // ignore to avoid reporting unhandled rejection }); $connecting->cancel(); }); diff --git a/src/LazyClient.php b/src/LazyClient.php index d82b257..0e42cc8 100644 --- a/src/LazyClient.php +++ b/src/LazyClient.php @@ -168,6 +168,8 @@ public function close() if ($this->promise !== null) { $this->promise->then(function (Client $redis) { $redis->close(); + }, function () { + // ignore to avoid reporting unhandled rejection }); if ($this->promise !== null) { $this->promise->cancel(); diff --git a/tests/FactoryLazyClientTest.php b/tests/FactoryLazyClientTest.php index 8b5005b..fb394d1 100644 --- a/tests/FactoryLazyClientTest.php +++ b/tests/FactoryLazyClientTest.php @@ -34,13 +34,13 @@ public function testConstructWithoutLoopAssignsLoopAutomatically() public function testWillConnectWithDefaultPort() { - $this->connector->expects($this->never())->method('connect')->with('redis.example.com:6379')->willReturn(Promise\reject(new \RuntimeException())); + $this->connector->expects($this->never())->method('connect'); $this->factory->createLazyClient('redis.example.com'); } public function testWillConnectToLocalhost() { - $this->connector->expects($this->never())->method('connect')->with('localhost:1337')->willReturn(Promise\reject(new \RuntimeException())); + $this->connector->expects($this->never())->method('connect'); $this->factory->createLazyClient('localhost:1337'); } @@ -147,7 +147,7 @@ public function testWillWriteSelectCommandIfRedisUnixUriContainsDbQueryParameter public function testWillRejectIfConnectorRejects() { - $this->connector->expects($this->never())->method('connect')->with('127.0.0.1:2')->willReturn(Promise\reject(new \RuntimeException())); + $this->connector->expects($this->never())->method('connect'); $redis = $this->factory->createLazyClient('redis://127.0.0.1:2'); $this->assertInstanceOf('Clue\React\Redis\Client', $redis); diff --git a/tests/FactoryStreamingClientTest.php b/tests/FactoryStreamingClientTest.php index 882af76..6ce03a5 100644 --- a/tests/FactoryStreamingClientTest.php +++ b/tests/FactoryStreamingClientTest.php @@ -44,13 +44,17 @@ public function testCtor() public function testWillConnectWithDefaultPort() { $this->connector->expects($this->once())->method('connect')->with('redis.example.com:6379')->willReturn(Promise\reject(new \RuntimeException())); - $this->factory->createClient('redis.example.com'); + $promise = $this->factory->createClient('redis.example.com'); + + $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection } public function testWillConnectToLocalhost() { $this->connector->expects($this->once())->method('connect')->with('localhost:1337')->willReturn(Promise\reject(new \RuntimeException())); - $this->factory->createClient('localhost:1337'); + $promise = $this->factory->createClient('localhost:1337'); + + $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection } public function testWillResolveIfConnectorResolves() diff --git a/tests/LazyClientTest.php b/tests/LazyClientTest.php index 2ad644e..e1af1c6 100644 --- a/tests/LazyClientTest.php +++ b/tests/LazyClientTest.php @@ -148,7 +148,10 @@ public function testPingAfterPreviousFactoryRejectsUnderlyingClientWillCreateNew new Promise(function () { }) ); - $this->redis->ping(); + $promise = $this->redis->ping(); + + $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection + $deferred->reject($error); $this->redis->ping(); @@ -213,7 +216,7 @@ public function testPingAfterPingWillNotStartIdleTimerWhenFirstPingResolves() $this->redis->ping(); $this->redis->ping(); - $deferred->resolve(); + $deferred->resolve(null); } public function testPingAfterPingWillStartAndCancelIdleTimerWhenSecondPingStartsAfterFirstResolves() @@ -232,15 +235,15 @@ public function testPingAfterPingWillStartAndCancelIdleTimerWhenSecondPingStarts $this->loop->expects($this->once())->method('cancelTimer')->with($timer); $this->redis->ping(); - $deferred->resolve(); + $deferred->resolve(null); $this->redis->ping(); } public function testPingFollowedByIdleTimerWillCloseUnderlyingConnectionWithoutCloseEvent() { $client = $this->getMockBuilder('Clue\React\Redis\Client')->getMock(); - $client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve()); - $client->expects($this->once())->method('close')->willReturn(\React\Promise\resolve()); + $client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve(null)); + $client->expects($this->once())->method('close'); $this->factory->expects($this->once())->method('createClient')->willReturn(\React\Promise\resolve($client)); @@ -295,14 +298,17 @@ public function testCloseAfterPingWillEmitCloseWithoutErrorWhenUnderlyingClientC $this->redis->on('error', $this->expectCallableNever()); $this->redis->on('close', $this->expectCallableOnce()); - $this->redis->ping(); + $promise = $this->redis->ping(); + + $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection + $this->redis->close(); } public function testCloseAfterPingWillCloseUnderlyingClientConnectionWhenAlreadyResolved() { $client = $this->getMockBuilder('Clue\React\Redis\Client')->getMock(); - $client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve()); + $client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve(null)); $client->expects($this->once())->method('close'); $deferred = new Deferred(); @@ -327,7 +333,7 @@ public function testCloseAfterPingWillCancelIdleTimerWhenPingIsAlreadyResolved() $this->loop->expects($this->once())->method('cancelTimer')->with($timer); $this->redis->ping(); - $deferred->resolve(); + $deferred->resolve(null); $this->redis->close(); } @@ -404,7 +410,7 @@ public function testEmitsNoErrorEventWhenUnderlyingClientEmitsError() $error = new \RuntimeException(); $client = $this->getMockBuilder('Clue\React\Redis\Client')->getMock(); - $client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve()); + $client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve(null)); $deferred = new Deferred(); $this->factory->expects($this->once())->method('createClient')->willReturn($deferred->promise()); @@ -419,7 +425,7 @@ public function testEmitsNoErrorEventWhenUnderlyingClientEmitsError() public function testEmitsNoCloseEventWhenUnderlyingClientEmitsClose() { $client = $this->getMockBuilder('Clue\React\Redis\Client')->getMock(); - $client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve()); + $client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve(null)); $deferred = new Deferred(); $this->factory->expects($this->once())->method('createClient')->willReturn($deferred->promise()); @@ -453,7 +459,7 @@ public function testEmitsNoCloseEventButWillCancelIdleTimerWhenUnderlyingConnect $this->redis->on('close', $this->expectCallableNever()); $this->redis->ping(); - $deferred->resolve(); + $deferred->resolve(null); $this->assertTrue(is_callable($closeHandler)); $closeHandler(); @@ -463,7 +469,7 @@ public function testEmitsMessageEventWhenUnderlyingClientEmitsMessageForPubSubCh { $messageHandler = null; $client = $this->getMockBuilder('Clue\React\Redis\Client')->getMock(); - $client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve()); + $client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve(null)); $client->expects($this->any())->method('on')->willReturnCallback(function ($event, $callback) use (&$messageHandler) { if ($event === 'message') { $messageHandler = $callback; @@ -485,7 +491,7 @@ public function testEmitsUnsubscribeAndPunsubscribeEventsWhenUnderlyingClientClo { $allHandler = null; $client = $this->getMockBuilder('Clue\React\Redis\Client')->getMock(); - $client->expects($this->exactly(6))->method('__call')->willReturn(\React\Promise\resolve()); + $client->expects($this->exactly(6))->method('__call')->willReturn(\React\Promise\resolve(null)); $client->expects($this->any())->method('on')->willReturnCallback(function ($event, $callback) use (&$allHandler) { if (!isset($allHandler[$event])) { $allHandler[$event] = $callback;