Skip to content

Commit

Permalink
Merge pull request #11 from roadrunner-php/bugfix/deprecate-reconnect
Browse files Browse the repository at this point in the history
Declare the reconnect parameter deprecated in the disconnect
  • Loading branch information
msmakouz authored Mar 6, 2024
2 parents 4633c33 + 2b4a7dd commit 1a25f03
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 7 deletions.
13 changes: 12 additions & 1 deletion src/Payload/Disconnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
1 change: 0 additions & 1 deletion src/RPCCentrifugoApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ public function disconnect(
new DTO\Disconnect([
'code' => $disconnect->code,
'reason' => $disconnect->reason,
'reconnect' => $disconnect->reconnect,
])
);
}
Expand Down
6 changes: 5 additions & 1 deletion src/Request/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Request/Connect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
3 changes: 3 additions & 0 deletions src/Request/RequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
53 changes: 53 additions & 0 deletions tests/Unit/RPCCentrifugoApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
);
}
}
6 changes: 3 additions & 3 deletions tests/Unit/Request/AbstractRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);

Expand All @@ -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()
);

Expand Down

0 comments on commit 1a25f03

Please sign in to comment.