From ca8e3287abffa6bb7b63cf0f40159b68e0a8ce87 Mon Sep 17 00:00:00 2001 From: Pierre Lejeune Date: Wed, 7 Jun 2023 08:25:31 +0200 Subject: [PATCH] [Cache] Fix RedisTrait::createConnection for cluster --- .../Cache/Tests/Traits/RedisTraitTest.php | 68 +++++++++++++++++++ .../Component/Cache/Traits/RedisTrait.php | 5 ++ 2 files changed, 73 insertions(+) create mode 100644 src/Symfony/Component/Cache/Tests/Traits/RedisTraitTest.php diff --git a/src/Symfony/Component/Cache/Tests/Traits/RedisTraitTest.php b/src/Symfony/Component/Cache/Tests/Traits/RedisTraitTest.php new file mode 100644 index 0000000000000..e7e368b3e829d --- /dev/null +++ b/src/Symfony/Component/Cache/Tests/Traits/RedisTraitTest.php @@ -0,0 +1,68 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Cache\Tests\Traits; + +use PHPUnit\Framework\SkippedTestSuiteError; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Cache\Traits\RedisTrait; + +class RedisTraitTest extends TestCase +{ + public static function setUpBeforeClass(): void + { + if (!getenv('REDIS_CLUSTER_HOSTS')) { + throw new SkippedTestSuiteError('REDIS_CLUSTER_HOSTS env var is not defined.'); + } + } + + /** + * @dataProvider provideCreateConnection + */ + public function testCreateConnection(string $dsn, string $expectedClass) + { + if (!class_exists($expectedClass)) { + throw new SkippedTestSuiteError(sprintf('The "%s" class is required.', $expectedClass)); + } + if (!getenv('REDIS_CLUSTER_HOSTS')) { + throw new SkippedTestSuiteError('REDIS_CLUSTER_HOSTS env var is not defined.'); + } + + $mock = self::getObjectForTrait(RedisTrait::class); + $connection = $mock::createConnection($dsn); + + self::assertInstanceOf($expectedClass, $connection); + } + + public static function provideCreateConnection(): array + { + $hosts = array_map(function ($host) { return sprintf('host[%s]', $host); }, explode(' ', getenv('REDIS_CLUSTER_HOSTS'))); + + return [ + [ + sprintf('redis:?%s&redis_cluster=1', $hosts[0]), + 'RedisCluster', + ], + [ + sprintf('redis:?%s&redis_cluster=true', $hosts[0]), + 'RedisCluster', + ], + [ + sprintf('redis:?%s', $hosts[0]), + 'Redis', + ], + [ + 'dsn' => sprintf('redis:?%s', implode('&', \array_slice($hosts, 0, 2))), + 'RedisArray', + ], + ]; + } +} diff --git a/src/Symfony/Component/Cache/Traits/RedisTrait.php b/src/Symfony/Component/Cache/Traits/RedisTrait.php index 67d866316938b..220e65e744d43 100644 --- a/src/Symfony/Component/Cache/Traits/RedisTrait.php +++ b/src/Symfony/Component/Cache/Traits/RedisTrait.php @@ -174,6 +174,11 @@ public static function createConnection(string $dsn, array $options = []) throw new CacheException(sprintf('Redis Sentinel support requires the "predis/predis" package or the "redis" extension v5.2 or higher: "%s".', $dsn)); } + if (isset($params['lazy'])) { + $params['lazy'] = filter_var($params['lazy'], \FILTER_VALIDATE_BOOLEAN); + } + $params['redis_cluster'] = filter_var($params['redis_cluster'], \FILTER_VALIDATE_BOOLEAN); + if ($params['redis_cluster'] && isset($params['redis_sentinel'])) { throw new InvalidArgumentException(sprintf('Cannot use both "redis_cluster" and "redis_sentinel" at the same time: "%s".', $dsn)); }