diff --git a/src/HttpApi/Controllers/FetchChannelsController.php b/src/HttpApi/Controllers/FetchChannelsController.php index 937000e08b..22d8c1fdd3 100644 --- a/src/HttpApi/Controllers/FetchChannelsController.php +++ b/src/HttpApi/Controllers/FetchChannelsController.php @@ -25,7 +25,7 @@ public function __invoke(Request $request) return [ 'user_count' => count($channel->getUsers()), ]; - })->toArray(), + })->toArray() ?: new \stdClass, ]; } } diff --git a/tests/HttpApi/FetchChannelsTest.php b/tests/HttpApi/FetchChannelsTest.php index 088dabfe5b..94a6eb83b7 100644 --- a/tests/HttpApi/FetchChannelsTest.php +++ b/tests/HttpApi/FetchChannelsTest.php @@ -80,4 +80,35 @@ public function it_returns_the_channel_information() ], ], json_decode($response->getContent(), true)); } + + /** @test */ + public function it_returns_empty_object_for_no_channels_found() + { + $connection = new Connection(); + + $auth_key = 'TestKey'; + $auth_timestamp = time(); + $auth_version = '1.0'; + + $queryParameters = http_build_query(compact('auth_key', 'auth_timestamp', 'auth_version')); + + $signature = + "GET\n/apps/1234/channels\n". + "auth_key={$auth_key}". + "&auth_timestamp={$auth_timestamp}". + "&auth_version={$auth_version}"; + + $auth_signature = hash_hmac('sha256', $signature, 'TestSecret'); + + $request = new Request('GET', "/apps/1234/channels?appId=1234&auth_signature={$auth_signature}&{$queryParameters}"); + + $controller = app(FetchChannelsController::class); + + $controller->onOpen($connection, $request); + + /** @var JsonResponse $response */ + $response = array_pop($connection->sentRawData); + + $this->assertSame('{"channels":{}}', $response->getContent()); + } }