Skip to content

Commit a335f0f

Browse files
authored
feat: datafile accessor (#211)
1 parent 67aa4cc commit a335f0f

File tree

7 files changed

+67
-4
lines changed

7 files changed

+67
-4
lines changed

Diff for: src/Optimizely/Config/DatafileProjectConfig.php

+14
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ class DatafileProjectConfig implements ProjectConfigInterface
8888
*/
8989
private $_botFiltering;
9090

91+
/**
92+
* @var string datafile.
93+
*/
94+
private $datafile;
95+
9196
/**
9297
* @var string Revision of the datafile.
9398
*/
@@ -196,6 +201,7 @@ public function __construct($datafile, $logger, $errorHandler)
196201
{
197202
$supportedVersions = array(self::V2, self::V3, self::V4);
198203
$config = json_decode($datafile, true);
204+
$this->datafile = $datafile;
199205
$this->_logger = $logger;
200206
$this->_errorHandler = $errorHandler;
201207
$this->_version = $config['version'];
@@ -355,6 +361,14 @@ public static function createProjectConfigFromDatafile($datafile, $skipJsonValid
355361
return $config;
356362
}
357363

364+
/**
365+
* @return string String representing contents of datafile.
366+
*/
367+
public function toDatafile()
368+
{
369+
return $this->datafile;
370+
}
371+
358372
/**
359373
* @return string String representing account ID from the datafile.
360374
*/

Diff for: src/Optimizely/Config/ProjectConfigInterface.php

+7
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,11 @@ public function getFeatureVariableFromKey($featureFlagKey, $variableKey);
159159
* @return boolean A boolean value that indicates if the experiment is a feature test.
160160
*/
161161
public function isFeatureExperiment($experimentId);
162+
163+
/**
164+
* Returns string representation of datafile.
165+
*
166+
* @return string A string value that contains datafile contents.
167+
*/
168+
public function toDatafile();
162169
}

Diff for: src/Optimizely/OptimizelyConfig/OptimizelyConfig.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,18 @@ class OptimizelyConfig implements \JsonSerializable
3737
*/
3838
private $featuresMap;
3939

40-
public function __construct($revision, array $experimentsMap, array $featuresMap)
40+
/**
41+
* @var string Contents of datafile.
42+
*/
43+
private $datafile;
44+
45+
46+
public function __construct($revision, array $experimentsMap, array $featuresMap, $datafile = null)
4147
{
4248
$this->revision = $revision;
4349
$this->experimentsMap = $experimentsMap;
4450
$this->featuresMap = $featuresMap;
51+
$this->datafile = $datafile;
4552
}
4653

4754
/**
@@ -52,6 +59,14 @@ public function getRevision()
5259
return $this->revision;
5360
}
5461

62+
/**
63+
* @return string Datafile contents.
64+
*/
65+
public function getDatafile()
66+
{
67+
return $this->datafile;
68+
}
69+
5570
/**
5671
* @return array Map of Experiment Keys to OptimizelyExperiments.
5772
*/

Diff for: src/Optimizely/OptimizelyConfig/OptimizelyConfigService.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class OptimizelyConfigService
3737
*/
3838
private $revision;
3939

40+
/**
41+
* @var string String denoting datafile.
42+
*/
43+
private $datafile;
44+
4045
/**
4146
* Map of experiment IDs to FeatureFlags.
4247
*
@@ -63,6 +68,7 @@ public function __construct(ProjectConfigInterface $projectConfig)
6368
$this->experiments = $projectConfig->getAllExperiments();
6469
$this->featureFlags = $projectConfig->getFeatureFlags();
6570
$this->revision = $projectConfig->getRevision();
71+
$this->datafile = $projectConfig->toDatafile();
6672

6773
$this->createLookupMaps();
6874
}
@@ -78,7 +84,8 @@ public function getConfig()
7884
return new OptimizelyConfig(
7985
$this->revision,
8086
$experimentsMaps[0],
81-
$featuresMap
87+
$featuresMap,
88+
$this->datafile
8289
);
8390
}
8491

Diff for: tests/ConfigTests/DatafileProjectConfigTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -719,4 +719,13 @@ public function testIsFeatureExperiment()
719719
$this->assertTrue($this->config->isFeatureExperiment($featureExperiment->getId()));
720720
$this->assertFalse($this->config->isFeatureExperiment($experiment->getId()));
721721
}
722+
723+
public function testToDatafile()
724+
{
725+
$expectedDatafile = DATAFILE_FOR_OPTIMIZELY_CONFIG;
726+
$this->config = new DatafileProjectConfig($expectedDatafile, $this->loggerMock, $this->errorHandlerMock);
727+
$actualDatafile = $this->config->toDatafile();
728+
729+
$this->assertEquals($expectedDatafile, $actualDatafile);
730+
}
722731
}

Diff for: tests/OptimizelyConfigTests/OptimizelyConfigServiceTest.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,16 @@ public function testJsonEncodeofOptimizelyConfig()
465465
}
466466
}';
467467

468-
$this->assertEquals(json_encode(json_decode($expectedJSON)), json_encode($response));
468+
$optimizelyConfig = json_decode($expectedJSON, true);
469+
$optimizelyConfig['datafile'] = DATAFILE_FOR_OPTIMIZELY_CONFIG;
470+
471+
$this->assertEquals(json_encode($optimizelyConfig), json_encode($response));
472+
}
473+
474+
public function testGetDatafile()
475+
{
476+
$expectedDatafile = DATAFILE_FOR_OPTIMIZELY_CONFIG;
477+
$actualDatafile = $this->optConfigService->getConfig()->getDatafile();
478+
$this->assertEquals($expectedDatafile, $actualDatafile);
469479
}
470480
}

Diff for: tests/OptimizelyConfigTests/OptimizelyEntitiesTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public function testOptimizelyConfigEntity()
4141
$expectedJson = '{
4242
"revision": "20",
4343
"experimentsMap" : {"a": "apple"},
44-
"featuresMap": {"o": "orange"}
44+
"featuresMap": {"o": "orange"},
45+
"datafile": null
4546
}';
4647

4748
$expectedJson = json_encode(json_decode($expectedJson));

0 commit comments

Comments
 (0)