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

Commit 7a651d7

Browse files
committed
Fixed tests
1 parent 9856fb6 commit 7a651d7

File tree

5 files changed

+221
-32
lines changed

5 files changed

+221
-32
lines changed

tests/Dashboard/AuthTest.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace BeyondCode\LaravelWebSockets\Test\Dashboard;
44

5-
use BeyondCode\LaravelWebSockets\Test\Mocks\Message;
5+
use BeyondCode\LaravelWebSockets\Test\Mocks\SignedMessage;
66
use BeyondCode\LaravelWebSockets\Test\Models\User;
77
use BeyondCode\LaravelWebSockets\Test\TestCase;
88

@@ -31,17 +31,12 @@ public function test_can_authenticate_dashboard_over_private_channel()
3131

3232
$this->pusherServer->onOpen($connection);
3333

34-
$signature = "{$connection->socketId}:private-channel";
35-
36-
$hashedAppSecret = hash_hmac('sha256', $signature, $connection->app->secret);
37-
38-
$message = new Message([
34+
$message = new SignedMessage([
3935
'event' => 'pusher:subscribe',
4036
'data' => [
41-
'auth' => "{$connection->app->key}:{$hashedAppSecret}",
4237
'channel' => 'private-channel',
4338
],
44-
]);
39+
], $connection, 'private-channel');
4540

4641
$this->pusherServer->onMessage($connection, $message);
4742

@@ -65,23 +60,20 @@ public function test_can_authenticate_dashboard_over_presence_channel()
6560

6661
$this->pusherServer->onOpen($connection);
6762

68-
$channelData = [
63+
$user = json_encode([
6964
'user_id' => 1,
7065
'user_info' => [
7166
'name' => 'Rick',
7267
],
73-
];
74-
75-
$signature = "{$connection->socketId}:presence-channel:".json_encode($channelData);
68+
]);
7669

77-
$message = new Message([
70+
$message = new SignedMessage([
7871
'event' => 'pusher:subscribe',
7972
'data' => [
80-
'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret),
8173
'channel' => 'presence-channel',
82-
'channel_data' => json_encode($channelData),
74+
'channel_data' => $user,
8375
],
84-
]);
76+
], $connection, 'presence-channel', $user);
8577

8678
$this->pusherServer->onMessage($connection, $message);
8779

tests/Mocks/Message.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,24 @@ public function getPayload(): string
3333
{
3434
return json_encode($this->payload);
3535
}
36+
37+
/**
38+
* Get the payload as object.
39+
*
40+
* @return stdClass
41+
*/
42+
public function getPayloadAsObject()
43+
{
44+
return json_decode($this->getPayload());
45+
}
46+
47+
/**
48+
* Get the payload as array.
49+
*
50+
* @return stdClass
51+
*/
52+
public function getPayloadAsArray(): array
53+
{
54+
return $this->payload;
55+
}
3656
}

tests/PrivateChannelTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,12 @@ public function test_connect_to_private_channel_with_valid_signature()
3131

3232
$this->pusherServer->onOpen($connection);
3333

34-
$signature = "{$connection->socketId}:private-channel";
35-
$hashedAppSecret = hash_hmac('sha256', $signature, $connection->app->secret);
36-
37-
$message = new Mocks\Message([
34+
$message = new Mocks\SignedMessage([
3835
'event' => 'pusher:subscribe',
3936
'data' => [
40-
'auth' => "{$connection->app->key}:{$hashedAppSecret}",
4137
'channel' => 'private-channel',
4238
],
43-
]);
39+
], $connection, 'private-channel');
4440

4541
$this->pusherServer->onMessage($connection, $message);
4642

tests/ReplicationTest.php

Lines changed: 189 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,115 @@ public function test_publishing_client_gets_subscribed()
2525
->assertCalledWithArgs('subscribe', [$this->channelManager->getRedisKey('1234', 'public-channel')]);
2626
}
2727

28-
public function test_events_get_replicated_across_connections()
28+
public function test_events_get_replicated_across_connections_for_public_channels()
2929
{
3030
$connection = $this->newActiveConnection(['public-channel']);
31+
$receiver = $this->newActiveConnection(['public-channel']);
3132

32-
$message = [
33+
$message = new Mocks\Message([
3334
'appId' => '1234',
3435
'serverId' => $this->channelManager->getServerId(),
3536
'event' => 'some-event',
3637
'data' => [
3738
'channel' => 'public-channel',
3839
'test' => 'yes',
3940
],
40-
];
41+
'socketId' => $connection->socketId,
42+
]);
4143

4244
$channel = $this->channelManager->find('1234', 'public-channel');
4345

4446
$channel->broadcastToEveryoneExcept(
45-
(object) $message, null, '1234', true
47+
$message->getPayloadAsObject(), $connection->socketId, '1234', true
4648
);
4749

48-
$connection->assertSentEvent('some-event', [
50+
$receiver->assertSentEvent('some-event', $message->getPayloadAsArray());
51+
52+
$this->getSubscribeClient()
53+
->assertNothingDispatched();
54+
55+
$this->getPublishClient()
56+
->assertCalledWithArgs('publish', [
57+
$this->channelManager->getRedisKey('1234', 'public-channel'),
58+
$message->getPayload()
59+
]);
60+
}
61+
62+
public function test_events_get_replicated_across_connections_for_private_channels()
63+
{
64+
$connection = $this->newPrivateConnection('private-channel');
65+
$receiver = $this->newPrivateConnection('private-channel');
66+
67+
$message = new Mocks\SignedMessage([
4968
'appId' => '1234',
5069
'serverId' => $this->channelManager->getServerId(),
51-
'data' => ['channel' => 'public-channel', 'test' => 'yes'],
52-
]);
70+
'event' => 'some-event',
71+
'data' => [
72+
'channel' => 'private-channel',
73+
'test' => 'yes',
74+
],
75+
'socketId' => $connection->socketId,
76+
], $connection, 'private-channel');
77+
78+
$channel = $this->channelManager->find('1234', 'private-channel');
79+
80+
$channel->broadcastToEveryoneExcept(
81+
$message->getPayloadAsObject(), $connection->socketId, '1234', true
82+
);
83+
84+
$receiver->assertSentEvent('some-event', $message->getPayloadAsArray());
5385

5486
$this->getSubscribeClient()
5587
->assertNothingDispatched();
5688

5789
$this->getPublishClient()
5890
->assertCalledWithArgs('publish', [
59-
$this->channelManager->getRedisKey('1234', 'public-channel'),
60-
json_encode($message),
91+
$this->channelManager->getRedisKey('1234', 'private-channel'),
92+
$message->getPayload()
93+
]);
94+
}
95+
96+
public function test_events_get_replicated_across_connections_for_presence_channels()
97+
{
98+
$connection = $this->newPresenceConnection('presence-channel');
99+
$receiver = $this->newPresenceConnection('presence-channel', ['user_id' => 2]);
100+
101+
$user = [
102+
'user_id' => 1,
103+
'user_info' => [
104+
'name' => 'Rick',
105+
],
106+
];
107+
108+
$encodedUser = json_encode($user);
109+
110+
$message = new Mocks\SignedMessage([
111+
'appId' => '1234',
112+
'serverId' => $this->channelManager->getServerId(),
113+
'event' => 'some-event',
114+
'data' => [
115+
'channel' => 'presence-channel',
116+
'channel_data' => $encodedUser,
117+
'test' => 'yes',
118+
],
119+
'socketId' => $connection->socketId,
120+
], $connection, 'presence-channel', $encodedUser);
121+
122+
$channel = $this->channelManager->find('1234', 'presence-channel');
123+
124+
$channel->broadcastToEveryoneExcept(
125+
$message->getPayloadAsObject(), $connection->socketId, '1234', true
126+
);
127+
128+
$receiver->assertSentEvent('some-event', $message->getPayloadAsArray());
129+
130+
$this->getSubscribeClient()
131+
->assertNothingDispatched();
132+
133+
$this->getPublishClient()
134+
->assertCalledWithArgs('publish', [
135+
$this->channelManager->getRedisKey('1234', 'presence-channel'),
136+
$message->getPayload()
61137
]);
62138
}
63139

@@ -186,4 +262,108 @@ public function test_not_ponged_connections_do_get_removed_for_presence_channels
186262
$this->assertCount(1, $members);
187263
});
188264
}
265+
266+
public function test_events_are_processed_by_on_message_on_public_channels()
267+
{
268+
$connection = $this->newActiveConnection(['public-channel']);
269+
270+
$message = new Mocks\Message([
271+
'appId' => '1234',
272+
'serverId' => 'different_server_id',
273+
'event' => 'some-event',
274+
'data' => [
275+
'channel' => 'public-channel',
276+
'test' => 'yes',
277+
],
278+
]);
279+
280+
$this->channelManager->onMessage(
281+
$this->channelManager->getRedisKey('1234', 'public-channel'),
282+
$message->getPayload()
283+
);
284+
285+
// The message does not contain appId and serverId anymore.
286+
$message = new Mocks\Message([
287+
'event' => 'some-event',
288+
'data' => [
289+
'channel' => 'public-channel',
290+
'test' => 'yes',
291+
],
292+
]);
293+
294+
$connection->assertSentEvent('some-event', $message->getPayloadAsArray());
295+
}
296+
297+
public function test_events_are_processed_by_on_message_on_private_channels()
298+
{
299+
$connection = $this->newPrivateConnection('private-channel');
300+
301+
$message = new Mocks\SignedMessage([
302+
'appId' => '1234',
303+
'serverId' => 'different_server_id',
304+
'event' => 'some-event',
305+
'data' => [
306+
'channel' => 'private-channel',
307+
'test' => 'yes',
308+
],
309+
], $connection, 'private-channel');
310+
311+
$this->channelManager->onMessage(
312+
$this->channelManager->getRedisKey('1234', 'private-channel'),
313+
$message->getPayload()
314+
);
315+
316+
// The message does not contain appId and serverId anymore.
317+
$message = new Mocks\SignedMessage([
318+
'event' => 'some-event',
319+
'data' => [
320+
'channel' => 'private-channel',
321+
'test' => 'yes',
322+
],
323+
], $connection, 'private-channel');
324+
325+
$connection->assertSentEvent('some-event', $message->getPayloadAsArray());
326+
}
327+
328+
public function test_events_are_processed_by_on_message_on_presence_channels()
329+
{
330+
$user = [
331+
'user_id' => 1,
332+
'user_info' => [
333+
'name' => 'Rick',
334+
],
335+
];
336+
337+
$connection = $this->newPresenceConnection('presence-channel', $user);
338+
339+
$encodedUser = json_encode($user);
340+
341+
$message = new Mocks\SignedMessage([
342+
'appId' => '1234',
343+
'serverId' => 'different_server_id',
344+
'event' => 'some-event',
345+
'data' => [
346+
'channel' => 'presence-channel',
347+
'channel_data' => $encodedUser,
348+
'test' => 'yes',
349+
],
350+
], $connection, 'presence-channel', $encodedUser);
351+
352+
$this->channelManager->onMessage(
353+
$this->channelManager->getRedisKey('1234', 'presence-channel'),
354+
$message->getPayload()
355+
);
356+
357+
// The message does not contain appId and serverId anymore.
358+
$message = new Mocks\SignedMessage([
359+
'event' => 'some-event',
360+
'data' => [
361+
'channel' => 'presence-channel',
362+
'channel_data' => $encodedUser,
363+
'test' => 'yes',
364+
],
365+
], $connection, 'presence-channel', $encodedUser);
366+
367+
$connection->assertSentEvent('some-event', $message->getPayloadAsArray());
368+
}
189369
}

tests/TriggerEventTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,11 @@ public function test_it_fires_event_across_servers()
190190
->assertCalledWithArgs('publish', [
191191
$this->channelManager->getRedisKey('1234', 'public-channel'),
192192
json_encode([
193-
'channel' => 'public-channel',
194193
'event' => null,
194+
'channel' => 'public-channel',
195195
'data' => null,
196196
'appId' => '1234',
197+
'socketId' => null,
197198
'serverId' => $this->channelManager->getServerId(),
198199
]),
199200
]);

0 commit comments

Comments
 (0)