Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Redis] add dsn support for symfony bundle. #266

Merged
merged 1 commit into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/redis/RedisMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function getTimestamp()
{
$value = $this->getHeader('timestamp');

return $value === null ? null : (int) $value;
return null === $value ? null : (int) $value;
}

/**
Expand Down
13 changes: 10 additions & 3 deletions pkg/redis/Symfony/RedisTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,23 @@ public function __construct($name = 'redis')
public function addConfiguration(ArrayNodeDefinition $builder)
{
$builder
->beforeNormalization()
->ifTrue(function ($node) {
return empty($node['dsn']) && (empty($node['host']) || empty($node['vendor']));
})
->thenInvalid('Invalid configuration %s')
->end()
->children()
->scalarNode('dsn')
->info('The redis connection given as DSN. For example redis://host:port?vendor=predis')
->end()
->scalarNode('host')
->isRequired()
->cannotBeEmpty()
->info('can be a host, or the path to a unix domain socket')
->end()
->integerNode('port')->end()
->enumNode('vendor')
->values(['phpredis', 'predis'])
->isRequired()
->cannotBeEmpty()
->info('The library used internally to interact with Redis server')
->end()
Expand All @@ -67,7 +74,7 @@ public function addConfiguration(ArrayNodeDefinition $builder)
public function createConnectionFactory(ContainerBuilder $container, array $config)
{
$factory = new Definition(RedisConnectionFactory::class);
$factory->setArguments([$config]);
$factory->setArguments([isset($config['dsn']) ? $config['dsn'] : $config]);

$factoryId = sprintf('enqueue.transport.%s.connection_factory', $this->getName());
$container->setDefinition($factoryId, $factory);
Expand Down
4 changes: 2 additions & 2 deletions pkg/redis/Tests/RedisConnectionFactoryConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static function provideConfigs()
];

yield [
'redis://localhost:1234?foo=bar&lazy=0&persisted=true',
'redis://localhost:1234?foo=bar&lazy=0&persisted=true&database=5',
[
'host' => 'localhost',
'port' => 1234,
Expand All @@ -117,7 +117,7 @@ public static function provideConfigs()
'persisted' => true,
'lazy' => false,
'foo' => 'bar',
'database' => 0,
'database' => 5,
],
];

Expand Down
20 changes: 20 additions & 0 deletions pkg/redis/Tests/Symfony/RedisTransportFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ public function testShouldAllowAddConfiguration()
], $config);
}

public function testShouldAllowAddConfigurationFromDSN()
{
$transport = new RedisTransportFactory();
$tb = new TreeBuilder();
$rootNode = $tb->root('foo');

$transport->addConfiguration($rootNode);
$processor = new Processor();
$config = $processor->process($tb->buildTree(), [[
'dsn' => 'redis://localhost:8080?vendor=predis&persisted=false&lazy=true&database=5',
]]);

$this->assertEquals([
'persisted' => false,
'lazy' => true,
'database' => 0,
'dsn' => 'redis://localhost:8080?vendor=predis&persisted=false&lazy=true&database=5',
], $config);
}

public function testShouldCreateConnectionFactory()
{
$container = new ContainerBuilder();
Expand Down