laravel-websockets/tests/Channels/PresenceChannelReplicationT...

137 lines
4.1 KiB
PHP
Raw Normal View History

<?php
namespace BeyondCode\LaravelWebSockets\Tests\Channels;
2020-08-14 13:03:12 +00:00
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
2020-08-14 06:43:47 +00:00
use BeyondCode\LaravelWebSockets\Tests\TestCase;
class PresenceChannelReplicationTest extends TestCase
{
2020-08-14 12:35:36 +00:00
/**
* {@inheritdoc}
*/
public function setUp(): void
{
parent::setUp();
$this->runOnlyOnRedisReplication();
}
2020-08-14 12:51:57 +00:00
/** @test */
public function clients_with_valid_auth_signatures_can_join_presence_channels()
{
$connection = $this->getWebSocketConnection();
$this->pusherServer->onOpen($connection);
$channelData = [
'user_id' => 1,
'user_info' => [
'name' => 'Marcel',
],
];
$signature = "{$connection->socketId}:presence-channel:".json_encode($channelData);
2020-08-22 17:53:33 +00:00
$message = new Message([
2020-08-14 12:51:57 +00:00
'event' => 'pusher:subscribe',
'data' => [
'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret),
'channel' => 'presence-channel',
'channel_data' => json_encode($channelData),
],
2020-08-22 17:53:33 +00:00
]);
2020-08-14 12:51:57 +00:00
$this->pusherServer->onMessage($connection, $message);
$this->getPublishClient()
->assertCalledWithArgs('hset', [
'1234:presence-channel',
$connection->socketId,
json_encode($channelData),
])
2020-08-24 11:16:21 +00:00
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-channel'])
2020-08-17 08:47:50 +00:00
->assertCalled('publish');
}
/** @test */
public function clients_with_valid_auth_signatures_can_leave_presence_channels()
{
$connection = $this->getWebSocketConnection();
$this->pusherServer->onOpen($connection);
$channelData = [
'user_id' => 1,
];
$signature = "{$connection->socketId}:presence-channel:".json_encode($channelData);
2020-08-22 17:53:33 +00:00
$message = new Message([
2020-08-17 08:47:50 +00:00
'event' => 'pusher:subscribe',
'data' => [
'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret),
'channel' => 'presence-channel',
'channel_data' => json_encode($channelData),
],
2020-08-22 17:53:33 +00:00
]);
2020-08-17 08:47:50 +00:00
$this->pusherServer->onMessage($connection, $message);
$this->getSubscribeClient()
->assertEventDispatched('message');
$this->getPublishClient()
->assertCalled('hset')
2020-08-24 11:16:21 +00:00
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-channel'])
2020-08-17 08:47:50 +00:00
->assertCalled('publish');
$this->getPublishClient()
->resetAssertions();
2020-08-22 17:53:33 +00:00
$message = new Message([
2020-08-17 08:47:50 +00:00
'event' => 'pusher:unsubscribe',
'data' => [
'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret),
'channel' => 'presence-channel',
],
2020-08-22 17:53:33 +00:00
]);
2020-08-17 08:47:50 +00:00
$this->pusherServer->onMessage($connection, $message);
$this->getPublishClient()
->assertCalled('hdel')
->assertCalled('publish');
}
/** @test */
public function clients_with_no_user_info_can_join_presence_channels()
{
$connection = $this->getWebSocketConnection();
$this->pusherServer->onOpen($connection);
$channelData = [
'user_id' => 1,
];
$signature = "{$connection->socketId}:presence-channel:".json_encode($channelData);
2020-08-22 17:53:33 +00:00
$message = new Message([
2020-08-17 08:47:50 +00:00
'event' => 'pusher:subscribe',
'data' => [
'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret),
'channel' => 'presence-channel',
'channel_data' => json_encode($channelData),
],
2020-08-22 17:53:33 +00:00
]);
2020-08-17 08:47:50 +00:00
$this->pusherServer->onMessage($connection, $message);
$this->getPublishClient()
->assertCalled('hset')
2020-08-24 11:16:21 +00:00
->assertcalledWithArgs('hgetall', ['laravel_database_1234:presence-channel'])
2020-08-17 08:47:50 +00:00
->assertCalled('publish');
2020-08-14 12:51:57 +00:00
}
}