Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Cache] Improve dbindex DSN parameter parsing #57674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/Symfony/Component/Cache/Tests/Traits/RedisTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\SkippedTestSuiteError;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\Traits\RedisTrait;

/**
Expand Down Expand Up @@ -133,4 +134,78 @@ public function testPconnectSelectsCorrectDatabase()
ini_set('redis.pconnect.connection_limit', $prevPoolSize);
}
}

/**
* @dataProvider provideDbIndexDsnParameter
*/
public function testDbIndexDsnParameter(string $dsn, int $expectedDb)
{
if (!getenv('REDIS_AUTHENTICATED_HOST')) {
self::markTestSkipped('REDIS_AUTHENTICATED_HOST env var is not defined.');
}

$mock = new class () {
use RedisTrait;
};
$connection = $mock::createConnection($dsn);
self::assertSame($expectedDb, $connection->getDbNum());
}

public static function provideDbIndexDsnParameter(): array
{
return [
[
'redis://:p%40ssword@'.getenv('REDIS_AUTHENTICATED_HOST'),
0,
],
[
'redis:?host['.getenv('REDIS_HOST').']',
0,
],
[
'redis:?host['.getenv('REDIS_HOST').']&dbindex=1',
1,
],
[
'redis://:p%40ssword@'.getenv('REDIS_AUTHENTICATED_HOST').'?dbindex=2',
2,
],
[
'redis://:p%40ssword@'.getenv('REDIS_AUTHENTICATED_HOST').'/4',
4,
],
[
'redis://:p%40ssword@'.getenv('REDIS_AUTHENTICATED_HOST').'/?dbindex=5',
5,
],
];
}

/**
* @dataProvider provideInvalidDbIndexDsnParameter
*/
public function testInvalidDbIndexDsnParameter(string $dsn)
{
if (!getenv('REDIS_AUTHENTICATED_HOST')) {
self::markTestSkipped('REDIS_AUTHENTICATED_HOST env var is not defined.');
}
$this->expectException(InvalidArgumentException::class);

$mock = new class () {
use RedisTrait;
};
$mock::createConnection($dsn);
}

public static function provideInvalidDbIndexDsnParameter(): array
{
return [
[
'redis://:p%40ssword@'.getenv('REDIS_AUTHENTICATED_HOST').'/abc'
],
[
'redis://:p%40ssword@'.getenv('REDIS_AUTHENTICATED_HOST').'/3?dbindex=6'
]
];
}
}
8 changes: 6 additions & 2 deletions src/Symfony/Component/Cache/Traits/RedisTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ public static function createConnection(string $dsn, array $options = [])
if (isset($params['host']) || isset($params['path'])) {
if (!isset($params['dbindex']) && isset($params['path'])) {
if (preg_match('#/(\d+)?$#', $params['path'], $m)) {
$params['dbindex'] = $m[1] ?? '0';
$params['dbindex'] = $m[1] ?? $query['dbindex'] ?? '0';
$params['path'] = substr($params['path'], 0, -\strlen($m[0]));
} elseif (isset($params['host'])) {
throw new InvalidArgumentException('Invalid Redis DSN: query parameter "dbindex" must be a number.');
throw new InvalidArgumentException('Invalid Redis DSN: parameter "dbindex" must be a number.');
}
}

Expand All @@ -170,6 +170,10 @@ public static function createConnection(string $dsn, array $options = [])
throw new InvalidArgumentException('Invalid Redis DSN: missing host.');
}

if (isset($params['dbindex'], $query['dbindex']) && $params['dbindex'] !== $query['dbindex']) {
throw new InvalidArgumentException('Invalid Redis DSN: path and query "dbindex" parameters mismatch.');
}

$params += $query + $options + self::$defaultConnectionOptions;

if (isset($params['redis_sentinel']) && !class_exists(\Predis\Client::class) && !class_exists(\RedisSentinel::class)) {
Expand Down
Loading