forked from optimizely/php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptimizelyUserContextTest.php
639 lines (518 loc) · 29.4 KB
/
OptimizelyUserContextTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
<?php
/**
* Copyright 2021, 2023 Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Optimizely\Tests;
use Exception;
use Optimizely\Decide\OptimizelyDecideOption;
use TypeError;
use Optimizely\Logger\NoOpLogger;
use Optimizely\Optimizely;
use Optimizely\OptimizelyUserContext;
use Optimizely\OptimizelyDecisionContext;
use Optimizely\OptimizelyForcedDecision;
use PHPUnit\Framework\TestCase;
class OptimizelyUserContextTest extends TestCase
{
private $datafile;
private $loggerMock;
private $optimizelyObject;
protected function setUp() : void
{
$this->datafile = DATAFILE;
// Mock Logger
$this->loggerMock = $this->getMockBuilder(NoOpLogger::class)
->setMethods(array('log'))
->getMock();
$this->optimizelyObject = new Optimizely($this->datafile, null, $this->loggerMock);
}
public function testOptimizelyUserContextIsCreatedWithExpectedValues()
{
$userId = 'test_user';
$attributes = [ "browser" => "chrome"];
$optUserContext = new OptimizelyUserContext($this->optimizelyObject, $userId, $attributes);
$this->assertEquals($userId, $optUserContext->getUserId());
$this->assertEquals($attributes, $optUserContext->getAttributes());
$this->assertSame($this->optimizelyObject, $optUserContext->getOptimizely());
}
public function testOptimizelyUserContextThrowsErrorWhenNonArrayPassedAsAttributes()
{
$userId = 'test_user';
try {
$optUserContext = new OptimizelyUserContext($this->optimizelyObject, $userId, 'HelloWorld');
} catch (Exception $exception) {
return;
} catch (TypeError $exception) {
return;
}
$this->fail('Unexpected behavior. UserContext should have thrown an error.');
}
public function testSetAttribute()
{
$userId = 'test_user';
$attributes = [ "browser" => "chrome"];
$optUserContext = new OptimizelyUserContext($this->optimizelyObject, $userId, $attributes);
$this->assertEquals($attributes, $optUserContext->getAttributes());
$optUserContext->setAttribute('color', 'red');
$this->assertEquals([
"browser" => "chrome",
"color" => "red"
], $optUserContext->getAttributes());
}
public function testSetAttributeOverridesValueOfExistingKey()
{
$userId = 'test_user';
$attributes = [ "browser" => "chrome"];
$optUserContext = new OptimizelyUserContext($this->optimizelyObject, $userId, $attributes);
$this->assertEquals($attributes, $optUserContext->getAttributes());
$optUserContext->setAttribute('browser', 'firefox');
$this->assertEquals(["browser" => "firefox"], $optUserContext->getAttributes());
}
public function testSetAttributeWhenNoAttributesProvidedInConstructor()
{
$userId = 'test_user';
$optUserContext = new OptimizelyUserContext($this->optimizelyObject, $userId);
$this->assertEquals([], $optUserContext->getAttributes());
$optUserContext->setAttribute('browser', 'firefox');
$this->assertEquals(["browser" => "firefox"], $optUserContext->getAttributes());
}
public function testDecideCallsAndReturnsOptimizelyDecideAPI()
{
$userId = 'test_user';
$attributes = [ "browser" => "chrome"];
$optimizelyMock = $this->getMockBuilder(Optimizely::class)
->setConstructorArgs(array($this->datafile, null, $this->loggerMock))
->setMethods(array('decide'))
->getMock();
$optUserContext = new OptimizelyUserContext($optimizelyMock, $userId, $attributes);
//assert that decide is called with expected params
$optimizelyMock->expects($this->exactly(1))
->method('decide')
->with(
$optUserContext,
'test_feature',
['DISABLE_DECISION_EVENT', 'ENABLED_FLAGS_ONLY']
)
->will($this->returnValue('Mocked return value'));
$this->assertEquals(
'Mocked return value',
$optUserContext->decide('test_feature', ['DISABLE_DECISION_EVENT', 'ENABLED_FLAGS_ONLY'])
);
}
public function testDecideResponseUserContextNotEqualToCalledUserContext()
{
$userId = 'test_user';
$attributes = [ "browser" => "chrome"];
$optlyObject = new Optimizely($this->datafile);
$optUserContext = new OptimizelyUserContext($optlyObject, $userId, $attributes);
$decision = $optUserContext->decide('test_feature', ['DISABLE_DECISION_EVENT', 'ENABLED_FLAGS_ONLY']);
$this->assertEquals(
$optUserContext->getAttributes(),
$decision->getUserContext()->getAttributes()
);
$optUserContext->setAttribute("test_key", "test_value");
$this->assertNotEquals(
$optUserContext->getAttributes(),
$decision->getUserContext()->getAttributes()
);
}
public function testDecideAllCallsAndReturnsOptimizelyDecideAllAPI()
{
$userId = 'test_user';
$attributes = [ "browser" => "chrome"];
$optimizelyMock = $this->getMockBuilder(Optimizely::class)
->setConstructorArgs(array($this->datafile, null, $this->loggerMock))
->setMethods(array('decideAll'))
->getMock();
$optUserContext = new OptimizelyUserContext($optimizelyMock, $userId, $attributes);
//assert that decideAll is called with expected params
$optimizelyMock->expects($this->exactly(1))
->method('decideAll')
->with(
$optUserContext,
['ENABLED_FLAGS_ONLY']
)
->will($this->returnValue('Mocked return value'));
$this->assertEquals(
'Mocked return value',
$optUserContext->decideAll(['ENABLED_FLAGS_ONLY'])
);
}
public function testDecideForKeysCallsAndReturnsOptimizelyDecideForKeysAPI()
{
$userId = 'test_user';
$attributes = [ "browser" => "chrome"];
$optimizelyMock = $this->getMockBuilder(Optimizely::class)
->setConstructorArgs(array($this->datafile, null, $this->loggerMock))
->setMethods(array('decideForKeys'))
->getMock();
$optUserContext = new OptimizelyUserContext($optimizelyMock, $userId, $attributes);
//assert that decideForKeys is called with expected params
$optimizelyMock->expects($this->exactly(1))
->method('decideForKeys')
->with(
$optUserContext,
['test_feature', 'test_experiment'],
['DISABLE_DECISION_EVENT']
)
->will($this->returnValue('Mocked return value'));
$this->assertEquals(
'Mocked return value',
$optUserContext->decideForKeys(['test_feature', 'test_experiment'], ['DISABLE_DECISION_EVENT'])
);
}
public function testTrackEventCallsAndReturnsOptimizelyTrackAPI()
{
$userId = 'test_user';
$attributes = [];
$eventKey = "test_event";
$eventTags = [ "revenue" => 50];
$optimizelyMock = $this->getMockBuilder(Optimizely::class)
->setConstructorArgs(array($this->datafile, null, $this->loggerMock))
->setMethods(array('track'))
->getMock();
$optUserContext = new OptimizelyUserContext($optimizelyMock, $userId, $attributes);
//assert that track is called with expected params
$optimizelyMock->expects($this->exactly(1))
->method('track')
->with(
$eventKey,
$userId,
$attributes,
$eventTags
)
->will($this->returnValue('Mocked return value'));
$this->assertEquals(
'Mocked return value',
$optUserContext->trackEvent($eventKey, $eventTags)
);
}
public function testJsonSerialize()
{
$userId = 'test_user';
$attributes = [ "browser" => "chrome"];
$optUserContext = new OptimizelyUserContext($this->optimizelyObject, $userId, $attributes);
$this->assertEquals([
'userId' => $userId,
'attributes' => $attributes
], json_decode(json_encode($optUserContext), true));
}
// Forced decision tests
public function testForcedDecisionValidDatafileReturnStatus()
{
$userId = 'test_user';
$attributes = [ "browser" => "chrome"];
$validOptlyObject = new Optimizely($this->datafile);
$optUserContext = new OptimizelyUserContext($validOptlyObject, $userId, $attributes);
$context = new OptimizelyDecisionContext("flag1", "targeted_delivery");
$decision = new OptimizelyForcedDecision("variation1");
$setForcedDecision = $optUserContext->setForcedDecision($context, $decision);
$this->assertTrue($setForcedDecision);
$getForcedDecision = $optUserContext->getForcedDecision($context);
$this->assertEquals($getForcedDecision, "variation1");
$removeForcedDecision = $optUserContext->removeForcedDecision($context);
$this->assertTrue($removeForcedDecision);
$removeAllForcedDecision = $optUserContext->removeAllForcedDecisions();
$this->assertTrue($removeAllForcedDecision);
}
public function testForcedDecisionFlagToDecision()
{
$userId = 'test_user';
$attributes = [ "browser" => "chrome"];
$validOptlyObject = new Optimizely($this->datafile);
$optUserContext = new OptimizelyUserContext($validOptlyObject, $userId, $attributes);
$context = new OptimizelyDecisionContext("boolean_single_variable_feature", null);
$decision = new OptimizelyForcedDecision("177773");
$setForcedDecision = $optUserContext->setForcedDecision($context, $decision);
$this->assertTrue($setForcedDecision);
$getForcedDecision = $optUserContext->getForcedDecision($context);
$this->assertEquals($getForcedDecision, "177773");
$decision = $optUserContext->decide('boolean_single_variable_feature', [OptimizelyDecideOption::INCLUDE_REASONS]);
$this->assertEquals($decision->getVariationKey(), "177773");
$this->assertNull($decision->getRuleKey());
$this->assertTrue($decision->getEnabled());
$this->assertEquals($decision->getFlagKey(), "boolean_single_variable_feature");
$this->assertEquals($decision->getUserContext()->getUserId(), $userId);
$this->assertEquals(count($decision->getUserContext()->getAttributes()), 1);
$this->assertEquals($decision->getReasons(), [
'Decided by forced decision.',
'Variation (177773) is mapped to flag (boolean_single_variable_feature) and user (test_user) in the forced decision map.'
]);
// Removing forced decision to test
$removeForcedDecision = $optUserContext->removeForcedDecision($context);
$this->assertTrue($removeForcedDecision);
$decision = $optUserContext->decide('boolean_single_variable_feature', [OptimizelyDecideOption::INCLUDE_REASONS]);
$this->assertEquals($decision->getVariationKey(), "177778");
$this->assertEquals($decision->getRuleKey(), "rollout_1_exp_3");
$this->assertTrue($decision->getEnabled());
$this->assertEquals($decision->getFlagKey(), "boolean_single_variable_feature");
$this->assertEquals($decision->getUserContext()->getUserId(), $userId);
$this->assertEquals(count($decision->getUserContext()->getAttributes()), 1);
$this->assertEquals($decision->getReasons(), [
"The feature flag 'boolean_single_variable_feature' is not used in any experiments.",
'Audiences for rule 1 collectively evaluated to FALSE.',
'User "test_user" does not meet conditions for targeting rule "1".',
'Audiences for rule 2 collectively evaluated to FALSE.',
'User "test_user" does not meet conditions for targeting rule "2".',
'Audiences for rule Everyone Else collectively evaluated to TRUE.',
'User "test_user" meets condition for targeting rule "Everyone Else".',
'Assigned bucket 3041 to user "test_user" with bucketing ID "test_user".',
'User "test_user" is in the traffic group of targeting rule "Everyone Else".',
"User 'test_user' is bucketed into rollout for feature flag 'boolean_single_variable_feature'."
]);
}
public function testForcedDecisionExperimentRuleToDecision()
{
$userId = 'test_user';
$attributes = [ "browser" => "chrome"];
$validOptlyObject = new Optimizely($this->datafile);
$optUserContext = new OptimizelyUserContext($validOptlyObject, $userId, $attributes);
$context = new OptimizelyDecisionContext("boolean_feature", 'test_experiment_2');
$decision = new OptimizelyForcedDecision("test_variation_1");
$setForcedDecision = $optUserContext->setForcedDecision($context, $decision);
$this->assertTrue($setForcedDecision);
$getForcedDecision = $optUserContext->getForcedDecision($context);
$this->assertEquals($getForcedDecision, "test_variation_1");
$decision = $optUserContext->decide('boolean_feature', [OptimizelyDecideOption::INCLUDE_REASONS]);
$this->assertEquals($decision->getVariationKey(), "test_variation_1");
$this->assertEquals($decision->getRuleKey(), "test_experiment_2");
$this->assertTrue($decision->getEnabled());
$this->assertEquals($decision->getFlagKey(), "boolean_feature");
$this->assertEquals($decision->getUserContext()->getUserId(), $userId);
$this->assertEquals(count($decision->getUserContext()->getAttributes()), 1);
$this->assertEquals($decision->getReasons(), [
'Decided by forced decision.',
'Variation (test_variation_1) is mapped to flag (boolean_feature), rule (test_experiment_2) and user (test_user) in the forced decision map.',
"The user 'test_user' is bucketed into experiment 'test_experiment_2' of feature 'boolean_feature'."
]);
// Removing forced decision to test
$removeForcedDecision = $optUserContext->removeForcedDecision($context);
$this->assertTrue($removeForcedDecision);
$decision = $optUserContext->decide('boolean_feature', [OptimizelyDecideOption::INCLUDE_REASONS]);
$this->assertEquals($decision->getVariationKey(), "test_variation_2");
$this->assertEquals($decision->getRuleKey(), "test_experiment_2");
$this->assertTrue($decision->getEnabled());
$this->assertEquals($decision->getFlagKey(), "boolean_feature");
$this->assertEquals($decision->getUserContext()->getUserId(), $userId);
$this->assertEquals(count($decision->getUserContext()->getAttributes()), 1);
$this->assertEquals($decision->getReasons(), [
'Audiences for experiment "test_experiment_2" collectively evaluated to TRUE.',
'Assigned bucket 9075 to user "test_user" with bucketing ID "test_user".',
'User "test_user" is in variation test_variation_2 of experiment test_experiment_2.',
"The user 'test_user' is bucketed into experiment 'test_experiment_2' of feature 'boolean_feature'."
]);
}
public function testForcedDecisionRuleDeliveryRuleToDecision()
{
$userId = 'test_user';
$attributes = [ "browser" => "chrome"];
$validOptlyObject = new Optimizely($this->datafile);
$optUserContext = new OptimizelyUserContext($validOptlyObject, $userId, $attributes);
$context = new OptimizelyDecisionContext("boolean_single_variable_feature", "rollout_1_exp_3");
$decision = new OptimizelyForcedDecision("177773");
$setForcedDecision = $optUserContext->setForcedDecision($context, $decision);
$this->assertTrue($setForcedDecision);
$getForcedDecision = $optUserContext->getForcedDecision($context);
$this->assertEquals($getForcedDecision, "177773");
$decision = $optUserContext->decide('boolean_single_variable_feature', [OptimizelyDecideOption::INCLUDE_REASONS]);
$this->assertEquals($decision->getVariationKey(), "177773");
$this->assertEquals($decision->getRuleKey(), "rollout_1_exp_3");
$this->assertTrue($decision->getEnabled());
$this->assertEquals($decision->getFlagKey(), "boolean_single_variable_feature");
$this->assertEquals($decision->getUserContext()->getUserId(), $userId);
$this->assertEquals(count($decision->getUserContext()->getAttributes()), 1);
$this->assertEquals($decision->getReasons(), [
"The feature flag 'boolean_single_variable_feature' is not used in any experiments.",
'Audiences for rule 1 collectively evaluated to FALSE.',
'User "test_user" does not meet conditions for targeting rule "1".',
'Audiences for rule 2 collectively evaluated to FALSE.',
'User "test_user" does not meet conditions for targeting rule "2".',
'Decided by forced decision.',
'Variation (177773) is mapped to flag (boolean_single_variable_feature), rule (rollout_1_exp_3) and user (test_user) in the forced decision map.',
"User 'test_user' is bucketed into rollout for feature flag 'boolean_single_variable_feature'."
]);
// Removing forced decision to test
$removeForcedDecision = $optUserContext->removeForcedDecision($context);
$this->assertTrue($removeForcedDecision);
$decision = $optUserContext->decide('boolean_single_variable_feature', [OptimizelyDecideOption::INCLUDE_REASONS]);
$this->assertEquals($decision->getVariationKey(), "177778");
$this->assertEquals($decision->getRuleKey(), "rollout_1_exp_3");
$this->assertTrue($decision->getEnabled());
$this->assertEquals($decision->getFlagKey(), "boolean_single_variable_feature");
$this->assertEquals($decision->getUserContext()->getUserId(), $userId);
$this->assertEquals(count($decision->getUserContext()->getAttributes()), 1);
$this->assertEquals($decision->getReasons(), [
"The feature flag 'boolean_single_variable_feature' is not used in any experiments.",
'Audiences for rule 1 collectively evaluated to FALSE.',
'User "test_user" does not meet conditions for targeting rule "1".',
'Audiences for rule 2 collectively evaluated to FALSE.',
'User "test_user" does not meet conditions for targeting rule "2".',
'Audiences for rule Everyone Else collectively evaluated to TRUE.',
'User "test_user" meets condition for targeting rule "Everyone Else".',
'Assigned bucket 3041 to user "test_user" with bucketing ID "test_user".',
'User "test_user" is in the traffic group of targeting rule "Everyone Else".',
"User 'test_user' is bucketed into rollout for feature flag 'boolean_single_variable_feature'."
]);
}
public function testForcedDecisionInvalidDeliveryRuleToDecision()
{
$userId = 'test_user';
$attributes = [ "browser" => "chrome"];
$validOptlyObject = new Optimizely($this->datafile);
$optUserContext = new OptimizelyUserContext($validOptlyObject, $userId, $attributes);
$context = new OptimizelyDecisionContext("boolean_single_variable_feature", "rollout_1_exp_3");
$decision = new OptimizelyForcedDecision("invalid");
$setForcedDecision = $optUserContext->setForcedDecision($context, $decision);
$this->assertTrue($setForcedDecision);
$decision = $optUserContext->decide('boolean_single_variable_feature', [OptimizelyDecideOption::INCLUDE_REASONS]);
$this->assertEquals($decision->getVariationKey(), "177778");
$this->assertEquals($decision->getRuleKey(), "rollout_1_exp_3");
$this->assertTrue($decision->getEnabled());
$this->assertEquals($decision->getFlagKey(), "boolean_single_variable_feature");
$this->assertEquals($decision->getUserContext()->getUserId(), $userId);
$this->assertEquals(count($decision->getUserContext()->getAttributes()), 1);
$this->assertEquals($decision->getReasons(), [
"The feature flag 'boolean_single_variable_feature' is not used in any experiments.",
'Audiences for rule 1 collectively evaluated to FALSE.',
'User "test_user" does not meet conditions for targeting rule "1".',
'Audiences for rule 2 collectively evaluated to FALSE.',
'User "test_user" does not meet conditions for targeting rule "2".',
'Invalid variation is mapped to flag (boolean_single_variable_feature), rule (rollout_1_exp_3) and user (test_user) in the forced decision map.',
'Audiences for rule Everyone Else collectively evaluated to TRUE.',
'User "test_user" meets condition for targeting rule "Everyone Else".',
'Assigned bucket 3041 to user "test_user" with bucketing ID "test_user".',
'User "test_user" is in the traffic group of targeting rule "Everyone Else".',
"User 'test_user' is bucketed into rollout for feature flag 'boolean_single_variable_feature'."
]);
}
public function testForcedDecisionInvalidExperimentRuleToDecision()
{
$userId = 'test_user';
$attributes = [ "browser" => "chrome"];
$validOptlyObject = new Optimizely($this->datafile);
$optUserContext = new OptimizelyUserContext($validOptlyObject, $userId, $attributes);
$context = new OptimizelyDecisionContext("boolean_feature", null);
$decision = new OptimizelyForcedDecision("invalid");
$setForcedDecision = $optUserContext->setForcedDecision($context, $decision);
$this->assertTrue($setForcedDecision);
$decision = $optUserContext->decide('boolean_feature', [OptimizelyDecideOption::INCLUDE_REASONS]);
$this->assertEquals("test_variation_2", $decision->getVariationKey());
$this->assertEquals("test_experiment_2", $decision->getRuleKey());
$this->assertTrue($decision->getEnabled());
$this->assertEquals("boolean_feature", $decision->getFlagKey());
$this->assertEquals($decision->getUserContext()->getUserId(), $userId);
$this->assertEquals(1, count($decision->getUserContext()->getAttributes()));
$this->assertEquals([
'Invalid variation is mapped to flag (boolean_feature) and user (test_user) in the forced decision map.',
'Audiences for experiment "test_experiment_2" collectively evaluated to TRUE.',
'Assigned bucket 9075 to user "test_user" with bucketing ID "test_user".',
'User "test_user" is in variation test_variation_2 of experiment test_experiment_2.',
"The user 'test_user' is bucketed into experiment 'test_experiment_2' of feature 'boolean_feature'."
], $decision->getReasons());
}
public function testForcedDecisionConflicts()
{
$featureKey = "boolean_feature";
$userId = 'test_user';
$attributes = [ "browser" => "chrome"];
$validOptlyObject = new Optimizely($this->datafile);
$optUserContext = new OptimizelyUserContext($validOptlyObject, $userId, $attributes);
$context = new OptimizelyDecisionContext($featureKey, null);
$decision = new OptimizelyForcedDecision("test_variation_1");
$setForcedDecision1 = $optUserContext->setForcedDecision($context, $decision);
$this->assertTrue($setForcedDecision1);
$context = new OptimizelyDecisionContext($featureKey, "test_experiment_2");
$decision = new OptimizelyForcedDecision("test_variation_2");
$setForcedDecision2 = $optUserContext->setForcedDecision($context, $decision);
$this->assertTrue($setForcedDecision2);
// flag-to-decision is the 1st priority
$decision = $optUserContext->decide('boolean_feature', [OptimizelyDecideOption::INCLUDE_REASONS]);
$this->assertEquals("test_variation_1", $decision->getVariationKey());
$this->assertNull($decision->getRuleKey());
$this->assertEquals([
'Decided by forced decision.',
'Variation (test_variation_1) is mapped to flag (boolean_feature) and user (test_user) in the forced decision map.'
], $decision->getReasons());
}
public function testGetForcedDecision()
{
$featureKey = "boolean_feature";
$userId = 'test_user';
$attributes = [ "browser" => "chrome"];
$validOptlyObject = new Optimizely($this->datafile);
$optUserContext = new OptimizelyUserContext($validOptlyObject, $userId, $attributes);
$contextWithNullRuleKey = new OptimizelyDecisionContext($featureKey, null);
$decision = new OptimizelyForcedDecision("test_variation_1");
$this->assertTrue($optUserContext->setForcedDecision($contextWithNullRuleKey, $decision));
$this->assertEquals("test_variation_1", $optUserContext->getForcedDecision($contextWithNullRuleKey));
$context = new OptimizelyDecisionContext($featureKey, null);
$decision = new OptimizelyForcedDecision("test_variation_2");
$this->assertTrue($optUserContext->setForcedDecision($context, $decision));
$this->assertEquals("test_variation_2", $optUserContext->getForcedDecision($context));
$context = new OptimizelyDecisionContext($featureKey, "test_experiment_2");
$decision = new OptimizelyForcedDecision("test_variation_1");
$this->assertTrue($optUserContext->setForcedDecision($context, $decision));
$this->assertEquals("test_variation_1", $optUserContext->getForcedDecision($context));
$context = new OptimizelyDecisionContext($featureKey, "test_experiment_2");
$decision = new OptimizelyForcedDecision("test_variation_2");
$this->assertTrue($optUserContext->setForcedDecision($context, $decision));
$this->assertEquals("test_variation_2", $optUserContext->getForcedDecision($context));
$this->assertEquals("test_variation_2", $optUserContext->getForcedDecision($contextWithNullRuleKey));
}
public function testRemoveForcedDecision()
{
$featureKey = "boolean_feature";
$userId = 'test_user';
$attributes = [ "browser" => "chrome"];
$validOptlyObject = new Optimizely($this->datafile);
$optUserContext = new OptimizelyUserContext($validOptlyObject, $userId, $attributes);
$contextWithFlag = new OptimizelyDecisionContext($featureKey, null);
$decisionForFlag = new OptimizelyForcedDecision("test_variation_1");
$contextWithRule = new OptimizelyDecisionContext($featureKey, "test_experiment_2");
$decisionForRule = new OptimizelyForcedDecision("test_variation_2");
$this->assertTrue($optUserContext->setForcedDecision($contextWithFlag, $decisionForFlag));
$this->assertTrue($optUserContext->setForcedDecision($contextWithRule, $decisionForRule));
$this->assertEquals("test_variation_1", $optUserContext->getForcedDecision($contextWithFlag));
$this->assertEquals("test_variation_2", $optUserContext->getForcedDecision($contextWithRule));
$this->assertTrue($optUserContext->removeForcedDecision($contextWithFlag));
$this->assertNull($optUserContext->getForcedDecision($contextWithFlag));
$this->assertEquals("test_variation_2", $optUserContext->getForcedDecision($contextWithRule));
$this->assertTrue($optUserContext->removeForcedDecision($contextWithRule));
$this->assertNull($optUserContext->getForcedDecision($contextWithRule));
$this->assertNull($optUserContext->getForcedDecision($contextWithFlag));
$this->assertFalse($optUserContext->removeForcedDecision($contextWithFlag)); // no more saved decisions
}
public function testRemoveAllForcedDecisions()
{
$featureKey = "boolean_feature";
$userId = 'test_user';
$attributes = [ "browser" => "chrome"];
$validOptlyObject = new Optimizely($this->datafile);
$optUserContext = new OptimizelyUserContext($validOptlyObject, $userId, $attributes);
$this->assertTrue($optUserContext->removeAllForcedDecisions()); // no saved decisions
$contextWithFlag = new OptimizelyDecisionContext($featureKey, null);
$decisionForFlag = new OptimizelyForcedDecision("test_variation_1");
$contextWithRule = new OptimizelyDecisionContext($featureKey, "test_experiment_2");
$decisionForRule = new OptimizelyForcedDecision("test_variation_2");
$this->assertTrue($optUserContext->setForcedDecision($contextWithFlag, $decisionForFlag));
$this->assertTrue($optUserContext->setForcedDecision($contextWithRule, $decisionForRule));
$this->assertEquals("test_variation_1", $optUserContext->getForcedDecision($contextWithFlag));
$this->assertEquals("test_variation_2", $optUserContext->getForcedDecision($contextWithRule));
$this->assertTrue($optUserContext->removeAllForcedDecisions());
$this->assertNull($optUserContext->getForcedDecision($contextWithRule));
$this->assertNull($optUserContext->getForcedDecision($contextWithFlag));
$this->assertTrue($optUserContext->removeAllForcedDecisions()); // no more saved decisions
}
}