Skip to content

Commit 34e8e66

Browse files
FIX: Forced variation not available in experiment (#237)
* Php forced Decision fix * added unit test and comments * nit fix * Added empty checks in flagkey and experiment key * removed unnesecary checks and moved getVariationFromExperiment above and then checking $variation = $config->getFlagVariationByKey($flagKey, $variationKey); * lint fix Co-authored-by: mnoman09 <[email protected]>
1 parent ebfed83 commit 34e8e66

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

src/Optimizely/Event/Builder/EventBuilder.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,13 @@ public function createImpressionEvent($config, $experimentId, $variationKey, $fl
252252
$eventParams = $this->getCommonParams($config, $userId, $attributes);
253253
$experiment = $config->getExperimentFromId($experimentId);
254254

255-
if (empty($experimentId)) {
255+
// When variation is not mapped to any flagKey
256+
$variation = $config->getVariationFromKeyByExperimentId($experimentId, $variationKey);
257+
258+
// Mapped flagKey can be directly used in variation in that case no experimentKey exist
259+
if ($variation && !$variation->getKey()) {
256260
$variation = $config->getFlagVariationByKey($flagKey, $variationKey);
257-
} else {
258-
$variation = $config->getVariationFromKeyByExperimentId($experimentId, $variationKey);
259261
}
260-
261262
$impressionParams = $this->getImpressionParams($experiment, $variation, $flagKey, $ruleKey, $ruleType, $enabled);
262263

263264
$eventParams[VISITORS][0][SNAPSHOTS][] = $impressionParams;

tests/EventTests/EventBuilderTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,53 @@ public function testCreateImpressionEventNoAttributesNoValue()
157157
$this->assertTrue($result[0], $result[1]);
158158
}
159159

160+
public function testCreateImpressionEventVariationFromSameFlagButDifferentExperiments()
161+
{
162+
$decisions = array('decisions' => [[
163+
'campaign_id'=> '5',
164+
'experiment_id'=> '122235',
165+
'variation_id'=> '177775',
166+
'metadata'=> [
167+
'flag_key' => 'string_single_variable_feature',
168+
'rule_key' => 'test_experiment_with_feature_rollout',
169+
'rule_type' => 'experiment',
170+
'variation_key'=> '177775',
171+
'enabled' => true
172+
]
173+
]]
174+
);
175+
$this->expectedImpressionEventParams['visitors'][0]['snapshots'][0] = $decisions +
176+
$this->expectedImpressionEventParams['visitors'][0]['snapshots'][0];
177+
$this->expectedImpressionEventParams['visitors'][0]['snapshots'][0]['events'][0] =
178+
[
179+
'entity_id'=> '5',
180+
'timestamp'=> $this->timestamp,
181+
'uuid'=> $this->uuid,
182+
'key'=> 'campaign_activated'
183+
];
184+
$expectedLogEvent = new LogEvent(
185+
$this->expectedEventUrl,
186+
$this->expectedImpressionEventParams,
187+
$this->expectedEventHttpVerb,
188+
$this->expectedEventHeaders
189+
);
190+
191+
$logEvent = $this->eventBuilder->createImpressionEvent(
192+
$this->config,
193+
'122235',
194+
'177775',
195+
'string_single_variable_feature',
196+
'test_experiment_with_feature_rollout',
197+
'experiment',
198+
true,
199+
$this->testUserId,
200+
null
201+
);
202+
$logEvent = $this->fakeParamsToReconcile($logEvent);
203+
$result = $this->areLogEventsEqual($expectedLogEvent, $logEvent);
204+
$this->assertTrue($result[0], $result[1]);
205+
}
206+
160207
public function testCreateImpressionEventWithAttributesNoValue()
161208
{
162209
array_unshift(

0 commit comments

Comments
 (0)