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

Commit 2066e80

Browse files
committed
Fixed tests to make sure the message is broadcasted properly both locally and across servers.
1 parent 546c4fd commit 2066e80

File tree

4 files changed

+438
-167
lines changed

4 files changed

+438
-167
lines changed

tests/PresenceChannelTest.php

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace BeyondCode\LaravelWebSockets\Test;
44

5+
use BeyondCode\LaravelWebSockets\API\TriggerEvent;
56
use BeyondCode\LaravelWebSockets\Server\Exceptions\InvalidSignature;
67
use Carbon\Carbon;
8+
use GuzzleHttp\Psr7\Request;
9+
use Illuminate\Http\JsonResponse;
10+
use Pusher\Pusher;
711
use Ratchet\ConnectionInterface;
812

913
class PresenceChannelTest extends TestCase
@@ -455,4 +459,146 @@ public function test_events_get_replicated_across_connections_for_presence_chann
455459
$message->getPayload(),
456460
]);
457461
}
462+
463+
public function test_it_fires_the_event_to_presence_channel()
464+
{
465+
$this->newPresenceConnection('presence-channel');
466+
467+
$connection = new Mocks\Connection;
468+
469+
$requestPath = '/apps/1234/events';
470+
471+
$routeParams = [
472+
'appId' => '1234',
473+
];
474+
475+
$queryString = Pusher::build_auth_query_string(
476+
'TestKey', 'TestSecret', 'POST', $requestPath, [
477+
'name' => 'some-event',
478+
'channels' => ['presence-channel'],
479+
'data' => json_encode(['some-data' => 'yes']),
480+
],
481+
);
482+
483+
$request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
484+
485+
$controller = app(TriggerEvent::class);
486+
487+
$controller->onOpen($connection, $request);
488+
489+
/** @var JsonResponse $response */
490+
$response = array_pop($connection->sentRawData);
491+
492+
$this->assertSame([], json_decode($response->getContent(), true));
493+
494+
$this->statisticsCollector
495+
->getAppStatistics('1234')
496+
->then(function ($statistic) {
497+
$this->assertEquals([
498+
'peak_connections_count' => 1,
499+
'websocket_messages_count' => 1,
500+
'api_messages_count' => 1,
501+
'app_id' => '1234',
502+
], $statistic->toArray());
503+
});
504+
}
505+
506+
public function test_it_fires_event_across_servers_when_there_are_not_users_locally_for_presence_channel()
507+
{
508+
$connection = new Mocks\Connection;
509+
510+
$requestPath = '/apps/1234/events';
511+
512+
$routeParams = [
513+
'appId' => '1234',
514+
];
515+
516+
$queryString = Pusher::build_auth_query_string(
517+
'TestKey', 'TestSecret', 'POST', $requestPath, [
518+
'name' => 'some-event',
519+
'channels' => ['presence-channel'],
520+
'data' => json_encode(['some-data' => 'yes']),
521+
],
522+
);
523+
524+
$request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
525+
526+
$controller = app(TriggerEvent::class);
527+
528+
$controller->onOpen($connection, $request);
529+
530+
/** @var JsonResponse $response */
531+
$response = array_pop($connection->sentRawData);
532+
533+
$this->assertSame([], json_decode($response->getContent(), true));
534+
535+
if (method_exists($this->channelManager, 'getPublishClient')) {
536+
$this->channelManager
537+
->getPublishClient()
538+
->assertCalledWithArgsCount(1, 'publish', [
539+
$this->channelManager->getRedisKey('1234', 'presence-channel'),
540+
json_encode([
541+
'event' => 'some-event',
542+
'channel' => 'presence-channel',
543+
'data' => json_encode(['some-data' => 'yes']),
544+
'appId' => '1234',
545+
'socketId' => null,
546+
'serverId' => $this->channelManager->getServerId(),
547+
]),
548+
]);
549+
}
550+
}
551+
552+
public function test_it_fires_event_across_servers_when_there_are_users_locally_for_presence_channel()
553+
{
554+
$wsConnection = $this->newPresenceConnection('presence-channel');
555+
556+
$connection = new Mocks\Connection;
557+
558+
$requestPath = '/apps/1234/events';
559+
560+
$routeParams = [
561+
'appId' => '1234',
562+
];
563+
564+
$queryString = Pusher::build_auth_query_string(
565+
'TestKey', 'TestSecret', 'POST', $requestPath, [
566+
'name' => 'some-event',
567+
'channels' => ['presence-channel'],
568+
'data' => json_encode(['some-data' => 'yes']),
569+
],
570+
);
571+
572+
$request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
573+
574+
$controller = app(TriggerEvent::class);
575+
576+
$controller->onOpen($connection, $request);
577+
578+
/** @var JsonResponse $response */
579+
$response = array_pop($connection->sentRawData);
580+
581+
$this->assertSame([], json_decode($response->getContent(), true));
582+
583+
if (method_exists($this->channelManager, 'getPublishClient')) {
584+
$this->channelManager
585+
->getPublishClient()
586+
->assertCalledWithArgsCount(1, 'publish', [
587+
$this->channelManager->getRedisKey('1234', 'presence-channel'),
588+
json_encode([
589+
'event' => 'some-event',
590+
'channel' => 'presence-channel',
591+
'data' => json_encode(['some-data' => 'yes']),
592+
'appId' => '1234',
593+
'socketId' => null,
594+
'serverId' => $this->channelManager->getServerId(),
595+
]),
596+
]);
597+
}
598+
599+
$wsConnection->assertSentEvent('some-event', [
600+
'channel' => 'presence-channel',
601+
'data' => json_encode(['some-data' => 'yes']),
602+
]);
603+
}
458604
}

tests/PrivateChannelTest.php

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace BeyondCode\LaravelWebSockets\Test;
44

5+
use BeyondCode\LaravelWebSockets\API\TriggerEvent;
56
use BeyondCode\LaravelWebSockets\Server\Exceptions\InvalidSignature;
67
use Carbon\Carbon;
8+
use GuzzleHttp\Psr7\Request;
9+
use Illuminate\Http\JsonResponse;
10+
use Pusher\Pusher;
711
use Ratchet\ConnectionInterface;
812

913
class PrivateChannelTest extends TestCase
@@ -263,4 +267,146 @@ public function test_events_get_replicated_across_connections_for_private_channe
263267
$message->getPayload(),
264268
]);
265269
}
270+
271+
public function test_it_fires_the_event_to_private_channel()
272+
{
273+
$this->newPrivateConnection('private-channel');
274+
275+
$connection = new Mocks\Connection;
276+
277+
$requestPath = '/apps/1234/events';
278+
279+
$routeParams = [
280+
'appId' => '1234',
281+
];
282+
283+
$queryString = Pusher::build_auth_query_string(
284+
'TestKey', 'TestSecret', 'POST', $requestPath, [
285+
'name' => 'some-event',
286+
'channels' => ['private-channel'],
287+
'data' => json_encode(['some-data' => 'yes']),
288+
],
289+
);
290+
291+
$request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
292+
293+
$controller = app(TriggerEvent::class);
294+
295+
$controller->onOpen($connection, $request);
296+
297+
/** @var JsonResponse $response */
298+
$response = array_pop($connection->sentRawData);
299+
300+
$this->assertSame([], json_decode($response->getContent(), true));
301+
302+
$this->statisticsCollector
303+
->getAppStatistics('1234')
304+
->then(function ($statistic) {
305+
$this->assertEquals([
306+
'peak_connections_count' => 1,
307+
'websocket_messages_count' => 1,
308+
'api_messages_count' => 1,
309+
'app_id' => '1234',
310+
], $statistic->toArray());
311+
});
312+
}
313+
314+
public function test_it_fires_event_across_servers_when_there_are_not_users_locally_for_private_channel()
315+
{
316+
$connection = new Mocks\Connection;
317+
318+
$requestPath = '/apps/1234/events';
319+
320+
$routeParams = [
321+
'appId' => '1234',
322+
];
323+
324+
$queryString = Pusher::build_auth_query_string(
325+
'TestKey', 'TestSecret', 'POST', $requestPath, [
326+
'name' => 'some-event',
327+
'channels' => ['private-channel'],
328+
'data' => json_encode(['some-data' => 'yes']),
329+
],
330+
);
331+
332+
$request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
333+
334+
$controller = app(TriggerEvent::class);
335+
336+
$controller->onOpen($connection, $request);
337+
338+
/** @var JsonResponse $response */
339+
$response = array_pop($connection->sentRawData);
340+
341+
$this->assertSame([], json_decode($response->getContent(), true));
342+
343+
if (method_exists($this->channelManager, 'getPublishClient')) {
344+
$this->channelManager
345+
->getPublishClient()
346+
->assertCalledWithArgsCount(1, 'publish', [
347+
$this->channelManager->getRedisKey('1234', 'private-channel'),
348+
json_encode([
349+
'event' => 'some-event',
350+
'channel' => 'private-channel',
351+
'data' => json_encode(['some-data' => 'yes']),
352+
'appId' => '1234',
353+
'socketId' => null,
354+
'serverId' => $this->channelManager->getServerId(),
355+
]),
356+
]);
357+
}
358+
}
359+
360+
public function test_it_fires_event_across_servers_when_there_are_users_locally_for_private_channel()
361+
{
362+
$wsConnection = $this->newPrivateConnection('private-channel');
363+
364+
$connection = new Mocks\Connection;
365+
366+
$requestPath = '/apps/1234/events';
367+
368+
$routeParams = [
369+
'appId' => '1234',
370+
];
371+
372+
$queryString = Pusher::build_auth_query_string(
373+
'TestKey', 'TestSecret', 'POST', $requestPath, [
374+
'name' => 'some-event',
375+
'channels' => ['private-channel'],
376+
'data' => json_encode(['some-data' => 'yes']),
377+
],
378+
);
379+
380+
$request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
381+
382+
$controller = app(TriggerEvent::class);
383+
384+
$controller->onOpen($connection, $request);
385+
386+
/** @var JsonResponse $response */
387+
$response = array_pop($connection->sentRawData);
388+
389+
$this->assertSame([], json_decode($response->getContent(), true));
390+
391+
if (method_exists($this->channelManager, 'getPublishClient')) {
392+
$this->channelManager
393+
->getPublishClient()
394+
->assertCalledWithArgsCount(1, 'publish', [
395+
$this->channelManager->getRedisKey('1234', 'private-channel'),
396+
json_encode([
397+
'event' => 'some-event',
398+
'channel' => 'private-channel',
399+
'data' => json_encode(['some-data' => 'yes']),
400+
'appId' => '1234',
401+
'socketId' => null,
402+
'serverId' => $this->channelManager->getServerId(),
403+
]),
404+
]);
405+
}
406+
407+
$wsConnection->assertSentEvent('some-event', [
408+
'channel' => 'private-channel',
409+
'data' => json_encode(['some-data' => 'yes']),
410+
]);
411+
}
266412
}

0 commit comments

Comments
 (0)