|
| 1 | +<?php |
| 2 | +namespace LaunchDarkly\Tests; |
| 3 | + |
| 4 | +require_once 'vendor/autoload.php'; |
| 5 | + |
| 6 | +use LaunchDarkly\LDClient; |
| 7 | +use LaunchDarkly\LDUserBuilder; |
| 8 | + |
| 9 | +class LDDFeatureRetrieverTest extends \PHPUnit_Framework_TestCase { |
| 10 | + |
| 11 | + public function testGet() { |
| 12 | + $redis = new \Predis\Client(array( |
| 13 | + "scheme" => "tcp", |
| 14 | + "host" => 'localhost', |
| 15 | + "port" => 6379)); |
| 16 | + $client = new LDClient("BOGUS_API_KEY", array('feature_requester_class' => '\\LaunchDarkly\\LDDFeatureRequester')); |
| 17 | + $builder = new LDUserBuilder(3); |
| 18 | + $user = $builder->build(); |
| 19 | + |
| 20 | + $redis->del("launchdarkly:features"); |
| 21 | + $this->assertEquals("jim", $client->toggle('foo', $user, 'jim')); |
| 22 | + $redis->hset("launchdarkly:features", 'foo', $this->gen_feature("foo", "bar")); |
| 23 | + $this->assertEquals("bar", $client->toggle('foo', $user, 'jim')); |
| 24 | + } |
| 25 | + |
| 26 | + public function testGetApc() { |
| 27 | + $redis = new \Predis\Client(array( |
| 28 | + "scheme" => "tcp", |
| 29 | + "host" => 'localhost', |
| 30 | + "port" => 6379)); |
| 31 | + $client = new LDClient("BOGUS_API_KEY", array('feature_requester_class' => '\\LaunchDarkly\\ApcLDDFeatureRequester', |
| 32 | + 'apc_expiration' => 1)); |
| 33 | + $builder = new LDUserBuilder(3); |
| 34 | + $user = $builder->build(); |
| 35 | + |
| 36 | + $redis->del("launchdarkly:features"); |
| 37 | + $this->assertEquals("jim", $client->toggle('foo', $user, 'jim')); |
| 38 | + $redis->hset("launchdarkly:features", 'foo', $this->gen_feature("foo", "bar")); |
| 39 | + $this->assertEquals("bar", $client->toggle('foo', $user, 'jim')); |
| 40 | + |
| 41 | + # cached value so not updated |
| 42 | + $redis->hset("launchdarkly:features", 'foo', $this->gen_feature("foo", "baz")); |
| 43 | + $this->assertEquals("bar", $client->toggle('foo', $user, 'jim')); |
| 44 | + |
| 45 | + apc_delete("launchdarkly:features.foo"); |
| 46 | + $this->assertEquals("baz", $client->toggle('foo', $user, 'jim')); |
| 47 | + } |
| 48 | + |
| 49 | + private function gen_feature($key, $val) { |
| 50 | + $data = <<<EOF |
| 51 | + {"name": "Feature $key", "key": "$key", "kind": "flag", "salt": "Zm9v", "on": true, |
| 52 | + "variations": [{"value": "$val", "weight": 100, |
| 53 | + "targets": [{"attribute": "key", "op": "in", "values": []}], |
| 54 | + "userTarget": {"attribute": "key", "op": "in", "values": []}}, |
| 55 | + {"value": false, "weight": 0, |
| 56 | + "targets": [{"attribute": "key", "op": "in", "values": []}], |
| 57 | + "userTarget": {"attribute": "key", "op": "in", "values": []}}], |
| 58 | + "commitDate": "2015-09-08T21:24:16.712Z", |
| 59 | + "creationDate": "2015-09-08T21:06:16.527Z", |
| 60 | + "version": 4} |
| 61 | +EOF; |
| 62 | + return $data; |
| 63 | + } |
| 64 | + |
| 65 | +} |
| 66 | + |
0 commit comments