Permalink
Browse files

wip

  • Loading branch information...
freekmurze committed Dec 3, 2018
1 parent f9af613 commit 082a631cdc794af16ab2140fcc417c5791f40c39
@@ -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>
@@ -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',
@@ -4,6 +4,7 @@
class Statistic
{
/** @var int|string */
protected $appId;
/** @var int */
@@ -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.';
}}
@@ -7,6 +7,7 @@
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 @@ public function register()
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']);
});
});
}
@@ -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'));
}
}
@@ -44,6 +44,10 @@ protected function getEnvironmentSetUp($app)
'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

0 comments on commit 082a631

Please sign in to comment.