diff --git a/src/Cache/RedisFactory.php b/src/Cache/RedisFactory.php index 3f9ca15..c0c4971 100644 --- a/src/Cache/RedisFactory.php +++ b/src/Cache/RedisFactory.php @@ -50,8 +50,9 @@ private function normalizeServer(string $server): array $parsedServer = parse_url(trim($server)); if (! is_array($parsedServer)) { throw new InvalidArgumentException(sprintf( - 'Provided server "%s" is not a valid URL with format schema://[[username]:password@]host:port', + 'Provided server "%s" is not a valid URL with format %s', $server, + 'schema://[[username]:password@]host:port/[database]', )); } @@ -73,6 +74,16 @@ private function normalizeServer(string $server): array $parsedServer['password'] = urldecode($pass); } + $database = $parsedServer['path'] ?? null; + unset($parsedServer['path']); + + if ($database !== null) { + /** + * @todo Validate this is an integer and throw an exception if not for the next major version release + */ + $parsedServer['database'] = (int) trim($database, '/'); + } + return $parsedServer; } diff --git a/test/Cache/RedisFactoryTest.php b/test/Cache/RedisFactoryTest.php index c72e302..d06120e 100644 --- a/test/Cache/RedisFactoryTest.php +++ b/test/Cache/RedisFactoryTest.php @@ -157,4 +157,29 @@ public static function provideServersWithCredentials(): iterable 'servers' => ['rediss://1.1.1.1:6379'], ], null, null, SSL::OPTIONS]; } + + #[Test, DataProvider('provideServersWithDatabases')] + public function databaseConfigurationIsApplied( + array $redisConfig, + ?int $expectedDatabase, + ): void { + $this->container->expects($this->once())->method('get')->with('config')->willReturn([ + 'cache' => ['redis' => $redisConfig], + ]); + + $client = ($this->factory)($this->container); + $conn = $client->getConnection(); + + self::assertEquals($expectedDatabase, $conn->getParameters()->database); // @phpstan-ignore-line + } + + public static function provideServersWithDatabases(): iterable + { + yield 'no database' => [[ + 'servers' => ['tcp://1.1.1.1:6379'], + ], null]; + yield 'database' => [[ + 'servers' => ['tcp://1.1.1.1:6379/5'], + ], 5]; + } }