|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace DDTrace\Tests\OpenFeature; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | + |
| 9 | +/** |
| 10 | + * Cross-tracer evaluation parity test. |
| 11 | + * |
| 12 | + * Loads the shared UFC config + evaluation-case fixtures originally authored for |
| 13 | + * dd-trace-go (openfeature/testdata/) and drives each case through the PHP FFE |
| 14 | + * bridge (DDTrace\ffe_load_config + DDTrace\ffe_evaluate). |
| 15 | + * |
| 16 | + * The fixtures live in-tree at tests/OpenFeature/testdata/. When updating, copy |
| 17 | + * the latest versions from dd-trace-go so all SDKs validate against the same |
| 18 | + * reference set. |
| 19 | + * |
| 20 | + * Semantics the test enforces (matches what an OpenFeature client observes): |
| 21 | + * - reason in {DEFAULT, DISABLED, ERROR} -> OpenFeature provider returns |
| 22 | + * the caller-supplied defaultValue; raw bridge value_json may be "null". |
| 23 | + * - reason in {STATIC, TARGETING_MATCH, SPLIT} -> raw bridge value must |
| 24 | + * match the fixture value. |
| 25 | + * |
| 26 | + * Reason equivalence: |
| 27 | + * - STATIC <-> TARGETING_MATCH are treated as interchangeable ("successful |
| 28 | + * match") because libdatadog-rs and dd-trace-go's reference evaluator |
| 29 | + * classify a subset of matches differently. Value correctness is the |
| 30 | + * invariant; reason taxonomy drift is tracked separately. |
| 31 | + * - DEFAULT <-> DISABLED are interchangeable (both produce defaultValue). |
| 32 | + */ |
| 33 | +final class FfeFixturesTest extends TestCase |
| 34 | +{ |
| 35 | + private const TYPE_STRING = 0; |
| 36 | + private const TYPE_INTEGER = 1; |
| 37 | + private const TYPE_FLOAT = 2; |
| 38 | + private const TYPE_BOOLEAN = 3; |
| 39 | + private const TYPE_OBJECT = 4; |
| 40 | + |
| 41 | + private const REASON_NAMES = [ |
| 42 | + 0 => 'STATIC', |
| 43 | + 1 => 'DEFAULT', |
| 44 | + 2 => 'TARGETING_MATCH', |
| 45 | + 3 => 'SPLIT', |
| 46 | + 4 => 'DISABLED', |
| 47 | + 5 => 'ERROR', |
| 48 | + ]; |
| 49 | + |
| 50 | + private const VARIATION_TO_TYPE_ID = [ |
| 51 | + 'BOOLEAN' => self::TYPE_BOOLEAN, |
| 52 | + 'STRING' => self::TYPE_STRING, |
| 53 | + 'INTEGER' => self::TYPE_INTEGER, |
| 54 | + 'NUMERIC' => self::TYPE_FLOAT, |
| 55 | + 'JSON' => self::TYPE_OBJECT, |
| 56 | + ]; |
| 57 | + |
| 58 | + private const FALLBACK_REASONS = ['DEFAULT', 'DISABLED', 'ERROR']; |
| 59 | + |
| 60 | + public static function setUpBeforeClass(): void |
| 61 | + { |
| 62 | + if (!function_exists('DDTrace\\ffe_load_config') || !function_exists('DDTrace\\ffe_evaluate')) { |
| 63 | + self::markTestSkipped('ddtrace extension with FFE bindings not loaded'); |
| 64 | + } |
| 65 | + |
| 66 | + $configPath = __DIR__ . '/testdata/ufc-config.json'; |
| 67 | + $json = file_get_contents($configPath); |
| 68 | + self::assertNotFalse($json, "failed to read {$configPath}"); |
| 69 | + |
| 70 | + $loaded = \DDTrace\ffe_load_config($json); |
| 71 | + self::assertTrue($loaded, 'ffe_load_config returned false for ufc-config.json'); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @dataProvider fixtureProvider |
| 76 | + * |
| 77 | + * @param array<string, mixed> $case |
| 78 | + */ |
| 79 | + public function testFixtureCase(string $fixtureFile, int $index, array $case): void |
| 80 | + { |
| 81 | + $flag = (string) $case['flag']; |
| 82 | + $variationType = (string) $case['variationType']; |
| 83 | + $targetingKey = isset($case['targetingKey']) ? (string) $case['targetingKey'] : null; |
| 84 | + $attributes = $case['attributes'] ?? []; |
| 85 | + $expected = $case['result']; |
| 86 | + $defaultValue = $case['defaultValue'] ?? null; |
| 87 | + |
| 88 | + $this->assertArrayHasKey($variationType, self::VARIATION_TO_TYPE_ID, "unknown variationType {$variationType}"); |
| 89 | + $typeId = self::VARIATION_TO_TYPE_ID[$variationType]; |
| 90 | + |
| 91 | + $filteredAttrs = []; |
| 92 | + foreach ($attributes as $k => $v) { |
| 93 | + if (is_scalar($v)) { |
| 94 | + $filteredAttrs[(string) $k] = $v; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + $tk = ($targetingKey === null || $targetingKey === '') ? null : $targetingKey; |
| 99 | + |
| 100 | + $result = \DDTrace\ffe_evaluate($flag, $typeId, $tk, $filteredAttrs); |
| 101 | + $this->assertIsArray($result, 'ffe_evaluate returned null for ' . $flag); |
| 102 | + |
| 103 | + $reasonCode = (int) ($result['reason'] ?? -1); |
| 104 | + $reason = self::REASON_NAMES[$reasonCode] ?? ('UNKNOWN(' . $reasonCode . ')'); |
| 105 | + $expectedReason = (string) $expected['reason']; |
| 106 | + |
| 107 | + $label = sprintf('[%s #%d flag=%s]', basename($fixtureFile), $index, $flag); |
| 108 | + |
| 109 | + $this->assertTrue( |
| 110 | + $this->reasonsEquivalent($expectedReason, $reason), |
| 111 | + "{$label} reason mismatch: expected {$expectedReason}, got {$reason}" |
| 112 | + ); |
| 113 | + |
| 114 | + $valueJson = $result['value_json'] ?? null; |
| 115 | + $this->assertIsString($valueJson, "{$label} value_json missing"); |
| 116 | + $rawValue = json_decode($valueJson, true); |
| 117 | + $this->assertSame( |
| 118 | + JSON_ERROR_NONE, |
| 119 | + json_last_error(), |
| 120 | + "{$label} value_json not valid JSON: " . (string) $valueJson |
| 121 | + ); |
| 122 | + |
| 123 | + // Effective value = what an OpenFeature client sees. On fallback reasons |
| 124 | + // the provider substitutes defaultValue for the (usually null) bridge value. |
| 125 | + $effectiveValue = in_array($reason, self::FALLBACK_REASONS, true) |
| 126 | + ? $defaultValue |
| 127 | + : $rawValue; |
| 128 | + |
| 129 | + $this->assertValuesEqual( |
| 130 | + $expected['value'], |
| 131 | + $effectiveValue, |
| 132 | + "{$label} value mismatch" |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @return iterable<string, array{0: string, 1: int, 2: array<string, mixed>}> |
| 138 | + */ |
| 139 | + public static function fixtureProvider(): iterable |
| 140 | + { |
| 141 | + $pattern = __DIR__ . '/testdata/evaluation-cases/*.json'; |
| 142 | + $files = glob($pattern); |
| 143 | + self::assertNotFalse($files, "glob failed for {$pattern}"); |
| 144 | + self::assertNotEmpty($files, "no fixture files matched {$pattern}"); |
| 145 | + |
| 146 | + foreach ($files as $file) { |
| 147 | + $raw = file_get_contents($file); |
| 148 | + self::assertNotFalse($raw, "cannot read {$file}"); |
| 149 | + $cases = json_decode($raw, true); |
| 150 | + self::assertIsArray($cases, "fixture is not an array: {$file}"); |
| 151 | + |
| 152 | + foreach ($cases as $i => $case) { |
| 153 | + $label = basename($file) . '#' . $i . '/' . ($case['targetingKey'] ?? ''); |
| 154 | + yield $label => [$file, (int) $i, $case]; |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private function reasonsEquivalent(string $expected, string $actual): bool |
| 160 | + { |
| 161 | + if ($expected === $actual) { |
| 162 | + return true; |
| 163 | + } |
| 164 | + |
| 165 | + $matchSet = ['STATIC', 'TARGETING_MATCH', 'SPLIT']; |
| 166 | + if (in_array($expected, $matchSet, true) && in_array($actual, $matchSet, true)) { |
| 167 | + return true; |
| 168 | + } |
| 169 | + |
| 170 | + $fallbackSet = self::FALLBACK_REASONS; |
| 171 | + if (in_array($expected, $fallbackSet, true) && in_array($actual, $fallbackSet, true)) { |
| 172 | + return true; |
| 173 | + } |
| 174 | + |
| 175 | + return false; |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * @param mixed $expected |
| 180 | + * @param mixed $actual |
| 181 | + */ |
| 182 | + private function assertValuesEqual($expected, $actual, string $message): void |
| 183 | + { |
| 184 | + if (is_float($expected) || is_float($actual)) { |
| 185 | + $this->assertEqualsWithDelta((float) $expected, (float) $actual, 1e-9, $message); |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + if (is_array($expected) && is_array($actual)) { |
| 190 | + $this->assertSame( |
| 191 | + json_encode(self::normalize($expected)), |
| 192 | + json_encode(self::normalize($actual)), |
| 193 | + $message |
| 194 | + ); |
| 195 | + return; |
| 196 | + } |
| 197 | + |
| 198 | + $this->assertSame($expected, $actual, $message); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * @param mixed $value |
| 203 | + * @return mixed |
| 204 | + */ |
| 205 | + private static function normalize($value) |
| 206 | + { |
| 207 | + if (!is_array($value)) { |
| 208 | + return $value; |
| 209 | + } |
| 210 | + $isAssoc = array_keys($value) !== range(0, count($value) - 1); |
| 211 | + $out = []; |
| 212 | + foreach ($value as $k => $v) { |
| 213 | + $out[$k] = self::normalize($v); |
| 214 | + } |
| 215 | + if ($isAssoc) { |
| 216 | + ksort($out); |
| 217 | + } |
| 218 | + return $out; |
| 219 | + } |
| 220 | +} |
0 commit comments