Skip to content

Commit

Permalink
Enable usage of username in dsn (#678)
Browse files Browse the repository at this point in the history
Closes #673
  • Loading branch information
AntoineVallerand committed Nov 10, 2022
1 parent e7ee8c4 commit 5797249
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Factory/PhpredisClientFactory.php
Expand Up @@ -152,7 +152,7 @@ private function createClient(RedisDsn $dsn, string $class, string $alias, array
$client->connect(...$connectParameters);
}

$username = $options['parameters']['username'] ?? null;
$username = $dsn->getUsername() ?? $options['parameters']['username'] ?? null;
$password = $dsn->getPassword() ?? $options['parameters']['password'] ?? null;
if ($username !== null && $password !== null) {
$client->auth([$username, $password]);
Expand Down
6 changes: 4 additions & 2 deletions tests/DependencyInjection/SncRedisExtensionTest.php
Expand Up @@ -429,11 +429,12 @@ public function testPhpRedisDuplicatedParameters(): void

$this->assertSame(2, $defaultParameters->getArgument(2)['parameters']['database']);
$this->assertSame('otherpassword', $defaultParameters->getArgument(2)['parameters']['password']);
$this->assertSame('otherusername', $defaultParameters->getArgument(2)['parameters']['username']);

$redis = $container->get('snc_redis.default');

$this->assertSame(1, $redis->getDBNum());
$this->assertSame('sncredis', $redis->getAuth());
$this->assertSame(['snc_redis', 'snc_password'], $redis->getAuth());
}

/**
Expand Down Expand Up @@ -733,11 +734,12 @@ private function getPhpRedisYamlConfigWithDuplicatedParameters(): string
default:
type: phpredis
alias: default
dsn: redis://redis:sncredis@localhost/1
dsn: redis://snc_redis:snc_password@localhost:8000/1
options:
parameters:
database: 2
password: otherpassword
username: otherusername
EOF;
}

Expand Down
38 changes: 34 additions & 4 deletions tests/Factory/PhpredisClientFactoryTest.php
Expand Up @@ -130,7 +130,7 @@ public function testDsnConfig(): void

$client = $factory->create(
Redis::class,
['redis://redis:sncredis@localhost:6379/2'],
['redis://sncredis@localhost:6379/2'],
[
'parameters' => [
'database' => 3,
Expand All @@ -148,13 +148,43 @@ public function testDsnConfig(): void
$this->assertNull($client->getPersistentID());
}

public function testDsnConfigWithUsername(): void
{
$this->logger->method('debug')->withConsecutive(
[$this->stringContains('Executing command "CONNECT localhost 8000 5')],
['Executing command "AUTH snc_redis snc_password"'],
['Executing command "SELECT 0"'],
);

$factory = new PhpredisClientFactory(new RedisCallInterceptor($this->redisLogger));

$client = $factory->create(
Redis::class,
['redis://snc_redis:snc_password@localhost:8000/0'],
[
'parameters' => [
'database' => 3,
'password' => 'secret',
],
'connection_timeout' => 5,
],
'alias_test',
true,
);

$this->assertInstanceOf(Redis::class, $client);
$this->assertSame(0, $client->getDBNum());
$this->assertSame(['snc_redis', 'snc_password'], $client->getAuth());
$this->assertNull($client->getPersistentID());
}

public function testNestedDsnConfig(): void
{
$factory = new PhpredisClientFactory(new RedisCallInterceptor($this->redisLogger));

$client = $factory->create(
Redis::class,
[['redis://redis:sncredis@localhost:6379/2']],
[['redis://sncredis@localhost:6379/2']],
[
'parameters' => [
'database' => 3,
Expand Down Expand Up @@ -234,7 +264,7 @@ public function testMethodsWithVariadicParameters(): void

$client = $factory->create(
Redis::class,
['redis://redis:sncredis@localhost:6379/2'],
['redis://sncredis@localhost:6379/2'],
[
'parameters' => [
'database' => 3,
Expand Down Expand Up @@ -265,7 +295,7 @@ public function testMethodWithPassByRefArgument(): void

$client = $factory->create(
Redis::class,
['redis://redis:sncredis@localhost:6379/2'],
['redis://sncredis@localhost:6379/2'],
['connection_timeout' => 5],
'alias_test',
true,
Expand Down
10 changes: 5 additions & 5 deletions tests/Functional/App/config.yaml
Expand Up @@ -19,20 +19,20 @@ snc_redis:
default:
type: phpredis
alias: default
dsn: redis://redis:sncredis@localhost
dsn: redis://sncredis@localhost
logging: '%kernel.debug%'
cache:
type: predis
alias: cache
dsn: redis://redis:sncredis@localhost/1
dsn: redis://sncredis@localhost/1
logging: false
cluster:
type: predis
alias: cluster
dsn:
- redis://redis:sncredis@127.0.0.1/3
- redis://redis:sncredis@127.0.0.1/4
- redis://redis:sncredis@127.0.0.1/5
- redis://sncredis@127.0.0.1/3
- redis://sncredis@127.0.0.1/4
- redis://sncredis@127.0.0.1/5
options:
prefix: foo
profile: 2.4
Expand Down

0 comments on commit 5797249

Please sign in to comment.