Skip to content

Commit

Permalink
Merge pull request #150 from petergallagher/redis-database-support
Browse files Browse the repository at this point in the history
Add redis database support
  • Loading branch information
acelaya authored Aug 23, 2024
2 parents 80979f1 + 892a56b commit 9c887c5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Cache/RedisFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]',
));
}

Expand All @@ -73,6 +74,14 @@ private function normalizeServer(string $server): array
$parsedServer['password'] = urldecode($pass);
}

$database = $parsedServer['path'] ?? null;
unset($parsedServer['path']);

if ($database !== null) {
// TODO For the next major version, validate this is an integer and throw an exception otherwise
$parsedServer['database'] = (int) trim($database, '/');
}

return $parsedServer;
}

Expand Down
25 changes: 25 additions & 0 deletions test/Cache/RedisFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}

0 comments on commit 9c887c5

Please sign in to comment.