Merge branch 'tests/redis-connector' of github.com:beyondcode/laravel-websockets into tests/redis-connector

This commit is contained in:
Alex Renoki 2020-08-17 13:05:41 +03:00
commit c2cec0e5c1
3 changed files with 98 additions and 101 deletions

View File

@ -17,142 +17,142 @@ class ChannelReplicationTest extends TestCase
$this->runOnlyOnRedisReplication(); $this->runOnlyOnRedisReplication();
} }
/** @test */ /** @test */
public function replication_clients_can_subscribe_to_channels() public function replication_clients_can_subscribe_to_channels()
{ {
$connection = $this->getWebSocketConnection(); $connection = $this->getWebSocketConnection();
$message = new Message(json_encode([ $message = new Message(json_encode([
'event' => 'pusher:subscribe', 'event' => 'pusher:subscribe',
'data' => [ 'data' => [
'channel' => 'basic-channel', 'channel' => 'basic-channel',
], ],
])); ]));
$this->pusherServer->onOpen($connection); $this->pusherServer->onOpen($connection);
$this->pusherServer->onMessage($connection, $message); $this->pusherServer->onMessage($connection, $message);
$connection->assertSentEvent('pusher_internal:subscription_succeeded', [ $connection->assertSentEvent('pusher_internal:subscription_succeeded', [
'channel' => 'basic-channel', 'channel' => 'basic-channel',
]); ]);
} }
/** @test */ /** @test */
public function replication_clients_can_unsubscribe_from_channels() public function replication_clients_can_unsubscribe_from_channels()
{ {
$connection = $this->getConnectedWebSocketConnection(['test-channel']); $connection = $this->getConnectedWebSocketConnection(['test-channel']);
$channel = $this->getChannel($connection, 'test-channel'); $channel = $this->getChannel($connection, 'test-channel');
$this->assertTrue($channel->hasConnections()); $this->assertTrue($channel->hasConnections());
$message = new Message(json_encode([ $message = new Message(json_encode([
'event' => 'pusher:unsubscribe', 'event' => 'pusher:unsubscribe',
'data' => [ 'data' => [
'channel' => 'test-channel', 'channel' => 'test-channel',
], ],
])); ]));
$this->pusherServer->onMessage($connection, $message); $this->pusherServer->onMessage($connection, $message);
$this->assertFalse($channel->hasConnections()); $this->assertFalse($channel->hasConnections());
} }
/** @test */ /** @test */
public function replication_a_client_cannot_broadcast_to_other_clients_by_default() public function replication_a_client_cannot_broadcast_to_other_clients_by_default()
{ {
// One connection inside channel "test-channel". // One connection inside channel "test-channel".
$existingConnection = $this->getConnectedWebSocketConnection(['test-channel']); $existingConnection = $this->getConnectedWebSocketConnection(['test-channel']);
$connection = $this->getConnectedWebSocketConnection(['test-channel']); $connection = $this->getConnectedWebSocketConnection(['test-channel']);
$message = new Message('{"event": "client-test", "data": {}, "channel": "test-channel"}'); $message = new Message('{"event": "client-test", "data": {}, "channel": "test-channel"}');
$this->pusherServer->onMessage($connection, $message); $this->pusherServer->onMessage($connection, $message);
$existingConnection->assertNotSentEvent('client-test'); $existingConnection->assertNotSentEvent('client-test');
} }
/** @test */ /** @test */
public function replication_a_client_can_be_enabled_to_broadcast_to_other_clients() public function replication_a_client_can_be_enabled_to_broadcast_to_other_clients()
{ {
config()->set('websockets.apps.0.enable_client_messages', true); config()->set('websockets.apps.0.enable_client_messages', true);
// One connection inside channel "test-channel". // One connection inside channel "test-channel".
$existingConnection = $this->getConnectedWebSocketConnection(['test-channel']); $existingConnection = $this->getConnectedWebSocketConnection(['test-channel']);
$connection = $this->getConnectedWebSocketConnection(['test-channel']); $connection = $this->getConnectedWebSocketConnection(['test-channel']);
$message = new Message('{"event": "client-test", "data": {}, "channel": "test-channel"}'); $message = new Message('{"event": "client-test", "data": {}, "channel": "test-channel"}');
$this->pusherServer->onMessage($connection, $message); $this->pusherServer->onMessage($connection, $message);
$existingConnection->assertSentEvent('client-test'); $existingConnection->assertSentEvent('client-test');
} }
/** @test */ /** @test */
public function replication_closed_connections_get_removed_from_all_connected_channels() public function replication_closed_connections_get_removed_from_all_connected_channels()
{ {
$connection = $this->getConnectedWebSocketConnection(['test-channel-1', 'test-channel-2']); $connection = $this->getConnectedWebSocketConnection(['test-channel-1', 'test-channel-2']);
$channel1 = $this->getChannel($connection, 'test-channel-1'); $channel1 = $this->getChannel($connection, 'test-channel-1');
$channel2 = $this->getChannel($connection, 'test-channel-2'); $channel2 = $this->getChannel($connection, 'test-channel-2');
$this->assertTrue($channel1->hasConnections()); $this->assertTrue($channel1->hasConnections());
$this->assertTrue($channel2->hasConnections()); $this->assertTrue($channel2->hasConnections());
$this->pusherServer->onClose($connection); $this->pusherServer->onClose($connection);
$this->assertFalse($channel1->hasConnections()); $this->assertFalse($channel1->hasConnections());
$this->assertFalse($channel2->hasConnections()); $this->assertFalse($channel2->hasConnections());
} }
/** @test */ /** @test */
public function replication_channels_can_broadcast_messages_to_all_connections() public function replication_channels_can_broadcast_messages_to_all_connections()
{ {
$connection1 = $this->getConnectedWebSocketConnection(['test-channel']); $connection1 = $this->getConnectedWebSocketConnection(['test-channel']);
$connection2 = $this->getConnectedWebSocketConnection(['test-channel']); $connection2 = $this->getConnectedWebSocketConnection(['test-channel']);
$channel = $this->getChannel($connection1, 'test-channel'); $channel = $this->getChannel($connection1, 'test-channel');
$channel->broadcast([ $channel->broadcast([
'event' => 'broadcasted-event', 'event' => 'broadcasted-event',
'channel' => 'test-channel', 'channel' => 'test-channel',
]); ]);
$connection1->assertSentEvent('broadcasted-event'); $connection1->assertSentEvent('broadcasted-event');
$connection2->assertSentEvent('broadcasted-event'); $connection2->assertSentEvent('broadcasted-event');
} }
/** @test */ /** @test */
public function replication_channels_can_broadcast_messages_to_all_connections_except_the_given_connection() public function replication_channels_can_broadcast_messages_to_all_connections_except_the_given_connection()
{ {
$connection1 = $this->getConnectedWebSocketConnection(['test-channel']); $connection1 = $this->getConnectedWebSocketConnection(['test-channel']);
$connection2 = $this->getConnectedWebSocketConnection(['test-channel']); $connection2 = $this->getConnectedWebSocketConnection(['test-channel']);
$channel = $this->getChannel($connection1, 'test-channel'); $channel = $this->getChannel($connection1, 'test-channel');
$channel->broadcastToOthers($connection1, (object) [ $channel->broadcastToOthers($connection1, (object) [
'event' => 'broadcasted-event', 'event' => 'broadcasted-event',
'channel' => 'test-channel', 'channel' => 'test-channel',
]); ]);
$connection1->assertNotSentEvent('broadcasted-event'); $connection1->assertNotSentEvent('broadcasted-event');
$connection2->assertSentEvent('broadcasted-event'); $connection2->assertSentEvent('broadcasted-event');
} }
/** @test */ /** @test */
public function replication_it_responds_correctly_to_the_ping_message() public function replication_it_responds_correctly_to_the_ping_message()
{ {
$connection = $this->getConnectedWebSocketConnection(); $connection = $this->getConnectedWebSocketConnection();
$message = new Message(json_encode([ $message = new Message(json_encode([
'event' => 'pusher:ping', 'event' => 'pusher:ping',
])); ]));
$this->pusherServer->onMessage($connection, $message); $this->pusherServer->onMessage($connection, $message);
$connection->assertSentEvent('pusher:pong'); $connection->assertSentEvent('pusher:pong');
} }
} }

View File

@ -6,7 +6,6 @@ use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
use BeyondCode\LaravelWebSockets\Tests\TestCase; use BeyondCode\LaravelWebSockets\Tests\TestCase;
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\InvalidSignature; use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\InvalidSignature;
class PrivateChannelReplicationTest extends TestCase class PrivateChannelReplicationTest extends TestCase
{ {
/** /**

View File

@ -8,7 +8,6 @@ use BeyondCode\LaravelWebSockets\Tests\TestCase;
use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Request;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Pusher\Pusher; use Pusher\Pusher;
use Symfony\Component\HttpKernel\Exception\HttpException;
class FetchChannelsReplicationTest extends TestCase class FetchChannelsReplicationTest extends TestCase
{ {
@ -55,7 +54,6 @@ class FetchChannelsReplicationTest extends TestCase
->assertCalled('multi') ->assertCalled('multi')
->assertCalledWithArgs('hlen', ['1234:presence-channel']) ->assertCalledWithArgs('hlen', ['1234:presence-channel'])
->assertCalled('exec'); ->assertCalled('exec');
} }
/** @test */ /** @test */