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) diff --git a/src/Payload/Disconnect.php b/src/Payload/Disconnect.php index 172de4f..67f3f2b 100644 --- a/src/Payload/Disconnect.php +++ b/src/Payload/Disconnect.php @@ -6,10 +6,21 @@ 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 ) { + /** @psalm-suppress DeprecatedProperty */ + $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/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)); } 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() );