diff --git a/src/EvaluationContextConverter.php b/src/EvaluationContextConverter.php index b838f8c..135ae89 100644 --- a/src/EvaluationContextConverter.php +++ b/src/EvaluationContextConverter.php @@ -131,7 +131,7 @@ private function buildSingleContext(array $attributes, string $kind, string $key foreach ($attributes as $k => $v) { // Key has been processed, so we can skip it. - if ($k === "key" || $key === "targetingKey") { + if ($k === "key" || $k === "targetingKey") { continue; } diff --git a/tests/EvaluationContextConverterTest.php b/tests/EvaluationContextConverterTest.php index 281ad38..ad5f157 100644 --- a/tests/EvaluationContextConverterTest.php +++ b/tests/EvaluationContextConverterTest.php @@ -156,6 +156,23 @@ public function testCanCreateMultiKindContext(): void $this->assertEquals("Org name", $orgContext->getName()); } + public function testMultiContextTargetingKeyIsNotAddedAsAnAttribute(): void + { + $attributes = [ + 'kind' => 'multi', + 'user' => ['targetingKey' => 'user-key', 'name' => 'User name'] + ]; + $context = new EvaluationContext(null, new Attributes($attributes)); + + $ldContext = $this->contextConverter->toLdContext($context); + + /** @var \LaunchDarkly\LDContext */ + $userContext = $ldContext->getIndividualContext("user"); + $this->assertEquals("user-key", $userContext->getKey()); + $contextJson = $userContext->jsonSerialize(); + $this->assertArrayNotHasKey("targetingKey", $contextJson); + } + public function testMultiContextDiscardsInvalidSingleKind(): void { $attributes = [