runOnlyOnRedisReplication(); Redis::hdel('laravel_database_1234', 'connections'); } /** @test */ public function redis_listener_responds_properly_on_payload() { $connection = $this->getConnectedWebSocketConnection(['test-channel']); $this->pusherServer->onOpen($connection); $channelData = [ 'user_id' => 1, 'user_info' => [ 'name' => 'Marcel', ], ]; $payload = json_encode([ 'appId' => '1234', 'event' => 'test', 'data' => $channelData, 'socketId' => $connection->socketId, ]); $this->getSubscribeClient()->onMessage('1234:test-channel', $payload); $this->getSubscribeClient() ->assertEventDispatched('message') ->assertCalledWithArgs('subscribe', ['laravel_database_1234:test-channel']) ->assertCalledWithArgs('onMessage', [ '1234:test-channel', $payload, ]); } /** @test */ public function redis_listener_responds_properly_on_payload_by_direct_call() { $connection = $this->getConnectedWebSocketConnection(['test-channel']); $this->pusherServer->onOpen($connection); $channelData = [ 'user_id' => 1, 'user_info' => [ 'name' => 'Marcel', ], ]; $payload = json_encode([ 'appId' => '1234', 'event' => 'test', 'data' => $channelData, 'socketId' => $connection->socketId, ]); $client = (new RedisClient)->boot( LoopFactory::create(), RedisFactory::class ); $client->onMessage('1234:test-channel', $payload); $client->getSubscribeClient() ->assertEventDispatched('message'); } /** @test */ public function redis_tracks_app_connections_count() { $connection = $this->getWebSocketConnection(); $this->pusherServer->onOpen($connection); $this->getSubscribeClient() ->assertCalledWithArgs('subscribe', ['laravel_database_1234']); $this->getPublishClient() ->assertCalledWithArgs('hincrby', ['laravel_database_1234', 'connections', 1]); } /** @test */ public function redis_tracks_app_connections_count_on_disconnect() { $connection = $this->getWebSocketConnection(); $this->pusherServer->onOpen($connection); $this->getSubscribeClient() ->assertCalledWithArgs('subscribe', ['laravel_database_1234']) ->assertNotCalledWithArgs('unsubscribe', ['laravel_database_1234']); $this->getPublishClient() ->assertCalledWithArgs('hincrby', ['laravel_database_1234', 'connections', 1]); $this->pusherServer->onClose($connection); $this->getPublishClient() ->assertCalledWithArgs('hincrby', ['laravel_database_1234', 'connections', -1]); $this->assertEquals(0, Redis::hget('laravel_database_1234', 'connections')); } }