trait InteractsWithRedis

Provides Redis integration testing support.

Auto-called by TestCase via setUpTraits():

  • setUpInteractsWithRedis() runs after app boots
  • tearDownInteractsWithRedis() runs via beforeApplicationDestroyed()

Tests that need Redis config overrides (prefix, DB number) should set them in defineEnvironment() via $app->make('config')->set(...).

Standalone ParaTest workers use separate logical databases for isolation. Redis Cluster only supports database zero, so Cluster suites run serially.

Tests that need to call select() to switch databases should set REDIS_TEST_SECONDARY_DB and use getSecondaryRedisDb(). The secondary DB is shared by tests that explicitly request it; never call flushdb() on it. Use unique keys and clean them up with del().

Environment Variables:

  • REDIS_HOST: Standalone Redis host
  • REDIS_CLUSTER_HOSTS_AND_PORTS: Comma-separated Redis Cluster seeds
  • REDIS_PORT: Redis port (default: 6379)
  • REDIS_DB: Base Redis database number (default: 0)
  • REDIS_TEST_DB_MIN: First Redis database available for parallel workers (default: REDIS_DB)
  • REDIS_TEST_DB_MAX: Last Redis database available for parallel workers (default: 15)
  • REDIS_TEST_SECONDARY_DB: Shared secondary database for select() tests (optional)
  • REDIS_PASSWORD: Redis password (optional)

Methods

void
setUpInteractsWithRedis()

Set up Redis for testing (auto-called by setUpTraits).

void
tearDownInteractsWithRedis()

Tear down Redis (auto-called via beforeApplicationDestroyed).

void
flushRedis()

Flush the Redis database.

bool
hasExplicitRedisConfig()

Determine if Redis integration testing was explicitly configured.

bool
usingRedisCluster()

Determine if Redis integration testing uses a Cluster.

int
getParallelRedisDb()

Get the primary Redis DB number for the current test worker.

int
getSecondaryRedisDb()

Get the secondary Redis DB for standalone tests that need to call select().

string|false
parallelTestingToken()

Get the current parallel testing token.

redisClient(string $connectionName = 'default')

Get the configured Redis connection for direct assertions.

redisClientWithoutPrefix()

Get a topology-aware Redis connection without a key prefix.

void
assertRedisKeysUseDifferentClusterSlots(string $firstKey, string $secondKey)

Assert that two physical keys occupy different Redis Cluster slots.

void
cleanupRedisKeysWithPatterns(string ...$patterns)

Clean up keys matching multiple patterns using the trait's standard Redis test semantics.

string
createRedisConnectionWithPrefix(string $optPrefix)

Create a named Redis connection with a specific OPT_PREFIX for testing.

string
createRedisConnectionWithOptions(string $name, array $options, int $maxConnections = 10)

Create a Redis connection with custom options for integration assertions.

Details

at line 54
protected void setUpInteractsWithRedis()

Set up Redis for testing (auto-called by setUpTraits).

Redis integration tests are opt-in via REDIS_HOST or REDIS_CLUSTER_HOSTS_AND_PORTS.

When running under ParaTest, assigns a configured per-worker Redis DB number to prevent cross-process interference.

Return Value

void

at line 73
protected void tearDownInteractsWithRedis()

Tear down Redis (auto-called via beforeApplicationDestroyed).

Return Value

void

at line 91
protected void flushRedis()

Flush the Redis database.

Return Value

void

at line 99
protected bool hasExplicitRedisConfig()

Determine if Redis integration testing was explicitly configured.

Return Value

bool

at line 107
protected bool usingRedisCluster()

Determine if Redis integration testing uses a Cluster.

Return Value

bool

at line 115
protected int getParallelRedisDb()

Get the primary Redis DB number for the current test worker.

Return Value

int

at line 128
protected int getSecondaryRedisDb()

Get the secondary Redis DB for standalone tests that need to call select().

Must always return a DB number different from getParallelRedisDb().

This DB is shared across all parallel workers. Never call flushdb() on it — use unique keys and clean up via del() instead.

Return Value

int

at line 136
protected string|false parallelTestingToken()

Get the current parallel testing token.

Return Value

string|false

at line 146
protected RedisProxy redisClient(string $connectionName = 'default')

Get the configured Redis connection for direct assertions.

Parameters

string $connectionName

Return Value

RedisProxy

at line 154
protected RedisProxy redisClientWithoutPrefix()

Get a topology-aware Redis connection without a key prefix.

Return Value

RedisProxy

at line 166
protected void assertRedisKeysUseDifferentClusterSlots(string $firstKey, string $secondKey)

Assert that two physical keys occupy different Redis Cluster slots.

Parameters

string $firstKey
string $secondKey

Return Value

void

at line 198
protected void cleanupRedisKeysWithPatterns(string ...$patterns)

Clean up keys matching multiple patterns using the trait's standard Redis test semantics.

If Redis was not explicitly enabled, cleanup is skipped just like setUpInteractsWithRedis(). When a topology is configured, connection failures still propagate as real test environment errors.

Parameters

string ...$patterns

Return Value

void

at line 221
protected string createRedisConnectionWithPrefix(string $optPrefix)

Create a named Redis connection with a specific OPT_PREFIX for testing.

Use this when a test needs multiple connections with different prefixes. For a single no-prefix connection, just set the prefix on the default connection in defineEnvironment() instead.

Parameters

string $optPrefix

Return Value

string

at line 235
protected string createRedisConnectionWithOptions(string $name, array $options, int $maxConnections = 10)

Create a Redis connection with custom options for integration assertions.

Parameters

string $name
array $options
int $maxConnections

Return Value

string