Skip to content

Commit d4a4aad

Browse files
authored
Fix isFeatureEnabled to always return a boolean value. (#75)
1 parent d0c5f4e commit d4a4aad

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

src/Optimizely/Optimizely.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ protected function sendImpressionEvent($experimentKey, $variationKey, $userId, $
239239
));
240240
}
241241
}
242-
242+
243243
/**
244244
* Buckets visitor and sends impression event to Optimizely.
245245
*
@@ -263,7 +263,7 @@ public function activate($experimentKey, $userId, $attributes = null)
263263
}
264264

265265
$this->sendImpressionEvent($experimentKey, $variationKey, $userId, $attributes);
266-
266+
267267
return $variationKey;
268268
}
269269

@@ -422,28 +422,28 @@ public function isFeatureEnabled($featureFlagKey, $userId, $attributes = null)
422422
{
423423
if (!$this->_isValid) {
424424
$this->_logger->log(Logger::ERROR, "Datafile has invalid format. Failing '".__FUNCTION__."'.");
425-
return null;
425+
return false;
426426
}
427427

428428
if (!$featureFlagKey) {
429429
$this->_logger->log(Logger::ERROR, "Feature Flag key cannot be empty.");
430-
return null;
430+
return false;
431431
}
432432

433433
if (!$userId) {
434434
$this->_logger->log(Logger::ERROR, "User ID cannot be empty.");
435-
return null;
435+
return false;
436436
}
437437

438438
$feature_flag = $this->_config->getFeatureFlagFromKey($featureFlagKey);
439439
if ($feature_flag && (!$feature_flag->getId())) {
440440
// Error logged in ProjectConfig - getFeatureFlagFromKey
441-
return null;
441+
return false;
442442
}
443443

444444
//validate feature flag
445445
if (!Validator::isFeatureFlagValid($this->_config, $feature_flag)) {
446-
return null;
446+
return false;
447447
}
448448

449449
$decision = $this->_decisionService->getVariationForFeature($feature_flag, $userId, $attributes);

tests/OptimizelyTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,44 +1742,44 @@ public function testIsFeatureEnabledGivenInvalidDataFile()
17421742

17431743
public function testIsFeatureEnabledGivenInvalidArguments()
17441744
{
1745-
// should return null and log a message when feature flag key is empty
1745+
// should return false and log a message when feature flag key is empty
17461746
$this->loggerMock->expects($this->at(0))
17471747
->method('log')
17481748
->with(Logger::ERROR, "Feature Flag key cannot be empty.");
17491749

1750-
$this->assertSame($this->optimizelyObject->isFeatureEnabled("", "user_id"), null);
1750+
$this->assertSame($this->optimizelyObject->isFeatureEnabled("", "user_id"), false);
17511751

1752-
// should return null and log a message when feature flag key is null
1752+
// should return false and log a message when feature flag key is null
17531753
$this->loggerMock->expects($this->at(0))
17541754
->method('log')
17551755
->with(Logger::ERROR, "Feature Flag key cannot be empty.");
17561756

1757-
$this->assertSame($this->optimizelyObject->isFeatureEnabled(null, "user_id"), null);
1757+
$this->assertSame($this->optimizelyObject->isFeatureEnabled(null, "user_id"), false);
17581758

1759-
// should return null and log a message when user id is empty
1759+
// should return false and log a message when user id is empty
17601760
$this->loggerMock->expects($this->at(0))
17611761
->method('log')
17621762
->with(Logger::ERROR, "User ID cannot be empty.");
17631763

1764-
$this->assertSame($this->optimizelyObject->isFeatureEnabled("boolean_feature", ""), null);
1764+
$this->assertSame($this->optimizelyObject->isFeatureEnabled("boolean_feature", ""), false);
17651765

1766-
// should return null and log a message when user id is null
1766+
// should return false and log a message when user id is null
17671767
$this->loggerMock->expects($this->at(0))
17681768
->method('log')
17691769
->with(Logger::ERROR, "User ID cannot be empty.");
17701770

1771-
$this->assertSame($this->optimizelyObject->isFeatureEnabled("boolean_feature", null), null);
1771+
$this->assertSame($this->optimizelyObject->isFeatureEnabled("boolean_feature", null), false);
17721772
}
17731773

17741774
public function testIsFeatureEnabledGivenFeatureFlagNotFound()
17751775
{
17761776
$feature_key = "abcd"; // Any string that is not a feature flag key in the data file
17771777

1778-
//should return null and log a message when no feature flag found against a valid feature key
1778+
//should return false and log a message when no feature flag found against a valid feature key
17791779
$this->loggerMock->expects($this->at(0))
17801780
->method('log')
17811781
->with(Logger::ERROR, "FeatureFlag Key \"{$feature_key}\" is not in datafile.");
1782-
$this->assertSame($this->optimizelyObject->isFeatureEnabled($feature_key, "user_id"), null);
1782+
$this->assertSame($this->optimizelyObject->isFeatureEnabled($feature_key, "user_id"), false);
17831783
}
17841784

17851785
public function testIsFeatureEnabledGivenInvalidFeatureFlag()
@@ -1798,8 +1798,8 @@ public function testIsFeatureEnabledGivenInvalidFeatureFlag()
17981798
$experimentIds [] = '122241';
17991799
$feature_flag->setExperimentIds($experimentIds);
18001800

1801-
//should return null when feature flag is invalid
1802-
$this->assertSame($optimizelyObj->isFeatureEnabled('mutex_group_feature', "user_id"), null);
1801+
//should return false when feature flag is invalid
1802+
$this->assertSame($optimizelyObj->isFeatureEnabled('mutex_group_feature', "user_id"), false);
18031803
}
18041804

18051805
public function testIsFeatureEnabledGivenFeatureFlagIsNotEnabledForUser()
@@ -2124,7 +2124,7 @@ public function testGetFeatureVariableValueForTypeGivenFeatureFlagIsEnabledForUs
21242124
public function testGetFeatureVariableValueForTypeGivenFeatureFlagIsEnabledForUserAndVariableNotInVariation()
21252125
{
21262126
// should return default value
2127-
2127+
21282128
$decisionServiceMock = $this->getMockBuilder(DecisionService::class)
21292129
->setConstructorArgs(array($this->loggerMock, $this->projectConfig))
21302130
->setMethods(array('getVariationForFeature'))

0 commit comments

Comments
 (0)