22namespace LaunchDarkly \Tests ;
33
44use InvalidArgumentException ;
5+ use LaunchDarkly \FeatureFlag ;
56use LaunchDarkly \FeatureRequester ;
67use LaunchDarkly \LDClient ;
78use LaunchDarkly \LDUser ;
@@ -15,9 +16,38 @@ public function testDefaultCtor()
1516 $ this ->assertInstanceOf (LDClient::class, new LDClient ("BOGUS_SDK_KEY " ));
1617 }
1718
18- public function testToggleDefault ()
19+ public function testVariationReturnsFlagValue ()
1920 {
20- MockFeatureRequester::$ val = null ;
21+ $ flagJson = array (
22+ 'key ' => 'feature ' ,
23+ 'version ' => 100 ,
24+ 'deleted ' => false ,
25+ 'on ' => false ,
26+ 'targets ' => array (),
27+ 'prerequisites ' => array (),
28+ 'rules ' => array (),
29+ 'offVariation ' => 1 ,
30+ 'fallthrough ' => array ('variation ' => 0 ),
31+ 'variations ' => array ('fall ' , 'off ' , 'on ' ),
32+ 'salt ' => ''
33+ );
34+ $ flag = FeatureFlag::decode ($ flagJson );
35+
36+ MockFeatureRequester::$ flags = array ('feature ' => $ flag );
37+ $ client = new LDClient ("someKey " , array (
38+ 'feature_requester_class ' => MockFeatureRequester::class,
39+ 'events ' => false
40+ ));
41+
42+ $ builder = new LDUserBuilder (3 );
43+ $ user = $ builder ->build ();
44+ $ value = $ client ->variation ('feature ' , $ user , 'default ' );
45+ $ this ->assertEquals ('off ' , $ value );
46+ }
47+
48+ public function testVariationReturnsDefaultForUnknownFlag ()
49+ {
50+ MockFeatureRequester::$ flags = array ();
2151 $ client = new LDClient ("someKey " , array (
2252 'feature_requester_class ' => MockFeatureRequester::class,
2353 'events ' => false
@@ -28,9 +58,9 @@ public function testToggleDefault()
2858 $ this ->assertEquals ('argdef ' , $ client ->variation ('foo ' , $ user , 'argdef ' ));
2959 }
3060
31- public function testToggleFromArray ()
61+ public function testVariationReturnsDefaultFromConfigurationForUnknownFlag ()
3262 {
33- MockFeatureRequester::$ val = null ;
63+ MockFeatureRequester::$ flags = array () ;
3464 $ client = new LDClient ("someKey " , array (
3565 'feature_requester_class ' => MockFeatureRequester::class,
3666 'events ' => false ,
@@ -42,9 +72,9 @@ public function testToggleFromArray()
4272 $ this ->assertEquals ('fromarray ' , $ client ->variation ('foo ' , $ user , 'argdef ' ));
4373 }
4474
45- public function testToggleEvent ()
75+ public function testVariationSendsEvent ()
4676 {
47- MockFeatureRequester::$ val = null ;
77+ MockFeatureRequester::$ flags = array () ;
4878 $ client = new LDClient ("someKey " , array (
4979 'feature_requester_class ' => MockFeatureRequester::class,
5080 'events ' => true
@@ -58,6 +88,81 @@ public function testToggleEvent()
5888 $ this ->assertEquals (1 , sizeof ($ queue ));
5989 }
6090
91+ public function testAllFlagsReturnsFlagValues ()
92+ {
93+ $ flagJson = array (
94+ 'key ' => 'feature ' ,
95+ 'version ' => 100 ,
96+ 'deleted ' => false ,
97+ 'on ' => false ,
98+ 'targets ' => array (),
99+ 'prerequisites ' => array (),
100+ 'rules ' => array (),
101+ 'offVariation ' => 1 ,
102+ 'fallthrough ' => array ('variation ' => 0 ),
103+ 'variations ' => array ('fall ' , 'off ' , 'on ' ),
104+ 'salt ' => ''
105+ );
106+ $ flag = FeatureFlag::decode ($ flagJson );
107+
108+ MockFeatureRequester::$ flags = array ('feature ' => $ flag );
109+ $ client = new LDClient ("someKey " , array (
110+ 'feature_requester_class ' => MockFeatureRequester::class,
111+ 'events ' => false
112+ ));
113+
114+ $ builder = new LDUserBuilder (3 );
115+ $ user = $ builder ->build ();
116+ $ values = $ client ->allFlags ($ user );
117+
118+ $ this ->assertEquals (array ('feature ' => 'off ' ), $ values );
119+ }
120+
121+ public function testAllFlagsStateReturnsState ()
122+ {
123+ $ flagJson = array (
124+ 'key ' => 'feature ' ,
125+ 'version ' => 100 ,
126+ 'deleted ' => false ,
127+ 'on ' => false ,
128+ 'targets ' => array (),
129+ 'prerequisites ' => array (),
130+ 'rules ' => array (),
131+ 'offVariation ' => 1 ,
132+ 'fallthrough ' => array ('variation ' => 0 ),
133+ 'variations ' => array ('fall ' , 'off ' , 'on ' ),
134+ 'salt ' => '' ,
135+ 'trackEvents ' => true ,
136+ 'debugEventsUntilDate ' => 1000
137+ );
138+ $ flag = FeatureFlag::decode ($ flagJson );
139+
140+ MockFeatureRequester::$ flags = array ('feature ' => $ flag );
141+ $ client = new LDClient ("someKey " , array (
142+ 'feature_requester_class ' => MockFeatureRequester::class,
143+ 'events ' => false
144+ ));
145+
146+ $ builder = new LDUserBuilder (3 );
147+ $ user = $ builder ->build ();
148+ $ state = $ client ->allFlagsState ($ user );
149+
150+ $ this ->assertTrue ($ state ->isValid ());
151+ $ this ->assertEquals (array ('feature ' => 'off ' ), $ state ->toValuesMap ());
152+ $ expectedState = array (
153+ 'feature ' => 'off ' ,
154+ '$flagsState ' => array (
155+ 'feature ' => array (
156+ 'variation ' => 1 ,
157+ 'version ' => 100 ,
158+ 'trackEvents ' => true ,
159+ 'debugEventsUntilDate ' => 1000
160+ )
161+ )
162+ );
163+ $ this ->assertEquals ($ expectedState , $ state ->toJson ());
164+ }
165+
61166 public function testOnlyValidFeatureRequester ()
62167 {
63168 $ this ->setExpectedException (InvalidArgumentException::class);
0 commit comments