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(...).
Parallel Testing (ParaTest): Each ParaTest worker gets its own Redis DB number to prevent cross-process interference. Sequential runs use REDIS_DB directly. Parallel runs allocate worker databases from REDIS_TEST_DB_MIN through REDIS_TEST_DB_MAX.
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: Redis host; must be set to enable Redis integration tests
- 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.
Check if REDIS_HOST was explicitly set in environment.
Get the base Redis DB number from environment.
Get the first Redis DB number available for parallel test workers.
Get the last Redis DB number available for parallel test workers.
Get the configured secondary Redis DB number.
Get the primary Redis DB number for the current test worker.
Get the primary Redis DB number for a parallel testing token.
Get the secondary Redis DB for tests that need to call select().
Get the current parallel testing token.
Get the configured Redis worker databases.
Get the zero-based Redis worker index for a ParaTest token.
Get a non-negative integer Redis environment value.
Parse a non-negative integer Redis environment value.
Get a raw phpredis client for direct Redis operations.
Get a raw phpredis client WITHOUT any OPT_PREFIX.
Clean up keys matching a pattern using raw client (no prefix).
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 55
protected void
setUpInteractsWithRedis()
Set up Redis for testing (auto-called by setUpTraits).
Redis integration tests are opt-in via REDIS_HOST. Port, password, and database settings are only read after REDIS_HOST is present.
When running under ParaTest, assigns a configured per-worker Redis DB number to prevent cross-process interference.
at line 72
protected void
tearDownInteractsWithRedis()
Tear down Redis (auto-called via beforeApplicationDestroyed).
at line 93
protected void
flushRedis()
Flush the Redis database.
at line 101
protected bool
hasExplicitRedisConfig()
Check if REDIS_HOST was explicitly set in environment.
at line 115
protected int
getBaseRedisDb()
Get the base Redis DB number from environment.
Reads from env directly (not config) because configureParallelRedisDb() mutates the config value — reading config here would cause the DB number to drift upward across tests in the same worker.
Default matches database.php: env('REDIS_DB', 0).
at line 123
protected int
getRedisTestDbMin()
Get the first Redis DB number available for parallel test workers.
at line 131
protected int
getRedisTestDbMax()
Get the last Redis DB number available for parallel test workers.
at line 139
protected int|null
getConfiguredSecondaryRedisDb()
Get the configured secondary Redis DB number.
at line 147
protected int
getParallelRedisDb()
Get the primary Redis DB number for the current test worker.
at line 155
protected int
redisDatabaseForParallelToken(string $token)
Get the primary Redis DB number for a parallel testing token.
at line 168
protected int
getSecondaryRedisDb()
Get the secondary Redis DB for 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 194
protected string|false
parallelTestingToken()
Get the current parallel testing token.
at line 206
protected array
redisWorkerDatabases()
Get the configured Redis worker databases.
at line 214
protected int
redisWorkerIndex(string $token)
Get the zero-based Redis worker index for a ParaTest token.
at line 222
protected int
integerRedisEnvironment(string $key, int $default)
Get a non-negative integer Redis environment value.
at line 230
protected int
integerRedisEnvironmentValue(string $key, mixed $value)
Parse a non-negative integer Redis environment value.
at line 241
protected Redis
redisClient(string $connectionName = 'default')
Get a raw phpredis client for direct Redis operations.
This client has OPT_PREFIX set to the test prefix, so keys are automatically prefixed when using this client.
at line 260
protected Redis
rawRedisClientWithoutPrefix(string $connectionName = 'default')
Get a raw phpredis client WITHOUT any OPT_PREFIX.
Useful for verifying actual key names in Redis. Uses the per-worker DB number for parallel safety.
at line 288
protected void
cleanupKeysWithPattern(string $pattern)
Clean up keys matching a pattern using raw client (no prefix).
at line 305
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(). If REDIS_HOST is set, connection failures still propagate as real test environment errors.
at line 332
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 346
protected string
createRedisConnectionWithOptions(string $name, array $options, int $maxConnections = 10)
Create a Redis connection with custom options for integration assertions.