From 3f5d108d77b3eab76be981d32edf4d4c8a96be4f Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Wed, 13 Mar 2019 16:02:54 -0300 Subject: [PATCH] Leverage "options.parameters" config in `PhpredisClientFactory::create()` --- Factory/PhpredisClientFactory.php | 4 ++ .../SncRedisExtensionTest.php | 70 +++++++++++++++++++ .../Factory}/PhpredisClientFactoryTest.php | 38 +++++++++- 3 files changed, 111 insertions(+), 1 deletion(-) rename {Factory => Tests/Factory}/PhpredisClientFactoryTest.php (60%) diff --git a/Factory/PhpredisClientFactory.php b/Factory/PhpredisClientFactory.php index 38f6ba98..0618e6d1 100644 --- a/Factory/PhpredisClientFactory.php +++ b/Factory/PhpredisClientFactory.php @@ -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'])) { diff --git a/Tests/DependencyInjection/SncRedisExtensionTest.php b/Tests/DependencyInjection/SncRedisExtensionTest.php index ce9da0e1..517e6ef3 100644 --- a/Tests/DependencyInjection/SncRedisExtensionTest.php +++ b/Tests/DependencyInjection/SncRedisExtensionTest.php @@ -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(); @@ -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( diff --git a/Factory/PhpredisClientFactoryTest.php b/Tests/Factory/PhpredisClientFactoryTest.php similarity index 60% rename from Factory/PhpredisClientFactoryTest.php rename to Tests/Factory/PhpredisClientFactoryTest.php index e9e3a861..6962213c 100644 --- a/Factory/PhpredisClientFactoryTest.php +++ b/Tests/Factory/PhpredisClientFactoryTest.php @@ -1,8 +1,9 @@ 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); @@ -46,6 +55,10 @@ public function testCreateFullConfig() 'prefix' => 'toto', 'serialization' => 'php', 'read_write_timeout' => 4, + 'parameters' => [ + 'database' => 3, + 'password' => 'secret', + ], ), 'alias_test' ); @@ -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()); + } }