Skip to content

Commit d9f93e3

Browse files
committed
[Cache] Add type to redis scan call to avoid using default "" which returns empty array
1 parent 28ba7d9 commit d9f93e3

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

src/Symfony/Component/Cache/Tests/Traits/RedisTraitTest.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@
1717

1818
class RedisTraitTest extends TestCase
1919
{
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-
2720
/**
2821
* @dataProvider provideCreateConnection
2922
*/
@@ -65,4 +58,31 @@ public static function provideCreateConnection(): array
6558
],
6659
];
6760
}
61+
62+
public function testClearUsesValidType()
63+
{
64+
if (!class_exists(\Redis::class)) {
65+
throw new SkippedTestSuiteError(sprintf('The "%s" class is required.', \Redis::class));
66+
}
67+
$redisMock = $this->getMockBuilder(\Redis::class)
68+
->disableOriginalConstructor()
69+
->getMock();
70+
$redisMock->method('info')->willReturn(['redis_version' => '3.0.0']);
71+
$redisMock->expects(self::once())
72+
->method('scan')
73+
->with(null, 'some-namespace*', 1000, 'string')
74+
->willReturn([0, []]);
75+
$mock = new class($redisMock) {
76+
use RedisTrait {
77+
doClear as public;
78+
}
79+
80+
public function __construct($redis)
81+
{
82+
$this->redis = $redis;
83+
}
84+
};
85+
86+
$mock->doClear('some-namespace');
87+
}
6888
}

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,10 @@ public static function createConnection(string $dsn, array $options = [])
291291
} elseif (is_a($class, \RedisArray::class, true)) {
292292
foreach ($hosts as $i => $host) {
293293
switch ($host['scheme']) {
294-
case 'tcp': $hosts[$i] = $host['host'].':'.$host['port']; break;
295-
case 'tls': $hosts[$i] = 'tls://'.$host['host'].':'.$host['port']; break;
294+
case 'tcp': $hosts[$i] = $host['host'].':'.$host['port'];
295+
break;
296+
case 'tls': $hosts[$i] = 'tls://'.$host['host'].':'.$host['port'];
297+
break;
296298
default: $hosts[$i] = $host['path'];
297299
}
298300
}
@@ -312,8 +314,10 @@ public static function createConnection(string $dsn, array $options = [])
312314
$initializer = static function () use ($class, $params, $dsn, $hosts) {
313315
foreach ($hosts as $i => $host) {
314316
switch ($host['scheme']) {
315-
case 'tcp': $hosts[$i] = $host['host'].':'.$host['port']; break;
316-
case 'tls': $hosts[$i] = 'tls://'.$host['host'].':'.$host['port']; break;
317+
case 'tcp': $hosts[$i] = $host['host'].':'.$host['port'];
318+
break;
319+
case 'tls': $hosts[$i] = 'tls://'.$host['host'].':'.$host['port'];
320+
break;
317321
default: $hosts[$i] = $host['path'];
318322
}
319323
}
@@ -328,9 +332,12 @@ public static function createConnection(string $dsn, array $options = [])
328332
$redis->setOption(\Redis::OPT_TCP_KEEPALIVE, $params['tcp_keepalive']);
329333
}
330334
switch ($params['failover']) {
331-
case 'error': $redis->setOption(\RedisCluster::OPT_SLAVE_FAILOVER, \RedisCluster::FAILOVER_ERROR); break;
332-
case 'distribute': $redis->setOption(\RedisCluster::OPT_SLAVE_FAILOVER, \RedisCluster::FAILOVER_DISTRIBUTE); break;
333-
case 'slaves': $redis->setOption(\RedisCluster::OPT_SLAVE_FAILOVER, \RedisCluster::FAILOVER_DISTRIBUTE_SLAVES); break;
335+
case 'error': $redis->setOption(\RedisCluster::OPT_SLAVE_FAILOVER, \RedisCluster::FAILOVER_ERROR);
336+
break;
337+
case 'distribute': $redis->setOption(\RedisCluster::OPT_SLAVE_FAILOVER, \RedisCluster::FAILOVER_DISTRIBUTE);
338+
break;
339+
case 'slaves': $redis->setOption(\RedisCluster::OPT_SLAVE_FAILOVER, \RedisCluster::FAILOVER_DISTRIBUTE_SLAVES);
340+
break;
334341
}
335342

336343
return $redis;
@@ -476,7 +483,7 @@ protected function doClear(string $namespace)
476483

477484
$cursor = null;
478485
do {
479-
$keys = $host instanceof \Predis\ClientInterface ? $host->scan($cursor, 'MATCH', $pattern, 'COUNT', 1000) : $host->scan($cursor, $pattern, 1000);
486+
$keys = $host instanceof \Predis\ClientInterface ? $host->scan($cursor, 'MATCH', $pattern, 'COUNT', 1000) : $host->scan($cursor, $pattern, 1000, 'string');
480487
if (isset($keys[1]) && \is_array($keys[1])) {
481488
$cursor = $keys[0];
482489
$keys = $keys[1];

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy