Skip to content

Commit 30e9e3c

Browse files
committed
use the standard method for specifying custom JSON serialization
1 parent e215364 commit 30e9e3c

File tree

3 files changed

+124
-6
lines changed

3 files changed

+124
-6
lines changed

src/LaunchDarkly/FeatureFlagsState.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
/**
55
* A snapshot of the state of all feature flags with regard to a specific user, generated by
6-
* calling LDClient.allFlagsState().
6+
* calling LDClient.allFlagsState(). Serializing this object to JSON using json_encode(), or
7+
* the jsonSerialize() method, will produce the appropriate data structure for bootstrapping
8+
* the LaunchDarkly JavaScript client.
79
*/
8-
class FeatureFlagsState
10+
class FeatureFlagsState implements \JsonSerializable
911
{
1012
/** @var bool */
1113
protected $_valid = false;
@@ -66,7 +68,7 @@ public function getFlagValue($key)
6668
* value, its value will be null.
6769
* <p>
6870
* Do not use this method if you are passing data to the front end to "bootstrap" the JavaScript client.
69-
* Instead, use toJson().
71+
* Instead, use jsonSerialize().
7072
* @return array an associative array of flag keys to JSON values
7173
*/
7274
public function toValuesMap()
@@ -78,12 +80,16 @@ public function toValuesMap()
7880
* Returns a JSON representation of the entire state map (as an associative array), in the format used
7981
* by the LaunchDarkly JavaScript SDK. Use this method if you are passing data to the front end in
8082
* order to "bootstrap" the JavaScript client.
83+
* <p>
84+
* Note that calling json_encode() on a FeatureFlagsState object will automatically use the
85+
* jsonSerialize() method.
8186
* @return array an associative array suitable for passing as a JSON object
8287
*/
83-
public function toJson()
88+
public function jsonSerialize()
8489
{
8590
$ret = array_replace([], $this->_flagValues);
8691
$ret['$flagsState'] = $this->_flagMetadata;
92+
$ret['$valid'] = $this->_valid;
8793
return $ret;
8894
}
8995
}

tests/FeatureFlagsStateTest.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
namespace LaunchDarkly\Tests;
3+
4+
use InvalidArgumentException;
5+
use LaunchDarkly\EvalResult;
6+
use LaunchDarkly\FeatureFlag;
7+
use LaunchDarkly\FeatureFlagsState;
8+
9+
class FeatureFlagsStateTest extends \PHPUnit_Framework_TestCase
10+
{
11+
private static $flag1Json = array(
12+
'key' => 'key1',
13+
'version' => 100,
14+
'deleted' => false,
15+
'on' => false,
16+
'targets' => array(),
17+
'prerequisites' => array(),
18+
'rules' => array(),
19+
'offVariation' => 0,
20+
'fallthrough' => array('variation' => 0),
21+
'variations' => array('value1'),
22+
'salt' => '',
23+
'trackEvents' => false
24+
);
25+
private static $flag2Json = array(
26+
'key' => 'key2',
27+
'version' => 200,
28+
'deleted' => false,
29+
'on' => false,
30+
'targets' => array(),
31+
'prerequisites' => array(),
32+
'rules' => array(),
33+
'offVariation' => 0,
34+
'fallthrough' => array('variation' => 0),
35+
'variations' => array('value2'),
36+
'salt' => '',
37+
'trackEvents' => true,
38+
'debugEventsUntilDate' => 1000
39+
);
40+
41+
public function testCanGetFlagValue()
42+
{
43+
$flag = FeatureFlag::decode(FeatureFlagsStateTest::$flag1Json);
44+
$state = new FeatureFlagsState(true);
45+
$state->addFlag($flag, new EvalResult(0, 'value1', array()));
46+
47+
$this->assertEquals('value1', $state->getFlagValue('key1'));
48+
}
49+
50+
public function testUnknownFlagReturnsNullValue()
51+
{
52+
$state = new FeatureFlagsState(true);
53+
54+
$this->assertNull($state->getFlagValue('key1'));
55+
}
56+
57+
public function testCanConvertToValuesMap()
58+
{
59+
$flag1 = FeatureFlag::decode(FeatureFlagsStateTest::$flag1Json);
60+
$flag2 = FeatureFlag::decode(FeatureFlagsStateTest::$flag2Json);
61+
$state = new FeatureFlagsState(true);
62+
$state->addFlag($flag1, new EvalResult(0, 'value1', array()));
63+
$state->addFlag($flag2, new EvalResult(0, 'value2', array()));
64+
65+
$expected = array('key1' => 'value1', 'key2' => 'value2');
66+
$this->assertEquals($expected, $state->toValuesMap());
67+
}
68+
69+
public function testCanConvertToJson()
70+
{
71+
$flag1 = FeatureFlag::decode(FeatureFlagsStateTest::$flag1Json);
72+
$flag2 = FeatureFlag::decode(FeatureFlagsStateTest::$flag2Json);
73+
$state = new FeatureFlagsState(true);
74+
$state->addFlag($flag1, new EvalResult(0, 'value1', array()));
75+
$state->addFlag($flag2, new EvalResult(1, 'value2', array()));
76+
77+
$expected = array(
78+
'key1' => 'value1',
79+
'key2' => 'value2',
80+
'$flagsState' => array(
81+
'key1' => array(
82+
'variation' => 0,
83+
'version' => 100,
84+
'trackEvents' => false
85+
),
86+
'key2' => array(
87+
'variation' => 1,
88+
'version' => 200,
89+
'trackEvents' => true,
90+
'debugEventsUntilDate' => 1000
91+
)
92+
),
93+
'$valid' => true
94+
);
95+
$this->assertEquals($expected, $state->jsonSerialize());
96+
}
97+
98+
public function testJsonEncodeUsesCustomSerializer()
99+
{
100+
$flag1 = FeatureFlag::decode(FeatureFlagsStateTest::$flag1Json);
101+
$flag2 = FeatureFlag::decode(FeatureFlagsStateTest::$flag2Json);
102+
$state = new FeatureFlagsState(true);
103+
$state->addFlag($flag1, new EvalResult(0, 'value1', array()));
104+
$state->addFlag($flag2, new EvalResult(1, 'value2', array()));
105+
106+
$expected = $state->jsonSerialize();
107+
$json = json_encode($state);
108+
$decoded = json_decode($json, true);
109+
$this->assertEquals($expected, $decoded);
110+
}
111+
}

tests/LDClientTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,10 @@ public function testAllFlagsStateReturnsState()
158158
'trackEvents' => true,
159159
'debugEventsUntilDate' => 1000
160160
)
161-
)
161+
),
162+
'$valid' => true
162163
);
163-
$this->assertEquals($expectedState, $state->toJson());
164+
$this->assertEquals($expectedState, $state->jsonSerialize());
164165
}
165166

166167
public function testOnlyValidFeatureRequester()

0 commit comments

Comments
 (0)