From cbf8cbf1dc41f94ff62299652b595e5bcd4fc3f0 Mon Sep 17 00:00:00 2001 From: freek Date: Mon, 3 Dec 2018 13:49:56 +0100 Subject: [PATCH] wip --- config/websockets.php | 2 +- .../WebsocketStatisticsEntriesController.php | 2 +- .../WebSocketsStatisticsEntry.php | 2 +- src/WebSocketsServiceProvider.php | 4 +- .../WebSocketsStatisticsControllerTest.php | 37 +++++++++++++++++++ tests/TestCase.php | 5 +++ 6 files changed, 47 insertions(+), 5 deletions(-) rename src/Statistics/{ => Models}/WebSocketsStatisticsEntry.php (75%) create mode 100644 tests/Statistics/Controllers/WebSocketsStatisticsControllerTest.php diff --git a/config/websockets.php b/config/websockets.php index 0904b0e..693f861 100644 --- a/config/websockets.php +++ b/config/websockets.php @@ -45,7 +45,7 @@ return [ /* * This model will be used to store the statistics of the WebSocketsServer */ - 'statistics_model' => \BeyondCode\LaravelWebSockets\Statistics\WebSocketsStatisticsEntry::class, + 'statistics_model' => \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry::class, /* * Define the optional SSL context for your WebSocket connections. diff --git a/src/Statistics/Http/Controllers/WebsocketStatisticsEntriesController.php b/src/Statistics/Http/Controllers/WebsocketStatisticsEntriesController.php index ffab25d..38c4d01 100644 --- a/src/Statistics/Http/Controllers/WebsocketStatisticsEntriesController.php +++ b/src/Statistics/Http/Controllers/WebsocketStatisticsEntriesController.php @@ -3,7 +3,7 @@ namespace BeyondCode\LaravelWebSockets\Statistics\Http\Controllers; use BeyondCode\LaravelWebSockets\Statistics\Rules\AppId; -use BeyondCode\LaravelWebSockets\Statistics\WebSocketsStatisticsEntry; +use BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry; use Illuminate\Http\Request; class WebsocketStatisticsEntriesController diff --git a/src/Statistics/WebSocketsStatisticsEntry.php b/src/Statistics/Models/WebSocketsStatisticsEntry.php similarity index 75% rename from src/Statistics/WebSocketsStatisticsEntry.php rename to src/Statistics/Models/WebSocketsStatisticsEntry.php index 2569306..637047e 100644 --- a/src/Statistics/WebSocketsStatisticsEntry.php +++ b/src/Statistics/Models/WebSocketsStatisticsEntry.php @@ -1,6 +1,6 @@ namespace('\\')->middleware(Authorize::class)->group(function() { + Route::prefix($prefix)->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::prefix($prefix)->group(function() { Route::post('statistics', [WebsocketStatisticsEntriesController::class, 'store']); }); }); diff --git a/tests/Statistics/Controllers/WebSocketsStatisticsControllerTest.php b/tests/Statistics/Controllers/WebSocketsStatisticsControllerTest.php new file mode 100644 index 0000000..c037377 --- /dev/null +++ b/tests/Statistics/Controllers/WebSocketsStatisticsControllerTest.php @@ -0,0 +1,37 @@ +withoutExceptionHandling(); + + $this->post( + action([WebsocketStatisticsEntriesController::class, 'store']), + $this->payload() + ); + + $entries = WebSocketsStatisticsEntry::get(); + + $this->assertCount(1, $entries); + + $this->assertArraySubset($this->payload(), $entries->first()->attributesToArray()); + } + + protected function payload(): array + { + return [ + 'app_id' => config('websockets.apps.0.id'), + 'peak_connections' => 1, + 'websocket_message_count' => 2, + 'api_message_count' => 3, + ]; + } +} \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php index 2c63920..be99228 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -8,6 +8,7 @@ use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler; use GuzzleHttp\Psr7\Request; use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection; use BeyondCode\LaravelWebSockets\WebSocketsServiceProvider; +use Illuminate\Support\Facades\Route; use Ratchet\ConnectionInterface; abstract class TestCase extends \Orchestra\Testbench\TestCase @@ -25,6 +26,8 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase $this->pusherServer = app(WebSocketHandler::class); $this->channelManager = app(ChannelManager::class); + + Route::webSockets(); } protected function getPackageProviders($app) @@ -48,6 +51,8 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase include_once __DIR__.'/../database/migrations/create_websockets_statistics_entries_table.php.stub'; (new \CreateWebSocketsStatisticsEntriesTable())->up(); + + } protected function getWebSocketConnection(string $url = '/?appKey=TestKey'): Connection