Skip to content

[Semaphore] allow redis cluster/sentinel dsn #59803

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
Feb 19, 2025
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
4 changes: 2 additions & 2 deletions src/Symfony/Component/Semaphore/Store/StoreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public static function createStore(#[\SensitiveParameter] object|string $connect

case !\is_string($connection):
throw new InvalidArgumentException(sprintf('Unsupported Connection: "%s".', $connection::class));
case str_starts_with($connection, 'redis://'):
case str_starts_with($connection, 'rediss://'):
case str_starts_with($connection, 'redis:'):
case str_starts_with($connection, 'rediss:'):
if (!class_exists(AbstractAdapter::class)) {
throw new InvalidArgumentException('Unsupported Redis DSN. Try running "composer require symfony/cache".');
}
Expand Down
49 changes: 16 additions & 33 deletions src/Symfony/Component/Semaphore/Tests/Store/StoreFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,37 @@
namespace Symfony\Component\Semaphore\Tests\Store;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Traits\RedisProxy;
use Symfony\Component\Semaphore\Store\RedisStore;
use Symfony\Component\Semaphore\Store\StoreFactory;

/**
* @author Jérémy Derussé <jeremy@derusse.com>
*
* @requires extension redis
*/
class StoreFactoryTest extends TestCase
{
public function testCreateRedisStore()
/**
* @dataProvider validConnections
*/
public function testCreateStore($connection, string $expectedStoreClass)
{
$store = StoreFactory::createStore($this->createMock(\Redis::class));
$store = StoreFactory::createStore($connection);

$this->assertInstanceOf(RedisStore::class, $store);
$this->assertInstanceOf($expectedStoreClass, $store);
}

public function testCreateRedisProxyStore()
public static function validConnections(): \Generator
{
if (!class_exists(RedisProxy::class)) {
$this->markTestSkipped();
}
yield [new \Predis\Client(), RedisStore::class];

$store = StoreFactory::createStore($this->createMock(RedisProxy::class));

$this->assertInstanceOf(RedisStore::class, $store);
}

public function testCreateRedisAsDsnStore()
{
if (!class_exists(RedisProxy::class)) {
$this->markTestSkipped();
if (class_exists(\Redis::class)) {
yield [new \Redis(), RedisStore::class];
}

$store = StoreFactory::createStore('redis://localhost');

$this->assertInstanceOf(RedisStore::class, $store);
}

public function testCreatePredisStore()
{
if (!class_exists(\Predis\Client::class)) {
$this->markTestSkipped();
if (class_exists(\Redis::class) && class_exists(AbstractAdapter::class)) {
yield ['redis://localhost', RedisStore::class];
yield ['redis://localhost?lazy=1', RedisStore::class];
yield ['redis://localhost?redis_cluster=1', RedisStore::class];
yield ['redis://localhost?redis_cluster=1&lazy=1', RedisStore::class];
yield ['redis:?host[localhost]&host[localhost:6379]&redis_cluster=1', RedisStore::class];
}

$store = StoreFactory::createStore(new \Predis\Client());

$this->assertInstanceOf(RedisStore::class, $store);
}
}
Loading