|
2 | 2 |
|
3 | 3 | namespace BeyondCode\LaravelWebSockets\Test;
|
4 | 4 |
|
| 5 | +use BeyondCode\LaravelWebSockets\API\TriggerEvent; |
5 | 6 | use BeyondCode\LaravelWebSockets\Server\Exceptions\InvalidSignature;
|
6 | 7 | use Carbon\Carbon;
|
| 8 | +use GuzzleHttp\Psr7\Request; |
| 9 | +use Illuminate\Http\JsonResponse; |
| 10 | +use Pusher\Pusher; |
7 | 11 | use Ratchet\ConnectionInterface;
|
8 | 12 |
|
9 | 13 | class PresenceChannelTest extends TestCase
|
@@ -455,4 +459,146 @@ public function test_events_get_replicated_across_connections_for_presence_chann
|
455 | 459 | $message->getPayload(),
|
456 | 460 | ]);
|
457 | 461 | }
|
| 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 | + } |
458 | 604 | }
|
0 commit comments