diff --git a/phpunit.xml.dist b/phpunit.xml.dist index f21ca6f..5102c74 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -25,4 +25,7 @@ + + + diff --git a/src/Statistics/Http/Controllers/WebsocketStatisticsEntriesController.php b/src/Statistics/Http/Controllers/WebsocketStatisticsEntriesController.php index bced6c1..ffab25d 100644 --- a/src/Statistics/Http/Controllers/WebsocketStatisticsEntriesController.php +++ b/src/Statistics/Http/Controllers/WebsocketStatisticsEntriesController.php @@ -2,6 +2,7 @@ namespace BeyondCode\LaravelWebSockets\Statistics\Http\Controllers; +use BeyondCode\LaravelWebSockets\Statistics\Rules\AppId; use BeyondCode\LaravelWebSockets\Statistics\WebSocketsStatisticsEntry; use Illuminate\Http\Request; @@ -10,12 +11,14 @@ class WebsocketStatisticsEntriesController public function store(Request $request) { $validatedAttributes = $request->validate([ - 'app_id' => 'required', + 'app_id' => ['required', new AppId()], 'peak_connections' => 'required|integer', 'websocket_message_count' => 'required|integer', 'api_message_count' => 'required|integer', ]); WebSocketsStatisticsEntry::create($validatedAttributes); + + return 'ok'; } } \ No newline at end of file diff --git a/src/Statistics/Logging/Statistic.php b/src/Statistics/Logging/Statistic.php index d4ce2a3..951ea99 100644 --- a/src/Statistics/Logging/Statistic.php +++ b/src/Statistics/Logging/Statistic.php @@ -6,6 +6,7 @@ use BeyondCode\LaravelWebSockets\Apps\App; class Statistic { + /** @var int|string */ protected $appId; /** @var int */ diff --git a/src/Statistics/Rules/AppId.php b/src/Statistics/Rules/AppId.php new file mode 100644 index 0000000..da6d62f --- /dev/null +++ b/src/Statistics/Rules/AppId.php @@ -0,0 +1,20 @@ +findById($value) ? true : false; + } + + public function message() + { + return 'There is no app registered with the given id. Make sure the websockets config file contains an app for this id or that your custom AppProvider returns an app for this id.'; +}} \ No newline at end of file diff --git a/src/WebSocketsServiceProvider.php b/src/WebSocketsServiceProvider.php index c6169d9..dea55a6 100644 --- a/src/WebSocketsServiceProvider.php +++ b/src/WebSocketsServiceProvider.php @@ -7,6 +7,7 @@ use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage; use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard; use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize; use BeyondCode\LaravelWebSockets\Server\Router; +use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController; use BeyondCode\LaravelWebSockets\Statistics\Logging\Logger; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Route; @@ -62,12 +63,17 @@ class WebSocketsServiceProvider extends ServiceProvider protected function registerRouteMacro() { - Route::macro('webSocketsDashboard', function($prefix = 'websockets') { + Route::macro('webSockets', function($prefix = 'websockets') { Route::prefix($prefix)->namespace('\\')->middleware(Authorize::class)->group(function() { Route::get('/', ShowDashboard::class); Route::post('auth', AuthenticateDashboard::class); Route::post('event', SendMessage::class); }); + + //TODO: add middleware + Route::prefix($prefix)->namespace('\\')->group(function() { + Route::post('statistics', [WebsocketStatisticsEntriesController::class, 'store']); + }); }); } diff --git a/tests/Statistics/Rules/AppIdTest.php b/tests/Statistics/Rules/AppIdTest.php new file mode 100644 index 0000000..56d344c --- /dev/null +++ b/tests/Statistics/Rules/AppIdTest.php @@ -0,0 +1,18 @@ +assertTrue($rule->passes('app_id', config('websockets.apps.0.id'))); + $this->assertFalse($rule->passes('app_id', 'invalid-app-id')); + } +} \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php index c4e10b1..2c63920 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -44,6 +44,10 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase 'enable_statistics' => true, ], ]); + + include_once __DIR__.'/../database/migrations/create_websockets_statistics_entries_table.php.stub'; + + (new \CreateWebSocketsStatisticsEntriesTable())->up(); } protected function getWebSocketConnection(string $url = '/?appKey=TestKey'): Connection