From 082a631cdc794af16ab2140fcc417c5791f40c39 Mon Sep 17 00:00:00 2001 From: freek Date: Mon, 3 Dec 2018 13:33:00 +0100 Subject: [PATCH] wip --- phpunit.xml.dist | 3 +++ .../WebsocketStatisticsEntriesController.php | 3 ++- src/Statistics/Logging/Statistic.php | 1 + src/Statistics/Rules/AppId.php | 20 +++++++++++++++++++ src/WebSocketsServiceProvider.php | 8 +++++++- tests/Statistics/Rules/AppIdTest.php | 18 +++++++++++++++++ tests/TestCase.php | 4 ++++ 7 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/Statistics/Rules/AppId.php create mode 100644 tests/Statistics/Rules/AppIdTest.php 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 d698626..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,7 +11,7 @@ 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', diff --git a/src/Statistics/Logging/Statistic.php b/src/Statistics/Logging/Statistic.php index 1fde485..e3e53dd 100644 --- a/src/Statistics/Logging/Statistic.php +++ b/src/Statistics/Logging/Statistic.php @@ -4,6 +4,7 @@ namespace BeyondCode\LaravelWebSockets\Statistics\Logging; 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 2064067..eeb36fb 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 Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Route; use BeyondCode\LaravelWebSockets\Apps\AppProvider; @@ -57,12 +58,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