Skip to content

Commit

Permalink
refact: user context is cloned when passed to Decide APIs in Optimize…
Browse files Browse the repository at this point in the history
…ly Class (#224)
  • Loading branch information
ozayr-zaviar committed Jan 22, 2021
1 parent c687e53 commit 06f4bbf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/Optimizely/OptimizelyUserContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,29 @@ public function __construct(Optimizely $optimizelyClient, $userId, array $attrib
$this->attributes = $attributes;
}

protected function copy()
{
return new OptimizelyUserContext($this->optimizelyClient, $this->userId, $this->attributes);
}

public function setAttribute($key, $value)
{
$this->attributes[$key] = $value;
}

public function decide($key, array $options = [])
{
return $this->optimizelyClient->decide($this, $key, $options);
return $this->optimizelyClient->decide($this->copy(), $key, $options);
}

public function decideForKeys(array $keys, array $options = [])
{
return $this->optimizelyClient->decideForKeys($this, $keys, $options);
return $this->optimizelyClient->decideForKeys($this->copy(), $keys, $options);
}

public function decideAll(array $options = [])
{
return $this->optimizelyClient->decideAll($this, $options);
return $this->optimizelyClient->decideAll($this->copy(), $options);
}

public function trackEvent($eventKey, array $eventTags = [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
namespace Optimizely\Tests;

require(dirname(__FILE__).'/TestData.php');

use Exception;
use TypeError;

Expand Down Expand Up @@ -137,6 +135,29 @@ public function testDecideCallsAndReturnsOptimizelyDecideAPI()
);
}

public function testDecideResponseUserContextNotEqualToCalledUserContext()
{
$userId = 'test_user';
$attributes = [ "browser" => "chrome"];

$optlyObject = new Optimizely($this->datafile);

$optUserContext = new OptimizelyUserContext($optlyObject, $userId, $attributes);
$decision = $optUserContext->decide('test_feature', ['DISABLE_DECISION_EVENT', 'ENABLED_FLAGS_ONLY']);

$this->assertEquals(
$optUserContext->getAttributes(),
$decision->getUserContext()->getAttributes()
);

$optUserContext->setAttribute("test_key", "test_value");

$this->assertNotEquals(
$optUserContext->getAttributes(),
$decision->getUserContext()->getAttributes()
);
}

public function testDecideAllCallsAndReturnsOptimizelyDecideAllAPI()
{
$userId = 'test_user';
Expand Down

0 comments on commit 06f4bbf

Please sign in to comment.