Skip to content

Commit 924a9c1

Browse files
authored
feat: Provider constructor takes LDClient options instead of created client (#14)
1 parent fca8910 commit 924a9c1

File tree

2 files changed

+34
-53
lines changed

2 files changed

+34
-53
lines changed

src/Provider.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,18 @@ class Provider implements OpenFeatureProvider
2626
private LDClient $client;
2727
private EvaluationContextConverter $contextConverter;
2828
private ResolutionDetailsConverter $detailsConverter;
29-
private LoggerInterface $logger;
3029

3130
/**
3231
* Instantiate a new instance of this provider, backed by the provided LDClient instance.
32+
*
33+
* @params string $sdkKey The SDK key to use when connecting to LaunchDarkly.
34+
* @param array<string,mixed> $options These options are passed directly
35+
* to the underlying {@link https://launchdarkly.github.io/php-server-sdk/classes/LaunchDarkly-LDClient.html#method___construct LDClient constructor}.
3336
*/
34-
public function __construct(LDClient $client, ?LoggerInterface $logger = null)
37+
public function __construct(string $sdkKey, array $options = [])
3538
{
36-
$this->logger = $logger ?? new NullLogger();
37-
38-
$this->client = $client;
39-
$this->contextConverter = new EvaluationContextConverter($this->logger);
39+
$this->client = new LDClient($sdkKey, $options);
40+
$this->contextConverter = new EvaluationContextConverter($options['logger'] ?? new NullLogger());
4041
$this->detailsConverter = new ResolutionDetailsConverter();
4142
}
4243

@@ -47,10 +48,18 @@ public function getMetadata(): Metadata
4748

4849
/**
4950
* Sets a logger instance on the object.
51+
*
52+
* NOTE: Changing the logger in this way will affect the logger used by the
53+
* EvaluationContextConverter. However, it will not affect the logger used
54+
* by the underlyling LDClient instance.
55+
*
56+
* If this functionality is important to you, please reach out to your
57+
* LaunchDarkly support contact, or open an issue on the {@link
58+
* https://github.com/launchdarkly/openfeature-php-server GitHub
59+
* repository} for this library.
5060
*/
5161
public function setLogger(LoggerInterface $logger): void
5262
{
53-
$this->logger = $logger;
5463
$this->contextConverter->setLogger($logger);
5564
}
5665

tests/ProviderTest.php

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
namespace LaunchDarkly\Tests;
66

7-
use LaunchDarkly\EvaluationDetail;
87
use LaunchDarkly\EvaluationReason;
9-
use LaunchDarkly\LDClient;
8+
use LaunchDarkly\Integrations;
109
use LaunchDarkly\OpenFeature\Provider;
1110
use OpenFeature\implementation\flags\Attributes;
1211
use OpenFeature\implementation\flags\EvaluationContext;
@@ -20,18 +19,14 @@ class ProviderTest extends TestCase
2019
{
2120
public function testMetadataNameIsSetCorrectly(): void
2221
{
23-
$client = $this->createMock(LDClient::class);
24-
$provider = new Provider($client);
22+
$provider = new Provider('sdk-key');
2523

2624
$this->assertEquals("LaunchDarkly\\OpenFeature", $provider->getMetadata()->getName());
2725
}
2826

2927
public function testNotProvidingContextReturnsError(): void
3028
{
31-
$client = $this->createMock(LDClient::class);
32-
$client->expects($this->never())->method($this->anything());
33-
34-
$provider = new Provider($client);
29+
$provider = new Provider('sdk-key');
3530
$resolutionDetails = $provider->resolveBooleanValue("flag-key", true, null);
3631

3732
$this->assertTrue($resolutionDetails->getValue());
@@ -45,32 +40,21 @@ public function testNotProvidingContextReturnsError(): void
4540

4641
public function testEvaluationResultsAreConvertedToDetails(): void
4742
{
48-
$detail = new EvaluationDetail(true, 1, EvaluationReason::fallthrough());
49-
50-
$client = $this->createMock(LDClient::class);
51-
$client->expects($this->once())
52-
->method('variationDetail')
53-
->willReturn($detail);
43+
$td = new Integrations\TestData();
44+
$td->update($td->flag('flag-key'));
5445

55-
$provider = new Provider($client);
46+
$provider = new Provider('sdk-key', ['feature_requester' => $td]);
5647
$resolutionDetails = $provider->resolveBooleanValue("flag-key", true, new EvaluationContext("user-key"));
5748

5849
$this->assertTrue($resolutionDetails->getValue());
5950
$this->assertEquals(EvaluationReason::FALLTHROUGH, $resolutionDetails->getReason());
60-
$this->assertEquals("1", $resolutionDetails->getVariant());
51+
$this->assertEquals("0", $resolutionDetails->getVariant());
6152
$this->assertNull($resolutionDetails->getError());
6253
}
6354

6455
public function testEvaluationErrorResultsAreConvertedCorrectly(): void
6556
{
66-
$detail = new EvaluationDetail(true, null, EvaluationReason::error(EvaluationReason::CLIENT_NOT_READY_ERROR));
67-
68-
$client = $this->createMock(LDClient::class);
69-
$client->expects($this->once())
70-
->method('variationDetail')
71-
->willReturn($detail);
72-
73-
$provider = new Provider($client);
57+
$provider = new Provider('sdk-key', ['base_uri' => 'http://invalid.host:8080', 'events_uri' => 'http://invalid.host:8080']);
7458
$resolutionDetails = $provider->resolveBooleanValue("flag-key", true, new EvaluationContext("user-key"));
7559

7660
$this->assertTrue($resolutionDetails->getValue());
@@ -79,19 +63,15 @@ public function testEvaluationErrorResultsAreConvertedCorrectly(): void
7963

8064
/** @var ResolutionError */
8165
$error = $resolutionDetails->getError();
82-
$this->assertEquals(ErrorCode::PROVIDER_NOT_READY(), $error->getResolutionErrorCode());
66+
$this->assertEquals(ErrorCode::GENERAL(), $error->getResolutionErrorCode());
8367
}
8468

8569
public function testInvalidTypesGenerateTypeMismatchResults(): void
8670
{
87-
$detail = new EvaluationDetail(true, 1, EvaluationReason::fallthrough());
71+
$td = new Integrations\TestData();
72+
$td->update($td->flag('flag-key'));
8873

89-
$client = $this->createMock(LDClient::class);
90-
$client->expects($this->once())
91-
->method('variationDetail')
92-
->willReturn($detail);
93-
94-
$provider = new Provider($client);
74+
$provider = new Provider('sdk-key', ['feature_requester' => $td]);
9575
$resolutionDetails = $provider->resolveStringValue("flag-key", "default-value", new EvaluationContext("user-key"));
9676

9777
$this->assertEquals("default-value", $resolutionDetails->getValue());
@@ -138,14 +118,10 @@ public function checkMethodAndResultMatchTypeProvider(): array
138118
*/
139119
public function testCheckMethodAndResultMatchType(mixed $defaultValue, mixed $returnValue, mixed $expectedValue, string $methodName): void
140120
{
141-
$detail = new EvaluationDetail($returnValue, 1, EvaluationReason::fallthrough());
142-
143-
$client = $this->createMock(LDClient::class);
144-
$client->expects($this->once())
145-
->method('variationDetail')
146-
->willReturn($detail);
121+
$td = new Integrations\TestData();
122+
$td->update($td->flag('flag-key')->variations($defaultValue, $returnValue)->variationForAll(1));
147123

148-
$provider = new Provider($client);
124+
$provider = new Provider('sdk-key', ['feature_requester' => $td]);
149125
$resolutionDetails = $provider->{$methodName}("flag-key", $defaultValue, new EvaluationContext("user-key"));
150126

151127
$this->assertEquals($expectedValue, $resolutionDetails->getValue());
@@ -158,14 +134,10 @@ public function testLoggerChangesShouldCascadeToEvaluationConverter(): void
158134
->method('warning')
159135
->with($this->equalTo("'kind' was set to non-string value; defaulting to user"));
160136

161-
$detail = new EvaluationDetail(true, 1, EvaluationReason::fallthrough());
162-
163-
$client = $this->createMock(LDClient::class);
164-
$client->expects($this->any())
165-
->method('variationDetail')
166-
->willReturn($detail);
137+
$td = new Integrations\TestData();
138+
$td->update($td->flag('flag-key'));
167139

168-
$provider = new Provider($client, $logger);
140+
$provider = new Provider('sdk-key', ['feature_requester' => $td, 'logger' => $logger]);
169141
$context = new EvaluationContext("user-key", new Attributes(['kind' => false]));
170142
$provider->resolveBooleanValue("flag-key", false, $context);
171143
}

0 commit comments

Comments
 (0)