From ed8758786395dd9dd9c9871ba7578e8fd0dfcd01 Mon Sep 17 00:00:00 2001 From: Fernando Varesi Date: Sun, 11 Feb 2018 21:37:27 -0300 Subject: [PATCH 1/2] issue #367 - pass options to predis client --- pkg/redis/RedisConnectionFactory.php | 4 +++- .../Tests/RedisConnectionFactoryTest.php | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) 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..2de187a26 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', 'host' => '172.18.0.7', '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); + } } From 23a4c451fbf47049354341c653162fee5e821d87 Mon Sep 17 00:00:00 2001 From: Fernando Varesi Date: Tue, 13 Feb 2018 17:56:51 -0300 Subject: [PATCH 2/2] fix redis server host in test --- pkg/redis/Tests/RedisConnectionFactoryTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/redis/Tests/RedisConnectionFactoryTest.php b/pkg/redis/Tests/RedisConnectionFactoryTest.php index 2de187a26..8ad9d0560 100644 --- a/pkg/redis/Tests/RedisConnectionFactoryTest.php +++ b/pkg/redis/Tests/RedisConnectionFactoryTest.php @@ -35,7 +35,7 @@ public function testShouldAcceptOptionsForPredisClient() ->setMethods(['createConnection']) ->getMock(); - $factory = new RedisConnectionFactory(['vendor' => 'predis', 'host' => '172.18.0.7', 'options' => ['foo' => 'bar']]); + $factory = new RedisConnectionFactory(['vendor' => 'predis', 'options' => ['foo' => 'bar']]); $context = $factory->createContext(); $predis = $context->getRedis();