Skip to content

Commit 0dff413

Browse files
authored
Merge pull request #88 from launchdarkly/cc/sc-137833/test-data-flag-builder
Test Data with Flag Builder
2 parents 0b34edc + 2ca4390 commit 0dff413

File tree

4 files changed

+1133
-0
lines changed

4 files changed

+1133
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
namespace LaunchDarkly\Integrations;
4+
5+
use LaunchDarkly\FeatureRequester;
6+
use LaunchDarkly\Impl\Model\FeatureFlag;
7+
use LaunchDarkly\Impl\Model\Segment;
8+
use LaunchDarkly\Integrations\TestData\FlagBuilder;
9+
10+
class TestData implements FeatureRequester
11+
{
12+
/** @var array */
13+
protected $_flagBuilders;
14+
/** @var array */
15+
protected $_currentFlags;
16+
17+
public function __construct()
18+
{
19+
$this->_flagBuilders = [];
20+
$this->_currentFlags = [];
21+
}
22+
23+
/**
24+
* Gets the configuration for a specific feature flag.
25+
*
26+
* @param string $key feature key
27+
* @return FeatureFlag|null The decoded FeatureFlag, or null if missing
28+
*/
29+
public function getFeature(string $key): ?FeatureFlag
30+
{
31+
return $this->_currentFlags[$key] ?? null;
32+
}
33+
34+
/**
35+
* Gets the configuration for a specific user segment.
36+
*
37+
* @param string $key segment key
38+
* @return Segment|null The decoded Segment, or null if missing
39+
*/
40+
public function getSegment(string $key): ?Segment
41+
{
42+
return null;
43+
}
44+
45+
/**
46+
* Gets all feature flags.
47+
*
48+
* @return array<string, FeatureFlag>|null The decoded FeatureFlags, or null if missing
49+
*/
50+
public function getAllFeatures(): ?array
51+
{
52+
return $this->_currentFlags;
53+
}
54+
55+
/**
56+
* Creates a new instance of the test data source
57+
*
58+
* @return TestData a new configurable test data source
59+
*/
60+
public function dataSource(): TestData
61+
{
62+
return new TestData();
63+
}
64+
65+
/**
66+
* Creates or copies a `FlagBuilder` for building a test flag configuration.
67+
*
68+
* If this flag key has already been defined in this `TestData` instance, then the builder
69+
* starts with the same configuration that was last provided for this flag.
70+
*
71+
* Otherwise, it starts with a new default configuration in which the flag has `true` and
72+
* `false` variations, is `true` for all users when targeting is turned on and
73+
* `false` otherwise, and currently has targeting turned on. You can change any of those
74+
* properties, and provide more complex behavior, using the `FlagBuilder` methods.
75+
*
76+
* Once you have set the desired configuration, pass the builder to `update`.
77+
*
78+
* @param string $key the flag key
79+
* @return FlagBuilder the flag configuration builder object
80+
*/
81+
public function flag(string $key): FlagBuilder
82+
{
83+
if (isset($this->_flagBuilders[$key])) {
84+
return $this->_flagBuilders[$key]->copy();
85+
} else {
86+
$flagBuilder = new FlagBuilder($key);
87+
return $flagBuilder->booleanFlag();
88+
}
89+
}
90+
91+
/**
92+
* Updates the test data with the specified flag configuration.
93+
*
94+
* This has the same effect as if a flag were added or modified on the LaunchDarkly dashboard.
95+
* It immediately propagates the flag change to any `LDClient` instance(s) that you have
96+
* already configured to use this `TestData`. If no `LDClient` has been started yet,
97+
* it simply adds this flag to the test data which will be provided to any `LDClient` that
98+
* you subsequently configure.
99+
*
100+
* Any subsequent changes to this `FlagBuilder` instance do not affect the test data,
101+
* unless you call `update(FlagBuilder)` again.
102+
*
103+
* @param FlagBuilder $flagBuilder a flag configuration builder
104+
* @return TestData the same `TestData` instance
105+
*/
106+
public function update(FlagBuilder $flagBuilder): TestData
107+
{
108+
$key = $flagBuilder->getKey();
109+
$oldVersion = 0;
110+
111+
$oldFlag = $this->_currentFlags[$key] ?? null;
112+
if ($oldFlag) {
113+
$oldVersion = $oldFlag['version'];
114+
}
115+
116+
$newFlag = $flagBuilder->build($oldVersion + 1);
117+
$newFeatureFlag = FeatureFlag::decode($newFlag);
118+
$this->_currentFlags[$key] = $newFeatureFlag;
119+
$this->_flagBuilders[$key] = $flagBuilder->copy();
120+
return $this;
121+
}
122+
}

0 commit comments

Comments
 (0)