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

151 lines
4.4 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;
use Illuminate\Support\Facades\Cache;
2020-08-14 06:43:47 +00:00
class PresenceChannelReplicationTest extends TestCase
{
/**
* The Redis manager instance.
*
* @var \Illuminate\Redis\RedisManager
*/
protected $redis;
2020-08-14 12:35:36 +00:00
/**
* {@inheritdoc}
*/
public function setUp(): void
{
parent::setUp();
$this->runOnlyOnRedisReplication();
$this->redis = Cache::getRedis();
2020-08-14 12:35:36 +00:00
}
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()
->assertNotCalledWithArgs('hset', [
2020-09-03 04:33:45 +00:00
'laravel_database_1234:presence-channel',
2020-08-14 12:51:57 +00:00
$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');
$this->assertNotNull(
$this->redis->hget('laravel_database_1234:presence-channel', $connection->socketId)
);
2020-08-17 08:47:50 +00:00
}
/** @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()
->assertNotCalled('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()
->assertNotCalled('hdel')
2020-08-17 08:47:50 +00:00
->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()
->assertNotCalled('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
}
}