From f8b7ece07cfa951dfd1cdb3bf090ae7bd7617ece 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 | 32 ++++++++- 3 files changed, 105 insertions(+), 1 deletion(-) rename {Factory => Tests/Factory}/PhpredisClientFactoryTest.php (66%) 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 66% rename from Factory/PhpredisClientFactoryTest.php rename to Tests/Factory/PhpredisClientFactoryTest.php index e9e3a861..4dab9d18 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() @@ -46,6 +49,10 @@ public function testCreateFullConfig() 'prefix' => 'toto', 'serialization' => 'php', 'read_write_timeout' => 4, + 'parameters' => [ + 'database' => 3, + 'password' => 'secret', + ], ), 'alias_test' ); @@ -54,6 +61,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( + '\Snc\RedisBundle\Client\Phpredis\Client', + '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()); + } }