From 4633c3384b9251830f377b64c9bee25bb0f7b40b Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Sat, 10 Feb 2024 13:15:06 +0100 Subject: [PATCH 1/3] Update README.md --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index bcb89d5..13e762b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,10 @@ + + + + + + + # RoadRunner Centrifugo Bridge [![PHP Version Require](https://poser.pugx.org/roadrunner-php/centrifugo/require/php)](https://packagist.org/packages/roadrunner-php/centrifugo) From e231d051945da3b7eb4846a0ff7adb3a815015b4 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Wed, 6 Mar 2024 14:40:26 +0200 Subject: [PATCH 2/3] Declare the reconnect parameter deprecated in the disconnect --- src/Payload/Disconnect.php | 12 ++++- src/RPCCentrifugoApi.php | 1 - src/Request/AbstractRequest.php | 6 ++- src/Request/RequestInterface.php | 3 ++ tests/Unit/RPCCentrifugoApiTest.php | 53 ++++++++++++++++++++++ tests/Unit/Request/AbstractRequestTest.php | 6 +-- 6 files changed, 75 insertions(+), 6 deletions(-) diff --git a/src/Payload/Disconnect.php b/src/Payload/Disconnect.php index 172de4f..c673773 100644 --- a/src/Payload/Disconnect.php +++ b/src/Payload/Disconnect.php @@ -6,10 +6,20 @@ final class Disconnect { + /** + * @deprecated + */ + public bool $reconnect = false; + + /** + * @param bool $reconnect This parameter is no longer used since v2.0.1 due to the removal of this option in + * centrifugal/centrifugo v5.0.0 API. It will be removed in v3.0.0. + */ public function __construct( public readonly int $code, public readonly string $reason, - public readonly bool $reconnect = false + bool $reconnect = false ) { + $this->reconnect = $reconnect; } } diff --git a/src/RPCCentrifugoApi.php b/src/RPCCentrifugoApi.php index 73b5e85..17c7d35 100644 --- a/src/RPCCentrifugoApi.php +++ b/src/RPCCentrifugoApi.php @@ -158,7 +158,6 @@ public function disconnect( new DTO\Disconnect([ 'code' => $disconnect->code, 'reason' => $disconnect->reason, - 'reconnect' => $disconnect->reconnect, ]) ); } diff --git a/src/Request/AbstractRequest.php b/src/Request/AbstractRequest.php index 40c38b7..bb3a6b2 100644 --- a/src/Request/AbstractRequest.php +++ b/src/Request/AbstractRequest.php @@ -60,11 +60,15 @@ final public function error(int $code, string $message, bool $temporary = false) $this->sendResponse($response); } + /** + * @param bool $reconnect This parameter is no longer used since v2.0.1 due to the removal of this option in + * centrifugal/centrifugo v5.0.0 API. It will be removed in v3.0.0. + */ final public function disconnect(int $code, string $reason, bool $reconnect = false): void { $response = $this->getResponseObject(); $response->setDisconnect( - new Disconnect(\compact('code', 'reason', 'reconnect')), + new Disconnect(\compact('code', 'reason')), ); $this->sendResponse($response); diff --git a/src/Request/RequestInterface.php b/src/Request/RequestInterface.php index 3e2366f..2a5e2cb 100644 --- a/src/Request/RequestInterface.php +++ b/src/Request/RequestInterface.php @@ -43,6 +43,9 @@ public function error(int $code, string $message, bool $temporary = false): void /** * Send disconnect response to Centrifugo server. + * + * @param bool $reconnect This parameter is no longer used since v2.0.1 due to the removal of this option in + * centrifugal/centrifugo v5.0.0 API. It will be removed in v3.0.0. */ public function disconnect(int $code, string $reason, bool $reconnect = false): void; } diff --git a/tests/Unit/RPCCentrifugoApiTest.php b/tests/Unit/RPCCentrifugoApiTest.php index eb277f1..12433e2 100644 --- a/tests/Unit/RPCCentrifugoApiTest.php +++ b/tests/Unit/RPCCentrifugoApiTest.php @@ -8,6 +8,7 @@ use PHPUnit\Framework\TestCase; use RoadRunner\Centrifugo\CentrifugoApiInterface; use RoadRunner\Centrifugo\Exception\CentrifugoApiResponseException; +use RoadRunner\Centrifugo\Payload\Disconnect; use RoadRunner\Centrifugo\RPCCentrifugoApi; use RoadRunner\Centrifugal\API\DTO\V1 as DTO; use Spiral\Goridge\RPC\Codec\ProtobufCodec; @@ -72,4 +73,56 @@ public function testPublishErrorHandling(): void $this->api->publish(channel: 'foo-channel', message: \json_encode(['foo' => 'bar']), skipHistory: true, tags: ['baz', 'baf']); } + + public function testDisconnectWithDisconnectObject(): void + { + $this->rpc->shouldReceive('call') + ->once() + ->withArgs(fn( + string $method, + DTO\DisconnectRequest $request, + string $responseClass + ): bool => $method === 'centrifuge.Unsubscribe' + && $request->getUser() === 'foo-user' + && $request->getClient() === 'foo-client' + && $request->getSession() === 'foo-session' + && $request->getDisconnect()->getCode() === 400 + && $request->getDisconnect()->getReason() === 'foo-reason' + && $responseClass === DTO\DisconnectResponse::class + ) + ->andReturn(new DTO\DisconnectResponse); + + $this->api->disconnect( + user: 'foo-user', + client: 'foo-client', + session: 'foo-session', + disconnect: new Disconnect(code: 400, reason: 'foo-reason'), + ); + } + + public function testDisconnectWithDisconnectObjectAndDeprecatedReconnect(): void + { + $this->rpc->shouldReceive('call') + ->once() + ->withArgs(fn( + string $method, + DTO\DisconnectRequest $request, + string $responseClass + ): bool => $method === 'centrifuge.Unsubscribe' + && $request->getUser() === 'foo-user' + && $request->getClient() === 'foo-client' + && $request->getSession() === 'foo-session' + && $request->getDisconnect()->getCode() === 400 + && $request->getDisconnect()->getReason() === 'foo-reason' + && $responseClass === DTO\DisconnectResponse::class + ) + ->andReturn(new DTO\DisconnectResponse); + + $this->api->disconnect( + user: 'foo-user', + client: 'foo-client', + session: 'foo-session', + disconnect: new Disconnect(code: 400, reason: 'foo-reason', reconnect: true), + ); + } } diff --git a/tests/Unit/Request/AbstractRequestTest.php b/tests/Unit/Request/AbstractRequestTest.php index 1c4cc4f..1e0899b 100644 --- a/tests/Unit/Request/AbstractRequestTest.php +++ b/tests/Unit/Request/AbstractRequestTest.php @@ -99,7 +99,7 @@ public function testDisconnect(): void { $worker = $this->createWorker(function (Payload $arg) { $expects = new Payload((new ConnectResponse()) - ->setDisconnect(new Disconnect(['code' => 111, 'reason' => 'some', 'reconnect' => false])) + ->setDisconnect(new Disconnect(['code' => 111, 'reason' => 'some'])) ->serializeToString() ); @@ -115,11 +115,11 @@ public function testDisconnect(): void $req->disconnect(111, 'some'); } - public function testDisconnectWithReconnect(): void + public function testDisconnectWithDeprecatedReconnect(): void { $worker = $this->createWorker(function (Payload $arg) { $expects = new Payload((new ConnectResponse()) - ->setDisconnect(new Disconnect(['code' => 111, 'reason' => 'some', 'reconnect' => true])) + ->setDisconnect(new Disconnect(['code' => 111, 'reason' => 'some'])) ->serializeToString() ); From 2b4a7ddbd10d0586bda413736250a5b492fec5b6 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Wed, 6 Mar 2024 14:49:37 +0200 Subject: [PATCH 3/3] Fix Psalm issues --- src/Payload/Disconnect.php | 1 + src/Request/Connect.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Payload/Disconnect.php b/src/Payload/Disconnect.php index c673773..67f3f2b 100644 --- a/src/Payload/Disconnect.php +++ b/src/Payload/Disconnect.php @@ -20,6 +20,7 @@ public function __construct( public readonly string $reason, bool $reconnect = false ) { + /** @psalm-suppress DeprecatedProperty */ $this->reconnect = $reconnect; } } diff --git a/src/Request/Connect.php b/src/Request/Connect.php index 115a4c9..79581e2 100644 --- a/src/Request/Connect.php +++ b/src/Request/Connect.php @@ -103,7 +103,7 @@ private function mapSubscriptions(array $subscriptions): array foreach ($subscriptions as $name => $subscription) { $sub = new DTO\SubscribeOptions(); - if ($subscription->expireAt) { + if ($subscription->expireAt !== null) { $sub->setExpireAt($this->parseExpiresAt($subscription->expireAt)); }