InteractsWithRedis
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
Set up Redis for testing (auto-called by setUpTraits).
Tear down Redis (auto-called via beforeApplicationDestroyed).
Flush the Redis database.
Determine if Redis integration testing was explicitly configured.
Determine if Redis integration testing uses a Cluster.
Get the primary Redis DB number for the current test worker.
Get the secondary Redis DB for standalone tests that need to call select().
Get the current parallel testing token.
Get the configured Redis connection for direct assertions.
Get a topology-aware Redis connection without a key prefix.
Assert that two physical keys occupy different Redis Cluster slots.
Clean up keys matching multiple patterns using the trait's standard Redis test semantics.
Create a named Redis connection with a specific OPT_PREFIX for testing.
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.
at line 73
protected void
tearDownInteractsWithRedis()
Tear down Redis (auto-called via beforeApplicationDestroyed).
at line 91
protected void
flushRedis()
Flush the Redis database.
at line 99
protected bool
hasExplicitRedisConfig()
Determine if Redis integration testing was explicitly configured.
at line 107
protected bool
usingRedisCluster()
Determine if Redis integration testing uses a Cluster.
at line 115
protected int
getParallelRedisDb()
Get the primary Redis DB number for the current test worker.
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.
at line 136
protected string|false
parallelTestingToken()
Get the current parallel testing token.
at line 146
protected RedisProxy
redisClient(string $connectionName = 'default')
Get the configured Redis connection for direct assertions.
at line 154
protected RedisProxy
redisClientWithoutPrefix()
Get a topology-aware Redis connection without a key prefix.
at line 166
protected void
assertRedisKeysUseDifferentClusterSlots(string $firstKey, string $secondKey)
Assert that two physical keys occupy different Redis Cluster slots.
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.
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.
at line 235
protected string
createRedisConnectionWithOptions(string $name, array $options, int $maxConnections = 10)
Create a Redis connection with custom options for integration assertions.