From a1b745f82231a8c656831eaca515f13fd17dd723 Mon Sep 17 00:00:00 2001 From: Tiago Butzke Date: Wed, 22 Feb 2017 19:56:58 +0100 Subject: [PATCH] Fixes #67 --- integration-tests/LDDFeatureRequesterTest.php | 52 ++++++++++++++++--- src/LaunchDarkly/LDDFeatureRequester.php | 18 ++++++- 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/integration-tests/LDDFeatureRequesterTest.php b/integration-tests/LDDFeatureRequesterTest.php index 3f96cd831..6d41355ed 100644 --- a/integration-tests/LDDFeatureRequesterTest.php +++ b/integration-tests/LDDFeatureRequesterTest.php @@ -2,6 +2,7 @@ namespace LaunchDarkly\Tests; use LaunchDarkly\LDClient; +use LaunchDarkly\LDUser; use LaunchDarkly\LDUserBuilder; use LaunchDarkly\LDDFeatureRequester; use LaunchDarkly\ApcLDDFeatureRequester; @@ -9,13 +10,14 @@ use LaunchDarkly\ApcuLDDFeatureRequester; class LDDFeatureRetrieverTest extends \PHPUnit_Framework_TestCase { + const API_KEY = 'BOGUS_API_KEY'; public function testGet() { $redis = new Client(array( "scheme" => "tcp", "host" => 'localhost', "port" => 6379)); - $client = new LDClient("BOGUS_API_KEY", array('feature_requester_class' => LDDFeatureRequester::class)); + $client = new LDClient(static::API_KEY, array('feature_requester_class' => LDDFeatureRequester::class)); $builder = new LDUserBuilder(3); $user = $builder->build(); @@ -33,7 +35,7 @@ public function testGetApc() { "scheme" => "tcp", "host" => 'localhost', "port" => 6379)); - $client = new LDClient("BOGUS_API_KEY", array('feature_requester_class' => ApcLDDFeatureRequester::class, + $client = new LDClient(static::API_KEY, array('feature_requester_class' => ApcLDDFeatureRequester::class, 'apc_expiration' => 1)); $builder = new LDUserBuilder(3); $user = $builder->build(); @@ -55,18 +57,18 @@ public function testGetApcu() { if (!extension_loaded('apcu')) { self::markTestSkipped('Install `apcu` extension to run this test.'); } - + $redis = new Client([ 'scheme' => 'tcp', 'host' => 'localhost', 'port' => 6379 ]); - - $client = new LDClient('BOGUS_API_KEY', [ + + $client = new LDClient(static::API_KEY, [ 'feature_requester_class' => ApcuLDDFeatureRequester::class, 'apc_expiration' => 1 ]); - + $builder = new LDUserBuilder(3); $user = $builder->build(); @@ -83,6 +85,42 @@ public function testGetApcu() { $this->assertEquals('bob', $client->variation('fiz', $user, 'alice')); } + public function testGetAllWithoutFeatures() + { + $redis = new \Predis\Client([ + 'scheme' => 'tcp', + 'host' => 'localhost', + 'port' => 6379, + ]); + $redis->flushall(); + + $client = new LDClient(static::API_KEY, ['feature_requester_class' => LDDFeatureRequester::class]); + $user = new LDUser(static::API_KEY); + $allFlags = $client->allFlags($user); + + $this->assertNull($allFlags); + } + + public function testGetAll() + { + $featureKey = 'foo'; + $featureValue = 'bar'; + + $redis = new \Predis\Client([ + 'scheme' => 'tcp', + 'host' => 'localhost', + 'port' => 6379, + ]); + $client = new LDClient(static::API_KEY, ['feature_requester_class' => LDDFeatureRequester::class]); + $redis->hset('launchdarkly:features', $featureKey, $this->gen_feature($featureKey, $featureValue)); + $user = new LDUser(static::API_KEY); + $allFlags = $client->allFlags($user); + + $this->assertInternalType('array', $allFlags); + $this->assertArrayHasKey($featureKey, $allFlags); + $this->assertEquals($featureValue, $allFlags[$featureKey]); + } + private function gen_feature($key, $val) { $data = [ 'name' => 'Feature ' . $key, @@ -130,7 +168,7 @@ private function gen_feature($key, $val) { 'offVariation' => null, 'deleted' => false, ]; - + return \json_encode($data); } diff --git a/src/LaunchDarkly/LDDFeatureRequester.php b/src/LaunchDarkly/LDDFeatureRequester.php index f163ee90f..a043dfa6c 100644 --- a/src/LaunchDarkly/LDDFeatureRequester.php +++ b/src/LaunchDarkly/LDDFeatureRequester.php @@ -96,7 +96,7 @@ public function getAll() { $redis = $this->get_connection(); $raw = $redis->hgetall($this->_features_key); if ($raw) { - $allFlags = array_map(FeatureFlag::getDecoder(), json_decode($raw, true)); + $allFlags = array_map(FeatureFlag::getDecoder(), $this->decodeFeatures($raw)); /** * @param $flag FeatureFlag * @return bool @@ -110,4 +110,18 @@ public function getAll() { return null; } } -} \ No newline at end of file + + /** + * @param array $features + * + * @return array + */ + private function decodeFeatures(array $features) + { + foreach ($features as $featureKey => $feature) { + $features[$featureKey] = json_decode($feature, true); + } + + return $features; + } +}