From c1b3bb365e6507fb2399a35155606dc7b6774fa0 Mon Sep 17 00:00:00 2001 From: Tim Younger Date: Wed, 22 Feb 2017 18:35:20 -0700 Subject: [PATCH 1/2] allow Predis\Client to be overridden via parameter. this will allow consumers to factory their own Client with different Connection backends, Sentinel support, etc. --- src/LaunchDarkly/LDDFeatureRequester.php | 8 +++ tests/LDDFeatureRequesterTest.php | 72 ++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 tests/LDDFeatureRequesterTest.php diff --git a/src/LaunchDarkly/LDDFeatureRequester.php b/src/LaunchDarkly/LDDFeatureRequester.php index f163ee90f..a4ce8fd7d 100644 --- a/src/LaunchDarkly/LDDFeatureRequester.php +++ b/src/LaunchDarkly/LDDFeatureRequester.php @@ -2,6 +2,7 @@ namespace LaunchDarkly; +use Predis\ClientInterface; use Psr\Log\LoggerInterface; class LDDFeatureRequester implements FeatureRequester { @@ -33,7 +34,14 @@ function __construct($baseUri, $apiKey, $options) { } + /** + * @return ClientInterface + */ protected function get_connection() { + if (isset($this->_options['predis_client']) && $this->_options['predis_client'] instanceof ClientInterface) { + return $this->_options['predis_client']; + } + /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ return new \Predis\Client(array( "scheme" => "tcp", diff --git a/tests/LDDFeatureRequesterTest.php b/tests/LDDFeatureRequesterTest.php new file mode 100644 index 000000000..97bfcc428 --- /dev/null +++ b/tests/LDDFeatureRequesterTest.php @@ -0,0 +1,72 @@ +logger = new NullLogger(); + + $this->predisClient = $this->getMockBuilder(ClientInterface::class) + ->setMethods(['hget']) + ->getMockForAbstractClass(); + } + + public function testGet() + { + $sut = new LDDFeatureRequester('example.com', 'MyApiKey', [ + 'logger' => $this->logger, + 'predis_client' => $this->predisClient, + ]); + + $this->predisClient->method('hget')->with('launchdarkly:features', 'foo') + ->willReturn(json_encode([ + 'key' => 'foo', + 'version' => 14, + 'on' => true, + 'prerequisites' => [], + 'salt' => 'c3lzb3BzLXRlc3Q=', + 'sel' => '8ed13de1bfb14507ba7e6dde01f3e035', + 'targets' => [ + [ + 'values' => [], + 'variation' => 0, + ], + [ + 'values' => [], + 'variation' => 1, + ], + ], + 'rules' => [], + 'fallthrough' => [ + 'variation' => 0, + ], + 'offVariation' => null, + 'variations' => [ + true, + false, + ], + 'deleted' => false, + ])); + + $featureFlag = $sut->get('foo'); + + self::assertInstanceOf(FeatureFlag::class, $featureFlag); + self::assertTrue($featureFlag->isOn()); + } +} From acc1ac5d1da6dd6dd243fe40793ece5518b509ed Mon Sep 17 00:00:00 2001 From: Tim Younger Date: Thu, 2 Mar 2017 21:20:35 -0700 Subject: [PATCH 2/2] move Predis Client override into constructor (where it belongs), and store a local private connection field so that fallback connections are not created over and over on subsequent feature requests. --- src/LaunchDarkly/LDDFeatureRequester.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/LaunchDarkly/LDDFeatureRequester.php b/src/LaunchDarkly/LDDFeatureRequester.php index a4ce8fd7d..a44bedc3c 100644 --- a/src/LaunchDarkly/LDDFeatureRequester.php +++ b/src/LaunchDarkly/LDDFeatureRequester.php @@ -12,6 +12,8 @@ class LDDFeatureRequester implements FeatureRequester { protected $_features_key; /** @var LoggerInterface */ private $_logger; + /** @var ClientInterface */ + private $_connection; function __construct($baseUri, $apiKey, $options) { $this->_baseUri = $baseUri; @@ -32,18 +34,21 @@ function __construct($baseUri, $apiKey, $options) { $this->_features_key = "$prefix:features"; $this->_logger = $options['logger']; + if (isset($this->_options['predis_client']) && $this->_options['predis_client'] instanceof ClientInterface) { + $this->_connection = $this->_options['predis_client']; + } } /** * @return ClientInterface */ protected function get_connection() { - if (isset($this->_options['predis_client']) && $this->_options['predis_client'] instanceof ClientInterface) { - return $this->_options['predis_client']; + if ($this->_connection instanceof ClientInterface) { + return $this->_connection; } /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ - return new \Predis\Client(array( + return $this->_connection = new \Predis\Client(array( "scheme" => "tcp", "host" => $this->_options['redis_host'], "port" => $this->_options['redis_port']));