This commit is contained in:
freek 2018-12-03 13:33:00 +01:00
parent f9af6133df
commit 082a631cdc
7 changed files with 55 additions and 2 deletions

View File

@ -25,4 +25,7 @@
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<php>
<env name="DB_CONNECTION" value="testing"/>
</php>
</phpunit>

View File

@ -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',

View File

@ -4,6 +4,7 @@ namespace BeyondCode\LaravelWebSockets\Statistics\Logging;
class Statistic
{
/** @var int|string */
protected $appId;
/** @var int */

View File

@ -0,0 +1,20 @@
<?php
namespace BeyondCode\LaravelWebSockets\Statistics\Rules;
use BeyondCode\LaravelWebSockets\Apps\AppProvider;
use Illuminate\Contracts\Validation\Rule;
class AppId implements Rule
{
public function passes($attribute, $value)
{
$appProvider = app(AppProvider::class);
return $appProvider->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.';
}}

View File

@ -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']);
});
});
}

View File

@ -0,0 +1,18 @@
<?php
namespace BeyondCode\LaravelWebSockets\Tests\Statistics\Rules;
use BeyondCode\LaravelWebSockets\Statistics\Rules\AppId;
use BeyondCode\LaravelWebSockets\Tests\TestCase;
class AppIdTest extends TestCase
{
/** @test */
public function it_can_validate_an_app_id()
{
$rule = new AppId();
$this->assertTrue($rule->passes('app_id', config('websockets.apps.0.id')));
$this->assertFalse($rule->passes('app_id', 'invalid-app-id'));
}
}

View File

@ -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