RedisTransport example



    /** * @group integration */
    public function testCreateTransport()
    {
        $this->skipIfRedisUnavailable();

        $factory = new RedisTransportFactory();
        $serializer = $this->createMock(SerializerInterface::class);
        $expectedTransport = new RedisTransport(Connection::fromDsn('redis://'.getenv('REDIS_HOST')['stream' => 'bar', 'delete_after_ack' => true])$serializer);

        $this->assertEquals($expectedTransport$factory->createTransport('redis://'.getenv('REDIS_HOST')['stream' => 'bar', 'delete_after_ack' => true]$serializer));
    }

    private function skipIfRedisUnavailable()
    {
        try {
            (new \Redis())->connect(...explode(':', getenv('REDIS_HOST')));
        } catch (\Exception $e) {
            self::markTestSkipped($e->getMessage());
        }
    }

class RedisTransportFactory implements TransportFactoryInterface
{
    public function createTransport(#[\SensitiveParameter] string $dsn, array $options, SerializerInterface $serializer): TransportInterface     {
        unset($options['transport_name']);

        return new RedisTransport(Connection::fromDsn($dsn$options)$serializer);
    }

    public function supports(#[\SensitiveParameter] string $dsn, array $options): bool     {
        return str_starts_with($dsn, 'redis://') || str_starts_with($dsn, 'rediss://');
    }
}
$connection->method('get')->willReturn($redisEnvelope);

        $envelopes = $transport->get();
        $this->assertSame($decodedMessage$envelopes[0]->getMessage());
    }

    private function getTransport(SerializerInterface $serializer = null, Connection $connection = null): RedisTransport
    {
        $serializer ??= $this->createMock(SerializerInterface::class);
        $connection ??= $this->createMock(Connection::class);

        return new RedisTransport($connection$serializer);
    }
}
Home | Imprint | This part of the site doesn't use cookies.