Browse Source

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.
Daniele Alessandri 8 years ago
parent
commit
a816adf6e7

+ 4 - 2
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

+ 7 - 1
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;
     }
 }

+ 26 - 0
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
      */