diff --git a/src/Statistics/Logger/MemoryStatisticsLogger.php b/src/Statistics/Logger/MemoryStatisticsLogger.php index 43c9db4..a0bee8e 100644 --- a/src/Statistics/Logger/MemoryStatisticsLogger.php +++ b/src/Statistics/Logger/MemoryStatisticsLogger.php @@ -125,4 +125,14 @@ class MemoryStatisticsLogger implements StatisticsLogger return $this->statistics[$appId]; } + + /** + * Get the saved statistics. + * + * @return array + */ + public function getStatistics(): array + { + return $this->statistics; + } } diff --git a/tests/Statistics/Logger/StatisticsLoggerTest.php b/tests/Statistics/Logger/StatisticsLoggerTest.php index 49abd19..c7f2365 100644 --- a/tests/Statistics/Logger/StatisticsLoggerTest.php +++ b/tests/Statistics/Logger/StatisticsLoggerTest.php @@ -3,6 +3,9 @@ namespace BeyondCode\LaravelWebSockets\Tests\Statistics\Controllers; use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger; +use BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger; +use BeyondCode\LaravelWebSockets\Statistics\Logger\NullStatisticsLogger; +use BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry; use BeyondCode\LaravelWebSockets\Tests\TestCase; class StatisticsLoggerTest extends TestCase @@ -43,4 +46,50 @@ class StatisticsLoggerTest extends TestCase $this->assertEquals(1, StatisticsLogger::getForAppId(1234)['peak_connection_count']); } + + /** @test */ + public function it_counts_connections_with_memory_logger() + { + $connection = $this->getConnectedWebSocketConnection(['channel-1']); + + $logger = new MemoryStatisticsLogger( + $this->channelManager, + $this->statisticsDriver + ); + + $logger->webSocketMessage($connection->app->id); + $logger->apiMessage($connection->app->id); + $logger->connection($connection->app->id); + $logger->disconnection($connection->app->id); + + $logger->save(); + + $this->assertCount(1, WebSocketsStatisticsEntry::all()); + + $entry = WebSocketsStatisticsEntry::first(); + + $this->assertEquals(1, $entry->peak_connection_count); + $this->assertEquals(1, $entry->websocket_message_count); + $this->assertEquals(1, $entry->api_message_count); + } + + /** @test */ + public function it_counts_connections_with_null_logger() + { + $connection = $this->getConnectedWebSocketConnection(['channel-1']); + + $logger = new NullStatisticsLogger( + $this->channelManager, + $this->statisticsDriver + ); + + $logger->webSocketMessage($connection->app->id); + $logger->apiMessage($connection->app->id); + $logger->connection($connection->app->id); + $logger->disconnection($connection->app->id); + + $logger->save(); + + $this->assertCount(0, WebSocketsStatisticsEntry::all()); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 81bd261..2062a83 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -31,6 +31,13 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase */ protected $channelManager; + /** + * The used statistics driver. + * + * @var \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver + */ + protected $statisticsDriver; + /** * {@inheritdoc} */ @@ -38,9 +45,11 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase { parent::setUp(); - $this->pusherServer = app(config('websockets.handlers.websocket')); + $this->pusherServer = $this->app->make(config('websockets.handlers.websocket')); - $this->channelManager = app(ChannelManager::class); + $this->channelManager = $this->app->make(ChannelManager::class); + + $this->statisticsDriver = $this->app->make(StatisticsDriver::class); StatisticsLogger::swap(new FakeStatisticsLogger( $this->channelManager,