2019-04-05 19:30:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace BeyondCode\LaravelWebSockets\Tests\Channels;
|
|
|
|
|
|
2020-08-17 08:47:50 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
|
2020-08-14 06:43:47 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Tests\TestCase;
|
|
|
|
|
|
|
|
|
|
class ChannelReplicationTest extends TestCase
|
2019-04-05 19:30:41 +00:00
|
|
|
{
|
2020-08-14 12:35:36 +00:00
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
|
|
|
|
public function setUp(): void
|
|
|
|
|
{
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$this->runOnlyOnRedisReplication();
|
|
|
|
|
}
|
2020-08-14 17:26:55 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
/** @test */
|
|
|
|
|
public function replication_clients_can_subscribe_to_channels()
|
|
|
|
|
{
|
|
|
|
|
$connection = $this->getWebSocketConnection();
|
|
|
|
|
|
2020-08-22 17:53:33 +00:00
|
|
|
$message = new Message([
|
2020-08-17 08:48:14 +00:00
|
|
|
'event' => 'pusher:subscribe',
|
|
|
|
|
'data' => [
|
|
|
|
|
'channel' => 'basic-channel',
|
|
|
|
|
],
|
2020-08-22 17:53:33 +00:00
|
|
|
]);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$this->pusherServer->onOpen($connection);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$this->pusherServer->onMessage($connection, $message);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$connection->assertSentEvent('pusher_internal:subscription_succeeded', [
|
|
|
|
|
'channel' => 'basic-channel',
|
|
|
|
|
]);
|
|
|
|
|
}
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
/** @test */
|
|
|
|
|
public function replication_clients_can_unsubscribe_from_channels()
|
|
|
|
|
{
|
|
|
|
|
$connection = $this->getConnectedWebSocketConnection(['test-channel']);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$channel = $this->getChannel($connection, 'test-channel');
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$this->assertTrue($channel->hasConnections());
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-22 17:53:33 +00:00
|
|
|
$message = new Message([
|
2020-08-17 08:48:14 +00:00
|
|
|
'event' => 'pusher:unsubscribe',
|
|
|
|
|
'data' => [
|
|
|
|
|
'channel' => 'test-channel',
|
|
|
|
|
],
|
2020-08-22 17:53:33 +00:00
|
|
|
]);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$this->pusherServer->onMessage($connection, $message);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$this->assertFalse($channel->hasConnections());
|
|
|
|
|
}
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
/** @test */
|
|
|
|
|
public function replication_a_client_cannot_broadcast_to_other_clients_by_default()
|
|
|
|
|
{
|
|
|
|
|
// One connection inside channel "test-channel".
|
|
|
|
|
$existingConnection = $this->getConnectedWebSocketConnection(['test-channel']);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$connection = $this->getConnectedWebSocketConnection(['test-channel']);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-22 17:53:33 +00:00
|
|
|
$message = new Message(['event' => 'client-test', 'data' => [], 'channel' => 'test-channel']);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$this->pusherServer->onMessage($connection, $message);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$existingConnection->assertNotSentEvent('client-test');
|
|
|
|
|
}
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
/** @test */
|
|
|
|
|
public function replication_a_client_can_be_enabled_to_broadcast_to_other_clients()
|
|
|
|
|
{
|
|
|
|
|
config()->set('websockets.apps.0.enable_client_messages', true);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
// One connection inside channel "test-channel".
|
|
|
|
|
$existingConnection = $this->getConnectedWebSocketConnection(['test-channel']);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$connection = $this->getConnectedWebSocketConnection(['test-channel']);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-22 17:53:33 +00:00
|
|
|
$message = new Message(['event' => 'client-test', 'data' => [], 'channel' => 'test-channel']);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$this->pusherServer->onMessage($connection, $message);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$existingConnection->assertSentEvent('client-test');
|
|
|
|
|
}
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
/** @test */
|
|
|
|
|
public function replication_closed_connections_get_removed_from_all_connected_channels()
|
|
|
|
|
{
|
|
|
|
|
$connection = $this->getConnectedWebSocketConnection(['test-channel-1', 'test-channel-2']);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$channel1 = $this->getChannel($connection, 'test-channel-1');
|
|
|
|
|
$channel2 = $this->getChannel($connection, 'test-channel-2');
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$this->assertTrue($channel1->hasConnections());
|
|
|
|
|
$this->assertTrue($channel2->hasConnections());
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$this->pusherServer->onClose($connection);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$this->assertFalse($channel1->hasConnections());
|
|
|
|
|
$this->assertFalse($channel2->hasConnections());
|
|
|
|
|
}
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
/** @test */
|
|
|
|
|
public function replication_channels_can_broadcast_messages_to_all_connections()
|
|
|
|
|
{
|
|
|
|
|
$connection1 = $this->getConnectedWebSocketConnection(['test-channel']);
|
|
|
|
|
$connection2 = $this->getConnectedWebSocketConnection(['test-channel']);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$channel = $this->getChannel($connection1, 'test-channel');
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$channel->broadcast([
|
|
|
|
|
'event' => 'broadcasted-event',
|
|
|
|
|
'channel' => 'test-channel',
|
|
|
|
|
]);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$connection1->assertSentEvent('broadcasted-event');
|
|
|
|
|
$connection2->assertSentEvent('broadcasted-event');
|
|
|
|
|
}
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
/** @test */
|
|
|
|
|
public function replication_channels_can_broadcast_messages_to_all_connections_except_the_given_connection()
|
|
|
|
|
{
|
|
|
|
|
$connection1 = $this->getConnectedWebSocketConnection(['test-channel']);
|
|
|
|
|
$connection2 = $this->getConnectedWebSocketConnection(['test-channel']);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$channel = $this->getChannel($connection1, 'test-channel');
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$channel->broadcastToOthers($connection1, (object) [
|
|
|
|
|
'event' => 'broadcasted-event',
|
|
|
|
|
'channel' => 'test-channel',
|
|
|
|
|
]);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$connection1->assertNotSentEvent('broadcasted-event');
|
|
|
|
|
$connection2->assertSentEvent('broadcasted-event');
|
|
|
|
|
}
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
/** @test */
|
|
|
|
|
public function replication_it_responds_correctly_to_the_ping_message()
|
|
|
|
|
{
|
|
|
|
|
$connection = $this->getConnectedWebSocketConnection();
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-22 17:53:33 +00:00
|
|
|
$message = new Message([
|
2020-08-17 08:48:14 +00:00
|
|
|
'event' => 'pusher:ping',
|
2020-08-22 17:53:33 +00:00
|
|
|
]);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$this->pusherServer->onMessage($connection, $message);
|
2020-08-17 08:47:50 +00:00
|
|
|
|
2020-08-17 08:48:14 +00:00
|
|
|
$connection->assertSentEvent('pusher:pong');
|
|
|
|
|
}
|
2019-04-05 19:30:41 +00:00
|
|
|
}
|