2019-04-05 19:30:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace BeyondCode\LaravelWebSockets\Tests\HttpApi;
|
|
|
|
|
|
2020-08-17 08:47:50 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchChannelsController;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection;
|
2020-08-14 06:43:47 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Tests\TestCase;
|
2020-08-17 08:47:50 +00:00
|
|
|
use GuzzleHttp\Psr7\Request;
|
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
|
use Pusher\Pusher;
|
2020-08-14 06:43:47 +00:00
|
|
|
|
|
|
|
|
class FetchChannelsReplicationTest extends TestCase
|
2019-04-05 19:30:41 +00:00
|
|
|
{
|
2020-08-14 12:35:36 +00:00
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
|
|
|
|
public function setUp(): void
|
|
|
|
|
{
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$this->runOnlyOnRedisReplication();
|
|
|
|
|
}
|
2020-08-14 17:26:55 +00:00
|
|
|
|
2020-08-17 08:47:50 +00:00
|
|
|
/** @test */
|
|
|
|
|
public function replication_it_returns_the_channel_information()
|
2020-08-14 17:26:55 +00:00
|
|
|
{
|
2020-08-17 08:47:50 +00:00
|
|
|
$this->joinPresenceChannel('presence-channel');
|
|
|
|
|
|
|
|
|
|
$connection = new Connection();
|
|
|
|
|
|
|
|
|
|
$requestPath = '/apps/1234/channels';
|
|
|
|
|
$routeParams = [
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$queryString = Pusher::build_auth_query_string('TestKey', 'TestSecret', 'GET', $requestPath);
|
|
|
|
|
|
|
|
|
|
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
|
|
|
|
|
|
|
|
|
|
$controller = app(FetchChannelsController::class);
|
|
|
|
|
|
|
|
|
|
$controller->onOpen($connection, $request);
|
|
|
|
|
|
|
|
|
|
/** @var JsonResponse $response */
|
|
|
|
|
$response = array_pop($connection->sentRawData);
|
|
|
|
|
|
|
|
|
|
$this->getSubscribeClient()
|
|
|
|
|
->assertEventDispatched('message');
|
|
|
|
|
|
|
|
|
|
$this->getPublishClient()
|
2020-09-03 13:50:37 +00:00
|
|
|
->assertNotCalled('hset')
|
2020-08-24 11:16:21 +00:00
|
|
|
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-channel'])
|
2020-08-17 08:47:50 +00:00
|
|
|
->assertCalled('publish')
|
|
|
|
|
->assertCalled('multi')
|
2020-08-24 11:16:21 +00:00
|
|
|
->assertCalledWithArgs('hlen', ['laravel_database_1234:presence-channel'])
|
2020-08-17 08:47:50 +00:00
|
|
|
->assertCalled('exec');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
public function replication_it_returns_the_channel_information_for_prefix()
|
|
|
|
|
{
|
|
|
|
|
$this->joinPresenceChannel('presence-global.1');
|
|
|
|
|
$this->joinPresenceChannel('presence-global.1');
|
|
|
|
|
$this->joinPresenceChannel('presence-global.2');
|
|
|
|
|
$this->joinPresenceChannel('presence-notglobal.2');
|
|
|
|
|
|
|
|
|
|
$connection = new Connection();
|
|
|
|
|
|
|
|
|
|
$requestPath = '/apps/1234/channels';
|
|
|
|
|
$routeParams = [
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$queryString = Pusher::build_auth_query_string('TestKey', 'TestSecret', 'GET', $requestPath, [
|
|
|
|
|
'filter_by_prefix' => 'presence-global',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
|
|
|
|
|
|
|
|
|
|
$controller = app(FetchChannelsController::class);
|
|
|
|
|
|
|
|
|
|
$controller->onOpen($connection, $request);
|
|
|
|
|
|
|
|
|
|
/** @var JsonResponse $response */
|
|
|
|
|
$response = array_pop($connection->sentRawData);
|
|
|
|
|
|
|
|
|
|
$this->getSubscribeClient()
|
|
|
|
|
->assertEventDispatched('message');
|
|
|
|
|
|
|
|
|
|
$this->getPublishClient()
|
2020-09-03 13:50:37 +00:00
|
|
|
->assertNotCalled('hset')
|
2020-08-24 11:16:21 +00:00
|
|
|
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-global.1'])
|
|
|
|
|
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-global.2'])
|
|
|
|
|
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-notglobal.2'])
|
2020-08-17 08:47:50 +00:00
|
|
|
->assertCalled('publish')
|
|
|
|
|
->assertCalled('multi')
|
2020-08-24 11:16:21 +00:00
|
|
|
->assertCalledWithArgs('hlen', ['laravel_database_1234:presence-global.1'])
|
|
|
|
|
->assertCalledWithArgs('hlen', ['laravel_database_1234:presence-global.2'])
|
|
|
|
|
->assertNotCalledWithArgs('hlen', ['laravel_database_1234:presence-notglobal.2'])
|
2020-08-17 08:47:50 +00:00
|
|
|
->assertCalled('exec');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
public function replication_it_returns_the_channel_information_for_prefix_with_user_count()
|
|
|
|
|
{
|
|
|
|
|
$this->joinPresenceChannel('presence-global.1');
|
|
|
|
|
$this->joinPresenceChannel('presence-global.1');
|
|
|
|
|
$this->joinPresenceChannel('presence-global.2');
|
|
|
|
|
$this->joinPresenceChannel('presence-notglobal.2');
|
|
|
|
|
|
|
|
|
|
$connection = new Connection();
|
|
|
|
|
|
|
|
|
|
$requestPath = '/apps/1234/channels';
|
|
|
|
|
$routeParams = [
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$queryString = Pusher::build_auth_query_string('TestKey', 'TestSecret', 'GET', $requestPath, [
|
|
|
|
|
'filter_by_prefix' => 'presence-global',
|
|
|
|
|
'info' => 'user_count',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
|
|
|
|
|
|
|
|
|
|
$controller = app(FetchChannelsController::class);
|
|
|
|
|
|
|
|
|
|
$controller->onOpen($connection, $request);
|
|
|
|
|
|
|
|
|
|
/** @var JsonResponse $response */
|
|
|
|
|
$response = array_pop($connection->sentRawData);
|
|
|
|
|
|
|
|
|
|
$this->getSubscribeClient()
|
|
|
|
|
->assertEventDispatched('message');
|
|
|
|
|
|
|
|
|
|
$this->getPublishClient()
|
2020-09-03 13:50:37 +00:00
|
|
|
->assertNotCalled('hset')
|
2020-08-24 11:16:21 +00:00
|
|
|
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-global.1'])
|
|
|
|
|
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-global.2'])
|
|
|
|
|
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-notglobal.2'])
|
2020-08-17 08:47:50 +00:00
|
|
|
->assertCalled('publish')
|
|
|
|
|
->assertCalled('multi')
|
2020-08-24 11:16:21 +00:00
|
|
|
->assertCalledWithArgs('hlen', ['laravel_database_1234:presence-global.1'])
|
|
|
|
|
->assertCalledWithArgs('hlen', ['laravel_database_1234:presence-global.2'])
|
|
|
|
|
->assertNotCalledWithArgs('hlen', ['laravel_database_1234:presence-notglobal.2'])
|
2020-08-17 08:47:50 +00:00
|
|
|
->assertCalled('exec');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
public function replication_it_returns_empty_object_for_no_channels_found()
|
|
|
|
|
{
|
|
|
|
|
$connection = new Connection();
|
|
|
|
|
|
|
|
|
|
$requestPath = '/apps/1234/channels';
|
|
|
|
|
$routeParams = [
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$queryString = Pusher::build_auth_query_string('TestKey', 'TestSecret', 'GET', $requestPath);
|
|
|
|
|
|
|
|
|
|
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
|
|
|
|
|
|
|
|
|
|
$controller = app(FetchChannelsController::class);
|
|
|
|
|
|
|
|
|
|
$controller->onOpen($connection, $request);
|
|
|
|
|
|
|
|
|
|
/** @var JsonResponse $response */
|
|
|
|
|
$response = array_pop($connection->sentRawData);
|
|
|
|
|
|
|
|
|
|
$this->getSubscribeClient()
|
|
|
|
|
->assertEventDispatched('message');
|
|
|
|
|
|
|
|
|
|
$this->getPublishClient()
|
|
|
|
|
->assertNotCalled('hset')
|
|
|
|
|
->assertNotCalled('hgetall')
|
|
|
|
|
->assertNotCalled('publish')
|
|
|
|
|
->assertCalled('multi')
|
|
|
|
|
->assertNotCalled('hlen')
|
|
|
|
|
->assertCalled('exec');
|
2020-08-14 17:26:55 +00:00
|
|
|
}
|
2019-04-05 19:30:41 +00:00
|
|
|
}
|