Add "default" as accepted string for "connections".

Using "default" returns a connection factory instance with the default
configuration. Basically it is no different than omitting "connections"
in the client options array but it can be useful when applications want
to automatically configure Predis to use phpiredis when it is loaded:

$client = new Predis\Client('tcp://127.0.0.1', [
  'connections' =>
      extension_loaded('phpiredis')
        ? 'phpiredis'
        : 'default'
]);

This is in response to ISSUE #397.
This commit is contained in:
Daniele Alessandri
2020-08-27 15:59:14 +02:00
parent 2c732c46c8
commit 56d704f5dd
2 changed files with 30 additions and 0 deletions
+3
View File
@@ -112,6 +112,9 @@ class Connections implements OptionInterface
$factory->define('unix', 'Predis\Connection\PhpiredisSocketConnection');
break;
case 'default':
return $factory;
default:
throw new \InvalidArgumentException(sprintf(
'%s does not recognize `%s` as a supported configuration string', static::class, $value
@@ -88,6 +88,33 @@ class ConnectionsTest extends PredisTestCase
$this->assertSame($default, $factory);
}
/**
* @group disconnected
*/
public function testAcceptsStringDefaultToReturnConnectionFactoryWithDefaultConfiguration()
{
$options = $this->getMockBuilder('Predis\Configuration\OptionsInterface')->getMock();
$default = $this->getMockBuilder('Predis\Connection\FactoryInterface')->getMock();
$default
->expects($this->never())
->method('define');
$option = $this->getMockBuilder('Predis\Configuration\Option\Connections')
->setMethods(array('getDefault'))
->getMock();
$option
->expects($this->once())
->method('getDefault')
->with($options)
->will($this->returnValue($default));
$factory = $option->filter($options, 'default');
$this->assertInstanceOf('Predis\Connection\FactoryInterface', $factory);
$this->assertSame($default, $factory);
}
/**
* @group disconnected
*/