From a816adf6e7c407d70dec8447d2115ee39a9cce29 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Fri, 13 May 2016 12:48:31 +0200 Subject: [PATCH] Set default parameters via client options. This is mostly useful when configuring the client to use redis-cluster or redis-sentinel in order to set a common password for authentication or database. In these kind of configurations it is impossible to pass them via connection parameters as connections are created dinamically by the client depending on the server response. --- CHANGELOG.md | 6 +++-- src/Configuration/ConnectionFactoryOption.php | 8 +++++- .../ConnectionFactoryOptionTest.php | 26 +++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8c1961a..b9c9341b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,8 +19,10 @@ v1.1.0 (2015-xx-xx) considered obsolete and will not be supported from the next major release. - Added support for default connection parameters in `Predis\Connection\Factory` - augmenting the user-supplied parameters used to create new connections. - but they do not override specific parameters when already defined. + augmenting the user-supplied parameters used to create new connections. These + parameters do not override specific parameters when already defined. They can + also be passed via client options when configuring the client using the newly + added `parameters` option. - Removed the default value for `timeout` from `Predis\Connection\Parameters`. The fallback to a default value is a responsibility of connection classes, but diff --git a/src/Configuration/ConnectionFactoryOption.php b/src/Configuration/ConnectionFactoryOption.php index ba38df96..bf8479c9 100644 --- a/src/Configuration/ConnectionFactoryOption.php +++ b/src/Configuration/ConnectionFactoryOption.php @@ -49,6 +49,12 @@ class ConnectionFactoryOption implements OptionInterface */ public function getDefault(OptionsInterface $options) { - return new Factory(); + $factory = new Factory(); + + if ($options->defined('parameters')) { + $factory->setDefaultParameters($options->parameters); + } + + return $factory; } } diff --git a/tests/Predis/Configuration/ConnectionFactoryOptionTest.php b/tests/Predis/Configuration/ConnectionFactoryOptionTest.php index 437b279a..d4863a4b 100644 --- a/tests/Predis/Configuration/ConnectionFactoryOptionTest.php +++ b/tests/Predis/Configuration/ConnectionFactoryOptionTest.php @@ -54,6 +54,32 @@ class ConnectionFactoryOptionTest extends PredisTestCase $this->assertSame($default, $factory); } + /** + * @group disconnected + */ + public function testUsesParametersOptionToSetDefaultParameters() + { + $parameters = array('database' => 5, 'password' => 'mypassword'); + + $default = $this->getMock('Predis\Connection\Factory'); + $options = $this->getMock('Predis\Configuration\OptionsInterface'); + + $options->expects($this->once()) + ->method('defined') + ->with('parameters') + ->will($this->returnValue(true)); + + $options->expects($this->once()) + ->method('__get') + ->with('parameters') + ->will($this->returnValue($parameters)); + + $option = new ConnectionFactoryOption(); + $factory = $option->getDefault($options); + + $this->assertSame($parameters, $factory->getDefaultParameters()); + } + /** * @group disconnected */