Skip to content

Commit 63449a1

Browse files
committed
Add tests, targeting rules for new attributes, support for user target.
1 parent 495850b commit 63449a1

File tree

7 files changed

+250
-15
lines changed

7 files changed

+250
-15
lines changed

src/LaunchDarkly/FeatureRep.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public function evaluate($user) {
3030
if (is_null($param)) {
3131
return null;
3232
} else {
33+
foreach ($this->_variations as $variation) {
34+
if ($variation->matchUser($user)) {
35+
return $variation->getValue();
36+
}
37+
}
38+
3339
foreach ($this->_variations as $variation) {
3440
if ($variation->matchTarget($user)) {
3541
return $variation->getValue();

src/LaunchDarkly/LDClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ protected static function _decode($json, $user) {
207207

208208
$ts = empty($v['targets']) ? [] : $v['targets'];
209209
$targets = array_map($makeTarget, $ts);
210-
return new Variation($v['value'], $v['weight'], $targets);
210+
return new Variation($v['value'], $v['weight'], $targets, $v['userTarget']);
211211
};
212212

213213
$vs = empty($json['variations']) ? [] : $json['variations'];

src/LaunchDarkly/LDUser.php

Lines changed: 127 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,74 @@
11
<?php
22
namespace LaunchDarkly;
33

4+
5+
class LDUserBuilder {
6+
protected $_key = null;
7+
protected $_secondary = null;
8+
protected $_ip = null;
9+
protected $_country = null;
10+
protected $_email = null;
11+
protected $_name = null;
12+
protected $_avatar = null;
13+
protected $_firstName = null;
14+
protected $_lastName = null;
15+
protected $_custom = [];
16+
17+
public function __construct($key) {
18+
$this->_key = $key;
19+
}
20+
21+
public function secondary($secondary) {
22+
$this->_secondary = $secondary;
23+
return $this;
24+
}
25+
26+
public function ip($ip) {
27+
$this->_ip = $ip;
28+
return $this;
29+
}
30+
31+
public function country($country) {
32+
$this->_country = $country;
33+
return $this;
34+
}
35+
36+
public function email($email) {
37+
$this->_email = $email;
38+
return $this;
39+
}
40+
41+
public function name($name) {
42+
$this->_name = $name;
43+
return $this;
44+
}
45+
46+
public function avatar($avatar) {
47+
$this->_avatar = $avatar;
48+
return $this;
49+
}
50+
51+
public function firstName($firstName) {
52+
$this->_firstName = $firstName;
53+
return $this;
54+
}
55+
56+
public function lastName($lastName) {
57+
$this->_lastName = $lastName;
58+
return $this;
59+
}
60+
61+
public function custom($custom) {
62+
$this->_custom = $custom;
63+
return $this;
64+
}
65+
66+
public function build() {
67+
return new LDUser($this->_key, $this->_secondary, $this->_ip, $this->_country, $this->_email, $this->_name, $this->_avatar, $this->_firstName, $this->_lastName, $this->_custom);
68+
}
69+
70+
}
71+
472
/**
573
* Contains specific attributes of a user browsing your site. The only mandatory property property is the key,
674
* which must uniquely identify each user. For authenticated users, this may be a username or e-mail address. For anonymous users,
@@ -11,24 +79,39 @@ class LDUser {
1179
protected $_secondary = null;
1280
protected $_ip = null;
1381
protected $_country = null;
82+
protected $_email = null;
83+
protected $_name = null;
84+
protected $_avatar = null;
85+
protected $_firstName = null;
86+
protected $_lastName = null;
1487
protected $_custom = [];
1588

1689
/**
1790
* @param string $key Unique key for the user. For authenticated users, this may be a username or e-mail address. For anonymous users, this could be an IP address or session ID.
1891
* @param string|null $secondary An optional secondary identifier
1992
* @param string|null $ip The user's IP address (optional)
2093
* @param string|null $country The user's country, as an ISO 3166-1 alpha-2 code (e.g. 'US') (optional)
21-
* @param array $custom Other custom attributes that can be used to create custom rules
94+
* @param string|null $email The user's e-mail address (optional)
95+
* @param string|null $name The user's full name (optional)
96+
* @param string|null $avatar A URL pointing to the user's avatar image (optional)
97+
* @param string|null $firstName The user's first name (optional)
98+
* @param string|null $lastName The user's last name (optional)
99+
* @param array|null $custom Other custom attributes that can be used to create custom rules
22100
*/
23-
public function __construct($key, $secondary = null, $ip = null, $country = null, $custom = []) {
101+
public function __construct($key, $secondary = null, $ip = null, $country = null, $email = null, $name = null, $avatar = null, $firstName = null, $lastName= null, $custom = []) {
24102
$this->_key = $key;
25103
$this->_secondary = $secondary;
26104
$this->_ip = $ip;
27105
$this->_country = $country;
106+
$this->_email = $email;
107+
$this->_name = $name;
108+
$this->_avatar = $avatar;
109+
$this->_firstName = $firstName;
110+
$this->_lastName = $lastName;
28111
$this->_custom = $custom;
29112
}
30113

31-
public function getCountryCode() {
114+
public function getCountry() {
32115
return $this->_country;
33116
}
34117

@@ -48,20 +131,55 @@ public function getSecondary() {
48131
return $this->_secondary;
49132
}
50133

134+
public function getEmail() {
135+
return $this->_email;
136+
}
137+
138+
public function getName() {
139+
return $this->_name;
140+
}
141+
142+
public function getAvatar() {
143+
return $this->_avatar;
144+
}
145+
146+
public function getFirstName() {
147+
return $this->_firstName;
148+
}
149+
150+
public function getLastName() {
151+
return $this->_lastName;
152+
}
153+
51154
public function toJSON() {
52155
$json = ["key" => $this->_key];
53156

54157
if (isset($this->_secondary)) {
55158
$json['secondary'] = $this->_secondary;
56159
}
57-
if (isset($this->ip)) {
58-
$json['ip'] = $this->ip;
160+
if (isset($this->_ip)) {
161+
$json['ip'] = $this->_ip;
162+
}
163+
if (isset($this->_country)) {
164+
$json['country'] = $this->_country;
165+
}
166+
if (isset($this->_email)) {
167+
$json['email'] = $this->_email;
168+
}
169+
if (isset($this->_name)) {
170+
$json['name'] = $this->_name;
171+
}
172+
if (isset($this->_avatar)) {
173+
$json['avatar'] = $this->_avatar;
174+
}
175+
if (isset($this->_firstName)) {
176+
$json['firstName'] = $this->_firstName;
59177
}
60-
if (isset($this->country)) {
61-
$json['country'] = $this->country;
178+
if (isset($this->_lastName)) {
179+
$json['lastName'] = $this->_lastName;
62180
}
63-
if (isset($this->custom)) {
64-
$json['custom'] = $this->custom;
181+
if (isset($this->_custom)) {
182+
$json['custom'] = $this->_custom;
65183
}
66184
return $json;
67185
}

src/LaunchDarkly/TargetRule.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ public function matchTarget($user) {
2929
case "country":
3030
$u_value = $user->getCountryCode();
3131
break;
32+
case "email":
33+
$u_value = $user->getEmail();
34+
break;
35+
case "name":
36+
$u_value = $user->getName();
37+
break;
38+
case "avatar":
39+
$u_value = $user->getAvatar();
40+
break;
41+
case "firstName":
42+
$u_value = $user->getFirstName();
43+
break;
44+
case "lastName":
45+
$u_value = $user->getLastName();
46+
break;
3247
default:
3348
$custom = $user->getCustom();
3449
if (is_array($custom)) {

src/LaunchDarkly/Variation.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,29 @@
77
class Variation {
88
protected $_value = null;
99
protected $_weight = 0;
10+
protected $_targetRule = null;
11+
protected $_userTarget = null;
1012
protected $_targets = [];
1113

12-
public function __construct($value, $weight, $targets) {
14+
public function __construct($value, $weight, $targets, $userTarget) {
1315
$this->_value = $value;
1416
$this->_weight = $weight;
1517
$this->_targets = $targets;
18+
$this->_userTarget = $userTarget;
19+
}
20+
21+
public function matchUser($user) {
22+
if ($this->_userTarget != null) {
23+
return $this->_userTarget->matchTarget($user);
24+
}
25+
return false;
1626
}
1727

1828
public function matchTarget($user) {
1929
foreach($this->_targets as $target) {
30+
if ($this->_userTarget != null && $target->_attribute == "key") {
31+
continue;
32+
}
2033
if ($target->matchTarget($user)) {
2134
return true;
2235
}

tests/FeatureRepTest.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace LaunchDarkly\Tests;
33

44
use LaunchDarkly\FeatureRep;
5+
use LaunchDarkly\LDUserBuilder;
56
use LaunchDarkly\LDUser;
67
use LaunchDarkly\TargetRule;
78
use LaunchDarkly\Variation;
@@ -10,19 +11,25 @@ class FeatureRepTest extends \PHPUnit_Framework_TestCase {
1011

1112
protected $_simpleFlag = null;
1213
protected $_disabledFlag = null;
14+
protected $_userTargetFlag = null;
1315

1416
protected function setUp() {
1517
parent::setUp();
1618
$targetUserOn = new TargetRule("key", "in", ["[email protected]"]);
1719
$targetGroupOn = new TargetRule("groups", "in", ["google", "microsoft"]);
1820
$targetUserOff = new TargetRule("key", "in", ["[email protected]"]);
1921
$targetGroupOff = new TargetRule("groups", "in", ["oracle"]);
22+
$targetEmailOn = new TargetRule("email", "in", ["[email protected]"]);
2023

21-
$trueVariation = new Variation(true, 80, [$targetUserOn, $targetGroupOn]);
22-
$falseVariation = new Variation(false, 20, [$targetUserOff, $targetGroupOff]);
24+
$trueVariation = new Variation(true, 80, [$targetUserOn, $targetGroupOn, $targetEmailOn], null);
25+
$falseVariation = new Variation(false, 20, [$targetUserOff, $targetGroupOff], null);
2326

2427
$this->_simpleFlag = new FeatureRep("Sample flag", "sample.flag", "feefifofum", true, [$trueVariation, $falseVariation]);
2528
$this->_disabledFlag = new FeatureRep("Sample flag", "sample.flag", "feefifofum", false, [$trueVariation, $falseVariation]);
29+
30+
$userTargetVariation = new Variation(false, 20, [], $targetUserOn);
31+
32+
$this->_userTargetFlag = new FeatureRep("Sample flag", "sample.flag", "feefifofum", true, [$trueVariation, $userTargetVariation]);
2633
}
2734

2835
protected function tearDown() {
@@ -43,13 +50,14 @@ public function testFlagForTargetedUserOn() {
4350
}
4451

4552
public function testFlagForTargetGroupOn() {
46-
$user = new LDUser("[email protected]", null, null, null, ["groups" => ["google", "microsoft"]]);
53+
// $user = new LDUser("[email protected]", null, null, null, null, null, null, null, null, ["groups" => ["google", "microsoft"]]);
54+
$user = (new LDUserBuilder("[email protected]"))->custom(["groups" => ["google", "microsoft"]])->build();
4755
$b = $this->_simpleFlag->evaluate($user);
4856
$this->assertEquals(true, $b);
4957
}
5058

5159
public function testFlagForTargetGroupOff() {
52-
$user = new LDUser("[email protected]", null, null, null, ["groups" => "oracle"]);
60+
$user = new LDUser("[email protected]", null, null, null, null, null, null, null, null, ["groups" => "oracle"]);
5361
$b = $this->_simpleFlag->evaluate($user);
5462
$this->assertEquals(false, $b);
5563
}
@@ -59,5 +67,17 @@ public function testDisabledFlagAlwaysOff() {
5967
$b = $this->_disabledFlag->evaluate($user);
6068
$this->assertEquals(null, $b);
6169
}
70+
71+
public function testUserRuleFlagForTargetUserOff() {
72+
$user = (new LDUserBuilder("[email protected]"))->build();
73+
$b = $this->_userTargetFlag->evaluate($user);
74+
$this->assertEquals(false, $b);
75+
}
76+
77+
public function testFlagForTargetEmailOff() {
78+
$user = (new LDUserBuilder("[email protected]"))->email("[email protected]")->build();
79+
$b = $this->_simpleFlag->evaluate($user);
80+
$this->assertEquals(true,$b);
81+
}
6282
}
6383

tests/LDUserTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
namespace LaunchDarkly\Tests;
3+
4+
use LaunchDarkly\LDUserBuilder;
5+
use LaunchDarkly\LDUser;
6+
7+
class LDUserTest extends \PHPUnit_Framework_TestCase {
8+
9+
public function testLDUserKey() {
10+
$user = (new LDUserBuilder("[email protected]"))->build();
11+
$this->assertEquals("[email protected]", $user->getKey());
12+
$this->assertEquals("[email protected]", $user->toJSON()['key']);
13+
}
14+
15+
public function testLDUserSecondary() {
16+
$user = (new LDUserBuilder("[email protected]"))->secondary("secondary")->build();
17+
$this->assertEquals("secondary", $user->getSecondary());
18+
$this->assertEquals("secondary", $user->toJSON()['secondary']);
19+
}
20+
21+
public function testLDUserIP() {
22+
$user = (new LDUserBuilder("[email protected]"))->ip("127.0.0.1")->build();
23+
$this->assertEquals("127.0.0.1", $user->getIP());
24+
$this->assertEquals("127.0.0.1", $user->toJSON()['ip']);
25+
}
26+
27+
public function testLDUserCountry() {
28+
$user = (new LDUserBuilder("[email protected]"))->country("US")->build();
29+
$this->assertEquals("US", $user->getCountry());
30+
$this->assertEquals("US", $user->toJSON()['country']);
31+
}
32+
33+
public function testLDUserEmail() {
34+
$user = (new LDUserBuilder("[email protected]"))->email("[email protected]")->build();
35+
$this->assertEquals("[email protected]", $user->getEmail());
36+
$this->assertEquals("[email protected]", $user->toJSON()['email']);
37+
}
38+
39+
public function testLDUserName() {
40+
$user = (new LDUserBuilder("[email protected]"))->name("Foo Bar")->build();
41+
$this->assertEquals("Foo Bar", $user->getName());
42+
$this->assertEquals("Foo Bar", $user->toJSON()['name']);
43+
}
44+
45+
public function testLDUserAvatar() {
46+
$user = (new LDUserBuilder("[email protected]"))->avatar("http://www.gravatar.com/avatar/1")->build();
47+
$this->assertEquals("http://www.gravatar.com/avatar/1", $user->getAvatar());
48+
$this->assertEquals("http://www.gravatar.com/avatar/1", $user->toJSON()['avatar']);
49+
}
50+
51+
public function testLDUserFirstName() {
52+
$user = (new LDUserBuilder("[email protected]"))->firstName("Foo")->build();
53+
$this->assertEquals("Foo", $user->getFirstName());
54+
$this->assertEquals("Foo", $user->toJSON()['firstName']);
55+
}
56+
57+
public function testLDUserLastName() {
58+
$user = (new LDUserBuilder("[email protected]"))->lastName("Bar")->build();
59+
$this->assertEquals("Bar", $user->getLastName());
60+
$this->assertEquals("Bar", $user->toJSON()['lastName']);
61+
}
62+
}
63+

0 commit comments

Comments
 (0)