Skip to content

Commit 4d11ddd

Browse files
authored
Merge pull request #69 from abacaphiliac/predis-client-override
allow Predis\Client to be overridden in LDDFeatureRequester
2 parents d7249ce + acc1ac5 commit 4d11ddd

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

src/LaunchDarkly/LDDFeatureRequester.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace LaunchDarkly;
33

44

5+
use Predis\ClientInterface;
56
use Psr\Log\LoggerInterface;
67

78
class LDDFeatureRequester implements FeatureRequester {
@@ -11,6 +12,8 @@ class LDDFeatureRequester implements FeatureRequester {
1112
protected $_features_key;
1213
/** @var LoggerInterface */
1314
private $_logger;
15+
/** @var ClientInterface */
16+
private $_connection;
1417

1518
function __construct($baseUri, $apiKey, $options) {
1619
$this->_baseUri = $baseUri;
@@ -31,11 +34,21 @@ function __construct($baseUri, $apiKey, $options) {
3134
$this->_features_key = "$prefix:features";
3235
$this->_logger = $options['logger'];
3336

37+
if (isset($this->_options['predis_client']) && $this->_options['predis_client'] instanceof ClientInterface) {
38+
$this->_connection = $this->_options['predis_client'];
39+
}
3440
}
3541

42+
/**
43+
* @return ClientInterface
44+
*/
3645
protected function get_connection() {
46+
if ($this->_connection instanceof ClientInterface) {
47+
return $this->_connection;
48+
}
49+
3750
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
38-
return new \Predis\Client(array(
51+
return $this->_connection = new \Predis\Client(array(
3952
"scheme" => "tcp",
4053
"host" => $this->_options['redis_host'],
4154
"port" => $this->_options['redis_port']));

tests/LDDFeatureRequesterTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace LaunchDarkly\Tests;
4+
5+
use LaunchDarkly\FeatureFlag;
6+
use LaunchDarkly\LDDFeatureRequester;
7+
use Predis\ClientInterface;
8+
use Psr\Log\LoggerInterface;
9+
use Psr\Log\NullLogger;
10+
11+
class LDDFeatureRequesterTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/** @var ClientInterface|\PHPUnit_Framework_MockObject_MockObject */
14+
private $predisClient;
15+
16+
/** @var LoggerInterface */
17+
private $logger;
18+
19+
protected function setUp()
20+
{
21+
parent::setUp();
22+
23+
$this->logger = new NullLogger();
24+
25+
$this->predisClient = $this->getMockBuilder(ClientInterface::class)
26+
->setMethods(['hget'])
27+
->getMockForAbstractClass();
28+
}
29+
30+
public function testGet()
31+
{
32+
$sut = new LDDFeatureRequester('example.com', 'MyApiKey', [
33+
'logger' => $this->logger,
34+
'predis_client' => $this->predisClient,
35+
]);
36+
37+
$this->predisClient->method('hget')->with('launchdarkly:features', 'foo')
38+
->willReturn(json_encode([
39+
'key' => 'foo',
40+
'version' => 14,
41+
'on' => true,
42+
'prerequisites' => [],
43+
'salt' => 'c3lzb3BzLXRlc3Q=',
44+
'sel' => '8ed13de1bfb14507ba7e6dde01f3e035',
45+
'targets' => [
46+
[
47+
'values' => [],
48+
'variation' => 0,
49+
],
50+
[
51+
'values' => [],
52+
'variation' => 1,
53+
],
54+
],
55+
'rules' => [],
56+
'fallthrough' => [
57+
'variation' => 0,
58+
],
59+
'offVariation' => null,
60+
'variations' => [
61+
true,
62+
false,
63+
],
64+
'deleted' => false,
65+
]));
66+
67+
$featureFlag = $sut->get('foo');
68+
69+
self::assertInstanceOf(FeatureFlag::class, $featureFlag);
70+
self::assertTrue($featureFlag->isOn());
71+
}
72+
}

0 commit comments

Comments
 (0)