|
| 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