From c2375d34971a4e712d1557967087fb44a8bae3e0 Mon Sep 17 00:00:00 2001 From: Tommaso Tofacchi Date: Mon, 28 Oct 2024 12:19:00 +0100 Subject: [PATCH] fix: correct merging of contexts with targetingKey Signed-off-by: Tommaso Tofacchi --- .../flags/EvaluationContextMerger.php | 6 +++--- tests/unit/EvaluationContextTest.php | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/implementation/flags/EvaluationContextMerger.php b/src/implementation/flags/EvaluationContextMerger.php index 406424c..512675f 100644 --- a/src/implementation/flags/EvaluationContextMerger.php +++ b/src/implementation/flags/EvaluationContextMerger.php @@ -47,10 +47,10 @@ public static function merge(?EvaluationContextInterface ...$contexts): Evaluati /** @var ?string $newTargetingKey */ $newTargetingKey = null; - if (!is_null($calculatedTargetingKey) && strlen($calculatedTargetingKey) > 0) { - $newTargetingKey = $calculatedTargetingKey; - } elseif (!is_null($additionalTargetingKey) && strlen($additionalTargetingKey) > 0) { + if (!is_null($additionalTargetingKey) && strlen($additionalTargetingKey) > 0) { $newTargetingKey = $additionalTargetingKey; + } elseif (!is_null($calculatedTargetingKey) && strlen($calculatedTargetingKey) > 0) { + $newTargetingKey = $calculatedTargetingKey; } $mergedAttributes = AttributesMerger::merge( diff --git a/tests/unit/EvaluationContextTest.php b/tests/unit/EvaluationContextTest.php index 318a5e5..875e017 100644 --- a/tests/unit/EvaluationContextTest.php +++ b/tests/unit/EvaluationContextTest.php @@ -127,4 +127,17 @@ public function testEvaluationContextMerging(): void $this->assertEquals($expectedEvaluationContextAttributes, $actualEvaluationContextAttributes); } + + public function testEvaluationContextMergingTargetingKey(): void + { + $firstEvaluationContext = new EvaluationContext('default'); + $secondEvaluationContext = new EvaluationContext('merged_key'); + + $expectedEvaluationContextAttributes = 'merged_key'; + + $actualEvaluationContext = EvaluationContext::merge($firstEvaluationContext, $secondEvaluationContext); + $actualEvaluationContextAttributes = $actualEvaluationContext->getTargetingKey(); + + $this->assertEquals($expectedEvaluationContextAttributes, $actualEvaluationContextAttributes); + } }