@@ -31,12 +31,14 @@ class OptimizelyTest extends \PHPUnit_Framework_TestCase
3131{
3232 private $ datafile ;
3333 private $ eventBuilderMock ;
34+ private $ optimizelyObject ;
3435 private $ projectConfig ;
3536
3637 public function setUp ()
3738 {
3839 $ this ->datafile = DATAFILE ;
3940 $ this ->projectConfig = new ProjectConfig ($ this ->datafile );
41+ $ this ->optimizelyObject = new Optimizely ($ this ->datafile );
4042
4143 // Mock EventBuilder
4244 $ this ->eventBuilderMock = $ this ->getMockBuilder (EventBuilder::class)
@@ -143,6 +145,92 @@ public function testValidateInputsInvalidFileJsonValidationSkipped()
143145 );
144146 }
145147
148+ public function testValidatePreconditionsExperimentNotRunning ()
149+ {
150+ $ validatePreconditions = new \ReflectionMethod ('Optimizely\Optimizely ' , 'validatePreconditions ' );
151+ $ validatePreconditions ->setAccessible (true );
152+
153+ $ this ->assertFalse (
154+ $ validatePreconditions ->invoke (
155+ $ this ->optimizelyObject ,
156+ $ this ->projectConfig ->getExperimentFromKey ('paused_experiment ' ),
157+ 'test_user ' ,
158+ [])
159+ );
160+ }
161+
162+ public function testValidatePreconditionsExperimentRunning ()
163+ {
164+ $ validatePreconditions = new \ReflectionMethod ('Optimizely\Optimizely ' , 'validatePreconditions ' );
165+ $ validatePreconditions ->setAccessible (true );
166+
167+ $ this ->assertTrue (
168+ $ validatePreconditions ->invoke (
169+ $ this ->optimizelyObject ,
170+ $ this ->projectConfig ->getExperimentFromKey ('test_experiment ' ),
171+ 'test_user ' ,
172+ [])
173+ );
174+ }
175+
176+ public function testValidatePreconditionsUserInForcedVariationNotInExperiment ()
177+ {
178+ $ validatePreconditions = new \ReflectionMethod ('Optimizely\Optimizely ' , 'validatePreconditions ' );
179+ $ validatePreconditions ->setAccessible (true );
180+
181+ $ this ->assertTrue (
182+ $ validatePreconditions ->invoke (
183+ $ this ->optimizelyObject ,
184+ $ this ->projectConfig ->getExperimentFromKey ('test_experiment ' ),
185+ 'user_1 ' ,
186+ [])
187+ );
188+ }
189+
190+ public function testValidatePreconditionsUserInForcedVariationInExperiment ()
191+ {
192+ $ validatePreconditions = new \ReflectionMethod ('Optimizely\Optimizely ' , 'validatePreconditions ' );
193+ $ validatePreconditions ->setAccessible (true );
194+
195+ $ this ->assertTrue (
196+ $ validatePreconditions ->invoke (
197+ $ this ->optimizelyObject ,
198+ $ this ->projectConfig ->getExperimentFromKey ('test_experiment ' ),
199+ 'user1 ' ,
200+ [])
201+ );
202+ }
203+
204+ public function testValidatePreconditionsUserNotInForcedVariationNotInExperiment ()
205+ {
206+ $ validatePreconditions = new \ReflectionMethod ('Optimizely\Optimizely ' , 'validatePreconditions ' );
207+ $ validatePreconditions ->setAccessible (true );
208+
209+ // Will get updated when we have audience evaluation and user does not meet conditions
210+ $ this ->assertTrue (
211+ $ validatePreconditions ->invoke (
212+ $ this ->optimizelyObject ,
213+ $ this ->projectConfig ->getExperimentFromKey ('test_experiment ' ),
214+ 'test_user ' ,
215+ [])
216+ );
217+ }
218+
219+ public function testValidatePreconditionsUserNotInForcedVariationInExperiment ()
220+ {
221+ $ validatePreconditions = new \ReflectionMethod ('Optimizely\Optimizely ' , 'validatePreconditions ' );
222+ $ validatePreconditions ->setAccessible (true );
223+
224+ // Will get updated when we have audience evaluation and user does meets conditions
225+ $ this ->assertTrue (
226+ $ validatePreconditions ->invoke (
227+ $ this ->optimizelyObject ,
228+ $ this ->projectConfig ->getExperimentFromKey ('test_experiment ' ),
229+ 'test_user ' ,
230+ [])
231+ );
232+ }
233+
146234 public function testActivateNoAttributes ()
147235 {
148236 $ this ->eventBuilderMock ->expects ($ this ->once ())
@@ -190,6 +278,32 @@ public function testActivateWithAttributes()
190278 $ this ->assertEquals ('control ' , $ optlyObject ->activate ('test_experiment ' , 'test_user ' , $ userAttributes ));
191279 }
192280
281+ public function testActivateExperimentNotRunning ()
282+ {
283+ $ this ->eventBuilderMock ->expects ($ this ->never ())
284+ ->method ('createImpressionEvent ' );
285+
286+ $ optlyObject = new Optimizely ($ this ->datafile , new ValidEventDispatcher ());
287+
288+ $ eventBuilder = new \ReflectionProperty (Optimizely::class, '_eventBuilder ' );
289+ $ eventBuilder ->setAccessible (true );
290+ $ eventBuilder ->setValue ($ optlyObject , $ this ->eventBuilderMock );
291+
292+ // Call activate
293+ $ this ->assertNull ($ optlyObject ->activate ('paused_experiment ' , 'test_user ' , null ));
294+ }
295+
296+
297+ public function testGetVariation ()
298+ {
299+ $ this ->assertEquals ('control ' , $ this ->optimizelyObject ->getVariation ('test_experiment ' , 'test_user ' ));
300+ }
301+
302+ public function testGetVariationExperimentNotRunning ()
303+ {
304+ $ this ->assertNull ($ this ->optimizelyObject ->getVariation ('paused_experiment ' , 'test_user ' ));
305+ }
306+
193307 public function testTrackNoAttributesNoEventValue ()
194308 {
195309 $ this ->eventBuilderMock ->expects ($ this ->once ())
0 commit comments