2018-11-27 20:56:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
2018-12-06 19:55:48 +00:00
|
|
|
namespace BeyondCode\LaravelWebSockets\Tests\Channels;
|
2018-11-27 20:56:25 +00:00
|
|
|
|
|
|
|
|
use BeyondCode\LaravelWebSockets\Tests\TestCase;
|
2018-12-04 21:22:33 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
|
2018-11-27 20:56:25 +00:00
|
|
|
|
|
|
|
|
class ChannelTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
/** @test */
|
|
|
|
|
public function clients_can_subscribe_to_channels()
|
|
|
|
|
{
|
|
|
|
|
$connection = $this->getWebSocketConnection();
|
|
|
|
|
|
|
|
|
|
$message = new Message(json_encode([
|
|
|
|
|
'event' => 'pusher:subscribe',
|
|
|
|
|
'data' => [
|
2018-12-04 21:22:33 +00:00
|
|
|
'channel' => 'basic-channel',
|
2018-11-27 20:56:25 +00:00
|
|
|
],
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$this->pusherServer->onOpen($connection);
|
|
|
|
|
|
|
|
|
|
$this->pusherServer->onMessage($connection, $message);
|
|
|
|
|
|
|
|
|
|
$connection->assertSentEvent('pusher_internal:subscription_succeeded', [
|
2018-12-04 21:22:33 +00:00
|
|
|
'channel' => 'basic-channel',
|
2018-11-27 20:56:25 +00:00
|
|
|
]);
|
|
|
|
|
}
|
2018-11-27 21:10:02 +00:00
|
|
|
|
2018-12-02 22:49:45 +00:00
|
|
|
/** @test */
|
|
|
|
|
public function clients_can_unsubscribe_from_channels()
|
|
|
|
|
{
|
|
|
|
|
$connection = $this->getConnectedWebSocketConnection(['test-channel']);
|
|
|
|
|
|
|
|
|
|
$channel = $this->getChannel($connection, 'test-channel');
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($channel->hasConnections());
|
|
|
|
|
|
|
|
|
|
$message = new Message(json_encode([
|
|
|
|
|
'event' => 'pusher:unsubscribe',
|
|
|
|
|
'data' => [
|
2018-12-04 21:22:33 +00:00
|
|
|
'channel' => 'test-channel',
|
2018-12-02 22:49:45 +00:00
|
|
|
],
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$this->pusherServer->onMessage($connection, $message);
|
|
|
|
|
|
|
|
|
|
$this->assertFalse($channel->hasConnections());
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-27 21:10:02 +00:00
|
|
|
/** @test */
|
2018-12-01 15:24:36 +00:00
|
|
|
public function a_client_cannot_broadcast_to_other_clients_by_default()
|
2018-11-27 21:10:02 +00:00
|
|
|
{
|
|
|
|
|
// One connection inside channel "test-channel".
|
|
|
|
|
$existingConnection = $this->getConnectedWebSocketConnection(['test-channel']);
|
|
|
|
|
|
|
|
|
|
$connection = $this->getConnectedWebSocketConnection(['test-channel']);
|
|
|
|
|
|
|
|
|
|
$message = new Message('{"event": "client-test", "data": {}, "channel": "test-channel"}');
|
|
|
|
|
|
|
|
|
|
$this->pusherServer->onMessage($connection, $message);
|
|
|
|
|
|
2018-12-01 15:24:36 +00:00
|
|
|
$existingConnection->assertNotSentEvent('client-test');
|
2018-11-27 21:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
2018-12-01 14:59:46 +00:00
|
|
|
/** @test */
|
2018-12-01 15:24:36 +00:00
|
|
|
public function a_client_can_be_enabled_to_broadcast_to_other_clients()
|
2018-12-01 14:59:46 +00:00
|
|
|
{
|
2018-12-01 15:24:36 +00:00
|
|
|
config()->set('websockets.apps.0.enable_client_messages', true);
|
2018-12-01 14:59:46 +00:00
|
|
|
|
|
|
|
|
// One connection inside channel "test-channel".
|
|
|
|
|
$existingConnection = $this->getConnectedWebSocketConnection(['test-channel']);
|
|
|
|
|
|
|
|
|
|
$connection = $this->getConnectedWebSocketConnection(['test-channel']);
|
|
|
|
|
|
|
|
|
|
$message = new Message('{"event": "client-test", "data": {}, "channel": "test-channel"}');
|
|
|
|
|
|
|
|
|
|
$this->pusherServer->onMessage($connection, $message);
|
|
|
|
|
|
2018-12-01 15:24:36 +00:00
|
|
|
$existingConnection->assertSentEvent('client-test');
|
2018-12-01 14:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-27 21:10:02 +00:00
|
|
|
/** @test */
|
2018-11-27 21:18:02 +00:00
|
|
|
public function closed_connections_get_removed_from_all_connected_channels()
|
2018-11-27 21:10:02 +00:00
|
|
|
{
|
2018-11-27 21:18:02 +00:00
|
|
|
$connection = $this->getConnectedWebSocketConnection(['test-channel-1', 'test-channel-2']);
|
2018-11-27 21:10:02 +00:00
|
|
|
|
2018-11-27 21:18:02 +00:00
|
|
|
$channel1 = $this->getChannel($connection, 'test-channel-1');
|
|
|
|
|
$channel2 = $this->getChannel($connection, 'test-channel-2');
|
2018-11-27 21:10:02 +00:00
|
|
|
|
2018-11-27 21:18:02 +00:00
|
|
|
$this->assertTrue($channel1->hasConnections());
|
|
|
|
|
$this->assertTrue($channel2->hasConnections());
|
2018-11-27 21:10:02 +00:00
|
|
|
|
|
|
|
|
$this->pusherServer->onClose($connection);
|
|
|
|
|
|
2018-11-27 21:18:02 +00:00
|
|
|
$this->assertFalse($channel1->hasConnections());
|
|
|
|
|
$this->assertFalse($channel2->hasConnections());
|
2018-11-27 21:10:02 +00:00
|
|
|
}
|
2018-11-27 21:26:29 +00:00
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
public function channels_can_broadcast_messages_to_all_connections()
|
|
|
|
|
{
|
|
|
|
|
$connection1 = $this->getConnectedWebSocketConnection(['test-channel']);
|
|
|
|
|
$connection2 = $this->getConnectedWebSocketConnection(['test-channel']);
|
|
|
|
|
|
|
|
|
|
$channel = $this->getChannel($connection1, 'test-channel');
|
|
|
|
|
|
|
|
|
|
$channel->broadcast([
|
|
|
|
|
'event' => 'broadcasted-event',
|
2018-12-04 21:22:33 +00:00
|
|
|
'channel' => 'test-channel',
|
2018-11-27 21:26:29 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$connection1->assertSentEvent('broadcasted-event');
|
|
|
|
|
$connection2->assertSentEvent('broadcasted-event');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
public function channels_can_broadcast_messages_to_all_connections_except_the_given_connection()
|
|
|
|
|
{
|
|
|
|
|
$connection1 = $this->getConnectedWebSocketConnection(['test-channel']);
|
|
|
|
|
$connection2 = $this->getConnectedWebSocketConnection(['test-channel']);
|
|
|
|
|
|
|
|
|
|
$channel = $this->getChannel($connection1, 'test-channel');
|
|
|
|
|
|
2018-11-27 21:27:08 +00:00
|
|
|
$channel->broadcastToOthers($connection1, [
|
2018-11-27 21:26:29 +00:00
|
|
|
'event' => 'broadcasted-event',
|
2018-12-04 21:22:33 +00:00
|
|
|
'channel' => 'test-channel',
|
2018-11-27 21:27:08 +00:00
|
|
|
]);
|
2018-11-27 21:26:29 +00:00
|
|
|
|
|
|
|
|
$connection1->assertNotSentEvent('broadcasted-event');
|
|
|
|
|
$connection2->assertSentEvent('broadcasted-event');
|
|
|
|
|
}
|
2018-11-28 22:59:58 +00:00
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
public function it_responds_correctly_to_the_ping_message()
|
|
|
|
|
{
|
2018-12-03 12:57:50 +00:00
|
|
|
$connection = $this->getConnectedWebSocketConnection();
|
2018-11-28 22:59:58 +00:00
|
|
|
|
|
|
|
|
$message = new Message(json_encode([
|
|
|
|
|
'event' => 'pusher:ping',
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$this->pusherServer->onMessage($connection, $message);
|
|
|
|
|
|
|
|
|
|
$connection->assertSentEvent('pusher:pong');
|
|
|
|
|
}
|
2018-12-04 21:22:33 +00:00
|
|
|
}
|