2424use Optimizely \Entity \Variation ;
2525use Optimizely \ErrorHandler \NoOpErrorHandler ;
2626use Optimizely \Logger \NoOpLogger ;
27+ use Optimizely \Optimizely ;
2728use Optimizely \ProjectConfig ;
2829use Optimizely \UserProfile \UserProfileServiceInterface ;
2930
@@ -107,9 +108,13 @@ public function testGetVariationReturnsWhitelistedVariation()
107108 $ expectedVariation = new Variation ('7722370027 ' , 'control ' );
108109 $ runningExperiment = $ this ->config ->getExperimentFromKey ('test_experiment ' );
109110
111+ $ callIndex = 0 ;
110112 $ this ->bucketerMock ->expects ($ this ->never ())
111113 ->method ('bucket ' );
112- $ this ->loggerMock ->expects ($ this ->at (0 ))
114+ $ this ->loggerMock ->expects ($ this ->at ($ callIndex ++))
115+ ->method ('log ' )
116+ ->with (Logger::DEBUG , 'User "user1" is not in the forced variation map. ' );
117+ $ this ->loggerMock ->expects ($ this ->at ($ callIndex ++))
113118 ->method ('log ' )
114119 ->with (Logger::INFO , 'User "user1" is forced in variation "control" of experiment "test_experiment". ' );
115120
@@ -131,9 +136,13 @@ public function testGetVariationReturnsWhitelistedVariationForGroupedExperiment(
131136 $ expectedVariation = new Variation ('7722260071 ' , 'group_exp_1_var_1 ' );
132137 $ runningExperiment = $ this ->config ->getExperimentFromKey ('group_experiment_1 ' );
133138
139+ $ callIndex = 0 ;
134140 $ this ->bucketerMock ->expects ($ this ->never ())
135141 ->method ('bucket ' );
136- $ this ->loggerMock ->expects ($ this ->at (0 ))
142+ $ this ->loggerMock ->expects ($ this ->at ($ callIndex ++))
143+ ->method ('log ' )
144+ ->with (Logger::DEBUG , 'User "user1" is not in the forced variation map. ' );
145+ $ this ->loggerMock ->expects ($ this ->at ($ callIndex ++))
137146 ->method ('log ' )
138147 ->with (Logger::INFO , 'User "user1" is forced in variation "group_exp_1_var_1" of experiment "group_experiment_1". ' );
139148
@@ -251,10 +260,13 @@ public function testGetVariationReturnsStoredVariationIfAvailable()
251260 $ runningExperiment = $ this ->config ->getExperimentFromKey ('test_experiment ' );
252261 $ expectedVariation = new Variation ('7722370027 ' , 'control ' );
253262
263+ $ callIndex = 0 ;
254264 $ this ->bucketerMock ->expects ($ this ->never ())
255265 ->method ('bucket ' );
256-
257- $ this ->loggerMock ->expects ($ this ->at (0 ))
266+ $ this ->loggerMock ->expects ($ this ->at ($ callIndex ++))
267+ ->method ('log ' )
268+ ->with (Logger::DEBUG , 'User "not_whitelisted_user" is not in the forced variation map. ' );
269+ $ this ->loggerMock ->expects ($ this ->at ($ callIndex ++))
258270 ->method ('log ' )
259271 ->with (Logger::INFO , 'Returning previously activated variation "control" of experiment "test_experiment" for user "not_whitelisted_user" from user profile. ' );
260272
@@ -285,14 +297,17 @@ public function testGetVariationBucketsIfNoStoredVariation()
285297 $ runningExperiment = $ this ->config ->getExperimentFromKey ('test_experiment ' );
286298 $ expectedVariation = new Variation ('7722370027 ' , 'control ' );
287299
300+ $ callIndex = 0 ;
288301 $ this ->bucketerMock ->expects ($ this ->once ())
289302 ->method ('bucket ' )
290303 ->willReturn ($ expectedVariation );
291-
292- $ this ->loggerMock ->expects ($ this ->at (0 ))
304+ $ this ->loggerMock ->expects ($ this ->at ($ callIndex ++))
305+ ->method ('log ' )
306+ ->with (Logger::DEBUG , sprintf ('User "%s" is not in the forced variation map. ' , $ userId ));
307+ $ this ->loggerMock ->expects ($ this ->at ($ callIndex ++))
293308 ->method ('log ' )
294309 ->with (Logger::INFO , 'No previously activated variation of experiment "test_experiment" for user "testUserId" found in user profile. ' );
295- $ this ->loggerMock ->expects ($ this ->at (1 ))
310+ $ this ->loggerMock ->expects ($ this ->at ($ callIndex ++ ))
296311 ->method ('log ' )
297312 ->with (Logger::INFO , 'Saved variation "control" of experiment "test_experiment" for user "testUserId". ' );
298313
@@ -330,14 +345,17 @@ public function testGetVariationBucketsIfStoredVariationIsInvalid()
330345 $ runningExperiment = $ this ->config ->getExperimentFromKey ('test_experiment ' );
331346 $ expectedVariation = new Variation ('7722370027 ' , 'control ' );
332347
348+ $ callIndex = 0 ;
333349 $ this ->bucketerMock ->expects ($ this ->once ())
334350 ->method ('bucket ' )
335351 ->willReturn ($ expectedVariation );
336-
337- $ this ->loggerMock ->expects ($ this ->at (0 ))
352+ $ this ->loggerMock ->expects ($ this ->at ($ callIndex ++))
353+ ->method ('log ' )
354+ ->with (Logger::DEBUG , sprintf ('User "%s" is not in the forced variation map. ' , $ userId ));
355+ $ this ->loggerMock ->expects ($ this ->at ($ callIndex ++))
338356 ->method ('log ' )
339357 ->with (Logger::INFO , 'User "testUserId" was previously bucketed into variation with ID "invalid" for experiment "test_experiment", but no matching variation was found for that user. We will re-bucket the user. ' );
340- $ this ->loggerMock ->expects ($ this ->at (1 ))
358+ $ this ->loggerMock ->expects ($ this ->at ($ callIndex ++ ))
341359 ->method ('log ' )
342360 ->with (Logger::INFO , 'Saved variation "control" of experiment "test_experiment" for user "testUserId". ' );
343361
@@ -379,14 +397,17 @@ public function testGetVariationBucketsIfUserProfileServiceLookupThrows()
379397 $ runningExperiment = $ this ->config ->getExperimentFromKey ('test_experiment ' );
380398 $ expectedVariation = new Variation ('7722370027 ' , 'control ' );
381399
400+ $ callIndex = 0 ;
382401 $ this ->bucketerMock ->expects ($ this ->once ())
383402 ->method ('bucket ' )
384403 ->willReturn ($ expectedVariation );
385-
386- $ this ->loggerMock ->expects ($ this ->at (0 ))
404+ $ this ->loggerMock ->expects ($ this ->at ($ callIndex ++))
405+ ->method ('log ' )
406+ ->with (Logger::DEBUG , sprintf ('User "%s" is not in the forced variation map. ' , $ userId ));
407+ $ this ->loggerMock ->expects ($ this ->at ($ callIndex ++))
387408 ->method ('log ' )
388409 ->with (Logger::ERROR , 'The User Profile Service lookup method failed: I am error. ' );
389- $ this ->loggerMock ->expects ($ this ->at (1 ))
410+ $ this ->loggerMock ->expects ($ this ->at ($ callIndex ++ ))
390411 ->method ('log ' )
391412 ->with (Logger::INFO , 'Saved variation "control" of experiment "test_experiment" for user "testUserId". ' );
392413
@@ -428,14 +449,17 @@ public function testGetVariationBucketsIfUserProfileServiceSaveThrows()
428449 $ runningExperiment = $ this ->config ->getExperimentFromKey ('test_experiment ' );
429450 $ expectedVariation = new Variation ('7722370027 ' , 'control ' );
430451
452+ $ callIndex = 0 ;
431453 $ this ->bucketerMock ->expects ($ this ->once ())
432454 ->method ('bucket ' )
433455 ->willReturn ($ expectedVariation );
434-
435- $ this ->loggerMock ->expects ($ this ->at (0 ))
456+ $ this ->loggerMock ->expects ($ this ->at ($ callIndex ++))
457+ ->method ('log ' )
458+ ->with (Logger::DEBUG , sprintf ('User "%s" is not in the forced variation map. ' , $ userId ));
459+ $ this ->loggerMock ->expects ($ this ->at ($ callIndex ++))
436460 ->method ('log ' )
437461 ->with (Logger::INFO , 'No user profile found for user with ID "testUserId". ' );
438- $ this ->loggerMock ->expects ($ this ->at (1 ))
462+ $ this ->loggerMock ->expects ($ this ->at ($ callIndex ++ ))
439463 ->method ('log ' )
440464 ->with (Logger::WARNING , 'Failed to save variation "control" of experiment "test_experiment" for user "testUserId". ' );
441465
@@ -463,4 +487,41 @@ public function testGetVariationBucketsIfUserProfileServiceSaveThrows()
463487 $ variation = $ this ->decisionService ->getVariation ($ runningExperiment , $ userId , $ this ->testUserAttributes );
464488 $ this ->assertEquals ($ expectedVariation , $ variation );
465489 }
490+
491+ public function testGetVariationUserWithSetForcedVariation ()
492+ {
493+ $ experimentKey = 'test_experiment ' ;
494+ $ pausedExperimentKey = 'paused_experiment ' ;
495+ $ userId = 'test_user ' ;
496+ $ forcedVariationKey = 'variation ' ;
497+ $ bucketedVariationKey = 'control ' ;
498+
499+ $ optlyObject = new Optimizely (DATAFILE , new ValidEventDispatcher (), $ this ->loggerMock );
500+
501+ $ userAttributes = [
502+ 'device_type ' => 'iPhone ' ,
503+ 'location ' => 'San Francisco '
504+ ];
505+
506+ $ optlyObject ->activate ($ experimentKey , $ userId , $ userAttributes );
507+
508+ // confirm normal bucketing occurs before setting the forced variation
509+ $ forcedVariationKey = $ optlyObject ->getVariation ($ experimentKey , $ userId , $ userAttributes );
510+ $ this ->assertEquals ($ bucketedVariationKey , $ forcedVariationKey );
511+
512+ // test valid experiment
513+ $ this ->assertTrue ($ optlyObject ->setForcedVariation ($ experimentKey , $ userId , $ forcedVariationKey ), sprintf ('Set variation to "%s" failed. ' , $ forcedVariationKey ));
514+ $ forcedVariationKey = $ optlyObject ->getVariation ($ experimentKey , $ userId , $ userAttributes );
515+ $ this ->assertEquals ($ forcedVariationKey , $ forcedVariationKey );
516+
517+ // clear forced variation and confirm that normal bucketing occurs
518+ $ this ->assertTrue ($ optlyObject ->setForcedVariation ($ experimentKey , $ userId , null ), sprintf ('Set variation to "%s" failed. ' , $ forcedVariationKey ));
519+ $ forcedVariationKey = $ optlyObject ->getVariation ($ experimentKey , $ userId , $ userAttributes );
520+ $ this ->assertEquals ($ bucketedVariationKey , $ forcedVariationKey );
521+
522+ // check that a paused experiment returns null
523+ $ this ->assertTrue ($ optlyObject ->setForcedVariation ($ pausedExperimentKey , $ userId , 'variation ' ), sprintf ('Set variation to "%s" failed. ' , $ forcedVariationKey ));
524+ $ forcedVariationKey = $ optlyObject ->getVariation ($ pausedExperimentKey , $ userId , $ userAttributes );
525+ $ this ->assertNull ($ forcedVariationKey );
526+ }
466527}
0 commit comments