Skip to content

Commit 06f4bbf

Browse files
authored
refact: user context is cloned when passed to Decide APIs in Optimizely Class (#224)
1 parent c687e53 commit 06f4bbf

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/Optimizely/OptimizelyUserContext.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,29 @@ public function __construct(Optimizely $optimizelyClient, $userId, array $attrib
3131
$this->attributes = $attributes;
3232
}
3333

34+
protected function copy()
35+
{
36+
return new OptimizelyUserContext($this->optimizelyClient, $this->userId, $this->attributes);
37+
}
38+
3439
public function setAttribute($key, $value)
3540
{
3641
$this->attributes[$key] = $value;
3742
}
3843

3944
public function decide($key, array $options = [])
4045
{
41-
return $this->optimizelyClient->decide($this, $key, $options);
46+
return $this->optimizelyClient->decide($this->copy(), $key, $options);
4247
}
4348

4449
public function decideForKeys(array $keys, array $options = [])
4550
{
46-
return $this->optimizelyClient->decideForKeys($this, $keys, $options);
51+
return $this->optimizelyClient->decideForKeys($this->copy(), $keys, $options);
4752
}
4853

4954
public function decideAll(array $options = [])
5055
{
51-
return $this->optimizelyClient->decideAll($this, $options);
56+
return $this->optimizelyClient->decideAll($this->copy(), $options);
5257
}
5358

5459
public function trackEvent($eventKey, array $eventTags = [])

tests/OptimizelyUserContextTests.php renamed to tests/OptimizelyUserContextTest.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
*/
1717
namespace Optimizely\Tests;
1818

19-
require(dirname(__FILE__).'/TestData.php');
20-
2119
use Exception;
2220
use TypeError;
2321

@@ -137,6 +135,29 @@ public function testDecideCallsAndReturnsOptimizelyDecideAPI()
137135
);
138136
}
139137

138+
public function testDecideResponseUserContextNotEqualToCalledUserContext()
139+
{
140+
$userId = 'test_user';
141+
$attributes = [ "browser" => "chrome"];
142+
143+
$optlyObject = new Optimizely($this->datafile);
144+
145+
$optUserContext = new OptimizelyUserContext($optlyObject, $userId, $attributes);
146+
$decision = $optUserContext->decide('test_feature', ['DISABLE_DECISION_EVENT', 'ENABLED_FLAGS_ONLY']);
147+
148+
$this->assertEquals(
149+
$optUserContext->getAttributes(),
150+
$decision->getUserContext()->getAttributes()
151+
);
152+
153+
$optUserContext->setAttribute("test_key", "test_value");
154+
155+
$this->assertNotEquals(
156+
$optUserContext->getAttributes(),
157+
$decision->getUserContext()->getAttributes()
158+
);
159+
}
160+
140161
public function testDecideAllCallsAndReturnsOptimizelyDecideAllAPI()
141162
{
142163
$userId = 'test_user';

0 commit comments

Comments
 (0)