Skip to content

Commit 54e918d

Browse files
committed
feat(api): Feature variable APIs return default variable value when featureEnabled property is false.
1 parent 7073e25 commit 54e918d

File tree

2 files changed

+95
-9
lines changed

2 files changed

+95
-9
lines changed

src/Optimizely/Optimizely.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -620,18 +620,26 @@ public function getFeatureVariableValueForType(
620620
);
621621
} else {
622622
$variation = $decision->getVariation();
623-
$variable_usage = $variation->getVariableUsageById($variable->getId());
624-
if ($variable_usage) {
625-
$variableValue = $variable_usage->getValue();
626-
$this->_logger->log(
627-
Logger::INFO,
628-
"Returning variable value '{$variableValue}' for variation '{$variation->getKey()}' ".
629-
"of feature flag '{$featureFlagKey}'"
630-
);
623+
if ($variation->getFeatureEnabled()) {
624+
$variableUsage = $variation->getVariableUsageById($variable->getId());
625+
if ($variableUsage) {
626+
$variableValue = $variableUsage->getValue();
627+
$this->_logger->log(
628+
Logger::INFO,
629+
"Returning variable value '{$variableValue}' for variation '{$variation->getKey()}' ".
630+
"of feature flag '{$featureFlagKey}'"
631+
);
632+
} else {
633+
$this->_logger->log(
634+
Logger::INFO,
635+
"Variable '{$variableKey}' is not used in variation '{$variation->getKey()}', ".
636+
"returning default value '{$variableValue}'."
637+
);
638+
}
631639
} else {
632640
$this->_logger->log(
633641
Logger::INFO,
634-
"Variable '{$variableKey}' is not used in variation '{$variation->getKey()}', ".
642+
"Feature '{$featureFlagKey}' for variation '{$variation->getKey()}' is not enabled, ".
635643
"returning default value '{$variableValue}'."
636644
);
637645
}

tests/OptimizelyTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2784,6 +2784,45 @@ public function testGetFeatureVariableValueForTypeGivenFeatureFlagIsEnabledForUs
27842784
);
27852785
}
27862786

2787+
public function testGetFeatureVariableValueForTypeReturnDefaultVariableValueGivenUserInExperimentAndFeatureFlagIsNotEnabled()
2788+
{
2789+
// should return specific value
2790+
$decisionServiceMock = $this->getMockBuilder(DecisionService::class)
2791+
->setConstructorArgs(array($this->loggerMock, $this->projectConfig))
2792+
->setMethods(array('getVariationForFeature'))
2793+
->getMock();
2794+
2795+
$decisionService = new \ReflectionProperty(Optimizely::class, '_decisionService');
2796+
$decisionService->setAccessible(true);
2797+
$decisionService->setValue($this->optimizelyObject, $decisionServiceMock);
2798+
2799+
$experiment = $this->projectConfig->getExperimentFromKey('test_experiment_double_feature');
2800+
$variation = $this->projectConfig->getVariationFromKey('test_experiment_double_feature', 'control');
2801+
$variation->setFeatureEnabled(false);
2802+
$expectedDecision = new FeatureDecision(
2803+
$experiment,
2804+
$variation,
2805+
FeatureDecision::DECISION_SOURCE_EXPERIMENT
2806+
);
2807+
2808+
$decisionServiceMock->expects($this->exactly(1))
2809+
->method('getVariationForFeature')
2810+
->will($this->returnValue($expectedDecision));
2811+
2812+
$this->loggerMock->expects($this->exactly(1))
2813+
->method('log')
2814+
->with(
2815+
Logger::INFO,
2816+
"Feature 'double_single_variable_feature' for variation 'control' is not enabled, ".
2817+
"returning default value '14.99'."
2818+
);
2819+
2820+
$this->assertSame(
2821+
$this->optimizelyObject->getFeatureVariableValueForType('double_single_variable_feature', 'double_variable', 'user_id', [], 'double'),
2822+
'14.99'
2823+
);
2824+
}
2825+
27872826
public function testGetFeatureVariableValueForTypeWithRolloutRule()
27882827
{
27892828
// should return specific value
@@ -2822,6 +2861,45 @@ public function testGetFeatureVariableValueForTypeWithRolloutRule()
28222861
$this->assertTrue($this->optimizelyObject->getFeatureVariableBoolean('boolean_single_variable_feature', 'boolean_variable', 'user_id', []));
28232862
}
28242863

2864+
public function testGetFeatureVariableValueReturnDefaultVariableValueGivenUserInRolloutAndFeatureFlagIsNotEnabled()
2865+
{
2866+
// should return specific value
2867+
$decisionServiceMock = $this->getMockBuilder(DecisionService::class)
2868+
->setConstructorArgs(array($this->loggerMock, $this->projectConfig))
2869+
->setMethods(array('getVariationForFeature'))
2870+
->getMock();
2871+
2872+
$decisionService = new \ReflectionProperty(Optimizely::class, '_decisionService');
2873+
$decisionService->setAccessible(true);
2874+
$decisionService->setValue($this->optimizelyObject, $decisionServiceMock);
2875+
2876+
$featureFlag = $this->projectConfig->getFeatureFlagFromKey('boolean_single_variable_feature');
2877+
$rolloutId = $featureFlag->getRolloutId();
2878+
$rollout = $this->projectConfig->getRolloutFromId($rolloutId);
2879+
$experiment = $rollout->getExperiments()[0];
2880+
$expectedVariation = $experiment->getVariations()[0];
2881+
$expectedVariation->setFeatureEnabled(false);
2882+
$expectedDecision = new FeatureDecision(
2883+
$experiment,
2884+
$expectedVariation,
2885+
FeatureDecision::DECISION_SOURCE_ROLLOUT
2886+
);
2887+
2888+
$decisionServiceMock->expects($this->exactly(1))
2889+
->method('getVariationForFeature')
2890+
->will($this->returnValue($expectedDecision));
2891+
2892+
$this->loggerMock->expects($this->exactly(1))
2893+
->method('log')
2894+
->with(
2895+
Logger::INFO,
2896+
"Feature 'boolean_single_variable_feature' for variation '177771' is not enabled, ".
2897+
"returning default value 'true'."
2898+
);
2899+
2900+
$this->assertTrue($this->optimizelyObject->getFeatureVariableBoolean('boolean_single_variable_feature', 'boolean_variable', 'user_id', []));
2901+
}
2902+
28252903
public function testGetFeatureVariableValueForTypeGivenFeatureFlagIsEnabledForUserAndVariableNotInVariation()
28262904
{
28272905
// should return default value

0 commit comments

Comments
 (0)