Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 499a153

Browse files
committed
wip
1 parent f62ac8f commit 499a153

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

src/Dashboard/Http/Controllers/DashboardApiController.php renamed to src/Dashboard/Http/Controllers/ShowStatistics.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver;
66
use Illuminate\Http\Request;
77

8-
class DashboardApiController
8+
class ShowStatistics
99
{
1010
/**
1111
* Get statistics for an app ID.
@@ -15,7 +15,7 @@ class DashboardApiController
1515
* @param mixed $appId
1616
* @return \Illuminate\Http\Response
1717
*/
18-
public function getStatistics(Request $request, StatisticsDriver $driver, $appId)
18+
public function __invoke(Request $request, StatisticsDriver $driver, $appId)
1919
{
2020
return $driver::get($appId, $request);
2121
}

src/WebSocketsServiceProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use BeyondCode\LaravelWebSockets\Apps\AppManager;
66
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\AuthenticateDashboard;
7-
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\DashboardApiController;
7+
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowStatistics;
88
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage;
99
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard;
1010
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize as AuthorizeDashboard;
@@ -124,9 +124,9 @@ protected function registerDashboardRoutes()
124124
'middleware' => config('websockets.dashboard.middleware', [AuthorizeDashboard::class]),
125125
], function () {
126126
Route::get('/', ShowDashboard::class)->name('dashboard');
127-
Route::get('/api/{appId}/statistics', [DashboardApiController::class, 'getStatistics'])->name('statistics');
127+
Route::get('/api/{appId}/statistics', ShowStatistics::class)->name('statistics');
128128
Route::post('/auth', AuthenticateDashboard::class)->name('auth');
129-
Route::post('/event', SendMessage::class)->name('send');
129+
Route::post('/event', SendMessage::class)->name('event');
130130
});
131131

132132
return $this;

tests/Dashboard/StatisticsTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace BeyondCode\LaravelWebSockets\Tests\Dashboard;
4+
5+
use BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger;
6+
use BeyondCode\LaravelWebSockets\Tests\TestCase;
7+
use BeyondCode\LaravelWebSockets\Tests\Models\User;
8+
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
9+
10+
class StatisticsTest extends TestCase
11+
{
12+
/** @test */
13+
public function can_get_statistics()
14+
{
15+
$connection = $this->getConnectedWebSocketConnection(['channel-1']);
16+
17+
$logger = new MemoryStatisticsLogger(
18+
$this->channelManager,
19+
$this->statisticsDriver
20+
);
21+
22+
$logger->webSocketMessage($connection->app->id);
23+
$logger->apiMessage($connection->app->id);
24+
$logger->connection($connection->app->id);
25+
$logger->disconnection($connection->app->id);
26+
27+
$logger->save();
28+
29+
$this->actingAs(factory(User::class)->create())
30+
->json('GET', route('laravel-websockets.statistics', ['appId' => '1234']))
31+
->assertResponseOk()
32+
->seeJsonStructure([
33+
'peak_connections' => ['x', 'y'],
34+
'websocket_message_count' => ['x', 'y'],
35+
'api_message_count' => ['x', 'y'],
36+
]);
37+
}
38+
39+
/** @test */
40+
public function cant_get_statistics_for_invalid_app_id()
41+
{
42+
$connection = $this->getConnectedWebSocketConnection(['channel-1']);
43+
44+
$logger = new MemoryStatisticsLogger(
45+
$this->channelManager,
46+
$this->statisticsDriver
47+
);
48+
49+
$logger->webSocketMessage($connection->app->id);
50+
$logger->apiMessage($connection->app->id);
51+
$logger->connection($connection->app->id);
52+
$logger->disconnection($connection->app->id);
53+
54+
$logger->save();
55+
56+
$this->actingAs(factory(User::class)->create())
57+
->json('GET', route('laravel-websockets.statistics', ['appId' => 'not_found']))
58+
->seeJson([
59+
'peak_connections' => ['x' => [], 'y' => []],
60+
'websocket_message_count' => ['x' => [], 'y' => []],
61+
'api_message_count' => ['x' => [], 'y' => []],
62+
]);
63+
}
64+
}

0 commit comments

Comments
 (0)