diff --git a/DependencyInjection/SncRedisExtension.php b/DependencyInjection/SncRedisExtension.php index 73903519..d6fee9ac 100644 --- a/DependencyInjection/SncRedisExtension.php +++ b/DependencyInjection/SncRedisExtension.php @@ -14,6 +14,7 @@ use Snc\RedisBundle\DependencyInjection\Configuration\Configuration; use Snc\RedisBundle\DependencyInjection\Configuration\RedisDsn; use Snc\RedisBundle\DependencyInjection\Configuration\RedisEnvDsn; +use Snc\RedisBundle\Factory\PredisParametersFactory; use Symfony\Component\DependencyInjection\Alias; use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; @@ -167,25 +168,6 @@ protected function loadPredisClient(array $client, ContainerBuilder $container) $connection['logging'] = $client['logging']; $connection['alias'] = $connectionAlias; - if ($dsn instanceof RedisDsn) { - if (null !== $dsn->getSocket()) { - $connection['scheme'] = 'unix'; - $connection['path'] = $dsn->getSocket(); - } else { - $connection['scheme'] = 'tcp'; - $connection['host'] = $dsn->getHost(); - $connection['port'] = $dsn->getPort(); - if (null !== $dsn->getDatabase()) { - $connection['path'] = $dsn->getDatabase(); - } - } - if (null !== $dsn->getDatabase()) { - $connection['database'] = $dsn->getDatabase(); - } - $connection['password'] = $dsn->getPassword(); - $connection['weight'] = $dsn->getWeight(); - } - $this->loadPredisConnectionParameters($client['alias'], $connection, $container, $dsn); } @@ -241,18 +223,14 @@ protected function loadPredisClient(array $client, ContainerBuilder $container) protected function loadPredisConnectionParameters($clientAlias, array $connection, ContainerBuilder $container, $dsn) { $parametersClass = $container->getParameter('snc_redis.connection_parameters.class'); - $parameterId = sprintf('snc_redis.connection.%s_parameters.%s', $connection['alias'], $clientAlias); + $parameterDef = new Definition($parametersClass); $parameterDef->setPublic(false); + $parameterDef->setFactory(array('Snc\RedisBundle\Factory\PredisParametersFactory', 'create')); $parameterDef->addArgument($connection); - - if ($dsn instanceof RedisEnvDsn) { - $parameterDef->setFactory(array('Snc\RedisBundle\Factory\PredisEnvParametersFactory', 'create')); - $parameterDef->addArgument($parametersClass); - $parameterDef->addArgument((string) $dsn); - } - + $parameterDef->addArgument($parametersClass); + $parameterDef->addArgument((string) $dsn); $parameterDef->addTag('snc_redis.connection_parameters', array('clientAlias' => $clientAlias)); $container->setDefinition($parameterId, $parameterDef); } diff --git a/Factory/PredisEnvParametersFactory.php b/Factory/PredisEnvParametersFactory.php deleted file mode 100644 index e3626c1b..00000000 --- a/Factory/PredisEnvParametersFactory.php +++ /dev/null @@ -1,30 +0,0 @@ -getSocket()) { + $options['scheme'] = 'unix'; + $options['path'] = $dsn->getSocket(); + } else { + $options['scheme'] = 'tcp'; + $options['host'] = $dsn->getHost(); + $options['port'] = $dsn->getPort(); + if (null !== $dsn->getDatabase()) { + $options['path'] = $dsn->getDatabase(); + } + } + if (null !== $dsn->getDatabase()) { + $options['database'] = $dsn->getDatabase(); + } + $options['password'] = $dsn->getPassword(); + $options['weight'] = $dsn->getWeight(); + + return $options; + } +} diff --git a/Tests/DependencyInjection/SncRedisExtensionEnvTest.php b/Tests/DependencyInjection/SncRedisExtensionEnvTest.php index c71355d0..a964096c 100644 --- a/Tests/DependencyInjection/SncRedisExtensionEnvTest.php +++ b/Tests/DependencyInjection/SncRedisExtensionEnvTest.php @@ -28,7 +28,7 @@ public function testPredisDefaultParameterConfigLoad() $container = $this->getConfiguredContainer('env_predis_minimal'); $this->assertSame( - array('Snc\RedisBundle\Factory\PredisEnvParametersFactory', 'create'), + array('Snc\RedisBundle\Factory\PredisParametersFactory', 'create'), $container->findDefinition('snc_redis.connection.default_parameters.default')->getFactory() ); } diff --git a/Tests/Factory/PredisEnvParametersFactoryTest.php b/Tests/Factory/PredisEnvParametersFactoryTest.php deleted file mode 100644 index 4f9daa2c..00000000 --- a/Tests/Factory/PredisEnvParametersFactoryTest.php +++ /dev/null @@ -1,56 +0,0 @@ - 123, - 'some' => 'string', - 'arbitrary' => true, - 'values' => array(1, 2, 3) - ) - ), - array( - 'redis://password@host:4711', - 'Predis\Connection\Parameters', - array() - ) - ); - } - - /** - * @param $dsn - * @param $class - * @param $options - * - * @dataProvider createDp - */ - public function testCreate($dsn, $class, $options) - { - $parameters = PredisEnvParametersFactory::create($options, $class, $dsn); - - $this->assertInstanceOf($class, $parameters); - - foreach ($options as $optionName => $optionValue) { - $this->assertEquals($optionValue, $parameters->{$optionName}); - } - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testCreateException() - { - PredisEnvParametersFactory::create(array(), '\stdClass', 'redis://localhost'); - } -} diff --git a/Tests/Factory/PredisParametersFactoryTest.php b/Tests/Factory/PredisParametersFactoryTest.php new file mode 100644 index 00000000..656f52f0 --- /dev/null +++ b/Tests/Factory/PredisParametersFactoryTest.php @@ -0,0 +1,97 @@ + 123, + 'some' => 'string', + 'arbitrary' => true, + 'values' => array(1, 2, 3) + ), + array( + 'test' => 123, + 'some' => 'string', + 'arbitrary' => true, + 'values' => array(1, 2, 3), + 'scheme' => 'tcp', + 'host' => 'ec2-34-321-123-45.us-east-1.compute.amazonaws.com', + 'port' => 3210, + 'path' => null, + 'alias' => null, + 'timeout' => null, + 'read_write_timeout' => null, + 'async_connect' => null, + 'tcp_nodelay' => null, + 'persistent' => null, + 'password' => 'df577d779b4f724c8c29b5eff5bcc534b732722b9df308a661f1b79014175063d5', + 'database' => null, + ), + ), + array( + 'redis://pw@/var/run/redis/redis-1.sock/10', + 'Predis\Connection\Parameters', + array( + 'test' => 124, + 'password' => 'toto', + 'alias' => 'one_alias', + ), + array( + 'test' => 124, + 'scheme' => 'unix', + 'host' => '127.0.0.1', + 'port' => 6379, + 'path' => '/var/run/redis/redis-1.sock', + 'alias' => 'one_alias', + 'timeout' => null, + 'read_write_timeout' => null, + 'async_connect' => null, + 'tcp_nodelay' => null, + 'persistent' => null, + 'password' => 'pw', + 'database' => 10, + ), + ) + ); + } + + /** + * @param string $dsn + * @param string $class + * @param array $options + * @param array $expectedParameters + * + * @dataProvider createDp + */ + public function testCreate($dsn, $class, $options, $expectedParameters) + { + $parameters = PredisParametersFactory::create($options, $class, $dsn); + + $this->assertInstanceOf($class, $parameters); + + foreach ($expectedParameters as $name => $value) { + $this->assertSame($value, $parameters->{$name}, "Wrong '$name' value"); + } + + // No user can exist within a redis connection. + $this->assertObjectNotHasAttribute('user', $parameters); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testCreateException() + { + PredisParametersFactory::create(array(), '\stdClass', 'redis://localhost'); + } +}