Skip to content

Commit

Permalink
Merge 1363d51 into 67aa4cc
Browse files Browse the repository at this point in the history
  • Loading branch information
oakbani committed Aug 27, 2020
2 parents 67aa4cc + 1363d51 commit 1ca1aea
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/Optimizely/Config/DatafileProjectConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ class DatafileProjectConfig implements ProjectConfigInterface
*/
private $_botFiltering;

/**
* @var string datafile.
*/
private $datafile;

/**
* @var string Revision of the datafile.
*/
Expand Down Expand Up @@ -196,6 +201,7 @@ public function __construct($datafile, $logger, $errorHandler)
{
$supportedVersions = array(self::V2, self::V3, self::V4);
$config = json_decode($datafile, true);
$this->datafile = $datafile;
$this->_logger = $logger;
$this->_errorHandler = $errorHandler;
$this->_version = $config['version'];
Expand Down Expand Up @@ -355,6 +361,14 @@ public static function createProjectConfigFromDatafile($datafile, $skipJsonValid
return $config;
}

/**
* @return string String representing contents of datafile.
*/
public function toDatafile()
{
return $this->datafile;
}

/**
* @return string String representing account ID from the datafile.
*/
Expand Down
7 changes: 7 additions & 0 deletions src/Optimizely/Config/ProjectConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,11 @@ public function getFeatureVariableFromKey($featureFlagKey, $variableKey);
* @return boolean A boolean value that indicates if the experiment is a feature test.
*/
public function isFeatureExperiment($experimentId);

/**
* Returns string representation of datafile.
*
* @return string A string value that contains datafile contents.
*/
public function toDatafile();
}
17 changes: 16 additions & 1 deletion src/Optimizely/OptimizelyConfig/OptimizelyConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,18 @@ class OptimizelyConfig implements \JsonSerializable
*/
private $featuresMap;

public function __construct($revision, array $experimentsMap, array $featuresMap)
/**
* @var string Contents of datafile.
*/
private $datafile;


public function __construct($revision, array $experimentsMap, array $featuresMap, $datafile = null)
{
$this->revision = $revision;
$this->experimentsMap = $experimentsMap;
$this->featuresMap = $featuresMap;
$this->datafile = $datafile;
}

/**
Expand All @@ -52,6 +59,14 @@ public function getRevision()
return $this->revision;
}

/**
* @return string Datafile contents.
*/
public function getDatafile()
{
return $this->datafile;
}

/**
* @return array Map of Experiment Keys to OptimizelyExperiments.
*/
Expand Down
9 changes: 8 additions & 1 deletion src/Optimizely/OptimizelyConfig/OptimizelyConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class OptimizelyConfigService
*/
private $revision;

/**
* @var string config datafile.
*/
private $datafile;

/**
* Map of experiment IDs to FeatureFlags.
*
Expand All @@ -63,6 +68,7 @@ public function __construct(ProjectConfigInterface $projectConfig)
$this->experiments = $projectConfig->getAllExperiments();
$this->featureFlags = $projectConfig->getFeatureFlags();
$this->revision = $projectConfig->getRevision();
$this->datafile = $projectConfig->toDatafile();

$this->createLookupMaps();
}
Expand All @@ -78,7 +84,8 @@ public function getConfig()
return new OptimizelyConfig(
$this->revision,
$experimentsMaps[0],
$featuresMap
$featuresMap,
$this->datafile
);
}

Expand Down
9 changes: 9 additions & 0 deletions tests/ConfigTests/DatafileProjectConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -719,4 +719,13 @@ public function testIsFeatureExperiment()
$this->assertTrue($this->config->isFeatureExperiment($featureExperiment->getId()));
$this->assertFalse($this->config->isFeatureExperiment($experiment->getId()));
}

public function testToDatafile()
{
$expectedDatafile = DATAFILE_FOR_OPTIMIZELY_CONFIG;
$this->config = new DatafileProjectConfig($expectedDatafile, $this->loggerMock, $this->errorHandlerMock);
$actualDatafile = $this->config->toDatafile();

$this->assertEquals($expectedDatafile, $actualDatafile);
}
}
12 changes: 11 additions & 1 deletion tests/OptimizelyConfigTests/OptimizelyConfigServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,16 @@ public function testJsonEncodeofOptimizelyConfig()
}
}';

$this->assertEquals(json_encode(json_decode($expectedJSON)), json_encode($response));
$optimizelyConfig = json_decode($expectedJSON, true);
$optimizelyConfig['datafile'] = DATAFILE_FOR_OPTIMIZELY_CONFIG;

$this->assertEquals(json_encode($optimizelyConfig), json_encode($response));
}

public function testGetDatafile()
{
$expectedDatafile = DATAFILE_FOR_OPTIMIZELY_CONFIG;
$actualDatafile = $this->optConfigService->getConfig()->getDatafile();
$this->assertEquals($expectedDatafile, $actualDatafile);
}
}

0 comments on commit 1ca1aea

Please sign in to comment.