Skip to content

Commit f10470c

Browse files
(test) added resolveObjectValue tests to FlagsmithProvider
Signed-off-by: Chris Lightfoot-Wild <[email protected]>
1 parent 9b8b892 commit f10470c

File tree

3 files changed

+129
-1
lines changed

3 files changed

+129
-1
lines changed

providers/Flagsmith/src/FlagsmithProvider.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use OpenFeature\interfaces\provider\Reason;
2121
use OpenFeature\interfaces\provider\ResolutionDetails;
2222

23+
use function array_is_list;
2324
use function is_array;
2425
use function is_null;
2526
use function is_string;
@@ -79,7 +80,7 @@ public function resolveObjectValue(string $flagKey, mixed $defaultValue, ?Evalua
7980
}
8081

8182
// Valid JSON document might not be an array, so error in this case.
82-
if (!is_array($value)) {
83+
if (!is_array($value) || array_is_list($value)) {
8384
throw new InvalidArgumentException("Flag [$flagKey] value must be a JSON encoded array");
8485
}
8586

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"project": {
3+
"name": "Test project",
4+
"organisation": {
5+
"feature_analytics": false,
6+
"name": "Test Org",
7+
"id": 1,
8+
"persist_trait_data": true,
9+
"stop_serving_flags": false
10+
},
11+
"id": 1,
12+
"hide_disabled_flags": false
13+
},
14+
"segment_overrides": [],
15+
"id": 1,
16+
"feature_states": [
17+
{
18+
"multivariate_feature_state_values": [],
19+
"feature_state_value": "{\"key\":\"value\"}",
20+
"id": 1,
21+
"feature": {
22+
"name": "object_feature",
23+
"type": "STANDARD",
24+
"id": 1
25+
},
26+
"segment_id": null,
27+
"enabled": true
28+
},
29+
{
30+
"multivariate_feature_state_values": [],
31+
"feature_state_value": "some-value",
32+
"id": 1,
33+
"feature": {
34+
"name": "disabled_object_feature",
35+
"type": "STANDARD",
36+
"id": 1
37+
},
38+
"segment_id": null,
39+
"enabled": false
40+
},
41+
{
42+
"multivariate_feature_state_values": [],
43+
"feature_state_value": "[\"foo\",\"bar\"]",
44+
"id": 1,
45+
"feature": {
46+
"name": "invalid_object_feature",
47+
"type": "STANDARD",
48+
"id": 1
49+
},
50+
"segment_id": null,
51+
"enabled": true
52+
}
53+
],
54+
"identity_overrides": [
55+
{
56+
"identifier": "overridden-id",
57+
"identity_uuid": "0f21cde8-63c5-4e50-baca-87897fa6cd01",
58+
"created_date": "2019-08-27T14:53:45.698555Z",
59+
"updated_at": "2023-07-14 16:12:00.000000",
60+
"environment_api_key": "B62qaMZNwfiqT76p38ggrQ",
61+
"identity_features": [
62+
{
63+
"id": 1,
64+
"feature": {
65+
"id": 1,
66+
"name": "some_feature",
67+
"type": "STANDARD"
68+
},
69+
"featurestate_uuid": "1bddb9a5-7e59-42c6-9be9-625fa369749f",
70+
"feature_state_value": "some-overridden-value",
71+
"enabled": false,
72+
"environment": 1,
73+
"identity": null,
74+
"feature_segment": null
75+
}
76+
]
77+
}
78+
]
79+
}

providers/Flagsmith/tests/Unit/FlagsmithProviderTest.php

+48
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,52 @@ public function testFloatResolutionWithMissingFlag(): void
193193
$this->assertEquals(ErrorCode::GENERAL(), $resolutionDetails->getError()?->getResolutionErrorCode());
194194
$this->assertEquals(Reason::ERROR, $resolutionDetails->getReason());
195195
}
196+
197+
public function testObjectResolutionWithEnabledFlag(): void
198+
{
199+
// Given
200+
$provider = $this->buildProvider(__DIR__ . '/../Fixtures/environments/object.json');
201+
202+
// When
203+
$resolutionDetails = $provider->resolveObjectValue('object_feature', ['a' => 'b']);
204+
205+
// Then
206+
$this->assertEquals(['key' => 'value'], $resolutionDetails->getValue());
207+
}
208+
209+
public function testObjectResolutionWithDisabledFlag(): void
210+
{
211+
// Given
212+
$provider = $this->buildProvider(__DIR__ . '/../Fixtures/environments/object.json');
213+
214+
// When
215+
$resolutionDetails = $provider->resolveObjectValue('disabled_object_feature', ['a' => 'b']);
216+
217+
// Then
218+
$this->assertEquals(['a' => 'b'], $resolutionDetails->getValue());
219+
}
220+
221+
public function testObjectResolutionWithMissingFlag(): void
222+
{
223+
// Given
224+
$provider = $this->buildProvider(__DIR__ . '/../Fixtures/environments/object.json');
225+
226+
// When
227+
$resolutionDetails = $provider->resolveObjectValue('missing_object_feature', ['c' => 3, 'p' => 'o']);
228+
229+
// Then
230+
$this->assertEquals(['c' => 3, 'p' => 'o'], $resolutionDetails->getValue());
231+
}
232+
233+
public function testObjectResolutionWithEnabledFlagWithInvalidValue(): void
234+
{
235+
// Given
236+
$provider = $this->buildProvider(__DIR__ . '/../Fixtures/environments/object.json');
237+
238+
// When
239+
$resolutionDetails = $provider->resolveObjectValue('invalid_object_feature', ['default' => 'value']);
240+
241+
// Then
242+
$this->assertEquals(['default' => 'value'], $resolutionDetails->getValue());
243+
}
196244
}

0 commit comments

Comments
 (0)