diff --git a/pkg/redis/RedisConnectionFactory.php b/pkg/redis/RedisConnectionFactory.php index 021a448f4..bf4f40057 100644 --- a/pkg/redis/RedisConnectionFactory.php +++ b/pkg/redis/RedisConnectionFactory.php @@ -28,6 +28,7 @@ class RedisConnectionFactory implements PsrConnectionFactory * 'persisted' => bool, Whether it use single persisted connection or open a new one for every context * 'lazy' => the connection will be performed as later as possible, if the option set to true * 'database' => Database index to select when connected (default value: 0) + * 'options' => options for predis client (default value: ['exceptions' => true]) * ]. * * or @@ -87,7 +88,8 @@ private function createRedis() } if ('predis' == $this->config['vendor'] && false == $this->redis) { - $this->redis = new PRedis(new Client($this->config, ['exceptions' => true])); + $options = array_replace(['exceptions' => true], empty($this->config['options']) ? [] : $this->config['options']); + $this->redis = new PRedis(new Client($this->config, $options)); } $this->redis->connect(); diff --git a/pkg/redis/Tests/RedisConnectionFactoryTest.php b/pkg/redis/Tests/RedisConnectionFactoryTest.php index b5d7347ab..8ad9d0560 100644 --- a/pkg/redis/Tests/RedisConnectionFactoryTest.php +++ b/pkg/redis/Tests/RedisConnectionFactoryTest.php @@ -28,4 +28,25 @@ public function testShouldCreateLazyContext() $this->assertAttributeEquals(null, 'redis', $context); $this->assertInternalType('callable', $this->readAttribute($context, 'redisFactory')); } + + public function testShouldAcceptOptionsForPredisClient() + { + $predisClientMock = $this->getMockBuilder(\Predis\Client::class) + ->setMethods(['createConnection']) + ->getMock(); + + $factory = new RedisConnectionFactory(['vendor' => 'predis', 'options' => ['foo' => 'bar']]); + + $context = $factory->createContext(); + $predis = $context->getRedis(); + + $reflector = new \ReflectionClass($predis); + $reflector_property = $reflector->getProperty('redis'); + $reflector_property->setAccessible(true); + $reflectorRedis = $reflector_property->getValue($predis); + $predisOptions = $reflectorRedis->getOptions(); + + $this->assertTrue($predisOptions->defined('foo')); + $this->assertEquals('bar', $predisOptions->foo); + } }