fix tests
This commit is contained in:
parent
3d94b97158
commit
2a23f2b6fb
|
|
@ -2,13 +2,19 @@
|
|||
|
||||
namespace BeyondCode\LaravelWebSockets\Facades;
|
||||
|
||||
use BeyondCode\LaravelWebSockets\Statistics\Logging\FakeStatisticsLogger;
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
/** @see \BeyondCode\LaravelWebSockets\Statistics\Logging\StatisticsLogger */
|
||||
/** @see \BeyondCode\LaravelWebSockets\Statistics\Logging\HttpStatisticsLogger */
|
||||
class StatisticsLogger extends Facade
|
||||
{
|
||||
protected static function getFacadeAccessor()
|
||||
{
|
||||
return 'websockets.statisticslogger';
|
||||
}
|
||||
|
||||
public static function fake()
|
||||
{
|
||||
static::swap(new FakeStatisticsLogger());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
namespace BeyondCode\LaravelWebSockets\Statistics\Logging;
|
||||
|
||||
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController;
|
||||
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
|
||||
use GuzzleHttp\Client;
|
||||
use Ratchet\ConnectionInterface;
|
||||
|
||||
class FakeStatisticsLogger implements StatisticsLogger
|
||||
{
|
||||
|
||||
public function logWebSocketMessage(ConnectionInterface $connection)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function logApiMessage($appId)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function logConnection(ConnectionInterface $connection)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function logDisconnection(ConnectionInterface $connection)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected function initializeStatistics($id)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
namespace BeyondCode\LaravelWebSockets\Statistics\Logging;
|
||||
|
||||
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController;
|
||||
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
|
||||
use GuzzleHttp\Client;
|
||||
use Ratchet\ConnectionInterface;
|
||||
|
||||
class HttpStatisticsLogger implements StatisticsLogger
|
||||
{
|
||||
/** @var Statistic[] */
|
||||
protected $statistics = [];
|
||||
|
||||
/** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */
|
||||
protected $channelManager;
|
||||
|
||||
public function __construct(ChannelManager $channelManager, Client $client)
|
||||
{
|
||||
$this->channelManager = $channelManager;
|
||||
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
public function logWebSocketMessage(ConnectionInterface $connection)
|
||||
{
|
||||
$this->initializeStatistics($connection->app->id);
|
||||
|
||||
$this->statistics[$connection->app->id]->logWebSocketMessage();
|
||||
}
|
||||
|
||||
public function logApiMessage($appId)
|
||||
{
|
||||
$this->initializeStatistics($appId);
|
||||
|
||||
$this->statistics[$appId]->logApiMessage();
|
||||
}
|
||||
|
||||
public function logConnection(ConnectionInterface $connection)
|
||||
{
|
||||
$this->initializeStatistics($connection->app->id);
|
||||
|
||||
$this->statistics[$connection->app->id]->logConnection();
|
||||
}
|
||||
|
||||
public function logDisconnection(ConnectionInterface $connection)
|
||||
{
|
||||
$this->initializeStatistics($connection->app->id);
|
||||
|
||||
$this->statistics[$connection->app->id]->logDisconnection();
|
||||
}
|
||||
|
||||
protected function initializeStatistics($id)
|
||||
{
|
||||
if (!isset($this->statistics[$id])) {
|
||||
$this->statistics[$id] = new Statistic($id);
|
||||
}
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
foreach ($this->statistics as $appId => $statistic) {
|
||||
if (! $statistic->isEnabled()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->client->postAsync(
|
||||
action([WebsocketStatisticsEntriesController::class, 'store']),
|
||||
$statistic->toArray()
|
||||
);
|
||||
|
||||
// Reset connection and message count
|
||||
$currentConnectionCount = collect($this->channelManager->getChannels($appId))
|
||||
->sum(function ($channel) {
|
||||
return count($channel->getSubscribedConnections());
|
||||
});
|
||||
|
||||
$statistic->reset($currentConnectionCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,80 +2,17 @@
|
|||
|
||||
namespace BeyondCode\LaravelWebSockets\Statistics\Logging;
|
||||
|
||||
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController;
|
||||
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
|
||||
use GuzzleHttp\Client;
|
||||
use Ratchet\ConnectionInterface;
|
||||
|
||||
class StatisticsLogger
|
||||
interface StatisticsLogger
|
||||
{
|
||||
/** @var Statistic[] */
|
||||
protected $statistics = [];
|
||||
public function logWebSocketMessage(ConnectionInterface $connection);
|
||||
|
||||
/** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */
|
||||
protected $channelManager;
|
||||
public function logApiMessage($appId);
|
||||
|
||||
public function __construct(ChannelManager $channelManager, Client $client)
|
||||
{
|
||||
$this->channelManager = $channelManager;
|
||||
public function logConnection(ConnectionInterface $connection);
|
||||
|
||||
$this->client = $client;
|
||||
}
|
||||
public function logDisconnection(ConnectionInterface $connection);
|
||||
|
||||
public function logWebSocketMessage(ConnectionInterface $connection)
|
||||
{
|
||||
$this->initializeStatistics($connection->app->id);
|
||||
|
||||
$this->statistics[$connection->app->id]->logWebSocketMessage();
|
||||
}
|
||||
|
||||
public function logApiMessage($appId)
|
||||
{
|
||||
$this->initializeStatistics($appId);
|
||||
|
||||
$this->statistics[$appId]->logApiMessage();
|
||||
}
|
||||
|
||||
public function logConnection(ConnectionInterface $connection)
|
||||
{
|
||||
$this->initializeStatistics($connection->app->id);
|
||||
|
||||
$this->statistics[$connection->app->id]->logConnection();
|
||||
}
|
||||
|
||||
public function logDisconnection(ConnectionInterface $connection)
|
||||
{
|
||||
$this->initializeStatistics($connection->app->id);
|
||||
|
||||
$this->statistics[$connection->app->id]->logDisconnection();
|
||||
}
|
||||
|
||||
protected function initializeStatistics($id)
|
||||
{
|
||||
if (!isset($this->statistics[$id])) {
|
||||
$this->statistics[$id] = new Statistic($id);
|
||||
}
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
foreach ($this->statistics as $appId => $statistic) {
|
||||
if (! $statistic->isEnabled()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->client->postAsync(
|
||||
action([WebsocketStatisticsEntriesController::class, 'store']),
|
||||
$statistic->toArray()
|
||||
);
|
||||
|
||||
// Reset connection and message count
|
||||
$currentConnectionCount = collect($this->channelManager->getChannels($appId))
|
||||
->sum(function ($channel) {
|
||||
return count($channel->getSubscribedConnections());
|
||||
});
|
||||
|
||||
$statistic->reset($currentConnectionCount);
|
||||
}
|
||||
}
|
||||
public function save();
|
||||
}
|
||||
|
|
@ -8,7 +8,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 BeyondCode\LaravelWebSockets\Statistics\Logging\StatisticsLogger;
|
||||
use BeyondCode\LaravelWebSockets\Statistics\Logging\HttpStatisticsLogger;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use BeyondCode\LaravelWebSockets\Apps\AppProvider;
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace BeyondCode\LaravelWebSockets\Tests;
|
||||
|
||||
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
|
||||
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
|
||||
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
|
||||
use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler;
|
||||
|
|
@ -29,6 +30,8 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
$this->channelManager = app(ChannelManager::class);
|
||||
|
||||
Route::webSockets();
|
||||
|
||||
StatisticsLogger::fake();
|
||||
}
|
||||
|
||||
protected function getPackageProviders($app)
|
||||
|
|
|
|||
Loading…
Reference in New Issue