|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DDTrace\Tests\FeatureFlags; |
| 4 | + |
| 5 | +use DDTrace\Tests\Common\BaseTestCase; |
| 6 | + |
| 7 | +final class EvaluationTest extends BaseTestCase |
| 8 | +{ |
| 9 | + private static $configLoaded = false; |
| 10 | + private static $configFlagKeys = []; |
| 11 | + |
| 12 | + public static function ddSetUpBeforeClass() |
| 13 | + { |
| 14 | + parent::ddSetUpBeforeClass(); |
| 15 | + |
| 16 | + if (!function_exists('dd_trace_internal_fn')) { |
| 17 | + self::markTestSkipped('dd_trace_internal_fn not available (extension not loaded)'); |
| 18 | + } |
| 19 | + |
| 20 | + $configPath = __DIR__ . '/fixtures/config/ufc-config.json'; |
| 21 | + $json = file_get_contents($configPath); |
| 22 | + self::$configLoaded = \dd_trace_internal_fn('ffe_load_config', $json); |
| 23 | + if (!self::$configLoaded) { |
| 24 | + self::fail('Failed to load UFC config from ' . $configPath); |
| 25 | + } |
| 26 | + |
| 27 | + // Track which flags exist in the config so we can distinguish |
| 28 | + // "flag not in config" from "flag exists but no matching allocation" |
| 29 | + $config = json_decode($json, true); |
| 30 | + if (isset($config['flags'])) { |
| 31 | + self::$configFlagKeys = array_keys($config['flags']); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Map variation type string to the integer type ID expected by ffe_evaluate. |
| 37 | + * 0 = string, 1 = integer, 2 = float, 3 = boolean, 4 = object (JSON) |
| 38 | + */ |
| 39 | + private static function variationTypeToId($variationType) |
| 40 | + { |
| 41 | + $map = [ |
| 42 | + 'STRING' => 0, |
| 43 | + 'INTEGER' => 1, |
| 44 | + 'NUMERIC' => 2, |
| 45 | + 'BOOLEAN' => 3, |
| 46 | + 'JSON' => 4, |
| 47 | + ]; |
| 48 | + return isset($map[$variationType]) ? $map[$variationType] : -1; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Build the attributes array for ffe_evaluate from the test case attributes. |
| 53 | + * Only scalar types (string, number, bool) are supported by the FFI bridge. |
| 54 | + */ |
| 55 | + private static function buildAttributes(array $attrs) |
| 56 | + { |
| 57 | + $result = []; |
| 58 | + foreach ($attrs as $key => $value) { |
| 59 | + if (is_string($value) || is_numeric($value) || is_bool($value)) { |
| 60 | + $result[$key] = $value; |
| 61 | + } |
| 62 | + } |
| 63 | + return $result; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Parse the value_json string returned by ffe_evaluate based on the variation type. |
| 68 | + */ |
| 69 | + private static function parseValueJson($valueJson, $variationType) |
| 70 | + { |
| 71 | + switch ($variationType) { |
| 72 | + case 'STRING': |
| 73 | + return json_decode($valueJson, true); |
| 74 | + case 'INTEGER': |
| 75 | + return (int) $valueJson; |
| 76 | + case 'NUMERIC': |
| 77 | + return (float) $valueJson; |
| 78 | + case 'BOOLEAN': |
| 79 | + return $valueJson === 'true'; |
| 80 | + case 'JSON': |
| 81 | + return json_decode($valueJson, true); |
| 82 | + default: |
| 83 | + return json_decode($valueJson, true); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Data provider that scans all evaluation case fixture files and flattens |
| 89 | + * every scenario into a [fileName, caseIndex, caseData] tuple. |
| 90 | + */ |
| 91 | + public function provideEvaluationCases() |
| 92 | + { |
| 93 | + $casesDir = __DIR__ . '/fixtures/evaluation-cases'; |
| 94 | + $files = glob($casesDir . '/*.json'); |
| 95 | + $dataset = []; |
| 96 | + |
| 97 | + foreach ($files as $filePath) { |
| 98 | + $fileName = basename($filePath, '.json'); |
| 99 | + $cases = json_decode(file_get_contents($filePath), true); |
| 100 | + |
| 101 | + foreach ($cases as $index => $case) { |
| 102 | + $label = sprintf('%s#%d (%s)', $fileName, $index, $case['flag']); |
| 103 | + $dataset[$label] = [$fileName, $index, $case]; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return $dataset; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @dataProvider provideEvaluationCases |
| 112 | + */ |
| 113 | + public function testEvaluation($fileName, $caseIndex, $case) |
| 114 | + { |
| 115 | + if (!self::$configLoaded) { |
| 116 | + $this->markTestSkipped('UFC config was not loaded'); |
| 117 | + } |
| 118 | + |
| 119 | + $flagKey = $case['flag']; |
| 120 | + $variationType = $case['variationType']; |
| 121 | + $typeId = self::variationTypeToId($variationType); |
| 122 | + $targetingKey = isset($case['targetingKey']) ? $case['targetingKey'] : ''; |
| 123 | + $attributes = isset($case['attributes']) ? self::buildAttributes($case['attributes']) : []; |
| 124 | + $defaultValue = isset($case['defaultValue']) ? $case['defaultValue'] : null; |
| 125 | + $expectedValue = $case['result']['value']; |
| 126 | + |
| 127 | + // Skip test cases that reference flags not present in the UFC config |
| 128 | + // AND expect a non-default result (these require a different config). |
| 129 | + if (!in_array($flagKey, self::$configFlagKeys) && $expectedValue !== $defaultValue) { |
| 130 | + $this->markTestSkipped( |
| 131 | + sprintf('Flag "%s" not in UFC config and expected non-default value', $flagKey) |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + $result = \dd_trace_internal_fn('ffe_evaluate', $flagKey, $typeId, $targetingKey, $attributes); |
| 136 | + |
| 137 | + $this->assertNotNull( |
| 138 | + $result, |
| 139 | + sprintf('ffe_evaluate returned null for %s#%d', $fileName, $caseIndex) |
| 140 | + ); |
| 141 | + |
| 142 | + $this->assertArrayHasKey('value_json', $result); |
| 143 | + |
| 144 | + $errorCode = isset($result['error_code']) ? (int) $result['error_code'] : 0; |
| 145 | + |
| 146 | + // When the evaluator returns an error, the Provider layer would return |
| 147 | + // the defaultValue. If the expected result equals the defaultValue, |
| 148 | + // verify the evaluator correctly returned an error (no match). |
| 149 | + if ($errorCode !== 0 && $expectedValue === $defaultValue) { |
| 150 | + // Evaluator correctly could not resolve — Provider returns default. |
| 151 | + $this->assertTrue(true); |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + // error_code=0 with reason=1 means DefaultAllocationNull (no matching |
| 156 | + // allocation). Same Provider-level default behavior applies. |
| 157 | + $reason = isset($result['reason']) ? (int) $result['reason'] : -1; |
| 158 | + if ($errorCode === 0 && $reason === 1 && $expectedValue === $defaultValue) { |
| 159 | + $this->assertTrue(true); |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + $actualValue = self::parseValueJson($result['value_json'], $variationType); |
| 164 | + |
| 165 | + if ($variationType === 'NUMERIC') { |
| 166 | + $this->assertEquals( |
| 167 | + $expectedValue, |
| 168 | + $actualValue, |
| 169 | + sprintf('Value mismatch for %s#%d (flag=%s)', $fileName, $caseIndex, $flagKey), |
| 170 | + 1e-10 |
| 171 | + ); |
| 172 | + } else { |
| 173 | + $this->assertSame( |
| 174 | + $expectedValue, |
| 175 | + $actualValue, |
| 176 | + sprintf('Value mismatch for %s#%d (flag=%s): expected %s, got %s', |
| 177 | + $fileName, $caseIndex, $flagKey, |
| 178 | + json_encode($expectedValue), json_encode($actualValue)) |
| 179 | + ); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments