24
24
use Optimizely \Entity \Variation ;
25
25
use Optimizely \ErrorHandler \NoOpErrorHandler ;
26
26
use Optimizely \Logger \NoOpLogger ;
27
+ use Optimizely \Optimizely ;
27
28
use Optimizely \ProjectConfig ;
28
29
use Optimizely \UserProfile \UserProfileServiceInterface ;
29
30
@@ -107,9 +108,13 @@ public function testGetVariationReturnsWhitelistedVariation()
107
108
$ expectedVariation = new Variation ('7722370027 ' , 'control ' );
108
109
$ runningExperiment = $ this ->config ->getExperimentFromKey ('test_experiment ' );
109
110
111
+ $ callIndex = 0 ;
110
112
$ this ->bucketerMock ->expects ($ this ->never ())
111
113
->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 ++))
113
118
->method ('log ' )
114
119
->with (Logger::INFO , 'User "user1" is forced in variation "control" of experiment "test_experiment". ' );
115
120
@@ -131,9 +136,13 @@ public function testGetVariationReturnsWhitelistedVariationForGroupedExperiment(
131
136
$ expectedVariation = new Variation ('7722260071 ' , 'group_exp_1_var_1 ' );
132
137
$ runningExperiment = $ this ->config ->getExperimentFromKey ('group_experiment_1 ' );
133
138
139
+ $ callIndex = 0 ;
134
140
$ this ->bucketerMock ->expects ($ this ->never ())
135
141
->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 ++))
137
146
->method ('log ' )
138
147
->with (Logger::INFO , 'User "user1" is forced in variation "group_exp_1_var_1" of experiment "group_experiment_1". ' );
139
148
@@ -251,10 +260,13 @@ public function testGetVariationReturnsStoredVariationIfAvailable()
251
260
$ runningExperiment = $ this ->config ->getExperimentFromKey ('test_experiment ' );
252
261
$ expectedVariation = new Variation ('7722370027 ' , 'control ' );
253
262
263
+ $ callIndex = 0 ;
254
264
$ this ->bucketerMock ->expects ($ this ->never ())
255
265
->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 ++))
258
270
->method ('log ' )
259
271
->with (Logger::INFO , 'Returning previously activated variation "control" of experiment "test_experiment" for user "not_whitelisted_user" from user profile. ' );
260
272
@@ -285,14 +297,17 @@ public function testGetVariationBucketsIfNoStoredVariation()
285
297
$ runningExperiment = $ this ->config ->getExperimentFromKey ('test_experiment ' );
286
298
$ expectedVariation = new Variation ('7722370027 ' , 'control ' );
287
299
300
+ $ callIndex = 0 ;
288
301
$ this ->bucketerMock ->expects ($ this ->once ())
289
302
->method ('bucket ' )
290
303
->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 ++))
293
308
->method ('log ' )
294
309
->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 ++ ))
296
311
->method ('log ' )
297
312
->with (Logger::INFO , 'Saved variation "control" of experiment "test_experiment" for user "testUserId". ' );
298
313
@@ -330,14 +345,17 @@ public function testGetVariationBucketsIfStoredVariationIsInvalid()
330
345
$ runningExperiment = $ this ->config ->getExperimentFromKey ('test_experiment ' );
331
346
$ expectedVariation = new Variation ('7722370027 ' , 'control ' );
332
347
348
+ $ callIndex = 0 ;
333
349
$ this ->bucketerMock ->expects ($ this ->once ())
334
350
->method ('bucket ' )
335
351
->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 ++))
338
356
->method ('log ' )
339
357
->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 ++ ))
341
359
->method ('log ' )
342
360
->with (Logger::INFO , 'Saved variation "control" of experiment "test_experiment" for user "testUserId". ' );
343
361
@@ -379,14 +397,17 @@ public function testGetVariationBucketsIfUserProfileServiceLookupThrows()
379
397
$ runningExperiment = $ this ->config ->getExperimentFromKey ('test_experiment ' );
380
398
$ expectedVariation = new Variation ('7722370027 ' , 'control ' );
381
399
400
+ $ callIndex = 0 ;
382
401
$ this ->bucketerMock ->expects ($ this ->once ())
383
402
->method ('bucket ' )
384
403
->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 ++))
387
408
->method ('log ' )
388
409
->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 ++ ))
390
411
->method ('log ' )
391
412
->with (Logger::INFO , 'Saved variation "control" of experiment "test_experiment" for user "testUserId". ' );
392
413
@@ -428,14 +449,17 @@ public function testGetVariationBucketsIfUserProfileServiceSaveThrows()
428
449
$ runningExperiment = $ this ->config ->getExperimentFromKey ('test_experiment ' );
429
450
$ expectedVariation = new Variation ('7722370027 ' , 'control ' );
430
451
452
+ $ callIndex = 0 ;
431
453
$ this ->bucketerMock ->expects ($ this ->once ())
432
454
->method ('bucket ' )
433
455
->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 ++))
436
460
->method ('log ' )
437
461
->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 ++ ))
439
463
->method ('log ' )
440
464
->with (Logger::WARNING , 'Failed to save variation "control" of experiment "test_experiment" for user "testUserId". ' );
441
465
@@ -463,4 +487,41 @@ public function testGetVariationBucketsIfUserProfileServiceSaveThrows()
463
487
$ variation = $ this ->decisionService ->getVariation ($ runningExperiment , $ userId , $ this ->testUserAttributes );
464
488
$ this ->assertEquals ($ expectedVariation , $ variation );
465
489
}
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
+ }
466
527
}
0 commit comments