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): Update relative date op names #61

Merged
merged 2 commits into from
Jan 26, 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
2 changes: 1 addition & 1 deletion lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ private function addLocalPersonAndGroupProperties(
array $groupProperties
): array {
$allPersonProperties = array_merge(
["\$current_distinct_id" => $distinctId],
["distinct_id" => $distinctId],
$personProperties
);

Expand Down
12 changes: 6 additions & 6 deletions lib/FeatureFlag.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ public static function matchProperty($property, $propertyValues)
}
}

if (in_array($operator, ["is_date_before", "is_date_after", "is_relative_date_before", "is_relative_date_after"])) {
if ($operator == 'is_relative_date_before' || $operator == 'is_relative_date_after') {
$parsedDate = FeatureFlag::relativeDateParseForFeatureFlagMatching($value);
} else {
if (in_array($operator, ["is_date_before", "is_date_after"])) {
$parsedDate = FeatureFlag::relativeDateParseForFeatureFlagMatching($value);

if (is_null($parsedDate)) {
$parsedDate = FeatureFlag::convertToDateTime($value);
}

Expand All @@ -86,7 +86,7 @@ public static function matchProperty($property, $propertyValues)
}

$overrideDate = FeatureFlag::convertToDateTime($overrideValue);
if ($operator == 'is_date_before' || $operator == 'is_relative_date_before') {
if ($operator == 'is_date_before') {
return $overrideDate < $parsedDate;
} else {
return $overrideDate > $parsedDate;
Expand All @@ -98,7 +98,7 @@ public static function matchProperty($property, $propertyValues)

public static function relativeDateParseForFeatureFlagMatching($value)
{
$regex = "/^(?<number>[0-9]+)(?<interval>[a-z])$/";
$regex = "/^-?(?<number>[0-9]+)(?<interval>[a-z])$/";
$parsedDt = new \DateTime("now", new \DateTimeZone("UTC"));
if (preg_match($regex, $value, $matches)) {
$number = intval($matches["number"]);
Expand Down
36 changes: 18 additions & 18 deletions test/FeatureFlagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,8 +607,8 @@ public function testMatchPropertyRelativeDateOperators(): void

$prop_a = [
"key" => "key",
"value" => "6h",
"operator" => "is_relative_date_before"
"value" => "-6h",
"operator" => "is_date_before"
];
self::assertTrue(FeatureFlag::matchProperty($prop_a, [
"key" => "2022-03-01",
Expand Down Expand Up @@ -652,7 +652,7 @@ public function testMatchPropertyRelativeDateOperators(): void
$prop_b = [
"key" => "key",
"value" => "1h",
"operator" => "is_relative_date_after"
"operator" => "is_date_after"
];
self::assertTrue(FeatureFlag::matchProperty($prop_b, [
"key" => "2022-05-02",
Expand All @@ -673,30 +673,30 @@ public function testMatchPropertyRelativeDateOperators(): void
$prop_c = [
"key" => "key",
"value" => 1234,
"operator" => "is_relative_date_after"
"operator" => "is_date_after"
];

try {
FeatureFlag::matchProperty($prop_c, [
"key" => "2022-05-30",
]);
} catch (InconclusiveMatchException $exception) {
self::assertStringContainsString("The date set on the flag is not a valid format", $exception->getMessage());
self::assertStringContainsString("The date provided 1234 must be a string or date object", $exception->getMessage());
}

try {
FeatureFlag::matchProperty($prop_c, [
"key" => 1,
]);
} catch (InconclusiveMatchException $exception) {
self::assertStringContainsString("The date set on the flag is not a valid format", $exception->getMessage());
self::assertStringContainsString("The date provided 1234 must be a string or date object", $exception->getMessage());
}

// # Try all possible relative dates
$prop_e = [
"key" => "key",
"value" => "1h",
"operator" => "is_relative_date_before"
"operator" => "is_date_before"
];
self::assertFalse(FeatureFlag::matchProperty($prop_e, [
"key" => "2022-05-01 00:00:00",
Expand All @@ -708,7 +708,7 @@ public function testMatchPropertyRelativeDateOperators(): void
$prop_f = [
"key" => "key",
"value" => "1d",
"operator" => "is_relative_date_before"
"operator" => "is_date_before"
];
self::assertTrue(FeatureFlag::matchProperty($prop_f, [
"key" => "2022-04-29 23:59:00",
Expand All @@ -720,7 +720,7 @@ public function testMatchPropertyRelativeDateOperators(): void
$prop_g = [
"key" => "key",
"value" => "1w",
"operator" => "is_relative_date_before"
"operator" => "is_date_before"
];
self::assertTrue(FeatureFlag::matchProperty($prop_g, [
"key" => "2022-04-23 00:00:00",
Expand All @@ -735,7 +735,7 @@ public function testMatchPropertyRelativeDateOperators(): void
$prop_h = [
"key" => "key",
"value" => "1m",
"operator" => "is_relative_date_before"
"operator" => "is_date_before"
];
self::assertTrue(FeatureFlag::matchProperty($prop_h, [
"key" => "2022-03-01 00:00:00",
Expand All @@ -747,7 +747,7 @@ public function testMatchPropertyRelativeDateOperators(): void
$prop_i = [
"key" => "key",
"value" => "1y",
"operator" => "is_relative_date_before"
"operator" => "is_date_before"
];
self::assertTrue(FeatureFlag::matchProperty($prop_i, [
"key" => "2021-04-28 00:00:00",
Expand All @@ -759,7 +759,7 @@ public function testMatchPropertyRelativeDateOperators(): void
$prop_j = [
"key" => "key",
"value" => "122h",
"operator" => "is_relative_date_after"
"operator" => "is_date_after"
];
self::assertTrue(FeatureFlag::matchProperty($prop_j, [
"key" => "2022-05-01 00:00:00",
Expand All @@ -771,7 +771,7 @@ public function testMatchPropertyRelativeDateOperators(): void
$prop_k = [
"key" => "key",
"value" => "2d",
"operator" => "is_relative_date_after"
"operator" => "is_date_after"
];
self::assertTrue(FeatureFlag::matchProperty($prop_k, [
"key" => "2022-05-01 00:00:00",
Expand All @@ -785,8 +785,8 @@ public function testMatchPropertyRelativeDateOperators(): void

$prop_l = [
"key" => "key",
"value" => "02w",
"operator" => "is_relative_date_after"
"value" => "-02w",
"operator" => "is_date_after"
];
self::assertTrue(FeatureFlag::matchProperty($prop_l, [
"key" => "2022-05-01 00:00:00",
Expand All @@ -798,7 +798,7 @@ public function testMatchPropertyRelativeDateOperators(): void
$prop_m = [
"key" => "key",
"value" => "1m",
"operator" => "is_relative_date_after"
"operator" => "is_date_after"
];
self::assertTrue(FeatureFlag::matchProperty($prop_m, [
"key" => "2022-04-01 00:00:01",
Expand All @@ -810,8 +810,8 @@ public function testMatchPropertyRelativeDateOperators(): void

$prop_n = [
"key" => "key",
"value" => "1y",
"operator" => "is_relative_date_after"
"value" => "-1y",
"operator" => "is_date_after"
];
self::assertTrue(FeatureFlag::matchProperty($prop_n, [
"key" => "2022-05-01 00:00:00",
Expand Down
18 changes: 9 additions & 9 deletions test/PostHogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public function testIsFeatureEnabled()
),
1 => array(
"path" => "/decide/?v=2",
"payload" => sprintf('{"api_key":"%s","distinct_id":"user-id","person_properties":{"$current_distinct_id":"user-id"}}', self::FAKE_API_KEY),
"payload" => sprintf('{"api_key":"%s","distinct_id":"user-id","person_properties":{"distinct_id":"user-id"}}', self::FAKE_API_KEY),
),
)
);
Expand All @@ -268,7 +268,7 @@ public function testIsFeatureEnabledGroups()
1 => array(
"path" => "/decide/?v=2",
"payload" => sprintf(
'{"api_key":"%s","distinct_id":"user-id","groups":{"company":"id:5"},"person_properties":{"$current_distinct_id":"user-id"},"group_properties":{"company":{"$group_key":"id:5"}}}',
'{"api_key":"%s","distinct_id":"user-id","groups":{"company":"id:5"},"person_properties":{"distinct_id":"user-id"},"group_properties":{"company":{"$group_key":"id:5"}}}',
self::FAKE_API_KEY
),
),
Expand All @@ -288,7 +288,7 @@ public function testGetFeatureFlag()
),
1 => array(
"path" => "/decide/?v=2",
"payload" => sprintf('{"api_key":"%s","distinct_id":"user-id","person_properties":{"$current_distinct_id":"user-id"}}', self::FAKE_API_KEY),
"payload" => sprintf('{"api_key":"%s","distinct_id":"user-id","person_properties":{"distinct_id":"user-id"}}', self::FAKE_API_KEY),
),
)
);
Expand Down Expand Up @@ -318,7 +318,7 @@ public function testGetFeatureFlagGroups()
1 => array(
"path" => "/decide/?v=2",
"payload" => sprintf(
'{"api_key":"%s","distinct_id":"user-id","groups":{"company":"id:5"},"person_properties":{"$current_distinct_id":"user-id"},"group_properties":{"company":{"$group_key":"id:5"}}}',
'{"api_key":"%s","distinct_id":"user-id","groups":{"company":"id:5"},"person_properties":{"distinct_id":"user-id"},"group_properties":{"company":{"$group_key":"id:5"}}}',
self::FAKE_API_KEY
),
),
Expand Down Expand Up @@ -483,7 +483,7 @@ public function testDefaultPropertiesGetAddedProperly(): void
),
1 => array(
"path" => "/decide/?v=2",
"payload" => sprintf('{"api_key":"%s","distinct_id":"some_id","groups":{"company":"id:5","instance":"app.posthog.com"},"person_properties":{"$current_distinct_id":"some_id","x1":"y1"},"group_properties":{"company":{"$group_key":"id:5","x":"y"},"instance":{"$group_key":"app.posthog.com"}}}', self::FAKE_API_KEY),
"payload" => sprintf('{"api_key":"%s","distinct_id":"some_id","groups":{"company":"id:5","instance":"app.posthog.com"},"person_properties":{"distinct_id":"some_id","x1":"y1"},"group_properties":{"company":{"$group_key":"id:5","x":"y"},"instance":{"$group_key":"app.posthog.com"}}}', self::FAKE_API_KEY),
),
)
);
Expand All @@ -495,15 +495,15 @@ public function testDefaultPropertiesGetAddedProperly(): void
'random_key',
'some_id',
array("company" => "id:5", "instance" => "app.posthog.com"),
array("\$current_distinct_id" => "override"),
array("distinct_id" => "override"),
array("company" => array("\$group_key" => "group_override"), "instance" => array("\$group_key" => "app.posthog.com"))
);
$this->assertEquals(
$this->http_client->calls,
array(
0 => array(
"path" => "/decide/?v=2",
"payload" => sprintf('{"api_key":"%s","distinct_id":"some_id","groups":{"company":"id:5","instance":"app.posthog.com"},"person_properties":{"$current_distinct_id":"override"},"group_properties":{"company":{"$group_key":"group_override"},"instance":{"$group_key":"app.posthog.com"}}}', self::FAKE_API_KEY),
"payload" => sprintf('{"api_key":"%s","distinct_id":"some_id","groups":{"company":"id:5","instance":"app.posthog.com"},"person_properties":{"distinct_id":"override"},"group_properties":{"company":{"$group_key":"group_override"},"instance":{"$group_key":"app.posthog.com"}}}', self::FAKE_API_KEY),
),
)
);
Expand All @@ -517,7 +517,7 @@ public function testDefaultPropertiesGetAddedProperly(): void
array(
0 => array(
"path" => "/decide/?v=2",
"payload" => sprintf('{"api_key":"%s","distinct_id":"some_id","groups":{"company":"id:5"},"person_properties":{"$current_distinct_id":"some_id"},"group_properties":{"company":{"$group_key":"id:5"}}}', self::FAKE_API_KEY),
"payload" => sprintf('{"api_key":"%s","distinct_id":"some_id","groups":{"company":"id:5"},"person_properties":{"distinct_id":"some_id"},"group_properties":{"company":{"$group_key":"id:5"}}}', self::FAKE_API_KEY),
),
)
);
Expand All @@ -531,7 +531,7 @@ public function testDefaultPropertiesGetAddedProperly(): void
array(
0 => array(
"path" => "/decide/?v=2",
"payload" => sprintf('{"api_key":"%s","distinct_id":"some_id","groups":{"company":"id:5","instance":"app.posthog.com"},"person_properties":{"$current_distinct_id":"some_id","x1":"y1"},"group_properties":{"company":{"$group_key":"id:5","x":"y"},"instance":{"$group_key":"app.posthog.com"}}}', self::FAKE_API_KEY),
"payload" => sprintf('{"api_key":"%s","distinct_id":"some_id","groups":{"company":"id:5","instance":"app.posthog.com"},"person_properties":{"distinct_id":"some_id","x1":"y1"},"group_properties":{"company":{"$group_key":"id:5","x":"y"},"instance":{"$group_key":"app.posthog.com"}}}', self::FAKE_API_KEY),
),
)
);
Expand Down
Loading