Skip to content

Commit a1b745f

Browse files
committed
Fixes #67
1 parent 241bca5 commit a1b745f

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

integration-tests/LDDFeatureRequesterTest.php

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22
namespace LaunchDarkly\Tests;
33

44
use LaunchDarkly\LDClient;
5+
use LaunchDarkly\LDUser;
56
use LaunchDarkly\LDUserBuilder;
67
use LaunchDarkly\LDDFeatureRequester;
78
use LaunchDarkly\ApcLDDFeatureRequester;
89
use Predis\Client;
910
use LaunchDarkly\ApcuLDDFeatureRequester;
1011

1112
class LDDFeatureRetrieverTest extends \PHPUnit_Framework_TestCase {
13+
const API_KEY = 'BOGUS_API_KEY';
1214

1315
public function testGet() {
1416
$redis = new Client(array(
1517
"scheme" => "tcp",
1618
"host" => 'localhost',
1719
"port" => 6379));
18-
$client = new LDClient("BOGUS_API_KEY", array('feature_requester_class' => LDDFeatureRequester::class));
20+
$client = new LDClient(static::API_KEY, array('feature_requester_class' => LDDFeatureRequester::class));
1921
$builder = new LDUserBuilder(3);
2022
$user = $builder->build();
2123

@@ -33,7 +35,7 @@ public function testGetApc() {
3335
"scheme" => "tcp",
3436
"host" => 'localhost',
3537
"port" => 6379));
36-
$client = new LDClient("BOGUS_API_KEY", array('feature_requester_class' => ApcLDDFeatureRequester::class,
38+
$client = new LDClient(static::API_KEY, array('feature_requester_class' => ApcLDDFeatureRequester::class,
3739
'apc_expiration' => 1));
3840
$builder = new LDUserBuilder(3);
3941
$user = $builder->build();
@@ -55,18 +57,18 @@ public function testGetApcu() {
5557
if (!extension_loaded('apcu')) {
5658
self::markTestSkipped('Install `apcu` extension to run this test.');
5759
}
58-
60+
5961
$redis = new Client([
6062
'scheme' => 'tcp',
6163
'host' => 'localhost',
6264
'port' => 6379
6365
]);
64-
65-
$client = new LDClient('BOGUS_API_KEY', [
66+
67+
$client = new LDClient(static::API_KEY, [
6668
'feature_requester_class' => ApcuLDDFeatureRequester::class,
6769
'apc_expiration' => 1
6870
]);
69-
71+
7072
$builder = new LDUserBuilder(3);
7173
$user = $builder->build();
7274

@@ -83,6 +85,42 @@ public function testGetApcu() {
8385
$this->assertEquals('bob', $client->variation('fiz', $user, 'alice'));
8486
}
8587

88+
public function testGetAllWithoutFeatures()
89+
{
90+
$redis = new \Predis\Client([
91+
'scheme' => 'tcp',
92+
'host' => 'localhost',
93+
'port' => 6379,
94+
]);
95+
$redis->flushall();
96+
97+
$client = new LDClient(static::API_KEY, ['feature_requester_class' => LDDFeatureRequester::class]);
98+
$user = new LDUser(static::API_KEY);
99+
$allFlags = $client->allFlags($user);
100+
101+
$this->assertNull($allFlags);
102+
}
103+
104+
public function testGetAll()
105+
{
106+
$featureKey = 'foo';
107+
$featureValue = 'bar';
108+
109+
$redis = new \Predis\Client([
110+
'scheme' => 'tcp',
111+
'host' => 'localhost',
112+
'port' => 6379,
113+
]);
114+
$client = new LDClient(static::API_KEY, ['feature_requester_class' => LDDFeatureRequester::class]);
115+
$redis->hset('launchdarkly:features', $featureKey, $this->gen_feature($featureKey, $featureValue));
116+
$user = new LDUser(static::API_KEY);
117+
$allFlags = $client->allFlags($user);
118+
119+
$this->assertInternalType('array', $allFlags);
120+
$this->assertArrayHasKey($featureKey, $allFlags);
121+
$this->assertEquals($featureValue, $allFlags[$featureKey]);
122+
}
123+
86124
private function gen_feature($key, $val) {
87125
$data = [
88126
'name' => 'Feature ' . $key,
@@ -130,7 +168,7 @@ private function gen_feature($key, $val) {
130168
'offVariation' => null,
131169
'deleted' => false,
132170
];
133-
171+
134172
return \json_encode($data);
135173
}
136174

src/LaunchDarkly/LDDFeatureRequester.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function getAll() {
9696
$redis = $this->get_connection();
9797
$raw = $redis->hgetall($this->_features_key);
9898
if ($raw) {
99-
$allFlags = array_map(FeatureFlag::getDecoder(), json_decode($raw, true));
99+
$allFlags = array_map(FeatureFlag::getDecoder(), $this->decodeFeatures($raw));
100100
/**
101101
* @param $flag FeatureFlag
102102
* @return bool
@@ -110,4 +110,18 @@ public function getAll() {
110110
return null;
111111
}
112112
}
113-
}
113+
114+
/**
115+
* @param array $features
116+
*
117+
* @return array
118+
*/
119+
private function decodeFeatures(array $features)
120+
{
121+
foreach ($features as $featureKey => $feature) {
122+
$features[$featureKey] = json_decode($feature, true);
123+
}
124+
125+
return $features;
126+
}
127+
}

0 commit comments

Comments
 (0)