8000 bug #50585 [Cache] Fix RedisTrait::createConnection for cluster (dark… · symfony/symfony@ca9a8c1 · GitHub
[go: up one dir, main page]

Skip to content

Commit ca9a8c1

Browse files
bug #50585 [Cache] Fix RedisTrait::createConnection for cluster (darkanakin41)
This PR was submitted for the 6.3 branch but it was squashed and merged into the 5.4 branch instead. Discussion ---------- [Cache] Fix RedisTrait::createConnection for cluster | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | No | Deprecations? | No | Tickets | Fix #50509 | License | MIT | Doc PR | N/A When the dsn contains `redis_cluster=1` or `redis_cluster=true` the value was not properly parsed, and the class generated was not the expected RedisCluster. The reason is that the PHP core function `match` make a `===` check. So, as the dsn is a string, the `$params['redis_cluster']` needs to be force to a bool Commits ------- ca8e328 [Cache] Fix RedisTrait::createConnection for cluster
2 parents dda4e7c + ca8e328 commit ca9a8c1

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Tests\Traits;
13+
14+
use PHPUnit\Framework\SkippedTestSuiteError;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\Cache\Traits\RedisTrait;
17+
18+
class RedisTraitTest extends TestCase
19+
{
20+
public static function setUpBeforeClass(): void
21+
{
22+
if (!getenv('REDIS_CLUSTER_HOSTS')) {
23+
throw new SkippedTestSuiteError('REDIS_CLUSTER_HOSTS env var is not defined.');
24+
}
25+
}
26+
27+
/**
28+
* @dataProvider provideCreateConnection
29+
*/
30+
public function testCreateConnection(string $dsn, string $expectedClass)
31+
{
32+
if (!class_exists($expectedClass)) {
33+
throw new SkippedTestSuiteError(sprintf('The "%s" class is required.', $expectedClass));
34+
}
35+
if (!getenv('REDIS_CLUSTER_HOSTS')) {
36+
throw new SkippedTestSuiteError('REDIS_CLUSTER_HOSTS env var is not defined.');
37+
}
38+
39+
$mock = self::getObjectForTrait(RedisTrait::class);
40+
$connection = $mock::createConnection($dsn);
41+
42+
self::assertInstanceOf($expectedClass, $connection);
43+
}
44+
45+
public static function provideCreateConnection(): array
46+
{
47+
$hosts = array_map(function ($host) { return sprintf('host[%s]', $host); }, explode(' ', getenv('REDIS_CLUSTER_HOSTS')));
48+
49+
return [
50+
[
51+
sprintf('redis:?%s&redis_cluster=1', $hosts[0]),
52+
'RedisCluster',
53+
],
54+
[
55+
sprintf('redis:?%s&redis_cluster=true', $hosts[0]),
56+
'RedisCluster',
57+
],
58+
[
59+
sprintf('redis:?%s', $hosts[0]),
60+
'Redis',
61+
],
62+
[
63+
'dsn' => sprintf('redis:?%s', implode('&', \array_slice($hosts, 0, 2))),
64+
'RedisArray',
65+
],
66+
];
67+
}
68+
}

src/Symfony/Component/Cache/Traits/RedisTrait.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ public static function createConnection(string $dsn, array $options = [])
174174
throw new CacheException(sprintf('Redis Sentinel support requires the "predis/predis" package or the "redis" extension v5.2 or higher: "%s".', $dsn));
175175
}
176176

177+
if (isset($params['lazy'])) {
178+
$params['lazy'] = filter_var($params['lazy'], \FILTER_VALIDATE_BOOLEAN);
179+
}
180+
$params['redis_cluster'] = filter_var($params['redis_cluster'], \FILTER_VALIDATE_BOOLEAN);
181+
177182
if ($params['redis_cluster'] && isset($params['redis_sentinel'])) {
178183
throw new InvalidArgumentException(sprintf('Cannot use both "redis_cluster" and "redis_sentinel" at the same time: "%s".', $dsn));
179184
}

0 commit comments

Comments
 (0)
0