55
66use LaunchDarkly \LDClient ;
77use LaunchDarkly \LDUserBuilder ;
8+ use Predis \Client ;
89
910class LDDFeatureRetrieverTest extends \PHPUnit_Framework_TestCase {
1011
@@ -24,6 +25,9 @@ public function testGet() {
2425 }
2526
2627 public function testGetApc () {
28+ if (!extension_loaded ('apc ' )) {
29+ self ::markTestSkipped ('Install `apc` extension to run this test. ' );
30+ }
2731 $ redis = new \Predis \Client (array (
2832 "scheme " => "tcp " ,
2933 "host " => 'localhost ' ,
@@ -46,20 +50,87 @@ public function testGetApc() {
4650 $ this ->assertEquals ("baz " , $ client ->variation ('foo ' , $ user , 'jim ' ));
4751 }
4852
53+ public function testGetApcu () {
54+ if (!extension_loaded ('apcu ' )) {
55+ self ::markTestSkipped ('Install `apcu` extension to run this test. ' );
56+ }
57+
58+ $ redis = new Client ([
59+ 'scheme ' => 'tcp ' ,
60+ 'host ' => 'localhost ' ,
61+ 'port ' => 6379
62+ ]);
63+
64+ $ client = new LDClient ('BOGUS_API_KEY ' , [
65+ 'feature_requester_class ' => '\LaunchDarkly\ApcuLDDFeatureRequester ' ,
66+ 'apc_expiration ' => 1
67+ ]);
68+
69+ $ builder = new LDUserBuilder (3 );
70+ $ user = $ builder ->build ();
71+
72+ $ redis ->del ('launchdarkly:features ' );
73+ $ this ->assertEquals ('alice ' , $ client ->variation ('fiz ' , $ user , 'alice ' ));
74+ $ redis ->hset ('launchdarkly:features ' , 'fiz ' , $ this ->gen_feature ('fiz ' , 'buz ' ));
75+ $ this ->assertEquals ('buz ' , $ client ->variation ('fiz ' , $ user , 'alice ' ));
76+
77+ # cached value so not updated
78+ $ redis ->hset ('launchdarkly:features ' , 'fiz ' , $ this ->gen_feature ('fiz ' , 'bob ' ));
79+ $ this ->assertEquals ('buz ' , $ client ->variation ('fiz ' , $ user , 'alice ' ));
80+
81+ \apcu_delete ('launchdarkly:features.fiz ' );
82+ $ this ->assertEquals ('bob ' , $ client ->variation ('fiz ' , $ user , 'alice ' ));
83+ }
84+
4985 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 ;
86+ $ data = [
87+ 'name ' => 'Feature ' . $ key ,
88+ 'key ' => $ key ,
89+ 'kind ' => 'flag ' ,
90+ 'salt ' => 'Zm9v ' ,
91+ 'on ' => true ,
92+ 'variations ' => [
93+ $ val ,
94+ false ,
95+ ],
96+ 'commitDate ' => '2015-09-08T21:24:16.712Z ' ,
97+ 'creationDate ' => '2015-09-08T21:06:16.527Z ' ,
98+ 'version ' => 4 ,
99+ 'prerequisites ' => [],
100+ 'targets ' => [
101+ [
102+ 'values ' => [
103+ $ val ,
104+ ],
105+ 'variation ' => 0 ,
106+ ],
107+ [
108+ 'values ' => [
109+ false ,
110+ ],
111+ 'variation ' => 1 ,
112+ ],
113+ ],
114+ 'rules ' => [],
115+ 'fallthrough ' => [
116+ 'rollout ' => [
117+ 'variations ' => [
118+ [
119+ 'variation ' => 0 ,
120+ 'weight ' => 95000 ,
121+ ],
122+ [
123+ 'variation ' => 1 ,
124+ 'weight ' => 5000 ,
125+ ],
126+ ],
127+ ],
128+ ],
129+ 'offVariation ' => null ,
130+ 'deleted ' => false ,
131+ ];
132+
133+ return \json_encode ($ data );
63134 }
64135
65136}
0 commit comments