Skip to content

feat: datafile accessor #211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 28, 2020
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
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be $_datafile?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be $_datafile to be consistent with other private variable conventions. @oakbani ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the privacy convention has changed. I think @oakbani had showed me that previously. See: https://www.php-fig.org/psr/psr-12/#43-properties-and-constants


/**
* @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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



public function __construct($revision, array $experimentsMap, array $featuresMap, $datafile = null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not provide a default null for datafile.

{
$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 String denoting 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);
}
}
3 changes: 2 additions & 1 deletion tests/OptimizelyConfigTests/OptimizelyEntitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public function testOptimizelyConfigEntity()
$expectedJson = '{
"revision": "20",
"experimentsMap" : {"a": "apple"},
"featuresMap": {"o": "orange"}
"featuresMap": {"o": "orange"},
"datafile": null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here.

}';

$expectedJson = json_encode(json_decode($expectedJson));
Expand Down