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

fix(flags): Safe access flags in decide v2 #55

Merged
merged 4 commits into from
Sep 25, 2023
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: 5 additions & 1 deletion lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,11 @@ public function getFeatureFlag(
if (!$flagWasEvaluatedLocally && !$onlyEvaluateLocally) {
try {
$featureFlags = $this->fetchFeatureVariants($distinctId, $groups, $personProperties, $groupProperties);
$result = $featureFlags[$key] ?? null;
if(array_key_exists($key, $featureFlags)) {
$result = $featureFlags[$key];
} else {
$result = null;
}
} catch (Exception $e) {
error_log("[PostHog][Client] Unable to get feature variants:" . $e->getMessage());
$result = null;
Expand Down
38 changes: 37 additions & 1 deletion test/FeatureFlagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// phpcs:ignoreFile
namespace PostHog\Test;

// comment out below to print all logs instead of failing tests
require_once 'test/error_log_mock.php';

use Exception;
use PHPUnit\Framework\TestCase;
use PostHog\FeatureFlag;
Expand All @@ -11,7 +14,7 @@
use PostHog\InconclusiveMatchException;
use PostHog\SizeLimitedHash;

class FeatureFlagMatch extends TestCase
class FeatureFlagTest extends TestCase
{
const FAKE_API_KEY = "random_key";

Expand All @@ -21,6 +24,16 @@ class FeatureFlagMatch extends TestCase
public function setUp(): void
{
date_default_timezone_set("UTC");

// Reset the errorMessages array before each test
global $errorMessages;
$errorMessages = [];
}

public function checkEmptyErrorLogs(): void
{
global $errorMessages;
$this->assertTrue(empty($errorMessages), "Error logs are not empty: " . implode("\n", $errorMessages));
}

public function testMatchPropertyEquals(): void
Expand Down Expand Up @@ -455,6 +468,8 @@ public function testFlagPersonProperties()

$this->assertTrue(PostHog::getFeatureFlag('person-flag', 'some-distinct-id', [], ["region" => "USA"]));
$this->assertFalse(PostHog::getFeatureFlag('person-flag', 'some-distinct-id-2', [], ["region" => "Canada"]));

$this->checkEmptyErrorLogs();
}

public function testFlagGroupProperties()
Expand Down Expand Up @@ -514,6 +529,27 @@ public function testFlagFallbackToDecide()

$this->assertEquals(PostHog::getFeatureFlag('feature-1', 'some-distinct'), 'decide-fallback-value');
$this->assertEquals(PostHog::getFeatureFlag('feature-2', 'some-distinct'), 'decide-fallback-value');

$this->checkEmptyErrorLogs();
}

public function testFlagFallbackToDecideWithFalseFlag()
{
$this->http_client = new MockedHttpClient(host: "app.posthog.com", flagEndpointResponse: MockedResponses::FALLBACK_TO_DECIDE_REQUEST);
$this->client = new Client(
self::FAKE_API_KEY,
[
"debug" => true,
],
$this->http_client,
"test"
);
PostHog::init(null, null, $this->client);

$this->assertEquals(PostHog::getFeatureFlag('unknown-flag???', 'some-distinct'), null);
$this->assertEquals(PostHog::getFeatureFlag('false-flag', 'some-distinct'), null);

$this->checkEmptyErrorLogs();
}

public function testFeatureFlagDefaultsComeIntoPlayOnlyWhenDecideErrorsOut()
Expand Down
17 changes: 16 additions & 1 deletion test/PostHogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace PostHog\Test;

// comment out below to print all logs instead of failing tests
require_once 'test/error_log_mock.php';

use Exception;
use PHPUnit\Framework\TestCase;
use PostHog\Client;
Expand All @@ -13,7 +16,7 @@ class PostHogTest extends TestCase

private $http_client;
private $client;

public function setUp(): void
{
date_default_timezone_set("UTC");
Expand All @@ -27,6 +30,16 @@ public function setUp(): void
"test"
);
PostHog::init(null, null, $this->client);

// Reset the errorMessages array before each test
global $errorMessages;
$errorMessages = [];
}

public function checkEmptyErrorLogs(): void
{
global $errorMessages;
$this->assertEmpty($errorMessages);
}

public function testInitWithParamApiKey(): void
Expand Down Expand Up @@ -167,6 +180,8 @@ public function testGetFeatureFlag()
public function testGetFeatureFlagDefault()
{
$this->assertEquals(PostHog::getFeatureFlag('blah', 'user-id'), null);

$this->checkEmptyErrorLogs();
}

public function testGetFeatureFlagGroups()
Expand Down
13 changes: 13 additions & 0 deletions test/error_log_mock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

// phpcs:disable PSR1.Files.SideEffects
namespace PostHog;

// The $errorMessages array captures logged messages.
$errorMessages = [];

function error_log($message)
{
global $errorMessages;
$errorMessages[] = $message;
}
Loading