From 2066e803b826d5c9c392a3ce024872a83033f323 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Fri, 18 Sep 2020 21:01:11 +0300 Subject: [PATCH] Fixed tests to make sure the message is broadcasted properly both locally and across servers. --- tests/PresenceChannelTest.php | 146 +++++++++++++++++++++++++++++ tests/PrivateChannelTest.php | 146 +++++++++++++++++++++++++++++ tests/PublicChannelTest.php | 146 +++++++++++++++++++++++++++++ tests/TriggerEventTest.php | 167 ---------------------------------- 4 files changed, 438 insertions(+), 167 deletions(-) diff --git a/tests/PresenceChannelTest.php b/tests/PresenceChannelTest.php index 6927b14..9234ab8 100644 --- a/tests/PresenceChannelTest.php +++ b/tests/PresenceChannelTest.php @@ -2,8 +2,12 @@ namespace BeyondCode\LaravelWebSockets\Test; +use BeyondCode\LaravelWebSockets\API\TriggerEvent; use BeyondCode\LaravelWebSockets\Server\Exceptions\InvalidSignature; use Carbon\Carbon; +use GuzzleHttp\Psr7\Request; +use Illuminate\Http\JsonResponse; +use Pusher\Pusher; use Ratchet\ConnectionInterface; class PresenceChannelTest extends TestCase @@ -455,4 +459,146 @@ class PresenceChannelTest extends TestCase $message->getPayload(), ]); } + + public function test_it_fires_the_event_to_presence_channel() + { + $this->newPresenceConnection('presence-channel'); + + $connection = new Mocks\Connection; + + $requestPath = '/apps/1234/events'; + + $routeParams = [ + 'appId' => '1234', + ]; + + $queryString = Pusher::build_auth_query_string( + 'TestKey', 'TestSecret', 'POST', $requestPath, [ + 'name' => 'some-event', + 'channels' => ['presence-channel'], + 'data' => json_encode(['some-data' => 'yes']), + ], + ); + + $request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams)); + + $controller = app(TriggerEvent::class); + + $controller->onOpen($connection, $request); + + /** @var JsonResponse $response */ + $response = array_pop($connection->sentRawData); + + $this->assertSame([], json_decode($response->getContent(), true)); + + $this->statisticsCollector + ->getAppStatistics('1234') + ->then(function ($statistic) { + $this->assertEquals([ + 'peak_connections_count' => 1, + 'websocket_messages_count' => 1, + 'api_messages_count' => 1, + 'app_id' => '1234', + ], $statistic->toArray()); + }); + } + + public function test_it_fires_event_across_servers_when_there_are_not_users_locally_for_presence_channel() + { + $connection = new Mocks\Connection; + + $requestPath = '/apps/1234/events'; + + $routeParams = [ + 'appId' => '1234', + ]; + + $queryString = Pusher::build_auth_query_string( + 'TestKey', 'TestSecret', 'POST', $requestPath, [ + 'name' => 'some-event', + 'channels' => ['presence-channel'], + 'data' => json_encode(['some-data' => 'yes']), + ], + ); + + $request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams)); + + $controller = app(TriggerEvent::class); + + $controller->onOpen($connection, $request); + + /** @var JsonResponse $response */ + $response = array_pop($connection->sentRawData); + + $this->assertSame([], json_decode($response->getContent(), true)); + + if (method_exists($this->channelManager, 'getPublishClient')) { + $this->channelManager + ->getPublishClient() + ->assertCalledWithArgsCount(1, 'publish', [ + $this->channelManager->getRedisKey('1234', 'presence-channel'), + json_encode([ + 'event' => 'some-event', + 'channel' => 'presence-channel', + 'data' => json_encode(['some-data' => 'yes']), + 'appId' => '1234', + 'socketId' => null, + 'serverId' => $this->channelManager->getServerId(), + ]), + ]); + } + } + + public function test_it_fires_event_across_servers_when_there_are_users_locally_for_presence_channel() + { + $wsConnection = $this->newPresenceConnection('presence-channel'); + + $connection = new Mocks\Connection; + + $requestPath = '/apps/1234/events'; + + $routeParams = [ + 'appId' => '1234', + ]; + + $queryString = Pusher::build_auth_query_string( + 'TestKey', 'TestSecret', 'POST', $requestPath, [ + 'name' => 'some-event', + 'channels' => ['presence-channel'], + 'data' => json_encode(['some-data' => 'yes']), + ], + ); + + $request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams)); + + $controller = app(TriggerEvent::class); + + $controller->onOpen($connection, $request); + + /** @var JsonResponse $response */ + $response = array_pop($connection->sentRawData); + + $this->assertSame([], json_decode($response->getContent(), true)); + + if (method_exists($this->channelManager, 'getPublishClient')) { + $this->channelManager + ->getPublishClient() + ->assertCalledWithArgsCount(1, 'publish', [ + $this->channelManager->getRedisKey('1234', 'presence-channel'), + json_encode([ + 'event' => 'some-event', + 'channel' => 'presence-channel', + 'data' => json_encode(['some-data' => 'yes']), + 'appId' => '1234', + 'socketId' => null, + 'serverId' => $this->channelManager->getServerId(), + ]), + ]); + } + + $wsConnection->assertSentEvent('some-event', [ + 'channel' => 'presence-channel', + 'data' => json_encode(['some-data' => 'yes']), + ]); + } } diff --git a/tests/PrivateChannelTest.php b/tests/PrivateChannelTest.php index d37517c..8708bda 100644 --- a/tests/PrivateChannelTest.php +++ b/tests/PrivateChannelTest.php @@ -2,8 +2,12 @@ namespace BeyondCode\LaravelWebSockets\Test; +use BeyondCode\LaravelWebSockets\API\TriggerEvent; use BeyondCode\LaravelWebSockets\Server\Exceptions\InvalidSignature; use Carbon\Carbon; +use GuzzleHttp\Psr7\Request; +use Illuminate\Http\JsonResponse; +use Pusher\Pusher; use Ratchet\ConnectionInterface; class PrivateChannelTest extends TestCase @@ -263,4 +267,146 @@ class PrivateChannelTest extends TestCase $message->getPayload(), ]); } + + public function test_it_fires_the_event_to_private_channel() + { + $this->newPrivateConnection('private-channel'); + + $connection = new Mocks\Connection; + + $requestPath = '/apps/1234/events'; + + $routeParams = [ + 'appId' => '1234', + ]; + + $queryString = Pusher::build_auth_query_string( + 'TestKey', 'TestSecret', 'POST', $requestPath, [ + 'name' => 'some-event', + 'channels' => ['private-channel'], + 'data' => json_encode(['some-data' => 'yes']), + ], + ); + + $request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams)); + + $controller = app(TriggerEvent::class); + + $controller->onOpen($connection, $request); + + /** @var JsonResponse $response */ + $response = array_pop($connection->sentRawData); + + $this->assertSame([], json_decode($response->getContent(), true)); + + $this->statisticsCollector + ->getAppStatistics('1234') + ->then(function ($statistic) { + $this->assertEquals([ + 'peak_connections_count' => 1, + 'websocket_messages_count' => 1, + 'api_messages_count' => 1, + 'app_id' => '1234', + ], $statistic->toArray()); + }); + } + + public function test_it_fires_event_across_servers_when_there_are_not_users_locally_for_private_channel() + { + $connection = new Mocks\Connection; + + $requestPath = '/apps/1234/events'; + + $routeParams = [ + 'appId' => '1234', + ]; + + $queryString = Pusher::build_auth_query_string( + 'TestKey', 'TestSecret', 'POST', $requestPath, [ + 'name' => 'some-event', + 'channels' => ['private-channel'], + 'data' => json_encode(['some-data' => 'yes']), + ], + ); + + $request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams)); + + $controller = app(TriggerEvent::class); + + $controller->onOpen($connection, $request); + + /** @var JsonResponse $response */ + $response = array_pop($connection->sentRawData); + + $this->assertSame([], json_decode($response->getContent(), true)); + + if (method_exists($this->channelManager, 'getPublishClient')) { + $this->channelManager + ->getPublishClient() + ->assertCalledWithArgsCount(1, 'publish', [ + $this->channelManager->getRedisKey('1234', 'private-channel'), + json_encode([ + 'event' => 'some-event', + 'channel' => 'private-channel', + 'data' => json_encode(['some-data' => 'yes']), + 'appId' => '1234', + 'socketId' => null, + 'serverId' => $this->channelManager->getServerId(), + ]), + ]); + } + } + + public function test_it_fires_event_across_servers_when_there_are_users_locally_for_private_channel() + { + $wsConnection = $this->newPrivateConnection('private-channel'); + + $connection = new Mocks\Connection; + + $requestPath = '/apps/1234/events'; + + $routeParams = [ + 'appId' => '1234', + ]; + + $queryString = Pusher::build_auth_query_string( + 'TestKey', 'TestSecret', 'POST', $requestPath, [ + 'name' => 'some-event', + 'channels' => ['private-channel'], + 'data' => json_encode(['some-data' => 'yes']), + ], + ); + + $request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams)); + + $controller = app(TriggerEvent::class); + + $controller->onOpen($connection, $request); + + /** @var JsonResponse $response */ + $response = array_pop($connection->sentRawData); + + $this->assertSame([], json_decode($response->getContent(), true)); + + if (method_exists($this->channelManager, 'getPublishClient')) { + $this->channelManager + ->getPublishClient() + ->assertCalledWithArgsCount(1, 'publish', [ + $this->channelManager->getRedisKey('1234', 'private-channel'), + json_encode([ + 'event' => 'some-event', + 'channel' => 'private-channel', + 'data' => json_encode(['some-data' => 'yes']), + 'appId' => '1234', + 'socketId' => null, + 'serverId' => $this->channelManager->getServerId(), + ]), + ]); + } + + $wsConnection->assertSentEvent('some-event', [ + 'channel' => 'private-channel', + 'data' => json_encode(['some-data' => 'yes']), + ]); + } } diff --git a/tests/PublicChannelTest.php b/tests/PublicChannelTest.php index 4c755fb..84f0d17 100644 --- a/tests/PublicChannelTest.php +++ b/tests/PublicChannelTest.php @@ -2,7 +2,11 @@ namespace BeyondCode\LaravelWebSockets\Test; +use BeyondCode\LaravelWebSockets\API\TriggerEvent; use Carbon\Carbon; +use GuzzleHttp\Psr7\Request; +use Illuminate\Http\JsonResponse; +use Pusher\Pusher; use Ratchet\ConnectionInterface; class PublicChannelTest extends TestCase @@ -244,4 +248,146 @@ class PublicChannelTest extends TestCase $message->getPayload(), ]); } + + public function test_it_fires_the_event_to_public_channel() + { + $this->newActiveConnection(['public-channel']); + + $connection = new Mocks\Connection; + + $requestPath = '/apps/1234/events'; + + $routeParams = [ + 'appId' => '1234', + ]; + + $queryString = Pusher::build_auth_query_string( + 'TestKey', 'TestSecret', 'POST', $requestPath, [ + 'name' => 'some-event', + 'channels' => ['public-channel'], + 'data' => json_encode(['some-data' => 'yes']), + ], + ); + + $request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams)); + + $controller = app(TriggerEvent::class); + + $controller->onOpen($connection, $request); + + /** @var JsonResponse $response */ + $response = array_pop($connection->sentRawData); + + $this->assertSame([], json_decode($response->getContent(), true)); + + $this->statisticsCollector + ->getAppStatistics('1234') + ->then(function ($statistic) { + $this->assertEquals([ + 'peak_connections_count' => 1, + 'websocket_messages_count' => 1, + 'api_messages_count' => 1, + 'app_id' => '1234', + ], $statistic->toArray()); + }); + } + + public function test_it_fires_event_across_servers_when_there_are_not_users_locally_for_public_channel() + { + $connection = new Mocks\Connection; + + $requestPath = '/apps/1234/events'; + + $routeParams = [ + 'appId' => '1234', + ]; + + $queryString = Pusher::build_auth_query_string( + 'TestKey', 'TestSecret', 'POST', $requestPath, [ + 'name' => 'some-event', + 'channels' => ['public-channel'], + 'data' => json_encode(['some-data' => 'yes']), + ], + ); + + $request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams)); + + $controller = app(TriggerEvent::class); + + $controller->onOpen($connection, $request); + + /** @var JsonResponse $response */ + $response = array_pop($connection->sentRawData); + + $this->assertSame([], json_decode($response->getContent(), true)); + + if (method_exists($this->channelManager, 'getPublishClient')) { + $this->channelManager + ->getPublishClient() + ->assertCalledWithArgsCount(1, 'publish', [ + $this->channelManager->getRedisKey('1234', 'public-channel'), + json_encode([ + 'event' => 'some-event', + 'channel' => 'public-channel', + 'data' => json_encode(['some-data' => 'yes']), + 'appId' => '1234', + 'socketId' => null, + 'serverId' => $this->channelManager->getServerId(), + ]), + ]); + } + } + + public function test_it_fires_event_across_servers_when_there_are_users_locally_for_public_channel() + { + $wsConnection = $this->newActiveConnection(['public-channel']); + + $connection = new Mocks\Connection; + + $requestPath = '/apps/1234/events'; + + $routeParams = [ + 'appId' => '1234', + ]; + + $queryString = Pusher::build_auth_query_string( + 'TestKey', 'TestSecret', 'POST', $requestPath, [ + 'name' => 'some-event', + 'channels' => ['public-channel'], + 'data' => json_encode(['some-data' => 'yes']), + ], + ); + + $request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams)); + + $controller = app(TriggerEvent::class); + + $controller->onOpen($connection, $request); + + /** @var JsonResponse $response */ + $response = array_pop($connection->sentRawData); + + $this->assertSame([], json_decode($response->getContent(), true)); + + if (method_exists($this->channelManager, 'getPublishClient')) { + $this->channelManager + ->getPublishClient() + ->assertCalledWithArgsCount(1, 'publish', [ + $this->channelManager->getRedisKey('1234', 'public-channel'), + json_encode([ + 'event' => 'some-event', + 'channel' => 'public-channel', + 'data' => json_encode(['some-data' => 'yes']), + 'appId' => '1234', + 'socketId' => null, + 'serverId' => $this->channelManager->getServerId(), + ]), + ]); + } + + $wsConnection->assertSentEvent('some-event', [ + 'channel' => 'public-channel', + 'data' => json_encode(['some-data' => 'yes']), + ]); + } } diff --git a/tests/TriggerEventTest.php b/tests/TriggerEventTest.php index 8952198..ef0bc2f 100644 --- a/tests/TriggerEventTest.php +++ b/tests/TriggerEventTest.php @@ -33,171 +33,4 @@ class TriggerEventTest extends TestCase $controller->onOpen($connection, $request); } - - public function test_it_fires_the_event_to_public_channel() - { - $this->newActiveConnection(['public-channel']); - - $connection = new Mocks\Connection; - - $requestPath = '/apps/1234/events'; - - $routeParams = [ - 'appId' => '1234', - ]; - - $queryString = Pusher::build_auth_query_string( - 'TestKey', 'TestSecret', 'GET', $requestPath, [ - 'channels' => 'public-channel', - ], - ); - - $request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams)); - - $controller = app(TriggerEvent::class); - - $controller->onOpen($connection, $request); - - /** @var JsonResponse $response */ - $response = array_pop($connection->sentRawData); - - $this->assertSame([], json_decode($response->getContent(), true)); - - $this->statisticsCollector - ->getAppStatistics('1234') - ->then(function ($statistic) { - $this->assertEquals([ - 'peak_connections_count' => 1, - 'websocket_messages_count' => 1, - 'api_messages_count' => 1, - 'app_id' => '1234', - ], $statistic->toArray()); - }); - } - - public function test_it_fires_the_event_to_presence_channel() - { - $this->newPresenceConnection('presence-channel'); - - $connection = new Mocks\Connection; - - $requestPath = '/apps/1234/events'; - - $routeParams = [ - 'appId' => '1234', - ]; - - $queryString = Pusher::build_auth_query_string( - 'TestKey', 'TestSecret', 'GET', $requestPath, [ - 'channels' => 'presence-channel', - ], - ); - - $request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams)); - - $controller = app(TriggerEvent::class); - - $controller->onOpen($connection, $request); - - /** @var JsonResponse $response */ - $response = array_pop($connection->sentRawData); - - $this->assertSame([], json_decode($response->getContent(), true)); - - $this->statisticsCollector - ->getAppStatistics('1234') - ->then(function ($statistic) { - $this->assertEquals([ - 'peak_connections_count' => 1, - 'websocket_messages_count' => 1, - 'api_messages_count' => 1, - 'app_id' => '1234', - ], $statistic->toArray()); - }); - } - - public function test_it_fires_the_event_to_private_channel() - { - $this->newPresenceConnection('private-channel'); - - $connection = new Mocks\Connection; - - $requestPath = '/apps/1234/events'; - - $routeParams = [ - 'appId' => '1234', - ]; - - $queryString = Pusher::build_auth_query_string( - 'TestKey', 'TestSecret', 'GET', $requestPath, [ - 'channels' => 'private-channel', - ], - ); - - $request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams)); - - $controller = app(TriggerEvent::class); - - $controller->onOpen($connection, $request); - - /** @var JsonResponse $response */ - $response = array_pop($connection->sentRawData); - - $this->assertSame([], json_decode($response->getContent(), true)); - - $this->statisticsCollector - ->getAppStatistics('1234') - ->then(function ($statistic) { - $this->assertEquals([ - 'peak_connections_count' => 1, - 'websocket_messages_count' => 1, - 'api_messages_count' => 1, - 'app_id' => '1234', - ], $statistic->toArray()); - }); - } - - public function test_it_fires_event_across_servers() - { - $connection = new Mocks\Connection; - - $requestPath = '/apps/1234/events'; - - $routeParams = [ - 'appId' => '1234', - ]; - - $queryString = Pusher::build_auth_query_string( - 'TestKey', 'TestSecret', 'GET', $requestPath, [ - 'channels' => 'public-channel', - ], - ); - - $request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams)); - - $controller = app(TriggerEvent::class); - - $controller->onOpen($connection, $request); - - /** @var JsonResponse $response */ - $response = array_pop($connection->sentRawData); - - $this->assertSame([], json_decode($response->getContent(), true)); - - if (method_exists($this->channelManager, 'getPublishClient')) { - $this->channelManager - ->getPublishClient() - ->assertCalledWithArgs('publish', [ - $this->channelManager->getRedisKey('1234', 'public-channel'), - json_encode([ - 'event' => null, - 'channel' => 'public-channel', - 'data' => null, - 'appId' => '1234', - 'socketId' => null, - 'serverId' => $this->channelManager->getServerId(), - ]), - ]); - } - } }