laravel-websockets/tests/PubSub/RedisDriverTest.php

123 lines
3.5 KiB
PHP
Raw Normal View History

2020-08-18 21:02:02 +00:00
<?php
namespace BeyondCode\LaravelWebSockets\Tests\PubSub;
2020-08-22 18:54:48 +00:00
use BeyondCode\LaravelWebSockets\PubSub\Drivers\RedisClient;
use BeyondCode\LaravelWebSockets\Tests\Mocks\RedisFactory;
2020-08-22 18:55:09 +00:00
use BeyondCode\LaravelWebSockets\Tests\TestCase;
2020-09-04 06:47:23 +00:00
use Illuminate\Support\Facades\Redis;
2020-08-22 18:54:48 +00:00
use React\EventLoop\Factory as LoopFactory;
2020-08-18 21:02:02 +00:00
class RedisDriverTest extends TestCase
{
/**
* {@inheritdoc}
*/
public function setUp(): void
{
parent::setUp();
$this->runOnlyOnRedisReplication();
2020-09-03 13:31:19 +00:00
2020-09-04 06:47:23 +00:00
Redis::hdel('laravel_database_1234', 'connections');
2020-08-18 21:02:02 +00:00
}
/** @test */
public function redis_listener_responds_properly_on_payload()
{
$connection = $this->getConnectedWebSocketConnection(['test-channel']);
$this->pusherServer->onOpen($connection);
$channelData = [
'user_id' => 1,
'user_info' => [
'name' => 'Marcel',
],
];
$payload = json_encode([
'appId' => '1234',
'event' => 'test',
'data' => $channelData,
2020-08-27 11:13:17 +00:00
'socketId' => $connection->socketId,
2020-08-18 21:02:02 +00:00
]);
$this->getSubscribeClient()->onMessage('1234:test-channel', $payload);
$this->getSubscribeClient()
->assertEventDispatched('message')
2020-08-24 11:16:21 +00:00
->assertCalledWithArgs('subscribe', ['laravel_database_1234:test-channel'])
2020-08-18 21:02:02 +00:00
->assertCalledWithArgs('onMessage', [
'1234:test-channel', $payload,
]);
}
2020-08-22 18:54:48 +00:00
/** @test */
public function redis_listener_responds_properly_on_payload_by_direct_call()
{
$connection = $this->getConnectedWebSocketConnection(['test-channel']);
$this->pusherServer->onOpen($connection);
$channelData = [
'user_id' => 1,
'user_info' => [
'name' => 'Marcel',
],
];
$payload = json_encode([
'appId' => '1234',
'event' => 'test',
'data' => $channelData,
2020-08-27 11:13:17 +00:00
'socketId' => $connection->socketId,
2020-08-22 18:54:48 +00:00
]);
$client = (new RedisClient)->boot(
LoopFactory::create(), RedisFactory::class
);
$client->onMessage('1234:test-channel', $payload);
$client->getSubscribeClient()
->assertEventDispatched('message');
}
2020-09-03 13:31:19 +00:00
/** @test */
public function redis_tracks_app_connections_count()
{
$connection = $this->getWebSocketConnection();
$this->pusherServer->onOpen($connection);
$this->getSubscribeClient()
->assertCalledWithArgs('subscribe', ['laravel_database_1234']);
$this->getPublishClient()
2020-09-04 06:47:23 +00:00
->assertCalledWithArgs('hincrby', ['laravel_database_1234', 'connections', 1]);
2020-09-03 13:31:19 +00:00
}
/** @test */
public function redis_tracks_app_connections_count_on_disconnect()
{
$connection = $this->getWebSocketConnection();
$this->pusherServer->onOpen($connection);
$this->getSubscribeClient()
->assertCalledWithArgs('subscribe', ['laravel_database_1234'])
->assertNotCalledWithArgs('unsubscribe', ['laravel_database_1234']);
$this->getPublishClient()
2020-09-04 06:47:23 +00:00
->assertCalledWithArgs('hincrby', ['laravel_database_1234', 'connections', 1]);
2020-09-03 13:31:19 +00:00
$this->pusherServer->onClose($connection);
$this->getPublishClient()
2020-09-04 06:47:23 +00:00
->assertCalledWithArgs('hincrby', ['laravel_database_1234', 'connections', -1]);
2020-09-03 13:31:19 +00:00
2020-09-04 06:47:23 +00:00
$this->assertEquals(0, Redis::hget('laravel_database_1234', 'connections'));
2020-09-03 13:31:19 +00:00
}
2020-08-18 21:02:02 +00:00
}