diff --git a/src/Optimizely/Bucketer.php b/src/Optimizely/Bucketer.php index e6bd0dbe..911ceb95 100644 --- a/src/Optimizely/Bucketer.php +++ b/src/Optimizely/Bucketer.php @@ -197,7 +197,7 @@ public function bucket(ProjectConfigInterface $config, Experiment $experiment, $ list($variationId, $reasons) = $this->findBucket($bucketingId, $userId, $experiment->getId(), $experiment->getTrafficAllocation()); $decideReasons = array_merge($decideReasons, $reasons); if (!empty($variationId)) { - $variation = $config->getVariationFromId($experiment->getKey(), $variationId); + $variation = $config->getVariationFromIdByExperimentId($experiment->getId(), $variationId); return [ $variation, $decideReasons ]; } diff --git a/src/Optimizely/Config/DatafileProjectConfig.php b/src/Optimizely/Config/DatafileProjectConfig.php index a51d6082..5f071b5b 100644 --- a/src/Optimizely/Config/DatafileProjectConfig.php +++ b/src/Optimizely/Config/DatafileProjectConfig.php @@ -1,6 +1,6 @@ _groupIdMap = ConfigParser::generateMap($groups, 'id', Group::class); - $this->_experimentKeyMap = ConfigParser::generateMap($experiments, 'key', Experiment::class); + $this->_experimentIdMap = ConfigParser::generateMap($experiments, 'id', Experiment::class); $this->_eventKeyMap = ConfigParser::generateMap($events, 'key', Event::class); $this->_attributeKeyMap = ConfigParser::generateMap($attributes, 'key', Attribute::class); $typedAudienceIdMap = ConfigParser::generateMap($typedAudiences, 'id', Audience::class); @@ -256,32 +266,38 @@ public function __construct($datafile, $logger, $errorHandler) $this->_featureFlags = ConfigParser::generateMap($featureFlags, null, FeatureFlag::class); foreach (array_values($this->_groupIdMap) as $group) { - $experimentsInGroup = ConfigParser::generateMap($group->getExperiments(), 'key', Experiment::class); + $experimentsInGroup = ConfigParser::generateMap($group->getExperiments(), 'id', Experiment::class); foreach (array_values($experimentsInGroup) as $experiment) { $experiment->setGroupId($group->getId()); $experiment->setGroupPolicy($group->getPolicy()); } - $this->_experimentKeyMap = $this->_experimentKeyMap + $experimentsInGroup; + $this->_experimentIdMap = $this->_experimentIdMap + $experimentsInGroup; } foreach ($this->_rollouts as $rollout) { foreach ($rollout->getExperiments() as $experiment) { - $this->_experimentKeyMap[$experiment->getKey()] = $experiment; + $this->_experimentIdMap[$experiment->getId()] = $experiment; } } $this->_variationKeyMap = []; $this->_variationIdMap = []; - $this->_experimentIdMap = []; + $this->_variationKeyMapByExperimentId = []; + $this->_variationIdMapByExperimentId = []; + $this->_experimentKeyMap = []; - foreach (array_values($this->_experimentKeyMap) as $experiment) { + foreach (array_values($this->_experimentIdMap) as $experiment) { $this->_variationKeyMap[$experiment->getKey()] = []; $this->_variationIdMap[$experiment->getKey()] = []; - $this->_experimentIdMap[$experiment->getId()] = $experiment; + $this->_variationIdMapByExperimentId[$experiment->getId()] = []; + $this->_variationKeyMapByExperimentId[$experiment->getId()] = []; + $this->_experimentKeyMap[$experiment->getKey()] = $experiment; foreach ($experiment->getVariations() as $variation) { $this->_variationKeyMap[$experiment->getKey()][$variation->getKey()] = $variation; $this->_variationIdMap[$experiment->getKey()][$variation->getId()] = $variation; + $this->_variationKeyMapByExperimentId[$experiment->getId()][$variation->getKey()] = $variation; + $this->_variationIdMapByExperimentId[$experiment->getId()][$variation->getId()] = $variation; } } @@ -300,17 +316,23 @@ public function __construct($datafile, $logger, $errorHandler) $rolloutVariationIdMap = []; $rolloutVariationKeyMap = []; + $rolloutVariationIdMapByExperimentId = []; + $rolloutVariationKeyMapByExperimentId = []; foreach ($this->_rollouts as $rollout) { $this->_rolloutIdMap[$rollout->getId()] = $rollout; foreach ($rollout->getExperiments() as $rule) { $rolloutVariationIdMap[$rule->getKey()] = []; $rolloutVariationKeyMap[$rule->getKey()] = []; + $rolloutVariationIdMapByExperimentId[$rule->getId()] = []; + $rolloutVariationKeyMapByExperimentId[$rule->getId()] = []; $variations = $rule->getVariations(); foreach ($variations as $variation) { $rolloutVariationIdMap[$rule->getKey()][$variation->getId()] = $variation; $rolloutVariationKeyMap[$rule->getKey()][$variation->getKey()] = $variation; + $rolloutVariationIdMapByExperimentId[$rule->getId()][$variation->getId()] = $variation; + $rolloutVariationKeyMapByExperimentId[$rule->getId()][$variation->getKey()] = $variation; } } } @@ -318,6 +340,8 @@ public function __construct($datafile, $logger, $errorHandler) // Add variations for rollout experiments to variationIdMap and variationKeyMap $this->_variationIdMap = $this->_variationIdMap + $rolloutVariationIdMap; $this->_variationKeyMap = $this->_variationKeyMap + $rolloutVariationKeyMap; + $this->_variationIdMapByExperimentId = $this->_variationIdMapByExperimentId + $rolloutVariationIdMapByExperimentId; + $this->_variationKeyMapByExperimentId = $this->_variationKeyMapByExperimentId + $rolloutVariationKeyMapByExperimentId; foreach (array_values($this->_featureFlags) as $featureFlag) { $this->_featureKeyMap[$featureFlag->getKey()] = $featureFlag; @@ -655,6 +679,60 @@ public function getVariationFromId($experimentKey, $variationId) return new Variation(); } + /** + * @param $experimentId string ID for experiment. + * @param $variationId string ID for variation. + * + * @return Variation Entity corresponding to the provided experiment ID and variation ID. + * Dummy entity is returned if key or ID is invalid. + */ + public function getVariationFromIdByExperimentId($experimentId, $variationId) + { + if (isset($this->_variationIdMapByExperimentId[$experimentId]) + && isset($this->_variationIdMapByExperimentId[$experimentId][$variationId]) + ) { + return $this->_variationIdMapByExperimentId[$experimentId][$variationId]; + } + + $this->_logger->log( + Logger::ERROR, + sprintf( + 'No variation ID "%s" defined in datafile for experiment "%s".', + $variationId, + $experimentId + ) + ); + $this->_errorHandler->handleError(new InvalidVariationException('Provided variation is not in datafile.')); + return new Variation(); + } + + /** + * @param $experimentId string ID for experiment. + * @param $variationKey string Key for variation. + * + * @return Variation Entity corresponding to the provided experiment ID and variation Key. + * Dummy entity is returned if key or ID is invalid. + */ + public function getVariationFromKeyByExperimentId($experimentId, $variationKey) + { + if (isset($this->_variationKeyMapByExperimentId[$experimentId]) + && isset($this->_variationKeyMapByExperimentId[$experimentId][$variationKey]) + ) { + return $this->_variationKeyMapByExperimentId[$experimentId][$variationKey]; + } + + $this->_logger->log( + Logger::ERROR, + sprintf( + 'No variation Key "%s" defined in datafile for experiment "%s".', + $variationKey, + $experimentId + ) + ); + $this->_errorHandler->handleError(new InvalidVariationException('Provided variation is not in datafile.')); + return new Variation(); + } + /** * Gets the feature variable instance given feature flag key and variable key * diff --git a/src/Optimizely/Config/ProjectConfigInterface.php b/src/Optimizely/Config/ProjectConfigInterface.php index 6255846b..53a6d6c2 100644 --- a/src/Optimizely/Config/ProjectConfigInterface.php +++ b/src/Optimizely/Config/ProjectConfigInterface.php @@ -1,6 +1,6 @@ getForcedVariations(); if (!is_null($forcedVariations) && isset($forcedVariations[$userId])) { $variationKey = $forcedVariations[$userId]; - $variation = $projectConfig->getVariationFromKey($experiment->getKey(), $variationKey); + $variation = $projectConfig->getVariationFromKeyByExperimentId($experiment->getId(), $variationKey); if ($variationKey && !empty($variation->getKey())) { $message = sprintf('User "%s" is forced in variation "%s" of experiment "%s".', $userId, $variationKey, $experiment->getKey()); diff --git a/src/Optimizely/Event/Builder/EventBuilder.php b/src/Optimizely/Event/Builder/EventBuilder.php index 3430ab1f..927bcba5 100644 --- a/src/Optimizely/Event/Builder/EventBuilder.php +++ b/src/Optimizely/Event/Builder/EventBuilder.php @@ -1,6 +1,6 @@ getCommonParams($config, $userId, $attributes); - $experiment = $config->getExperimentFromKey($experimentKey); - $variation = $config->getVariationFromKey($experimentKey, $variationKey); + $experiment = $config->getExperimentFromId($experimentId); + $variation = $config->getVariationFromKeyByExperimentId($experimentId, $variationKey); $impressionParams = $this->getImpressionParams($experiment, $variation, $flagKey, $ruleKey, $ruleType, $enabled); $eventParams[VISITORS][0][SNAPSHOTS][] = $impressionParams; diff --git a/src/Optimizely/Optimizely.php b/src/Optimizely/Optimizely.php index 380f40ca..5dcacbe6 100644 --- a/src/Optimizely/Optimizely.php +++ b/src/Optimizely/Optimizely.php @@ -201,16 +201,17 @@ private function validateUserInputs($attributes, $eventTags = null) } /** - * @param string Experiment key + * @param string Experiment ID * @param string Variation key * @param string User ID * @param array Associative array of user attributes * @param DatafileProjectConfig DatafileProjectConfig instance */ - protected function sendImpressionEvent($config, $experimentKey, $variationKey, $flagKey, $ruleKey, $ruleType, $enabled, $userId, $attributes) + protected function sendImpressionEvent($config, $experimentId, $variationKey, $flagKey, $ruleKey, $ruleType, $enabled, $userId, $attributes) { + $experimentKey = $config->getExperimentFromId($experimentId)->getKey(); $impressionEvent = $this->_eventBuilder - ->createImpressionEvent($config, $experimentKey, $variationKey, $flagKey, $ruleKey, $ruleType, $enabled, $userId, $attributes); + ->createImpressionEvent($config, $experimentId, $variationKey, $flagKey, $ruleKey, $ruleType, $enabled, $userId, $attributes); $this->_logger->log(Logger::INFO, sprintf('Activating user "%s" in experiment "%s".', $userId, $experimentKey)); $this->_logger->log( Logger::DEBUG, @@ -244,10 +245,10 @@ protected function sendImpressionEvent($config, $experimentKey, $variationKey, $ $this->notificationCenter->sendNotifications( NotificationType::ACTIVATE, array( - $config->getExperimentFromKey($experimentKey), + $config->getExperimentFromId($experimentId), $userId, $attributes, - $config->getVariationFromKey($experimentKey, $variationKey), + $config->getVariationFromKeyByExperimentId($experimentId, $variationKey), $impressionEvent ) ); @@ -340,6 +341,7 @@ public function decide(OptimizelyUserContext $userContext, $key, array $decideOp $variationKey = null; $featureEnabled = false; $ruleKey = null; + $experimentId = null; $flagKey = $key; $allVariables = []; $decisionEventDispatched = false; @@ -360,9 +362,11 @@ public function decide(OptimizelyUserContext $userContext, $key, array $decideOp $variationKey = $variation->getKey(); $featureEnabled = $variation->getFeatureEnabled(); $ruleKey = $decision->getExperiment()->getKey(); + $experimentId = $decision->getExperiment()->getId(); } else { $variationKey = null; $ruleKey = null; + $experimentId = null; } // send impression only if decide options do not contain DISABLE_DECISION_EVENT @@ -374,7 +378,7 @@ public function decide(OptimizelyUserContext $userContext, $key, array $decideOp if ($source == FeatureDecision::DECISION_SOURCE_FEATURE_TEST || $sendFlagDecisions) { $this->sendImpressionEvent( $config, - $ruleKey, + $experimentId, $variationKey === null ? '' : $variationKey, $flagKey, $ruleKey === null ? '' : $ruleKey, @@ -531,8 +535,9 @@ public function activate($experimentKey, $userId, $attributes = null) $this->_logger->log(Logger::INFO, sprintf('Not activating user "%s".', $userId)); return null; } + $experimentId = $config->getExperimentFromKey($experimentKey)->getId(); - $this->sendImpressionEvent($config, $experimentKey, $variationKey, '', $experimentKey, FeatureDecision::DECISION_SOURCE_EXPERIMENT, true, $userId, $attributes); + $this->sendImpressionEvent($config, $experimentId, $variationKey, '', $experimentKey, FeatureDecision::DECISION_SOURCE_EXPERIMENT, true, $userId, $attributes); return $variationKey; } @@ -818,11 +823,13 @@ public function isFeatureEnabled($featureFlagKey, $userId, $attributes = null) $featureEnabled = $variation->getFeatureEnabled(); } $ruleKey = $decision->getExperiment() ? $decision->getExperiment()->getKey() : ''; - $this->sendImpressionEvent($config, $ruleKey, $variation ? $variation->getKey() : '', $featureFlagKey, $ruleKey, $decision->getSource(), $featureEnabled, $userId, $attributes); + $experimentId = $decision->getExperiment() ? $decision->getExperiment()->getId() : ''; + $this->sendImpressionEvent($config, $experimentId, $variation ? $variation->getKey() : '', $featureFlagKey, $ruleKey, $decision->getSource(), $featureEnabled, $userId, $attributes); } if ($variation) { $experimentKey = $decision->getExperiment()->getKey(); + $experimentId = $decision->getExperiment()->getId(); $featureEnabled = $variation->getFeatureEnabled(); if ($decision->getSource() == FeatureDecision::DECISION_SOURCE_FEATURE_TEST) { $sourceInfo = (object) array( @@ -830,7 +837,7 @@ public function isFeatureEnabled($featureFlagKey, $userId, $attributes = null) 'variationKey'=> $variation->getKey() ); - $this->sendImpressionEvent($config, $experimentKey, $variation->getKey(), $featureFlagKey, $experimentKey, $decision->getSource(), $featureEnabled, $userId, $attributes); + $this->sendImpressionEvent($config, $experimentId, $variation->getKey(), $featureFlagKey, $experimentKey, $decision->getSource(), $featureEnabled, $userId, $attributes); } else { $this->_logger->log(Logger::INFO, "The user '{$userId}' is not being experimented on Feature Flag '{$featureFlagKey}'."); } diff --git a/tests/ConfigTests/DatafileProjectConfigTest.php b/tests/ConfigTests/DatafileProjectConfigTest.php index 88423a5c..f7bd7938 100644 --- a/tests/ConfigTests/DatafileProjectConfigTest.php +++ b/tests/ConfigTests/DatafileProjectConfigTest.php @@ -146,6 +146,7 @@ public function testInit() '177776' => $this->config->getExperimentFromId('177776'), '177774' => $this->config->getExperimentFromId('177774'), '177779' => $this->config->getExperimentFromId('177779'), + '7716830083' => $this->config->getExperimentFromId('7716830083'), ], $experimentIdMap->getValue($this->config) ); @@ -533,7 +534,7 @@ public function testGetEventValidKey() $event = $this->config->getEvent('purchase'); $this->assertEquals('purchase', $event->getKey()); $this->assertEquals('7718020063', $event->getId()); - $this->assertEquals(['7716830082', '7723330021', '7718750065', '7716830585'], $event->getExperimentIds()); + $this->assertEquals(['7716830082', '7716830083', '7723330021', '7718750065', '7716830585'], $event->getExperimentIds()); } public function testGetEventInvalidKey() @@ -653,6 +654,13 @@ public function testGetVariationFromKeyValidExperimentKeyValidVariationKey() $this->assertEquals('control', $variation->getKey()); } + public function testGetVariationFromKeyValidExperimentIdValidVariationKey() + { + $variation = $this->config->getVariationFromKeyByExperimentId('7716830083', 'control'); + $this->assertEquals('7722370028', $variation->getId()); + $this->assertEquals('control', $variation->getKey()); + } + public function testGetVariationFromKeyValidExperimentKeyInvalidVariationKey() { $this->loggerMock->expects($this->once()) @@ -690,6 +698,13 @@ public function testGetVariationFromIdValidExperimentKeyValidVariationId() $this->assertEquals('7722370027', $variation->getId()); } + public function testGetVariationFromIdValidExperimentIdValidVariationId() + { + $variation = $this->config->getVariationFromIdByExperimentId('7716830083', '7722370028'); + $this->assertEquals('control', $variation->getKey()); + $this->assertEquals('7722370028', $variation->getId()); + } + public function testGetVariationFromIdValidExperimentKeyInvalidVariationId() { $this->loggerMock->expects($this->once()) diff --git a/tests/EventTests/EventBuilderTest.php b/tests/EventTests/EventBuilderTest.php index 4d14fa7c..6bc04efc 100644 --- a/tests/EventTests/EventBuilderTest.php +++ b/tests/EventTests/EventBuilderTest.php @@ -1,6 +1,6 @@ eventBuilder->createImpressionEvent( $this->config, - 'test_experiment', + '7716830082', 'variation', 'test_experiment', 'test_experiment', @@ -202,7 +202,7 @@ public function testCreateImpressionEventWithAttributesNoValue() ]; $logEvent = $this->eventBuilder->createImpressionEvent( $this->config, - 'test_experiment', + '7716830082', 'variation', 'test_experiment', 'test_experiment', @@ -243,7 +243,7 @@ public function testCreateImpressionEventWithFalseAttributesNoValue() ]; $logEvent = $this->eventBuilder->createImpressionEvent( $this->config, - 'test_experiment', + '7716830082', 'variation', 'test_experiment', 'test_experiment', @@ -285,7 +285,7 @@ public function testCreateImpressionEventWithZeroAttributesNoValue() ]; $logEvent = $this->eventBuilder->createImpressionEvent( $this->config, - 'test_experiment', + '7716830082', 'variation', 'test_experiment', 'test_experiment', @@ -317,7 +317,7 @@ public function testCreateImpressionEventWithInvalidAttributesNoValue() ]; $logEvent = $this->eventBuilder->createImpressionEvent( $this->config, - 'test_experiment', + '7716830082', 'variation', 'test_experiment', 'test_experiment', @@ -358,7 +358,7 @@ public function testCreateImpressionEventWithUserAgentWhenBotFilteringIsEnabled( ]; $logEvent = $this->eventBuilder->createImpressionEvent( $this->config, - 'test_experiment', + '7716830082', 'variation', 'test_experiment', 'test_experiment', @@ -406,7 +406,7 @@ public function testCreateImpressionEventWithInvalidAttributeTypes() ]; $logEvent = $this->eventBuilder->createImpressionEvent( $this->config, - 'test_experiment', + '7716830082', 'variation', 'test_experiment', 'test_experiment', @@ -457,7 +457,7 @@ public function testCreateImpressionEventWithUserAgentWhenBotFilteringIsDisabled $logEvent = $this->eventBuilder->createImpressionEvent( $configMock, - 'test_experiment', + '7716830082', 'variation', 'test_experiment', 'test_experiment', @@ -504,7 +504,7 @@ public function testCreateImpressionEventWithUserAgentWhenBotFilteringIsNull() $logEvent = $this->eventBuilder->createImpressionEvent( $configMock, - 'test_experiment', + '7716830082', 'variation', 'test_experiment', 'test_experiment', @@ -834,7 +834,7 @@ public function testCreateImpressionEventWithBucketingIDAttribute() ]; $logEvent = $this->eventBuilder->createImpressionEvent( $this->config, - 'test_experiment', + '7716830082', 'variation', 'test_experiment', 'test_experiment', diff --git a/tests/OptimizelyTest.php b/tests/OptimizelyTest.php index c24f4107..51504308 100644 --- a/tests/OptimizelyTest.php +++ b/tests/OptimizelyTest.php @@ -449,7 +449,7 @@ public function testDecide() ->method('sendImpressionEvent') ->with( $this->projectConfig, - 'test_experiment_double_feature', + '122238', 'control', 'double_single_variable_feature', 'test_experiment_double_feature', @@ -609,7 +609,7 @@ public function testDecidewhenUserIsBucketedIntoFeatureExperiment() ->method('sendImpressionEvent') ->with( $this->projectConfig, - 'test_experiment_double_feature', + '122238', 'control', 'double_single_variable_feature', 'test_experiment_double_feature', @@ -700,7 +700,7 @@ public function testDecidewhenUserIsBucketedIntoRolloutAndSendFlagDecisionIsTrue ->method('sendImpressionEvent') ->with( $this->anything(), - 'test_experiment_double_feature', + '122238', 'control', 'double_single_variable_feature', 'test_experiment_double_feature', @@ -1159,7 +1159,7 @@ public function testDecideRespectsUserProfileServiceLookup() ->method('sendImpressionEvent') ->with( $this->projectConfig, - 'test_experiment_double_feature', + '122238', 'variation', 'double_single_variable_feature', 'test_experiment_double_feature', @@ -1233,7 +1233,7 @@ public function testDecideRespectsUserProfileServiceSave() ->method('sendImpressionEvent') ->with( $this->projectConfig, - 'test_experiment_double_feature', + '122238', 'control', 'double_single_variable_feature', 'test_experiment_double_feature', @@ -1307,7 +1307,7 @@ public function testDecideOptionIgnoreUserProfileService() ->method('sendImpressionEvent') ->with( $this->projectConfig, - 'test_experiment_double_feature', + '122238', 'control', 'double_single_variable_feature', 'test_experiment_double_feature', @@ -1383,7 +1383,7 @@ public function testDecideOptionIgnoreUserProfileServiceWhenPassedInDefaultOptio ->method('sendImpressionEvent') ->with( $this->projectConfig, - 'test_experiment_double_feature', + '122238', 'control', 'double_single_variable_feature', 'test_experiment_double_feature', @@ -1458,7 +1458,7 @@ public function testDecideOptionExcludeVariables() ->method('sendImpressionEvent') ->with( $this->projectConfig, - 'test_experiment_double_feature', + '122238', 'control', 'double_single_variable_feature', 'test_experiment_double_feature', @@ -1546,7 +1546,7 @@ public function testDecideOptionExcludeVariablesWhenPassedInDefaultOptions() ->method('sendImpressionEvent') ->with( $this->projectConfig, - 'test_experiment_double_feature', + '122238', 'control', 'double_single_variable_feature', 'test_experiment_double_feature', @@ -1686,7 +1686,7 @@ public function testDecideOptionIncludeReasons() ->method('sendImpressionEvent') ->with( $this->projectConfig, - 'test_experiment_double_feature', + '122238', 'control', 'double_single_variable_feature', 'test_experiment_double_feature', @@ -2144,7 +2144,7 @@ public function testActivateWithEmptyUserID() // Verify that sendImpressionEvent is called with expected attributes $optimizelyMock->expects($this->exactly(1)) ->method('sendImpressionEvent') - ->with($this->projectConfig, 'test_experiment', 'variation', '', 'test_experiment', 'experiment', true, '', $userAttributes); + ->with($this->projectConfig, '7716830082', 'variation', '', 'test_experiment', 'experiment', true, '', $userAttributes); // Call activate $this->assertEquals('variation', $optimizelyMock->activate('test_experiment', '', $userAttributes)); @@ -2239,7 +2239,7 @@ public function testActivateNoAudienceNoAttributes() // Verify that sendImpression is called with expected params $optimizelyMock->expects($this->exactly(1)) ->method('sendImpressionEvent') - ->with($this->projectConfig, 'group_experiment_1', 'group_exp_1_var_2', '', 'group_experiment_1', 'experiment', true, 'user_1', null); + ->with($this->projectConfig, '7723330021', 'group_exp_1_var_2', '', 'group_experiment_1', 'experiment', true, 'user_1', null); // Call activate $this->assertSame('group_exp_1_var_2', $optimizelyMock->activate('group_experiment_1', 'user_1')); @@ -2272,7 +2272,7 @@ public function testActivateNoAudienceNoAttributesAfterSetForcedVariation() // Verify that sendImpression is called with expected params $optimizelyMock->expects($this->exactly(1)) ->method('sendImpressionEvent') - ->with($this->projectConfig, 'group_experiment_1', 'group_exp_1_var_2', '', 'group_experiment_1', 'experiment', true, 'user_1', null); + ->with($this->projectConfig, '7723330021', 'group_exp_1_var_2', '', 'group_experiment_1', 'experiment', true, 'user_1', null); // set forced variation $this->assertTrue($optimizelyMock->setForcedVariation($experimentKey, $userId, $variationKey), 'Set variation for paused experiment should have failed.'); @@ -2333,7 +2333,7 @@ public function testActivateWithAttributes() // Verify that sendImpressionEvent is called with expected attributes $optimizelyMock->expects($this->exactly(1)) ->method('sendImpressionEvent') - ->with($this->projectConfig, 'test_experiment', 'control', '', 'test_experiment', 'experiment', true, 'test_user', $userAttributes); + ->with($this->projectConfig, '7716830082', 'control', '', 'test_experiment', 'experiment', true, 'test_user', $userAttributes); // Call activate $this->assertEquals('control', $optimizelyMock->activate('test_experiment', 'test_user', $userAttributes)); @@ -2366,7 +2366,7 @@ public function testActivateWithAttributesOfDifferentTypes() // Verify that sendImpressionEvent is called with expected attributes $optimizelyMock->expects($this->exactly(1)) ->method('sendImpressionEvent') - ->with($this->projectConfig, 'test_experiment', 'control', '', 'test_experiment', 'experiment', true, 'test_user', $userAttributes); + ->with($this->projectConfig, '7716830082', 'control', '', 'test_experiment', 'experiment', true, 'test_user', $userAttributes); // Call activate $this->assertEquals('control', $optimizelyMock->activate('test_experiment', 'test_user', $userAttributes)); @@ -2391,7 +2391,7 @@ public function testActivateWithAttributesTypedAudienceMatch() // Verify that sendImpressionEvent is called with expected attributes $optimizelyMock->expects($this->at(0)) ->method('sendImpressionEvent') - ->with($this->projectConfigForTypedAudience, 'typed_audience_experiment', 'A', '', 'typed_audience_experiment', 'experiment', true, 'test_user', $userAttributes); + ->with($this->projectConfigForTypedAudience, '1323241597', 'A', '', 'typed_audience_experiment', 'experiment', true, 'test_user', $userAttributes); // Should be included via exact match string audience with id '3468206642' $this->assertEquals('A', $optimizelyMock->activate('typed_audience_experiment', 'test_user', $userAttributes)); @@ -2403,7 +2403,7 @@ public function testActivateWithAttributesTypedAudienceMatch() // Verify that sendImpressionEvent is called with expected attributes $optimizelyMock->expects($this->at(0)) ->method('sendImpressionEvent') - ->with($this->projectConfigForTypedAudience, 'typed_audience_experiment', 'A', '', 'typed_audience_experiment', 'experiment', true, 'test_user', $userAttributes); + ->with($this->projectConfigForTypedAudience, '1323241597', 'A', '', 'typed_audience_experiment', 'experiment', true, 'test_user', $userAttributes); //Should be included via exact match number audience with id '3468206646' $this->assertEquals('A', $optimizelyMock->activate('typed_audience_experiment', 'test_user', $userAttributes)); @@ -2443,7 +2443,7 @@ public function testActivateWithAttributesComplexAudienceMatch() // Verify that sendImpressionEvent is called once with expected attributes $optimizelyMock->expects($this->exactly(1)) ->method('sendImpressionEvent') - ->with($this->projectConfigForTypedAudience, 'audience_combinations_experiment', 'A', '', 'audience_combinations_experiment', 'experiment', true, 'test_user', $userAttributes); + ->with($this->projectConfigForTypedAudience, '1323241598', 'A', '', 'audience_combinations_experiment', 'experiment', true, 'test_user', $userAttributes); // Should be included via substring match string audience with id '3988293898', and // exact match number audience with id '3468206646' @@ -4306,7 +4306,7 @@ public function testIsFeatureEnabledGivenFeatureExperimentAndFeatureEnabledIsTru // 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, 'user_id', []); + ->with($this->projectConfig, '122238', 'control', 'double_single_variable_feature', 'test_experiment_double_feature', FeatureDecision::DECISION_SOURCE_FEATURE_TEST, true, 'user_id', []); $this->loggerMock->expects($this->at(0)) ->method('log') @@ -4411,7 +4411,7 @@ public function testIsFeatureEnabledGivenFeatureExperimentAndFeatureEnabledIsFal $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, 'user_id', []); + ->with($this->projectConfig, '122238', 'variation', 'double_single_variable_feature', 'test_experiment_double_feature', FeatureDecision::DECISION_SOURCE_FEATURE_TEST, false, 'user_id', []); $this->loggerMock->expects($this->at(0)) ->method('log') @@ -4794,7 +4794,7 @@ public function testIsFeatureEnabledWithEmptyUserID() // 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, '', []); + ->with($this->projectConfig, '122238', 'control', 'double_single_variable_feature', 'test_experiment_double_feature', FeatureDecision::DECISION_SOURCE_FEATURE_TEST, true, '', []); $this->loggerMock->expects($this->at(0)) ->method('log') @@ -6429,7 +6429,7 @@ public function testSendImpressionEventWithNoAttributes() ->method('createImpressionEvent') ->with( $this->projectConfig, - 'group_experiment_1', + '7723330021', 'group_exp_1_var_2', 'group_experiment_1', 'group_experiment_1', @@ -6487,7 +6487,7 @@ public function testSendImpressionEventWithNoAttributes() 'Dispatching impression event to URL logx.optimizely.com/decision with params {"param1":"val1","param2":"val2"}.' ); - $optlyObject->sendImpressionEvent($this->projectConfig, 'group_experiment_1', 'group_exp_1_var_2', 'group_experiment_1', 'group_experiment_1', 'experiment', true, 'user_1', null); + $optlyObject->sendImpressionEvent($this->projectConfig, '7723330021', 'group_exp_1_var_2', 'group_experiment_1', 'group_experiment_1', 'experiment', true, 'user_1', null); } public function testSendImpressionEventDispatchFailure() @@ -6510,7 +6510,7 @@ public function testSendImpressionEventDispatchFailure() ->method('log') ->with(Logger::ERROR, 'Unable to dispatch impression event. Error '); - $optlyObject->sendImpressionEvent($this->projectConfig, 'test_experiment', 'control', 'test_experiment', 'test_experiment', 'experiment', true, 'test_user', []); + $optlyObject->sendImpressionEvent($this->projectConfig, '7716830082', 'control', 'test_experiment', 'test_experiment', 'experiment', true, 'test_user', []); } public function testSendImpressionEventWithAttributes() @@ -6528,7 +6528,7 @@ public function testSendImpressionEventWithAttributes() ->method('createImpressionEvent') ->with( $this->projectConfig, - 'test_experiment', + '7716830082', 'control', 'test_experiment', 'test_experiment', @@ -6576,7 +6576,7 @@ public function testSendImpressionEventWithAttributes() $optlyObject->notificationCenter = $this->notificationCenterMock; - $optlyObject->sendImpressionEvent($this->projectConfig, 'test_experiment', 'control', 'test_experiment', 'test_experiment', 'experiment', true, 'test_user', $userAttributes); + $optlyObject->sendImpressionEvent($this->projectConfig, '7716830082', 'control', 'test_experiment', 'test_experiment', 'experiment', true, 'test_user', $userAttributes); } /* @@ -6752,7 +6752,7 @@ public function testRolloutSendImpressionWhenSendFlagDecisionFlagInDatafile() // Verify that sendImpressionEvent is called with expected attributes $optimizelyMock->expects($this->exactly(1)) ->method('sendImpressionEvent') - ->with($this->anything(), 'rollout_1_exp_1', '177771', 'boolean_single_variable_feature', 'rollout_1_exp_1', 'rollout', true, 'user_id', []); + ->with($this->anything(), '177770', '177771', 'boolean_single_variable_feature', 'rollout_1_exp_1', 'rollout', true, 'user_id', []); $this->assertTrue($optimizelyMock->isFeatureEnabled('boolean_single_variable_feature', 'user_id', [])); } diff --git a/tests/TestData.php b/tests/TestData.php index 9543af48..0d668702 100644 --- a/tests/TestData.php +++ b/tests/TestData.php @@ -31,6 +31,42 @@ 'DATAFILE', '{ "experiments": [ + { + "status": "Running", + "key": "test_experiment", + "layerId": "7719770040", + "trafficAllocation": [ + { + "entityId": "", + "endOfRange": 1500 + }, + { + "entityId": "7722370028", + "endOfRange": 4000 + }, + { + "entityId": "7721010010", + "endOfRange": 8000 + } + ], + "audienceIds": [ + "7718080042" + ], + "variations": [ + { + "id": "7722370028", + "key": "control" + }, + { + "id": "7721010010", + "key": "variation" + } + ], + "forcedVariations": { + "user1": "control" + }, + "id": "7716830083" + }, { "status": "Running", "key": "test_experiment", @@ -575,6 +611,7 @@ { "experimentIds": [ "7716830082", + "7716830083", "7723330021", "7718750065", "7716830585" @@ -590,6 +627,7 @@ { "experimentIds":[ "7716830082", + "7716830083", "122230" ], "id": "7718020065", @@ -1694,9 +1732,9 @@ public function generateBucketValue($bucketingId) */ class OptimizelyTester extends Optimizely { - public function sendImpressionEvent($config, $experimentKey, $variationKey, $flagKey, $ruleKey, $ruleType, $enabled, $userId, $attributes) + public function sendImpressionEvent($config, $experimentId, $variationKey, $flagKey, $ruleKey, $ruleType, $enabled, $userId, $attributes) { - parent::sendImpressionEvent($config, $experimentKey, $variationKey, $flagKey, $ruleKey, $ruleType, $enabled, $userId, $attributes); + parent::sendImpressionEvent($config, $experimentId, $variationKey, $flagKey, $ruleKey, $ruleType, $enabled, $userId, $attributes); } public function validateInputs(array $values, $logLevel = Logger::ERROR)