Skip to content

Commit

Permalink
Merge pull request #505 from phansys/phpredis_parameters
Browse files Browse the repository at this point in the history
Leverage "options.parameters" config in `PhpredisClientFactory::create()`
  • Loading branch information
B-Galati authored Mar 26, 2019
2 parents fad5047 + 3f5d108 commit a98f08a
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Factory/PhpredisClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ public function create($class, $dsn, $options, $alias)

if (null !== $parsedDsn->getPassword()) {
$client->auth($parsedDsn->getPassword());
} elseif (isset($options['parameters']['password'])) {
$client->auth($options['parameters']['password']);
}

if (null !== $parsedDsn->getDatabase()) {
$client->select($parsedDsn->getDatabase());
} elseif (isset($options['parameters']['database'])) {
$client->select($options['parameters']['database']);
}

if (isset($options['read_write_timeout'])) {
Expand Down
70 changes: 70 additions & 0 deletions Tests/DependencyInjection/SncRedisExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,46 @@ public function testClusterOption()
$this->assertEquals(array('snc_redis.default' => array(array('alias' => 'default'))), $container->findTaggedServiceIds('snc_redis.client'));
}

/**
* Test provided options are respected
*/
public function testPhpRedisParameters()
{
$extension = new SncRedisExtension();
$config = $this->parseYaml($this->getPhpRedisYamlConfigWithParameters());
$extension->load(array($config), $container = $this->getContainer());

$defaultParameters = $container->getDefinition('snc_redis.phpredis.default');

$this->assertSame(1, $defaultParameters->getArgument(2)['parameters']['database']);
$this->assertSame('pass', $defaultParameters->getArgument(2)['parameters']['password']);

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

$this->assertSame(1, $redis->getDBNum());
$this->assertSame('pass', $redis->getAuth());
}

/**
* Test parameters provided at DSN overrides the provided options
*/
public function testPhpRedisDuplicatedParameters()
{
$extension = new SncRedisExtension();
$config = $this->parseYaml($this->getPhpRedisYamlConfigWithDuplicatedParameters());
$extension->load(array($config), $container = $this->getContainer());

$defaultParameters = $container->getDefinition('snc_redis.phpredis.default');

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

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

$this->assertSame(1, $redis->getDBNum());
$this->assertSame('pass', $redis->getAuth());
}

private function parseYaml($yaml)
{
$parser = new Parser();
Expand Down Expand Up @@ -603,6 +643,36 @@ private function getMultipleReplicationYamlConfig()
EOF;
}

private function getPhpRedisYamlConfigWithParameters()
{
return <<<'EOF'
clients:
default:
type: phpredis
alias: default
dsn: redis://localhost
options:
parameters:
database: 1
password: pass
EOF;
}

private function getPhpRedisYamlConfigWithDuplicatedParameters()
{
return <<<'EOF'
clients:
default:
type: phpredis
alias: default
dsn: redis://redis:pass@localhost/1
options:
parameters:
database: 2
password: word
EOF;
}

private function getContainer()
{
return new ContainerBuilder(new ParameterBag(array(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace Snc\RedisBundle\Factory;
namespace Snc\RedisBundle\Tests\Factory;

use PHPUnit\Framework\TestCase;
use Snc\RedisBundle\Factory\PhpredisClientFactory;
use Snc\RedisBundle\Logger\RedisLogger;

class PhpredisClientFactoryTest extends TestCase
Expand Down Expand Up @@ -30,10 +31,18 @@ public function testCreateMinimalConfig()
$this->assertInstanceOf('\Redis', $client);
$this->assertNull($client->getOption(\Redis::OPT_PREFIX));
$this->assertSame(0, $client->getOption(\Redis::OPT_SERIALIZER));
$this->assertSame(0, $client->getDBNum());
$this->assertNull($client->getAuth());
}

public function testCreateFullConfig()
{
// @todo: Remove this condition when the inheritance from `\Redis` is fixed
// see https://github.com/snc/SncRedisBundle/issues/399
if (version_compare(phpversion('redis'), '4.0.0') >= 0) {
$this->markTestSkipped('This test cannot be executed on Redis extension version ' . phpversion('redis'));
}

$logger = $this->getMockBuilder('Snc\RedisBundle\Logger\RedisLogger')->getMock();
$factory = new PhpredisClientFactory($logger);

Expand All @@ -46,6 +55,10 @@ public function testCreateFullConfig()
'prefix' => 'toto',
'serialization' => 'php',
'read_write_timeout' => 4,
'parameters' => [
'database' => 3,
'password' => 'secret',
],
),
'alias_test'
);
Expand All @@ -54,6 +67,29 @@ public function testCreateFullConfig()
$this->assertSame('toto', $client->getOption(\Redis::OPT_PREFIX));
$this->assertSame(1, $client->getOption(\Redis::OPT_SERIALIZER));
$this->assertSame(4., $client->getOption(\Redis::OPT_READ_TIMEOUT));
$this->assertSame(3, $client->getDBNum());
$this->assertSame('secret', $client->getAuth());
$this->assertAttributeSame($logger, 'logger', $client);
}

public function testDsnConfig()
{
$factory = new PhpredisClientFactory();

$client = $factory->create(
'\Redis',
'redis://redis:pass@localhost:6379/2',
array(
'parameters' => [
'database' => 3,
'password' => 'secret',
],
),
'alias_test'
);

$this->assertInstanceOf('\Redis', $client);
$this->assertSame(2, $client->getDBNum());
$this->assertSame('pass', $client->getAuth());
}
}

0 comments on commit a98f08a

Please sign in to comment.