From 0dde250b19bafae4b0d2c23ab00e957e625b4b69 Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Tue, 15 Sep 2020 18:28:39 +0200 Subject: [PATCH] Add test to ensure presence channel HTTP API responses are correct --- tests/HttpApi/FetchChannelTest.php | 33 ++++++++++++++++++++++++++++++ tests/TestCase.php | 4 ++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/tests/HttpApi/FetchChannelTest.php b/tests/HttpApi/FetchChannelTest.php index 262f93c..2535b32 100644 --- a/tests/HttpApi/FetchChannelTest.php +++ b/tests/HttpApi/FetchChannelTest.php @@ -66,6 +66,39 @@ class FetchChannelTest extends TestCase ], json_decode($response->getContent(), true)); } + /** @test */ + public function it_returns_the_channel_information_for_presence_channel() + { + $this->joinPresenceChannel('presence-global', 'user:1'); + $this->joinPresenceChannel('presence-global', 'user:2'); + $this->joinPresenceChannel('presence-global', 'user:2'); + + $connection = new Connection(); + + $requestPath = "/apps/1234/channel/presence-global"; + $routeParams = [ + 'appId' => '1234', + 'channelName' => 'presence-global', + ]; + + $queryString = Pusher::build_auth_query_string('TestKey', 'TestSecret', 'GET', $requestPath); + + $request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams)); + + $controller = app(FetchChannelController::class); + + $controller->onOpen($connection, $request); + + /** @var JsonResponse $response */ + $response = array_pop($connection->sentRawData); + + $this->assertSame([ + 'occupied' => true, + 'subscription_count' => 3, + 'user_count' => 2, + ], json_decode($response->getContent(), true)); + } + /** @test */ public function it_returns_404_for_invalid_channels() { diff --git a/tests/TestCase.php b/tests/TestCase.php index c52e83b..ea5168f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -89,14 +89,14 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase return $connection; } - protected function joinPresenceChannel($channel): Connection + protected function joinPresenceChannel($channel, $userId = null): Connection { $connection = $this->getWebSocketConnection(); $this->pusherServer->onOpen($connection); $channelData = [ - 'user_id' => 1, + 'user_id' => $userId ?? 1, 'user_info' => [ 'name' => 'Marcel', ],