diff --git a/src/Dashboard/Http/Controllers/DashboardApiController.php b/src/Dashboard/Http/Controllers/ShowStatistics.php similarity index 81% rename from src/Dashboard/Http/Controllers/DashboardApiController.php rename to src/Dashboard/Http/Controllers/ShowStatistics.php index c240905..134cb62 100644 --- a/src/Dashboard/Http/Controllers/DashboardApiController.php +++ b/src/Dashboard/Http/Controllers/ShowStatistics.php @@ -5,7 +5,7 @@ namespace BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers; use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver; use Illuminate\Http\Request; -class DashboardApiController +class ShowStatistics { /** * Get statistics for an app ID. @@ -15,7 +15,7 @@ class DashboardApiController * @param mixed $appId * @return \Illuminate\Http\Response */ - public function getStatistics(Request $request, StatisticsDriver $driver, $appId) + public function __invoke(Request $request, StatisticsDriver $driver, $appId) { return $driver::get($appId, $request); } diff --git a/src/WebSocketsServiceProvider.php b/src/WebSocketsServiceProvider.php index fd9853b..12fb58f 100644 --- a/src/WebSocketsServiceProvider.php +++ b/src/WebSocketsServiceProvider.php @@ -4,7 +4,7 @@ namespace BeyondCode\LaravelWebSockets; use BeyondCode\LaravelWebSockets\Apps\AppManager; use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\AuthenticateDashboard; -use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\DashboardApiController; +use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowStatistics; use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage; use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard; use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize as AuthorizeDashboard; @@ -124,9 +124,9 @@ class WebSocketsServiceProvider extends ServiceProvider 'middleware' => config('websockets.dashboard.middleware', [AuthorizeDashboard::class]), ], function () { Route::get('/', ShowDashboard::class)->name('dashboard'); - Route::get('/api/{appId}/statistics', [DashboardApiController::class, 'getStatistics'])->name('statistics'); + Route::get('/api/{appId}/statistics', ShowStatistics::class)->name('statistics'); Route::post('/auth', AuthenticateDashboard::class)->name('auth'); - Route::post('/event', SendMessage::class)->name('send'); + Route::post('/event', SendMessage::class)->name('event'); }); return $this; diff --git a/tests/Dashboard/StatisticsTest.php b/tests/Dashboard/StatisticsTest.php new file mode 100644 index 0000000..26c2d27 --- /dev/null +++ b/tests/Dashboard/StatisticsTest.php @@ -0,0 +1,64 @@ +getConnectedWebSocketConnection(['channel-1']); + + $logger = new MemoryStatisticsLogger( + $this->channelManager, + $this->statisticsDriver + ); + + $logger->webSocketMessage($connection->app->id); + $logger->apiMessage($connection->app->id); + $logger->connection($connection->app->id); + $logger->disconnection($connection->app->id); + + $logger->save(); + + $this->actingAs(factory(User::class)->create()) + ->json('GET', route('laravel-websockets.statistics', ['appId' => '1234'])) + ->assertResponseOk() + ->seeJsonStructure([ + 'peak_connections' => ['x', 'y'], + 'websocket_message_count' => ['x', 'y'], + 'api_message_count' => ['x', 'y'], + ]); + } + + /** @test */ + public function cant_get_statistics_for_invalid_app_id() + { + $connection = $this->getConnectedWebSocketConnection(['channel-1']); + + $logger = new MemoryStatisticsLogger( + $this->channelManager, + $this->statisticsDriver + ); + + $logger->webSocketMessage($connection->app->id); + $logger->apiMessage($connection->app->id); + $logger->connection($connection->app->id); + $logger->disconnection($connection->app->id); + + $logger->save(); + + $this->actingAs(factory(User::class)->create()) + ->json('GET', route('laravel-websockets.statistics', ['appId' => 'not_found'])) + ->seeJson([ + 'peak_connections' => ['x' => [], 'y' => []], + 'websocket_message_count' => ['x' => [], 'y' => []], + 'api_message_count' => ['x' => [], 'y' => []], + ]); + } +}