Skip to content
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

Update to latest version: forced features #1

Merged
merged 2 commits into from
Jul 17, 2024
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
37 changes: 36 additions & 1 deletion src/Growthbook.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Growthbook implements LoggerAwareInterface
private $attributes = [];
/** @var Feature<mixed>[] */
private $features = [];
/** @var array<string, FeatureResult<mixed>> */
private $forcedFeatures = [];
/** @var array<string,int> */
private $forcedVariations = [];
/** @var bool */
Expand Down Expand Up @@ -63,7 +65,7 @@ public static function create(): Growthbook
}

/**
* @param array{enabled?:bool,logger?:\Psr\Log\LoggerInterface,url?:string,attributes?:array<string,mixed>,features?:array<string,mixed>,forcedVariations?:array<string,int>,qaMode?:bool,trackingCallback?:callable,cache?:\Psr\SimpleCache\CacheInterface,httpClient?:\Psr\Http\Client\ClientInterface,requestFactory?:\Psr\Http\Message\RequestFactoryInterface,decryptionKey?:string} $options
* @param array{enabled?:bool,logger?:\Psr\Log\LoggerInterface,url?:string,attributes?:array<string,mixed>,features?:array<string,mixed>,forcedVariations?:array<string,int>,qaMode?:bool,trackingCallback?:callable,cache?:\Psr\SimpleCache\CacheInterface,httpClient?:\Psr\Http\Client\ClientInterface,requestFactory?:\Psr\Http\Message\RequestFactoryInterface,decryptionKey?:string,forcedFeatures?:array<string, FeatureResult<mixed>>} $options
*/
public function __construct(array $options = [])
{
Expand All @@ -75,6 +77,7 @@ public function __construct(array $options = [])
"attributes",
"features",
"forcedVariations",
"forcedFeatures",
"qaMode",
"trackingCallback",
"cache",
Expand Down Expand Up @@ -104,6 +107,10 @@ public function __construct(array $options = [])
// Ignore errors from discovery
}

if(array_key_exists("forcedFeatures", $options)) {
$this->withForcedFeatures($options['forcedFeatures']);
}

if (array_key_exists("features", $options)) {
$this->withFeatures(($options["features"]));
}
Expand Down Expand Up @@ -155,6 +162,16 @@ public function withForcedVariations(array $forcedVariations): Growthbook
$this->forcedVariations = $forcedVariations;
return $this;
}

/**
* @param array<string, FeatureResult<mixed>> $forcedFeatures
* @return Growthbook
*/
public function withForcedFeatures(array $forcedFeatures)
{
$this->forcedFeatures = $forcedFeatures;
return $this;
}
/**
* @param string $url
* @return Growthbook
Expand Down Expand Up @@ -211,6 +228,13 @@ public function getForcedVariations(): array
{
return $this->forcedVariations;
}
/**
* @return array<string, FeatureResult<mixed>>
*/
public function getForcedFeatured()
{
return $this->forcedFeatures;
}
/**
* @return string
*/
Expand Down Expand Up @@ -267,6 +291,17 @@ public function getFeature(string $key): FeatureResult
}
$this->log(LogLevel::DEBUG, "Evaluating feature - $key");
$feature = $this->features[$key];

// Check if the feature is forced
if (array_key_exists($key, $this->forcedFeatures)) {
$this->log(LogLevel::DEBUG, "Feature Forced - $key", [
"feature" => $key,
"result" => $this->forcedFeatures[$key],
]);

return $this->forcedFeatures[$key];
}

if ($feature->rules) {
foreach ($feature->rules as $rule) {
if ($rule->condition) {
Expand Down
14 changes: 14 additions & 0 deletions tests/GrowthbookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_once __DIR__ . '/../vendor/autoload.php';

use Growthbook\Condition;
use Growthbook\FeatureResult;
use Growthbook\Growthbook;
use Growthbook\InlineExperiment;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -330,6 +331,19 @@ public function testFluentInterface(): void
);
}

public function testForcedFeatures(): void
{
$gb = Growthbook::create()
->withFeatures([
'feature-1'=>['defaultValue' => false, 'rules' => []]
])
->withForcedFeatures([
'feature-1' => new FeatureResult(true, 'forcedFeature')
]);

$this->assertSame(true, $gb->getFeature('feature-1')->value);
}

public function testInlineExperiment(): void
{
$condition = ['country'=>'US'];
Expand Down