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

Commit b41f8b7

Browse files
committed
Creating SignedMessage class for testing
1 parent df45ee8 commit b41f8b7

File tree

4 files changed

+70
-30
lines changed

4 files changed

+70
-30
lines changed

tests/Mocks/SignedMessage.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace BeyondCode\LaravelWebSockets\Test\Mocks;
4+
5+
use Ratchet\ConnectionInterface;
6+
7+
class SignedMessage extends Message
8+
{
9+
/**
10+
* Create a new signed message instance.
11+
*
12+
* @param array $payload
13+
* @param ConnectionInterface $connection
14+
* @param string $channelName
15+
* @param string|null $encodedUser
16+
* @return void
17+
*/
18+
public function __construct(array $payload, ConnectionInterface $connection, string $channelName, string $encodedUser = null)
19+
{
20+
parent::__construct($payload);
21+
22+
$signature = "{$connection->socketId}:{$channelName}";
23+
24+
if ($encodedUser) {
25+
$signature .= ":{$encodedUser}";
26+
}
27+
28+
$hash = hash_hmac('sha256', $signature, $connection->app->secret);
29+
30+
$this->payload['data']['auth'] = "{$connection->app->key}:{$hash}";
31+
}
32+
}

tests/PresenceChannelTest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,13 @@ public function test_connect_to_presence_channel_with_valid_signature()
4040

4141
$encodedUser = json_encode($user);
4242

43-
$signature = "{$connection->socketId}:presence-channel:".$encodedUser;
44-
$hashedAppSecret = hash_hmac('sha256', $signature, $connection->app->secret);
45-
46-
$message = new Mocks\Message([
43+
$message = new Mocks\SignedMessage([
4744
'event' => 'pusher:subscribe',
4845
'data' => [
49-
'auth' => "{$connection->app->key}:{$hashedAppSecret}",
5046
'channel' => 'presence-channel',
51-
'channel_data' => json_encode($user),
47+
'channel_data' => $encodedUser,
5248
],
53-
]);
49+
], $connection, 'presence-channel', $encodedUser);
5450

5551
$this->pusherServer->onMessage($connection, $message);
5652

@@ -187,7 +183,7 @@ public function test_statistics_get_collected_for_presenece_channels()
187183
});
188184
}
189185

190-
public function test_local_connections_for_private_channels()
186+
public function test_local_connections_for_presence_channels()
191187
{
192188
$this->newPresenceConnection('presence-channel', ['user_id' => 1]);
193189
$this->newPresenceConnection('presence-channel-2', ['user_id' => 2]);

tests/ReplicationTest.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,32 @@
44

55
class ReplicationTest extends TestCase
66
{
7-
public function test_events_get_replicated_across_connections()
7+
/**
8+
* {@inheritdoc}
9+
*/
10+
public function setUp(): void
811
{
12+
parent::setUp();
13+
914
$this->runOnlyOnRedisReplication();
15+
}
16+
17+
public function test_publishing_client_gets_subscribed()
18+
{
19+
$this->newActiveConnection(['public-channel']);
1020

21+
$this->getSubscribeClient()
22+
->assertCalledWithArgs('subscribe', [$this->channelManager->getRedisKey('1234')])
23+
->assertCalledWithArgs('subscribe', [$this->channelManager->getRedisKey('1234', 'public-channel')]);
24+
}
25+
26+
public function test_events_get_replicated_across_connections()
27+
{
1128
$connection = $this->newActiveConnection(['public-channel']);
1229

1330
$message = [
1431
'appId' => '1234',
15-
'serverId' => 0,
32+
'serverId' => $this->channelManager->getServerId(),
1633
'event' => 'some-event',
1734
'data' => [
1835
'channel' => 'public-channel',
@@ -31,12 +48,19 @@ public function test_events_get_replicated_across_connections()
3148
'serverId' => $this->channelManager->getServerId(),
3249
'data' => ['channel' => 'public-channel', 'test' => 'yes'],
3350
]);
51+
52+
$this->getSubscribeClient()
53+
->assertNothingDispatched();
54+
55+
$this->getPublishClient()
56+
->assertCalledWithArgs('publish', [
57+
$this->channelManager->getRedisKey('1234', 'public-channel'),
58+
json_encode($message),
59+
]);
3460
}
3561

3662
public function test_not_ponged_connections_do_get_removed_for_public_channels()
3763
{
38-
$this->runOnlyOnRedisReplication();
39-
4064
$connection = $this->newActiveConnection(['public-channel']);
4165

4266
// Make the connection look like it was lost 1 day ago.
@@ -65,8 +89,6 @@ public function test_not_ponged_connections_do_get_removed_for_public_channels()
6589

6690
public function test_not_ponged_connections_do_get_removed_for_private_channels()
6791
{
68-
$this->runOnlyOnRedisReplication();
69-
7092
$connection = $this->newPrivateConnection('private-channel');
7193

7294
// Make the connection look like it was lost 1 day ago.
@@ -95,8 +117,6 @@ public function test_not_ponged_connections_do_get_removed_for_private_channels(
95117

96118
public function test_not_ponged_connections_do_get_removed_for_presence_channels()
97119
{
98-
$this->runOnlyOnRedisReplication();
99-
100120
$connection = $this->newPresenceConnection('presence-channel');
101121

102122
// Make the connection look like it was lost 1 day ago.

tests/TestCase.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -331,18 +331,15 @@ protected function newPresenceConnection($channel, array $user = [], string $app
331331
'user_info' => ['name' => 'Rick'],
332332
];
333333

334-
$signature = "{$connection->socketId}:{$channel}:".json_encode($user);
334+
$encodedUser = json_encode($user);
335335

336-
$hash = hash_hmac('sha256', $signature, $connection->app->secret);
337-
338-
$message = new Mocks\Message([
336+
$message = new Mocks\SignedMessage([
339337
'event' => 'pusher:subscribe',
340338
'data' => [
341-
'auth' => "{$connection->app->key}:{$hash}",
342339
'channel' => $channel,
343-
'channel_data' => json_encode($user),
340+
'channel_data' => $encodedUser,
344341
],
345-
]);
342+
], $connection, $channel, $encodedUser);
346343

347344
$this->pusherServer->onMessage($connection, $message);
348345

@@ -363,17 +360,12 @@ protected function newPrivateConnection($channel, string $appKey = 'TestKey', ar
363360

364361
$this->pusherServer->onOpen($connection);
365362

366-
$signature = "{$connection->socketId}:{$channel}";
367-
368-
$hash = hash_hmac('sha256', $signature, $connection->app->secret);
369-
370-
$message = new Mocks\Message([
363+
$message = new Mocks\SignedMessage([
371364
'event' => 'pusher:subscribe',
372365
'data' => [
373-
'auth' => "{$connection->app->key}:{$hash}",
374366
'channel' => $channel,
375367
],
376-
]);
368+
], $connection, $channel);
377369

378370
$this->pusherServer->onMessage($connection, $message);
379371

0 commit comments

Comments
 (0)