|
| 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 | +} |
0 commit comments