Skip to content

Fixes #67 #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 45 additions & 7 deletions integration-tests/LDDFeatureRequesterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@
namespace LaunchDarkly\Tests;

use LaunchDarkly\LDClient;
use LaunchDarkly\LDUser;
use LaunchDarkly\LDUserBuilder;
use LaunchDarkly\LDDFeatureRequester;
use LaunchDarkly\ApcLDDFeatureRequester;
use Predis\Client;
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();

Expand All @@ -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();
Expand All @@ -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();

Expand All @@ -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,
Expand Down Expand Up @@ -130,7 +168,7 @@ private function gen_feature($key, $val) {
'offVariation' => null,
'deleted' => false,
];

return \json_encode($data);
}

Expand Down
18 changes: 16 additions & 2 deletions src/LaunchDarkly/LDDFeatureRequester.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -110,4 +110,18 @@ public function getAll() {
return null;
}
}
}

/**
* @param array $features
*
* @return array
*/
private function decodeFeatures(array $features)
{
foreach ($features as $featureKey => $feature) {
$features[$featureKey] = json_decode($feature, true);
}

return $features;
}
}