Skip to content

Commit 2bf1cba

Browse files
author
elliot
committed
add alias functionality and some related tests
1 parent d03019a commit 2bf1cba

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

src/LaunchDarkly/Impl/EventFactory.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public function newEvalEvent($flag, $user, $detail, $default, $prereqOfFlag = nu
3939
if (($addExperimentData || $this->_withReasons) && $detail->getReason()) {
4040
$e['reason'] = $detail->getReason()->jsonSerialize();
4141
}
42+
if ($user->getAnonymous()) {
43+
$e['contextKind'] = 'anonymousUser';
44+
}
4245
return $e;
4346
}
4447

@@ -63,6 +66,9 @@ public function newDefaultEvent($flag, $user, $detail)
6366
if ($this->_withReasons && $detail->getReason()) {
6467
$e['reason'] = $detail->getReason()->jsonSerialize();
6568
}
69+
if ($user->getAnonymous()) {
70+
$e['contextKind'] = 'anonymousUser';
71+
}
6672
return $e;
6773
}
6874

@@ -80,6 +86,9 @@ public function newUnknownFlagEvent($key, $user, $detail)
8086
if ($this->_withReasons && $detail->getReason()) {
8187
$e['reason'] = $detail->getReason()->jsonSerialize();
8288
}
89+
if ($user->getAnonymous()) {
90+
$e['contextKind'] = 'anonymousUser';
91+
}
8392
return $e;
8493
}
8594

@@ -107,9 +116,35 @@ public function newCustomEvent($eventName, $user, $data, $metricValue)
107116
if (isset($metricValue)) {
108117
$e['metricValue'] = $metricValue;
109118
}
119+
if ($user->getAnonymous()) {
120+
$e['contextKind'] = 'anonymousUser';
121+
}
122+
return $e;
123+
}
124+
125+
public function newAliasEvent($user, $previousUser)
126+
{
127+
$e = array(
128+
'kind' => 'alias',
129+
'key' => strval($user->getKey()),
130+
'contextKind' => static::contextKind($user),
131+
'previousKey' => strval($previousUser->getKey()),
132+
'previousContextKind' => static::contextKind($previousUser),
133+
'creationDate' => Util::currentTimeUnixMillis()
134+
);
135+
110136
return $e;
111137
}
112138

139+
private static function contextKind($user)
140+
{
141+
if ($user->getAnonymous()) {
142+
return 'anonymousUser';
143+
} else {
144+
return 'user';
145+
}
146+
}
147+
113148
private static function isExperiment($flag, $reason)
114149
{
115150
if ($reason) {

src/LaunchDarkly/LDClient.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,12 @@ public function allFlagsState($user, $options = array())
408408
return $state;
409409
}
410410

411+
public function alias($user, $previousUser)
412+
{
413+
$event = $this->_eventFactoryDefault->newAliasEvent($user, $previousUser);
414+
$this->_eventProcessor->enqueue($event);
415+
}
416+
411417
/**
412418
* Generates an HMAC sha256 hash for use in Secure mode.
413419
*

tests/LDClientTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,67 @@ public function testTrackSendsEventWithDataAndMetricValue()
593593
$this->assertEquals($metricValue, $event['metricValue']);
594594
}
595595

596+
public function testAliasEventsAreCorrect()
597+
{
598+
$ep = new MockEventProcessor();
599+
$client = $this->makeClient(array('event_processor' => $ep));
600+
601+
$user_builder = new LDUserBuilder("[email protected]");
602+
$user = $user_builder->anonymous(false)->build();
603+
$anon_builder = new LDUserBuilder("[email protected]");
604+
$anon = $anon_builder->anonymous(true)->build();
605+
606+
$client->alias($user, $anon);
607+
608+
$queue = $ep->getEvents();
609+
$this->assertEquals(1, sizeof($queue));
610+
611+
$event = $queue[0];
612+
613+
$this->assertEquals('alias', $event['kind']);
614+
$this->assertEquals($user->getKey(), $event['key']);
615+
$this->assertEquals('user', $event['contextKind']);
616+
$this->assertEquals($anon->getKey(), $event['previousKey']);
617+
$this->assertEquals('anonymousUser', $event['previousContextKind']);
618+
}
619+
620+
public function testAnonymousVariationHasContextKind()
621+
{
622+
$ep = new MockEventProcessor();
623+
$client = $this->makeClient(array('event_processor' => $ep));
624+
625+
$flag = $this->makeOffFlagWithValue('feature', 'value');
626+
627+
$anon_builder = new LDUserBuilder("[email protected]");
628+
$anon = $anon_builder->anonymous(true)->build();
629+
630+
$client->variation('feature', $anon, 'default');
631+
632+
$queue = $ep->getEvents();
633+
$this->assertEquals(1, sizeof($queue));
634+
635+
$event = $queue[0];
636+
637+
$this->assertEquals('anonymousUser', $event['contextKind']);
638+
}
639+
640+
public function testCustomEventHasContextKind()
641+
{
642+
$ep = new MockEventProcessor();
643+
$client = $this->makeClient(array('event_processor' => $ep));
644+
645+
646+
$anon_builder = new LDUserBuilder("[email protected]");
647+
$anon = $anon_builder->anonymous(true)->build();
648+
649+
$client->track('eventkey', $anon);
650+
$queue = $ep->getEvents();
651+
$this->assertEquals(1, sizeof($queue));
652+
$event = $queue[0];
653+
$this->assertEquals('custom', $event['kind']);
654+
$this->assertEquals('anonymousUser', $event['contextKind']);
655+
}
656+
596657
public function testEventsAreNotPublishedIfSendEventsIsFalse()
597658
{
598659
// In order to do this test, we cannot provide a mock object for Event_Processor_,

0 commit comments

Comments
 (0)