Skip to content

Commit

Permalink
tests: ignore user profile
Browse files Browse the repository at this point in the history
  • Loading branch information
oakbani committed Dec 14, 2020
1 parent 4bebeef commit af0fbc2
Show file tree
Hide file tree
Showing 2 changed files with 301 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Optimizely/Optimizely.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,8 @@ public function decide(OptimizelyUserContext $userContext, $key, array $decideOp
}
}

$shouldIncludeReasons = in_array(OptimizelyDecideOption::INCLUDE_REASONS, $decideOptions);

// send notification
$this->notificationCenter->sendNotifications(
NotificationType::DECISION,
Expand All @@ -414,14 +416,12 @@ public function decide(OptimizelyUserContext $userContext, $key, array $decideOp
'variables' => $allVariables,
'variation' => $variationKey,
'ruleKey' => $ruleKey,
'reasons' => $decideReasons,
'reasons' => $shouldIncludeReasons ? $decideReasons:[],
'decisionEventDispatched' => $decisionEventDispatched
)
)
);

$shouldIncludeReasons = in_array(OptimizelyDecideOption::INCLUDE_REASONS, $decideOptions);

return new OptimizelyDecision(
$variationKey,
$featureEnabled,
Expand Down
298 changes: 298 additions & 0 deletions tests/OptimizelyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use Optimizely\Logger\DefaultLogger;
use Optimizely\Optimizely;
use Optimizely\OptimizelyUserContext;
use Optimizely\UserProfile\UserProfileServiceInterface;

class OptimizelyTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -1100,6 +1101,303 @@ public function testDecideOptionDisableDecisionEventWhenPassedInDefaultOptions()
$optimizelyDecision = $optimizelyMock->decide($userContext, 'double_single_variable_feature');
}

public function testDecideRespectsUserProfileServiceLookup()
{
$userProfileServiceMock = $this->getMockBuilder(UserProfileServiceInterface::class)
->getMock();

$optimizelyMock = $this->getMockBuilder(Optimizely::class)
->setConstructorArgs(array($this->datafile, null, $this->loggerMock, null, null, $userProfileServiceMock))
->setMethods(array('sendImpressionEvent'))
->getMock();

$userContext = $optimizelyMock->createUserContext('test_user', ['device_type' => 'iPhone']);

// mock userProfileServiceMock to return stored variation 'variation' rather than 'control'.
$storedUserProfile = array(
'user_id' => 'test_user',
'experiment_bucket_map' => array(
'122238' => array(
'variation_id' => '122240'
)
)
);

$userProfileServiceMock->expects($this->once())
->method('lookup')
->willReturn($storedUserProfile);


$userProfileServiceMock->expects($this->never())
->method('save');

//Verify that sendNotifications is called with expected params
$arrayParam = array(
DecisionNotificationTypes::FLAG,
'test_user',
['device_type' => 'iPhone'],
(object) array(
'flagKey'=>'double_single_variable_feature',
'enabled'=> false,
'variables'=> ["double_variable" => 14.99],
'variation' => 'variation',
'ruleKey' => 'test_experiment_double_feature',
'reasons' => [],
'decisionEventDispatched' => true
)
);

$this->notificationCenterMock->expects($this->exactly(1))
->method('sendNotifications')
->with(
NotificationType::DECISION,
$arrayParam
);

//assert that sendImpressionEvent is called with expected params
$optimizelyMock->expects($this->exactly(1))
->method('sendImpressionEvent')
->with(
$this->projectConfig,
'test_experiment_double_feature',
'variation',
'double_single_variable_feature',
'test_experiment_double_feature',
FeatureDecision::DECISION_SOURCE_FEATURE_TEST,
false,
'test_user',
['device_type' => 'iPhone']
);

$optimizelyMock->notificationCenter = $this->notificationCenterMock;

$optimizelyDecision = $optimizelyMock->decide($userContext, 'double_single_variable_feature', []);
}

public function testDecideRespectsUserProfileServiceSave()
{
$userProfileServiceMock = $this->getMockBuilder(UserProfileServiceInterface::class)
->getMock();

$optimizelyMock = $this->getMockBuilder(Optimizely::class)
->setConstructorArgs(array($this->datafile, null, $this->loggerMock, null, null, $userProfileServiceMock))
->setMethods(array('sendImpressionEvent'))
->getMock();

$userContext = $optimizelyMock->createUserContext('test_user', ['device_type' => 'iPhone']);

// mock lookup to return null so that normal bucketing happens
$userProfileServiceMock->expects($this->once())
->method('lookup')
->willReturn(null);


// assert that save is called with expected user profile map.
$storedUserProfile = array(
'user_id' => 'test_user',
'experiment_bucket_map' => array(
'122238' => array(
'variation_id' => '122239'
)
)
);
$userProfileServiceMock->expects($this->once())
->method('save')
->with($storedUserProfile);

//Verify that sendNotifications is called with expected params
$arrayParam = array(
DecisionNotificationTypes::FLAG,
'test_user',
['device_type' => 'iPhone'],
(object) array(
'flagKey'=>'double_single_variable_feature',
'enabled'=> true,
'variables'=> ["double_variable" => 42.42],
'variation' => 'control',
'ruleKey' => 'test_experiment_double_feature',
'reasons' => [],
'decisionEventDispatched' => true
)
);

$this->notificationCenterMock->expects($this->exactly(1))
->method('sendNotifications')
->with(
NotificationType::DECISION,
$arrayParam
);

//assert that sendImpressionEvent is called with expected params
$optimizelyMock->expects($this->exactly(1))
->method('sendImpressionEvent')
->with(
$this->projectConfig,
'test_experiment_double_feature',
'control',
'double_single_variable_feature',
'test_experiment_double_feature',
FeatureDecision::DECISION_SOURCE_FEATURE_TEST,
true,
'test_user',
['device_type' => 'iPhone']
);

$optimizelyMock->notificationCenter = $this->notificationCenterMock;

$optimizelyDecision = $optimizelyMock->decide($userContext, 'double_single_variable_feature', []);
}

public function testDecideOptionIgnoreUserProfileService()
{
$userProfileServiceMock = $this->getMockBuilder(UserProfileServiceInterface::class)
->getMock();

$optimizelyMock = $this->getMockBuilder(Optimizely::class)
->setConstructorArgs(array($this->datafile, null, $this->loggerMock, null, null, $userProfileServiceMock))
->setMethods(array('sendImpressionEvent'))
->getMock();

$userContext = $optimizelyMock->createUserContext('test_user', ['device_type' => 'iPhone']);

// mock userProfileServiceMock to return stored variation 'variation' rather than 'control'.
$storedUserProfile = array(
'user_id' => 'test_user',
'experiment_bucket_map' => array(
'122238' => array(
'variation_id' => '122240'
)
)
);

// assert lookup isn't called.
$userProfileServiceMock->expects($this->never())
->method('lookup');

// assert save isn't called.
$userProfileServiceMock->expects($this->never())
->method('save');


//Verify that sendNotifications is called with expected params
$arrayParam = array(
DecisionNotificationTypes::FLAG,
'test_user',
['device_type' => 'iPhone'],
(object) array(
'flagKey'=>'double_single_variable_feature',
'enabled'=> true,
'variables'=> ["double_variable" => 42.42],
'variation' => 'control',
'ruleKey' => 'test_experiment_double_feature',
'reasons' => [],
'decisionEventDispatched' => true
)
);

$this->notificationCenterMock->expects($this->exactly(1))
->method('sendNotifications')
->with(
NotificationType::DECISION,
$arrayParam
);

//assert that sendImpressionEvent is called with expected params
$optimizelyMock->expects($this->exactly(1))
->method('sendImpressionEvent')
->with(
$this->projectConfig,
'test_experiment_double_feature',
'control',
'double_single_variable_feature',
'test_experiment_double_feature',
FeatureDecision::DECISION_SOURCE_FEATURE_TEST,
true,
'test_user',
['device_type' => 'iPhone']
);

$optimizelyMock->notificationCenter = $this->notificationCenterMock;

$optimizelyDecision = $optimizelyMock->decide($userContext, 'double_single_variable_feature', ['IGNORE_USER_PROFILE_SERVICE']);
}

public function testDecideOptionIgnoreUserProfileServiceWhenPassedInDefaultOptions()
{
$userProfileServiceMock = $this->getMockBuilder(UserProfileServiceInterface::class)
->getMock();

$optimizelyMock = $this->getMockBuilder(Optimizely::class)
->setConstructorArgs(array($this->datafile,
null, $this->loggerMock, null, null, $userProfileServiceMock, null, null, null, ['IGNORE_USER_PROFILE_SERVICE']
))
->setMethods(array('sendImpressionEvent'))
->getMock();

$userContext = $optimizelyMock->createUserContext('test_user', ['device_type' => 'iPhone']);

// mock userProfileServiceMock to return stored variation 'variation' rather than 'control'.
$storedUserProfile = array(
'user_id' => 'test_user',
'experiment_bucket_map' => array(
'122238' => array(
'variation_id' => '122240'
)
)
);

// assert lookup isn't called.
$userProfileServiceMock->expects($this->never())
->method('lookup');

// assert save isn't called.
$userProfileServiceMock->expects($this->never())
->method('save');


//Verify that sendNotifications is called with expected params
$arrayParam = array(
DecisionNotificationTypes::FLAG,
'test_user',
['device_type' => 'iPhone'],
(object) array(
'flagKey'=>'double_single_variable_feature',
'enabled'=> true,
'variables'=> ["double_variable" => 42.42],
'variation' => 'control',
'ruleKey' => 'test_experiment_double_feature',
'reasons' => [],
'decisionEventDispatched' => true
)
);

$this->notificationCenterMock->expects($this->exactly(1))
->method('sendNotifications')
->with(
NotificationType::DECISION,
$arrayParam
);

//assert that sendImpressionEvent is called with expected params
$optimizelyMock->expects($this->exactly(1))
->method('sendImpressionEvent')
->with(
$this->projectConfig,
'test_experiment_double_feature',
'control',
'double_single_variable_feature',
'test_experiment_double_feature',
FeatureDecision::DECISION_SOURCE_FEATURE_TEST,
true,
'test_user',
['device_type' => 'iPhone']
);

$optimizelyMock->notificationCenter = $this->notificationCenterMock;

$optimizelyDecision = $optimizelyMock->decide($userContext, 'double_single_variable_feature', []);
}

public function testDecideOptionExcludeVariables()
{
$optimizelyMock = $this->getMockBuilder(Optimizely::class)
Expand Down

0 comments on commit af0fbc2

Please sign in to comment.