diff --git a/lib/Client.php b/lib/Client.php index cc0f0d8..72e8090 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -598,7 +598,7 @@ private function addLocalPersonAndGroupProperties( array $groupProperties ): array { $allPersonProperties = array_merge( - ["\$current_distinct_id" => $distinctId], + ["distinct_id" => $distinctId], $personProperties ); diff --git a/lib/FeatureFlag.php b/lib/FeatureFlag.php index 66ef7e2..f499870 100644 --- a/lib/FeatureFlag.php +++ b/lib/FeatureFlag.php @@ -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); } @@ -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; @@ -98,7 +98,7 @@ public static function matchProperty($property, $propertyValues) public static function relativeDateParseForFeatureFlagMatching($value) { - $regex = "/^(?[0-9]+)(?[a-z])$/"; + $regex = "/^-?(?[0-9]+)(?[a-z])$/"; $parsedDt = new \DateTime("now", new \DateTimeZone("UTC")); if (preg_match($regex, $value, $matches)) { $number = intval($matches["number"]); diff --git a/test/FeatureFlagTest.php b/test/FeatureFlagTest.php index ff235db..5e5fbe2 100644 --- a/test/FeatureFlagTest.php +++ b/test/FeatureFlagTest.php @@ -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", @@ -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", @@ -673,7 +673,7 @@ public function testMatchPropertyRelativeDateOperators(): void $prop_c = [ "key" => "key", "value" => 1234, - "operator" => "is_relative_date_after" + "operator" => "is_date_after" ]; try { @@ -681,7 +681,7 @@ public function testMatchPropertyRelativeDateOperators(): void "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 { @@ -689,14 +689,14 @@ public function testMatchPropertyRelativeDateOperators(): void "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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", diff --git a/test/PostHogTest.php b/test/PostHogTest.php index 0640ac9..e3a2a7e 100644 --- a/test/PostHogTest.php +++ b/test/PostHogTest.php @@ -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), ), ) ); @@ -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 ), ), @@ -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), ), ) ); @@ -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 ), ), @@ -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), ), ) ); @@ -495,7 +495,7 @@ 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( @@ -503,7 +503,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":"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), ), ) ); @@ -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), ), ) ); @@ -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), ), ) );