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

Commit aa014ad

Browse files
committed
Moved all websockets testing related stuff into a concern
1 parent e099c46 commit aa014ad

File tree

2 files changed

+200
-183
lines changed

2 files changed

+200
-183
lines changed

tests/Concerns/TestsWebSockets.php

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
<?php
2+
3+
namespace BeyondCode\LaravelWebSockets\Tests\Concerns;
4+
5+
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
6+
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
7+
use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver;
8+
use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection;
9+
use BeyondCode\LaravelWebSockets\Tests\Mocks\FakeMemoryStatisticsLogger;
10+
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
11+
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
12+
use GuzzleHttp\Psr7\Request;
13+
use Ratchet\ConnectionInterface;
14+
use React\EventLoop\Factory as LoopFactory;
15+
16+
trait TestsWebSockets
17+
{
18+
/**
19+
* A test Pusher server.
20+
*
21+
* @var \BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler
22+
*/
23+
protected $pusherServer;
24+
25+
/**
26+
* The test Channel manager.
27+
*
28+
* @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager
29+
*/
30+
protected $channelManager;
31+
32+
/**
33+
* The used statistics driver.
34+
*
35+
* @var \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver
36+
*/
37+
protected $statisticsDriver;
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function setUp(): void
43+
{
44+
parent::setUp();
45+
46+
$this->pusherServer = $this->app->make(config('websockets.handlers.websocket'));
47+
48+
$this->channelManager = $this->app->make(ChannelManager::class);
49+
50+
$this->statisticsDriver = $this->app->make(StatisticsDriver::class);
51+
52+
StatisticsLogger::swap(new FakeMemoryStatisticsLogger(
53+
$this->channelManager,
54+
app(StatisticsDriver::class)
55+
));
56+
57+
$this->configurePubSub();
58+
}
59+
60+
/**
61+
* Get the websocket connection for a specific URL.
62+
*
63+
* @param mixed $appKey
64+
* @param array $headers
65+
* @return \BeyondCode\LaravelWebSockets\Tests\Mocks\Connection
66+
*/
67+
protected function getWebSocketConnection(string $appKey = 'TestKey', array $headers = []): Connection
68+
{
69+
$connection = new Connection;
70+
71+
$connection->httpRequest = new Request('GET', "/?appKey={$appKey}", $headers);
72+
73+
return $connection;
74+
}
75+
76+
/**
77+
* Get a connected websocket connection.
78+
*
79+
* @param array $channelsToJoin
80+
* @param string $appKey
81+
* @param array $headers
82+
* @return \BeyondCode\LaravelWebSockets\Tests\Mocks\Connection
83+
*/
84+
protected function getConnectedWebSocketConnection(array $channelsToJoin = [], string $appKey = 'TestKey', array $headers = []): Connection
85+
{
86+
$connection = new Connection;
87+
88+
$connection->httpRequest = new Request('GET', "/?appKey={$appKey}", $headers);
89+
90+
$this->pusherServer->onOpen($connection);
91+
92+
foreach ($channelsToJoin as $channel) {
93+
$message = new Message([
94+
'event' => 'pusher:subscribe',
95+
'data' => [
96+
'channel' => $channel,
97+
],
98+
]);
99+
100+
$this->pusherServer->onMessage($connection, $message);
101+
}
102+
103+
return $connection;
104+
}
105+
106+
/**
107+
* Join a presence channel.
108+
*
109+
* @param string $channel
110+
* @return \BeyondCode\LaravelWebSockets\Tests\Mocks\Connection
111+
*/
112+
protected function joinPresenceChannel($channel): Connection
113+
{
114+
$connection = $this->getWebSocketConnection();
115+
116+
$this->pusherServer->onOpen($connection);
117+
118+
$channelData = [
119+
'user_id' => 1,
120+
'user_info' => [
121+
'name' => 'Marcel',
122+
],
123+
];
124+
125+
$signature = "{$connection->socketId}:{$channel}:".json_encode($channelData);
126+
127+
$message = new Message([
128+
'event' => 'pusher:subscribe',
129+
'data' => [
130+
'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret),
131+
'channel' => $channel,
132+
'channel_data' => json_encode($channelData),
133+
],
134+
]);
135+
136+
$this->pusherServer->onMessage($connection, $message);
137+
138+
return $connection;
139+
}
140+
141+
/**
142+
* Get a channel from connection.
143+
*
144+
* @param \Ratchet\ConnectionInterface $connection
145+
* @param string $channelName
146+
* @return \BeyondCode\LaravelWebSockets\WebSockets\Channels\Channel|null
147+
*/
148+
protected function getChannel(ConnectionInterface $connection, string $channelName)
149+
{
150+
return $this->channelManager->findOrCreate($connection->app->id, $channelName);
151+
}
152+
153+
/**
154+
* Configure the replicator clients.
155+
*
156+
* @return void
157+
*/
158+
protected function configurePubSub()
159+
{
160+
// Replace the publish and subscribe clients with a Mocked
161+
// factory lazy instance on boot.
162+
$this->app->singleton(ReplicationInterface::class, function () {
163+
$driver = config('websockets.replication.driver', 'local');
164+
165+
$client = config(
166+
"websockets.replication.{$driver}.client",
167+
\BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient::class
168+
);
169+
170+
return (new $client)->boot(
171+
LoopFactory::create(), Mocks\RedisFactory::class
172+
);
173+
});
174+
}
175+
176+
/**
177+
* Get the subscribed client for the replication.
178+
*
179+
* @return ReplicationInterface
180+
*/
181+
protected function getSubscribeClient()
182+
{
183+
return $this->app
184+
->make(ReplicationInterface::class)
185+
->getSubscribeClient();
186+
}
187+
188+
/**
189+
* Get the publish client for the replication.
190+
*
191+
* @return ReplicationInterface
192+
*/
193+
protected function getPublishClient()
194+
{
195+
return $this->app
196+
->make(ReplicationInterface::class)
197+
->getPublishClient();
198+
}
199+
}

0 commit comments

Comments
 (0)