Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 2a6d91a

Browse files
committed
Added tests for statistics loggers drivers
1 parent 108a717 commit 2a6d91a

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

src/Statistics/Logger/MemoryStatisticsLogger.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,14 @@ protected function findOrMakeStatisticForAppId($appId): Statistic
125125

126126
return $this->statistics[$appId];
127127
}
128+
129+
/**
130+
* Get the saved statistics.
131+
*
132+
* @return array
133+
*/
134+
public function getStatistics(): array
135+
{
136+
return $this->statistics;
137+
}
128138
}

tests/Statistics/Logger/StatisticsLoggerTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace BeyondCode\LaravelWebSockets\Tests\Statistics\Controllers;
44

55
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
6+
use BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger;
7+
use BeyondCode\LaravelWebSockets\Statistics\Logger\NullStatisticsLogger;
8+
use BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry;
69
use BeyondCode\LaravelWebSockets\Tests\TestCase;
710

811
class StatisticsLoggerTest extends TestCase
@@ -43,4 +46,50 @@ public function it_counts_unique_connections_no_channel_subscriptions()
4346

4447
$this->assertEquals(1, StatisticsLogger::getForAppId(1234)['peak_connection_count']);
4548
}
49+
50+
/** @test */
51+
public function it_counts_connections_with_memory_logger()
52+
{
53+
$connection = $this->getConnectedWebSocketConnection(['channel-1']);
54+
55+
$logger = new MemoryStatisticsLogger(
56+
$this->channelManager,
57+
$this->statisticsDriver
58+
);
59+
60+
$logger->webSocketMessage($connection->app->id);
61+
$logger->apiMessage($connection->app->id);
62+
$logger->connection($connection->app->id);
63+
$logger->disconnection($connection->app->id);
64+
65+
$logger->save();
66+
67+
$this->assertCount(1, WebSocketsStatisticsEntry::all());
68+
69+
$entry = WebSocketsStatisticsEntry::first();
70+
71+
$this->assertEquals(1, $entry->peak_connection_count);
72+
$this->assertEquals(1, $entry->websocket_message_count);
73+
$this->assertEquals(1, $entry->api_message_count);
74+
}
75+
76+
/** @test */
77+
public function it_counts_connections_with_null_logger()
78+
{
79+
$connection = $this->getConnectedWebSocketConnection(['channel-1']);
80+
81+
$logger = new NullStatisticsLogger(
82+
$this->channelManager,
83+
$this->statisticsDriver
84+
);
85+
86+
$logger->webSocketMessage($connection->app->id);
87+
$logger->apiMessage($connection->app->id);
88+
$logger->connection($connection->app->id);
89+
$logger->disconnection($connection->app->id);
90+
91+
$logger->save();
92+
93+
$this->assertCount(0, WebSocketsStatisticsEntry::all());
94+
}
4695
}

tests/TestCase.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,25 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
3131
*/
3232
protected $channelManager;
3333

34+
/**
35+
* The used statistics driver.
36+
*
37+
* @var \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver
38+
*/
39+
protected $statisticsDriver;
40+
3441
/**
3542
* {@inheritdoc}
3643
*/
3744
public function setUp(): void
3845
{
3946
parent::setUp();
4047

41-
$this->pusherServer = app(config('websockets.handlers.websocket'));
48+
$this->pusherServer = $this->app->make(config('websockets.handlers.websocket'));
49+
50+
$this->channelManager = $this->app->make(ChannelManager::class);
4251

43-
$this->channelManager = app(ChannelManager::class);
52+
$this->statisticsDriver = $this->app->make(StatisticsDriver::class);
4453

4554
StatisticsLogger::swap(new FakeStatisticsLogger(
4655
$this->channelManager,

0 commit comments

Comments
 (0)