Skip to content

Commit 69a1882

Browse files
authored
add unit test for passing a preconfigured client + improve prefix logic (#5)
1 parent 8c11cac commit 69a1882

File tree

3 files changed

+82
-7
lines changed

3 files changed

+82
-7
lines changed

src/LaunchDarkly/Impl/Integrations/PHPRedisFeatureRequester.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct($baseUri, $sdkKey, $options)
2222
}
2323

2424
$client = $this->_options['phpredis_client'] ?? null;
25-
if ($client instanceof Redis) {
25+
if ($client instanceof \Redis) {
2626
$this->_redisInstance = $client;
2727
} else {
2828
$this->_redisOptions = [
@@ -36,13 +36,13 @@ public function __construct($baseUri, $sdkKey, $options)
3636
protected function readItemString(string $namespace, string $key): ?string
3737
{
3838
$redis = $this->getConnection();
39-
return $redis->hget($namespace, $key);
39+
return $redis->hget("$this->_prefix:$namespace", $key);
4040
}
4141

4242
protected function readItemStringList(string $namespace): ?array
4343
{
4444
$redis = $this->getConnection();
45-
$raw = $redis->hgetall($namespace);
45+
$raw = $redis->hgetall("$this->_prefix:$namespace");
4646
return $raw ? array_values($raw) : null;
4747
}
4848

@@ -51,7 +51,7 @@ protected function readItemStringList(string $namespace): ?array
5151
*/
5252
protected function getConnection()
5353
{
54-
if ($this->_redisInstance instanceof Redis) {
54+
if ($this->_redisInstance instanceof \Redis) {
5555
return $this->_redisInstance;
5656
}
5757

@@ -62,7 +62,6 @@ protected function getConnection()
6262
$this->_redisOptions["timeout"],
6363
'launchdarkly/php-server-sdk-redis-phpredis'
6464
);
65-
$redis->setOption(\Redis::OPT_PREFIX, "$this->_prefix:"); // use custom prefix on all keys
6665
return $this->_redisInstance = $redis;
6766
}
6867
}

tests/PHPRedisFeatureRequesterTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ protected function clearExistingData($prefix): void
3535

3636
protected function makeRequester($prefix): FeatureRequester
3737
{
38-
$factory = PHPRedis::featureRequester();
39-
return $factory('', '', ['redis_prefix' => $prefix]);
38+
$factory = PHPRedis::featureRequester([
39+
'redis_prefix' => $prefix
40+
]);
41+
return $factory('', '', []);
4042
}
4143

4244
protected function putSerializedItem($prefix, $namespace, $key, $version, $json): void
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace LaunchDarkly\Impl\Integrations\Tests;
4+
5+
use LaunchDarkly\FeatureRequester;
6+
use LaunchDarkly\Impl\Integrations\PHPRedisFeatureRequester;
7+
use LaunchDarkly\Integrations\PHPRedis;
8+
use LaunchDarkly\SharedTest\DatabaseFeatureRequesterTestBase;
9+
use \Redis;
10+
11+
class PHPRedisFeatureRequesterWithClientTest extends DatabaseFeatureRequesterTestBase
12+
{
13+
const CLIENT_PREFIX = 'clientprefix';
14+
15+
/** @var ClientInterface */
16+
private static $redisClient;
17+
18+
public static function setUpBeforeClass(): void
19+
{
20+
self::$redisClient = new \Redis();
21+
self::$redisClient->pconnect(
22+
'localhost',
23+
6379,
24+
null,
25+
'RedisFeatureRequesterWithClientTest'
26+
);
27+
self::$redisClient->setOption(\Redis::OPT_PREFIX, self::CLIENT_PREFIX);
28+
29+
// Setting a prefix parameter on the Redis client causes it to prepend
30+
// that string to every key *in addition to* the other prefix that the SDK
31+
// integration is applying. This is done transparently so we do not need to
32+
// add CLIENT_PREFIX in putItem below. We're doing it so we can be sure
33+
// that the PHPRedisFeatureRequester really is using the same client we
34+
// passed to it; if it didn't, the tests would fail because putItem was
35+
// creating keys with both prefixes but PHPRedisFeatureRequester was
36+
// looking for keys with only one prefix.
37+
}
38+
39+
protected function clearExistingData($prefix): void
40+
{
41+
$p = self::realPrefix($prefix);
42+
$keys = self::$redisClient->keys("$p:*");
43+
foreach ($keys as $key) {
44+
if (substr($key, 0, strlen(self::CLIENT_PREFIX)) === self::CLIENT_PREFIX) {
45+
// remove extra prefix from the queried keys because del() will re-add it
46+
$key = substr($key, strlen(self::CLIENT_PREFIX));
47+
}
48+
self::$redisClient->del($key);
49+
}
50+
}
51+
52+
protected function makeRequester($prefix): FeatureRequester
53+
{
54+
$factory = PHPRedis::featureRequester([
55+
'redis_prefix' => $prefix,
56+
'phpredis_client' => self::$redisClient
57+
]);
58+
return $factory('', '', []);
59+
}
60+
61+
protected function putSerializedItem($prefix, $namespace, $key, $version, $json): void
62+
{
63+
$p = self::realPrefix($prefix);
64+
self::$redisClient->hset("$p:$namespace", $key, $json);
65+
}
66+
67+
private static function realPrefix($prefix)
68+
{
69+
if ($prefix === null || $prefix === '') {
70+
return 'launchdarkly';
71+
}
72+
return $prefix;
73+
}
74+
}

0 commit comments

Comments
 (0)