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