Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Optimizely/Utils/ConfigParser.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016, Optimizely
* Copyright 2016-2017, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,7 +36,9 @@ public static function generateMap($entities, $entityId, $entityClass)
forEach ($entity as $key => $value)
{
$propSetter = 'set'.ucfirst($key);
$entityObject->$propSetter($value);
if (method_exists($entityObject, $propSetter)) {
$entityObject->$propSetter($value);
}
}

if (is_null($entityId)) {
Expand Down
240 changes: 239 additions & 1 deletion tests/ProjectConfigTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016, Optimizely
* Copyright 2016-2017, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -171,6 +171,244 @@ public function testInit()
], $variationIdMap->getValue($this->config));
}

public function testInitWithDatafileV3()
{
// Init with v3 datafile
$this->config = new ProjectConfig(DATAFILE_V3, $this->loggerMock, $this->errorHandlerMock);

// Check version
$version = new \ReflectionProperty(ProjectConfig::class, '_version');
$version->setAccessible(true);
$this->assertEquals('2', $version->getValue($this->config));

// Check account ID
$accountId = new \ReflectionProperty(ProjectConfig::class, '_accountId');
$accountId->setAccessible(true);
$this->assertEquals('1592310167', $accountId->getValue($this->config));

// Check project ID
$projectId = new \ReflectionProperty(ProjectConfig::class, '_projectId');
$projectId->setAccessible(true);
$this->assertEquals('7720880029', $projectId->getValue($this->config));

// Check revision
$revision = new \ReflectionProperty(ProjectConfig::class, '_revision');
$revision->setAccessible(true);
$this->assertEquals('15', $revision->getValue($this->config));

// Check group ID map
$groupIdMap = new \ReflectionProperty(ProjectConfig::class, '_groupIdMap');
$groupIdMap->setAccessible(true);
$this->assertEquals([
'7722400015' => $this->config->getGroup('7722400015')
], $groupIdMap->getValue($this->config));

// Check experiment key map
$experimentKeyMap = new \ReflectionProperty(ProjectConfig::class, '_experimentKeyMap');
$experimentKeyMap->setAccessible(true);
$this->assertEquals([
'test_experiment' => $this->config->getExperimentFromKey('test_experiment'),
'paused_experiment' => $this->config->getExperimentFromKey('paused_experiment'),
'group_experiment_1' => $this->config->getExperimentFromKey('group_experiment_1'),
'group_experiment_2' => $this->config->getExperimentFromKey('group_experiment_2')
], $experimentKeyMap->getValue($this->config));

// Check experiment ID map
$experimentIdMap = new \ReflectionProperty(ProjectConfig::class, '_experimentIdMap');
$experimentIdMap->setAccessible(true);
$this->assertEquals([
'7716830082' => $this->config->getExperimentFromId('7716830082'),
'7723330021' => $this->config->getExperimentFromId('7723330021'),
'7718750065' => $this->config->getExperimentFromId('7718750065'),
'7716830585' => $this->config->getExperimentFromId('7716830585')
], $experimentIdMap->getValue($this->config));

// Check event key map
$eventKeyMap = new \ReflectionProperty(ProjectConfig::class, '_eventKeyMap');
$eventKeyMap->setAccessible(true);
$this->assertEquals([
'purchase' => $this->config->getEvent('purchase')
], $eventKeyMap->getValue($this->config));

// Check attribute key map
$attributeKeyMap = new \ReflectionProperty(ProjectConfig::class, '_attributeKeyMap');
$attributeKeyMap->setAccessible(true);
$this->assertEquals([
'device_type' => $this->config->getAttribute('device_type'),
'location' => $this->config->getAttribute('location')
], $attributeKeyMap->getValue($this->config));

// Check audience ID map
$audienceIdMap = new \ReflectionProperty(ProjectConfig::class, '_audienceIdMap');
$audienceIdMap->setAccessible(true);
$this->assertEquals([
'7718080042' => $this->config->getAudience('7718080042')
], $audienceIdMap->getValue($this->config));

// Check variation key map
$variationKeyMap = new \ReflectionProperty(ProjectConfig::class, '_variationKeyMap');
$variationKeyMap->setAccessible(true);
$this->assertEquals([
'test_experiment' => [
'control' => $this->config->getVariationFromKey('test_experiment', 'control'),
'variation' => $this->config->getVariationFromKey('test_experiment', 'variation')
],
'paused_experiment' => [
'control' => $this->config->getVariationFromKey('paused_experiment', 'control'),
'variation' => $this->config->getVariationFromKey('paused_experiment', 'variation')
],
'group_experiment_1' => [
'group_exp_1_var_1' => $this->config->getVariationFromKey('group_experiment_1', 'group_exp_1_var_1'),
'group_exp_1_var_2' => $this->config->getVariationFromKey('group_experiment_1', 'group_exp_1_var_2')
],
'group_experiment_2' => [
'group_exp_2_var_1' => $this->config->getVariationFromKey('group_experiment_2', 'group_exp_2_var_1'),
'group_exp_2_var_2' => $this->config->getVariationFromKey('group_experiment_2', 'group_exp_2_var_2')
]
], $variationKeyMap->getValue($this->config));

// Check variation ID map
$variationIdMap = new \ReflectionProperty(ProjectConfig::class, '_variationIdMap');
$variationIdMap->setAccessible(true);
$this->assertEquals([
'test_experiment' => [
'7722370027' => $this->config->getVariationFromId('test_experiment', '7722370027'),
'7721010009' => $this->config->getVariationFromId('test_experiment', '7721010009')
],
'paused_experiment' => [
'7722370427' => $this->config->getVariationFromId('paused_experiment', '7722370427'),
'7721010509' => $this->config->getVariationFromId('paused_experiment', '7721010509')
],
'group_experiment_1' => [
'7722260071' => $this->config->getVariationFromId('group_experiment_1', '7722260071'),
'7722360022' => $this->config->getVariationFromId('group_experiment_1', '7722360022')
],
'group_experiment_2' => [
'7713030086' => $this->config->getVariationFromId('group_experiment_2', '7713030086'),
'7725250007' => $this->config->getVariationFromId('group_experiment_2', '7725250007')
]
], $variationIdMap->getValue($this->config));
}

public function testInitWithMoreData()
{
// Init with datafile consisting of more fields
$this->config = new ProjectConfig(DATAFILE_MORE_DATA, $this->loggerMock, $this->errorHandlerMock);

// Check version
$version = new \ReflectionProperty(ProjectConfig::class, '_version');
$version->setAccessible(true);
$this->assertEquals('2', $version->getValue($this->config));

// Check account ID
$accountId = new \ReflectionProperty(ProjectConfig::class, '_accountId');
$accountId->setAccessible(true);
$this->assertEquals('1592310167', $accountId->getValue($this->config));

// Check project ID
$projectId = new \ReflectionProperty(ProjectConfig::class, '_projectId');
$projectId->setAccessible(true);
$this->assertEquals('7720880029', $projectId->getValue($this->config));

// Check revision
$revision = new \ReflectionProperty(ProjectConfig::class, '_revision');
$revision->setAccessible(true);
$this->assertEquals('15', $revision->getValue($this->config));

// Check group ID map
$groupIdMap = new \ReflectionProperty(ProjectConfig::class, '_groupIdMap');
$groupIdMap->setAccessible(true);
$this->assertEquals([
'7722400015' => $this->config->getGroup('7722400015')
], $groupIdMap->getValue($this->config));

// Check experiment key map
$experimentKeyMap = new \ReflectionProperty(ProjectConfig::class, '_experimentKeyMap');
$experimentKeyMap->setAccessible(true);
$this->assertEquals([
'test_experiment' => $this->config->getExperimentFromKey('test_experiment'),
'paused_experiment' => $this->config->getExperimentFromKey('paused_experiment'),
'group_experiment_1' => $this->config->getExperimentFromKey('group_experiment_1'),
'group_experiment_2' => $this->config->getExperimentFromKey('group_experiment_2')
], $experimentKeyMap->getValue($this->config));

// Check experiment ID map
$experimentIdMap = new \ReflectionProperty(ProjectConfig::class, '_experimentIdMap');
$experimentIdMap->setAccessible(true);
$this->assertEquals([
'7716830082' => $this->config->getExperimentFromId('7716830082'),
'7723330021' => $this->config->getExperimentFromId('7723330021'),
'7718750065' => $this->config->getExperimentFromId('7718750065'),
'7716830585' => $this->config->getExperimentFromId('7716830585')
], $experimentIdMap->getValue($this->config));

// Check event key map
$eventKeyMap = new \ReflectionProperty(ProjectConfig::class, '_eventKeyMap');
$eventKeyMap->setAccessible(true);
$this->assertEquals([
'purchase' => $this->config->getEvent('purchase')
], $eventKeyMap->getValue($this->config));

// Check attribute key map
$attributeKeyMap = new \ReflectionProperty(ProjectConfig::class, '_attributeKeyMap');
$attributeKeyMap->setAccessible(true);
$this->assertEquals([
'device_type' => $this->config->getAttribute('device_type'),
'location' => $this->config->getAttribute('location')
], $attributeKeyMap->getValue($this->config));

// Check audience ID map
$audienceIdMap = new \ReflectionProperty(ProjectConfig::class, '_audienceIdMap');
$audienceIdMap->setAccessible(true);
$this->assertEquals([
'7718080042' => $this->config->getAudience('7718080042')
], $audienceIdMap->getValue($this->config));

// Check variation key map
$variationKeyMap = new \ReflectionProperty(ProjectConfig::class, '_variationKeyMap');
$variationKeyMap->setAccessible(true);
$this->assertEquals([
'test_experiment' => [
'control' => $this->config->getVariationFromKey('test_experiment', 'control'),
'variation' => $this->config->getVariationFromKey('test_experiment', 'variation')
],
'paused_experiment' => [
'control' => $this->config->getVariationFromKey('paused_experiment', 'control'),
'variation' => $this->config->getVariationFromKey('paused_experiment', 'variation')
],
'group_experiment_1' => [
'group_exp_1_var_1' => $this->config->getVariationFromKey('group_experiment_1', 'group_exp_1_var_1'),
'group_exp_1_var_2' => $this->config->getVariationFromKey('group_experiment_1', 'group_exp_1_var_2')
],
'group_experiment_2' => [
'group_exp_2_var_1' => $this->config->getVariationFromKey('group_experiment_2', 'group_exp_2_var_1'),
'group_exp_2_var_2' => $this->config->getVariationFromKey('group_experiment_2', 'group_exp_2_var_2')
]
], $variationKeyMap->getValue($this->config));

// Check variation ID map
$variationIdMap = new \ReflectionProperty(ProjectConfig::class, '_variationIdMap');
$variationIdMap->setAccessible(true);
$this->assertEquals([
'test_experiment' => [
'7722370027' => $this->config->getVariationFromId('test_experiment', '7722370027'),
'7721010009' => $this->config->getVariationFromId('test_experiment', '7721010009')
],
'paused_experiment' => [
'7722370427' => $this->config->getVariationFromId('paused_experiment', '7722370427'),
'7721010509' => $this->config->getVariationFromId('paused_experiment', '7721010509')
],
'group_experiment_1' => [
'7722260071' => $this->config->getVariationFromId('group_experiment_1', '7722260071'),
'7722360022' => $this->config->getVariationFromId('group_experiment_1', '7722360022')
],
'group_experiment_2' => [
'7713030086' => $this->config->getVariationFromId('group_experiment_2', '7713030086'),
'7725250007' => $this->config->getVariationFromId('group_experiment_2', '7725250007')
]
], $variationIdMap->getValue($this->config));
}

public function testGetAccountId()
{
$this->assertEquals('1592310167', $this->config->getAccountId());
Expand Down
39 changes: 38 additions & 1 deletion tests/TestData.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016, Optimizely
* Copyright 2016-2017, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,6 +39,43 @@
"events": [{"experimentIds": ["7716830082", "7723330021", "7718750065", "7716830585"], "id": "7718020063", "key": "purchase"}],
"revision": "15"}');

define('DATAFILE_V3',
'{"experiments": [{"status": "Running", "key": "test_experiment", "layerId": "7719770039",
"trafficAllocation": [{"entityId": "", "endOfRange": 1500}, {"entityId": "7722370027", "endOfRange": 4000},
{"entityId": "7721010009", "endOfRange": 8000}], "audienceIds": ["7718080042"],
"variations": [{"id": "7722370027", "key": "control", "variables": [{"id": "8284765437", "value": "true"}]}, {"id": "7721010009", "key": "variation", "variables": [{"id": "8284765437", "value": "false"}]}],
"forcedVariations": {"user1": "control"}, "id": "7716830082"}, {"status": "Paused", "key": "paused_experiment", "layerId": "7719779139",
"trafficAllocation": [{"entityId": "7722370427", "endOfRange": 5000},
{"entityId": "7721010509", "endOfRange": 8000}], "audienceIds": [],
"variations": [{"id": "7722370427", "key": "control", "variables": []}, {"id": "7721010509", "key": "variation", "variables": []}],
"forcedVariations": {}, "id": "7716830585"}], "version": "2",
"audiences": [{"conditions": "[\"and\", [\"or\", [\"or\", {\"name\": \"device_type\", \"type\": \"custom_attribute\", \"value\": \"iPhone\"}]], [\"or\", [\"or\", {\"name\": \"location\", \"type\": \"custom_attribute\", \"value\": \"San Francisco\"}]]]", "id": "7718080042", "name": "iPhone users in San Francisco"}],
"groups": [{"policy": "random", "trafficAllocation": [{"entityId": "", "endOfRange": 500}, {"entityId": "7723330021", "endOfRange": 2000}, {"entityId": "7718750065", "endOfRange": 6000}], "experiments": [{"status": "Running", "key": "group_experiment_1", "layerId": "7721010011", "trafficAllocation": [{"entityId": "7722260071", "endOfRange": 5000}, {"entityId": "7722360022", "endOfRange": 10000}], "audienceIds": [], "variations": [{"id": "7722260071", "key": "group_exp_1_var_1", "variables": []}, {"id": "7722360022", "key": "group_exp_1_var_2", "variables": []}], "forcedVariations": {"user1": "group_exp_1_var_1"}, "id": "7723330021"}, {"status": "Running", "key": "group_experiment_2", "layerId": "7721020020", "trafficAllocation": [{"entityId": "7713030086", "endOfRange": 5000}, {"entityId": "7725250007", "endOfRange": 10000}], "audienceIds": [],
"variations": [{"id": "7713030086", "key": "group_exp_2_var_1", "variables": []}, {"id": "7725250007", "key": "group_exp_2_var_2", "variables": []}], "forcedVariations": {}, "id": "7718750065"}], "id": "7722400015"}],
"attributes": [{"id": "7723280020", "key": "device_type"}, {"id": "7723340004", "key": "location"}],
"projectId": "7720880029", "accountId": "1592310167",
"events": [{"experimentIds": ["7716830082", "7723330021", "7718750065", "7716830585"], "id": "7718020063", "key": "purchase"}],
"anonymizeIP": false, "variables": [{"defaultValue": "true", "type": "boolean", "id": "8284765437", "key": "is_working"}],
"revision": "15"}');

define('DATAFILE_MORE_DATA',
'{"experiments": [{"status": "Running", "key": "test_experiment", "layerId": "7719770039",
"trafficAllocation": [{"entityId": "", "endOfRange": 1500}, {"entityId": "7722370027", "endOfRange": 4000},
{"entityId": "7721010009", "endOfRange": 8000}], "audienceIds": ["7718080042"],
"variations": [{"id": "7722370027", "key": "control"}, {"id": "7721010009", "key": "variation"}],
"forcedVariations": {"user1": "control"}, "id": "7716830082"}, {"status": "Paused", "key": "paused_experiment", "layerId": "7719779139",
"trafficAllocation": [{"entityId": "7722370427", "endOfRange": 5000},
{"entityId": "7721010509", "endOfRange": 8000}], "audienceIds": [],
"variations": [{"id": "7722370427", "key": "control"}, {"id": "7721010509", "key": "variation", "some_additiona_key": "some_additional_value"}],
"forcedVariations": {}, "id": "7716830585"}], "version": "2",
"audiences": [{"conditions": "[\"and\", [\"or\", [\"or\", {\"name\": \"device_type\", \"type\": \"custom_attribute\", \"value\": \"iPhone\"}]], [\"or\", [\"or\", {\"name\": \"location\", \"type\": \"custom_attribute\", \"value\": \"San Francisco\"}]]]", "id": "7718080042", "name": "iPhone users in San Francisco"}],
"groups": [{"policy": "random", "trafficAllocation": [{"entityId": "", "endOfRange": 500}, {"entityId": "7723330021", "endOfRange": 2000}, {"entityId": "7718750065", "endOfRange": 6000}], "experiments": [{"status": "Running", "key": "group_experiment_1", "layerId": "7721010011", "trafficAllocation": [{"entityId": "7722260071", "endOfRange": 5000}, {"entityId": "7722360022", "endOfRange": 10000}], "audienceIds": [], "variations": [{"id": "7722260071", "key": "group_exp_1_var_1"}, {"id": "7722360022", "key": "group_exp_1_var_2"}], "forcedVariations": {"user1": "group_exp_1_var_1"}, "id": "7723330021"}, {"status": "Running", "key": "group_experiment_2", "layerId": "7721020020", "trafficAllocation": [{"entityId": "7713030086", "endOfRange": 5000}, {"entityId": "7725250007", "endOfRange": 10000}], "audienceIds": [],
"variations": [{"id": "7713030086", "key": "group_exp_2_var_1"}, {"id": "7725250007", "key": "group_exp_2_var_2"}], "forcedVariations": {}, "id": "7718750065"}], "id": "7722400015"}],
"attributes": [{"id": "7723280020", "key": "device_type"}, {"id": "7723340004", "key": "location"}],
"projectId": "7720880029", "accountId": "1592310167",
"events": [{"experimentIds": ["7716830082", "7723330021", "7718750065", "7716830585"], "id": "7718020063", "key": "purchase"}],
"revision": "15", "random_data_key": [{"key_1": "value_1", "key_2": "value_2"}]}');

/**
* Class TestBucketer
* Extending Bucketer for the sake of tests.
Expand Down