|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Optimizely\BugBash; |
| 4 | + |
| 5 | +require_once '../vendor/autoload.php'; |
| 6 | +require_once '../bug-bash/_bug-bash-autoload.php'; |
| 7 | + |
| 8 | +use Monolog\Logger; |
| 9 | +use Optimizely\Decide\OptimizelyDecideOption; |
| 10 | +use Optimizely\Logger\DefaultLogger; |
| 11 | +use Optimizely\Notification\NotificationType; |
| 12 | +use Optimizely\Optimizely; |
| 13 | +use Optimizely\OptimizelyFactory; |
| 14 | +use Optimizely\OptimizelyUserContext; |
| 15 | + |
| 16 | +// 1. Change this SDK key to your project's SDK Key |
| 17 | +const SDK_KEY = '<your-sdk-key>'; |
| 18 | + |
| 19 | +// 2. Change this to your flag key |
| 20 | +const FLAG_KEY = '<your-flag-key>'; |
| 21 | + |
| 22 | +// 3. Uncomment each scenario 1 by 1 modifying the contents of the method |
| 23 | +// to test additional scenarios. |
| 24 | + |
| 25 | +$test = new DecideTests(); |
| 26 | +$test->verifyDecisionProperties(); |
| 27 | +// $test->testWithVariationsOfDecideOptions(); |
| 28 | +// $test->verifyLogsImpressionsEventsDispatched(); |
| 29 | +// $test->verifyResultsPageInYourProjectShowsImpressionEvent(); |
| 30 | +// $test->verifyDecisionListenerWasCalled(); |
| 31 | +// $test->verifyAnInvalidFlagKeyIsHandledCorrectly(); |
| 32 | + |
| 33 | +// 4. Change the current folder into the bug-bash directory |
| 34 | +// cd bug-bash/ |
| 35 | + |
| 36 | +// 5. Run the following command to execute the uncommented tests above: |
| 37 | +// php Decide.php |
| 38 | + |
| 39 | +// https://docs.developers.optimizely.com/feature-experimentation/docs/decide-methods-php |
| 40 | +class DecideTests |
| 41 | +{ |
| 42 | + // verify decision return properties with default DecideOptions |
| 43 | + public function verifyDecisionProperties(): void |
| 44 | + { |
| 45 | + $decision = $this->userContext->decide(FLAG_KEY); |
| 46 | + |
| 47 | + $this->printDecision($decision, "Check that the following decision properties are expected for user $this->userId"); |
| 48 | + } |
| 49 | + |
| 50 | + // test decide w all options: DISABLE_DECISION_EVENT, ENABLED_FLAGS_ONLY, IGNORE_USER_PROFILE_SERVICE, INCLUDE_REASONS, EXCLUDE_VARIABLES (will need to add variables) |
| 51 | + public function testWithVariationsOfDecideOptions(): void |
| 52 | + { |
| 53 | + $options = [ |
| 54 | + OptimizelyDecideOption::INCLUDE_REASONS, |
| 55 | + // OptimizelyDecideOption::DISABLE_DECISION_EVENT, |
| 56 | + // OptimizelyDecideOption::ENABLED_FLAGS_ONLY, // ⬅️ Disable some of your flags |
| 57 | + // OptimizelyDecideOption::IGNORE_USER_PROFILE_SERVICE, |
| 58 | + // OptimizelyDecideOption::EXCLUDE_VARIABLES, |
| 59 | + ]; |
| 60 | + |
| 61 | + $decision = $this->userContext->decide(FLAG_KEY, $options); |
| 62 | + |
| 63 | + $this->printDecision($decision, 'Modify the OptimizelyDecideOptions and check the decision variables expected'); |
| 64 | + } |
| 65 | + |
| 66 | + // verify in logs that impression event of this decision was dispatched |
| 67 | + public function verifyLogsImpressionsEventsDispatched(): void |
| 68 | + { |
| 69 | + // 💡️ Create a new flag with an A/B Test eg "product_version" |
| 70 | + $featureFlagKey = 'product_version'; |
| 71 | + $logger = new DefaultLogger(Logger::DEBUG); |
| 72 | + $localOptimizelyClient = new Optimizely(datafile: null, logger: $logger, sdkKey: SDK_KEY); |
| 73 | + $localUserContext = $localOptimizelyClient->createUserContext($this->userId); |
| 74 | + |
| 75 | + // review the DEBUG output, ensuring you see an impression log |
| 76 | + // "Dispatching impression event to URL https://logx.optimizely.com/v1/events with params..." |
| 77 | + $localUserContext->decide($featureFlagKey); |
| 78 | + } |
| 79 | + |
| 80 | + // verify on Results page that impression even was created |
| 81 | + public function verifyResultsPageInYourProjectShowsImpressionEvent(): void |
| 82 | + { |
| 83 | + print "Go to your project's results page and verify decisions events are showing (5 min delay)"; |
| 84 | + } |
| 85 | + |
| 86 | + // verify that decision listener contains correct information |
| 87 | + public function verifyDecisionListenerWasCalled(): void |
| 88 | + { |
| 89 | + // Check that this was called during the... |
| 90 | + $onDecision = function ($type, $userId, $attributes, $decisionInfo) { |
| 91 | + print ">>> [$this->outputTag] OnDecision: |
| 92 | + type: $type, |
| 93 | + userId: $userId, |
| 94 | + attributes: " . print_r($attributes, true) . " |
| 95 | + decisionInfo: " . print_r($decisionInfo, true) . "\r\n"; |
| 96 | + }; |
| 97 | + $this->optimizelyClient->notificationCenter->addNotificationListener( |
| 98 | + NotificationType::DECISION, |
| 99 | + $onDecision |
| 100 | + ); |
| 101 | + |
| 102 | + // ...decide. |
| 103 | + $this->userContext->decide(FLAG_KEY); |
| 104 | + } |
| 105 | + |
| 106 | + // verify that invalid flag key is handled correctly |
| 107 | + public function verifyAnInvalidFlagKeyIsHandledCorrectly(): void |
| 108 | + { |
| 109 | + $logger = new DefaultLogger(Logger::ERROR); |
| 110 | + $localOptimizelyClient = new Optimizely(datafile: null, logger: $logger, sdkKey: SDK_KEY); |
| 111 | + $userId = 'user-' . mt_rand(10, 99); |
| 112 | + $localUserContext = $localOptimizelyClient->createUserContext($userId); |
| 113 | + |
| 114 | + // ensure you see an error -- Optimizely.ERROR: FeatureFlag Key "a_key_not_in_the_project" is not in datafile. |
| 115 | + $localUserContext->decide("a_key_not_in_the_project"); |
| 116 | + } |
| 117 | + |
| 118 | + private Optimizely $optimizelyClient; |
| 119 | + private string $userId; |
| 120 | + private ?OptimizelyUserContext $userContext; |
| 121 | + private string $outputTag = "Decide"; |
| 122 | + |
| 123 | + public function __construct() |
| 124 | + { |
| 125 | + $this->optimizelyClient = OptimizelyFactory::createDefaultInstance(SDK_KEY); |
| 126 | + |
| 127 | + $this->userId = 'user-' . mt_rand(10, 99); |
| 128 | + $attributes = ['age' => 25, 'country' => 'canada', 'abandoned_cart' => false]; |
| 129 | + $this->userContext = $this->optimizelyClient->createUserContext($this->userId, $attributes); |
| 130 | + } |
| 131 | + |
| 132 | + private function printDecision($decision, $message): void |
| 133 | + { |
| 134 | + $enabled = $decision->getEnabled() ? "true" : "false"; |
| 135 | + |
| 136 | + print ">>> [$this->outputTag] $message: |
| 137 | + enabled: $enabled, |
| 138 | + flagKey: {$decision->getFlagKey()}, |
| 139 | + ruleKey: {$decision->getRuleKey()}, |
| 140 | + variationKey: {$decision->getVariationKey()}, |
| 141 | + variables: " . print_r($decision->getVariables(), true) . ", |
| 142 | + reasons: " . print_r($decision->getReasons(), true) . "\r\n"; |
| 143 | + } |
| 144 | +} |
0 commit comments