diff --git a/test/FeatureFlagTest.php b/test/FeatureFlagTest.php index 1fbbc56..06f490c 100644 --- a/test/FeatureFlagTest.php +++ b/test/FeatureFlagTest.php @@ -523,7 +523,7 @@ public function testMatchPropertyDateOperators(): void self::assertFalse(FeatureFlag::matchProperty($prop_a, [ "key" => "2022-05-30", ])); - + // is date after // is date after // const property_b = { key: 'key', value: '2022-05-01', operator: 'is_date_after' } @@ -604,7 +604,7 @@ public function testMatchPropertyDateOperators(): void public function testMatchPropertyRelativeDateOperators(): void { ClockMock::executeAtFrozenDateTime(new \DateTime('2022-05-01'), function () { - + $prop_a = [ "key" => "key", "value" => "-6h", @@ -794,7 +794,7 @@ public function testMatchPropertyRelativeDateOperators(): void self::assertFalse(FeatureFlag::matchProperty($prop_l, [ "key" => "2022-04-16 00:00:00", ])); - + $prop_m = [ "key" => "key", "value" => "1m", @@ -848,7 +848,7 @@ public function testMatchPropertyWithNones(): void self::assertTrue(FeatureFlag::matchProperty($prop_a, [ "key" => "nul", ])); - + $prop_b = [ "key" => "key", "value" => "null", @@ -949,7 +949,7 @@ public function testRelativeDateParsingOverflow() public function testRelativeDateParsingHours() { - + ClockMock::executeAtFrozenDateTime(new \DateTime('2020-01-01T12:01:20Z'), function () { self::assertEquals(FeatureFlag::relativeDateParseForFeatureFlagMatching('1h'), new \DateTime('2020-01-01T11:01:20Z')); self::assertEquals(FeatureFlag::relativeDateParseForFeatureFlagMatching('2h'), new \DateTime('2020-01-01T10:01:20Z')); @@ -1014,7 +1014,7 @@ public function testRelativeDateParsingMonths() self::assertEquals(FeatureFlag::relativeDateParseForFeatureFlagMatching('1y'), new \DateTime('2019-04-03T00:00:00Z')); self::assertEquals(FeatureFlag::relativeDateParseForFeatureFlagMatching('12m'), FeatureFlag::relativeDateParseForFeatureFlagMatching('1y')); - + }); } @@ -1191,7 +1191,7 @@ public function testGetAllFlagsWithFallback() public function testGetAllFlagsWithFallbackEmptyLocalFlags() { - $this->http_client = new MockedHttpClient(host: "app.posthog.com", flagEndpointResponse:[]); + $this->http_client = new MockedHttpClient(host: "app.posthog.com", flagEndpointResponse: []); $this->client = new Client( self::FAKE_API_KEY, [ @@ -1210,7 +1210,7 @@ public function testGetAllFlagsWithFallbackEmptyLocalFlags() public function testGetAllFlagsWithNoFallback() { - $this->http_client = new MockedHttpClient(host: "app.posthog.com", flagEndpointResponse:MockedResponses::MULTIPLE_FLAGS_LOCAL_EVALUATE_REQUEST); + $this->http_client = new MockedHttpClient(host: "app.posthog.com", flagEndpointResponse: MockedResponses::MULTIPLE_FLAGS_LOCAL_EVALUATE_REQUEST); $this->client = new Client( self::FAKE_API_KEY, [ @@ -1284,13 +1284,13 @@ public function testSimpleFlag() $this->assertEquals( $this->http_client->calls, - array( - 0 => array( + array ( + 0 => array ( "path" => "/api/feature_flag/local_evaluation?send_cohorts&token=random_key", "payload" => null, ), // no decide because local eval, but capture flag event - 1 => array( + 1 => array ( "path" => "/batch/", 'payload' => '{"batch":[{"properties":{"$feature\/simple-flag":true,"$active_feature_flags":["simple-flag"],"$feature_flag":"simple-flag","$feature_flag_response":true,"$lib":"posthog-php","$lib_version":"3.0.3","$lib_consumer":"LibCurl","$groups":[]},"distinct_id":"some-distinct-id","event":"$feature_flag_called","$groups":[],"library":"posthog-php","library_version":"3.0.3","library_consumer":"LibCurl","groups":[],"timestamp":"2022-05-01T00:00:00+00:00","type":"capture"}],"api_key":"random_key"}', ), @@ -1548,7 +1548,7 @@ public function testFeatureFlagsLocalEvaluationForNegatedCohorts() array() ); } - + public function testComputingFlagWithoutRolloutLocally() { $this->http_client = new MockedHttpClient(host: "app.posthog.com", flagEndpointResponse: MockedResponses::LOCAL_EVALUATION_WITH_NO_ROLLOUT_REQUEST); diff --git a/test/PostHogTest.php b/test/PostHogTest.php index e9e22ff..a9fe472 100644 --- a/test/PostHogTest.php +++ b/test/PostHogTest.php @@ -97,7 +97,7 @@ public function testCaptureWithSendFeatureFlagsOption(): void $this->assertTrue( PostHog::capture( - array( + array ( "distinctId" => "john", "event" => "Module PHP Event", "send_feature_flags" => true @@ -108,16 +108,16 @@ public function testCaptureWithSendFeatureFlagsOption(): void $this->assertEquals( $this->http_client->calls, - array( - 0 => array( + array ( + 0 => array ( "path" => "/api/feature_flag/local_evaluation?send_cohorts&token=random_key", "payload" => null, ), - 1 => array( + 1 => array ( "path" => "/decide/?v=2", "payload" => sprintf('{"api_key":"%s","distinct_id":"john"}', self::FAKE_API_KEY), ), - 2 => array( + 2 => array ( "path" => "/batch/", "payload" => '{"batch":[{"event":"Module PHP Event","send_feature_flags":true,"properties":{"$feature\/simpleFlag":true,"$feature\/having_fun":false,"$feature\/enabled-flag":true,"$feature\/disabled-flag":false,"$feature\/multivariate-simple-test":"variant-simple-value","$feature\/simple-test":true,"$feature\/multivariate-test":"variant-value","$feature\/group-flag":"decide-fallback-value","$feature\/complex-flag":"decide-fallback-value","$feature\/beta-feature":"decide-fallback-value","$feature\/beta-feature2":"alakazam","$feature\/feature-1":"decide-fallback-value","$feature\/feature-2":"decide-fallback-value","$feature\/variant-1":"variant-1","$feature\/variant-3":"variant-3","$active_feature_flags":["simpleFlag","enabled-flag","multivariate-simple-test","simple-test","multivariate-test","group-flag","complex-flag","beta-feature","beta-feature2","feature-1","feature-2","variant-1","variant-3"],"$lib":"posthog-php","$lib_version":"3.0.3","$lib_consumer":"LibCurl"},"library":"posthog-php","library_version":"3.0.3","library_consumer":"LibCurl","distinct_id":"john","groups":[],"timestamp":"2022-05-01T00:00:00+00:00","type":"capture"}],"api_key":"random_key"}', ), @@ -152,7 +152,7 @@ public function testCaptureWithLocalSendFlags(): void ClockMock::executeAtFrozenDateTime(new \DateTime('2022-05-01'), function () { $this->assertTrue( PostHog::capture( - array( + array ( "distinctId" => "john", "event" => "Module PHP Event", ) @@ -160,15 +160,15 @@ public function testCaptureWithLocalSendFlags(): void ); PostHog::flush(); - + $this->assertEquals( $this->http_client->calls, - array( - 0 => array( + array ( + 0 => array ( "path" => "/api/feature_flag/local_evaluation?send_cohorts&token=random_key", "payload" => null, ), - 1 => array( + 1 => array ( "path" => "/batch/", "payload" => '{"batch":[{"event":"Module PHP Event","properties":{"$feature\/true-flag":true,"$active_feature_flags":["true-flag"],"$lib":"posthog-php","$lib_version":"3.0.3","$lib_consumer":"LibCurl"},"library":"posthog-php","library_version":"3.0.3","library_consumer":"LibCurl","distinct_id":"john","groups":[],"timestamp":"2022-05-01T00:00:00+00:00","type":"capture"}],"api_key":"random_key"}', ), @@ -193,10 +193,10 @@ public function testCaptureWithLocalSendFlagsNoOverrides(): void ClockMock::executeAtFrozenDateTime(new \DateTime('2022-05-01'), function () { $this->assertTrue( PostHog::capture( - array( + array ( "distinctId" => "john", "event" => "Module PHP Event", - "properties" => array( + "properties" => array ( "\$feature/true-flag" => "random-override" ) ) @@ -204,15 +204,15 @@ public function testCaptureWithLocalSendFlagsNoOverrides(): void ); PostHog::flush(); - + $this->assertEquals( $this->http_client->calls, - array( - 0 => array( + array ( + 0 => array ( "path" => "/api/feature_flag/local_evaluation?send_cohorts&token=random_key", "payload" => null, ), - 1 => array( + 1 => array ( "path" => "/batch/", "payload" => '{"batch":[{"event":"Module PHP Event","properties":{"$feature\/true-flag":"random-override","$active_feature_flags":["true-flag"],"$lib":"posthog-php","$lib_version":"3.0.3","$lib_consumer":"LibCurl"},"library":"posthog-php","library_version":"3.0.3","library_consumer":"LibCurl","distinct_id":"john","groups":[],"timestamp":"2022-05-01T00:00:00+00:00","type":"capture"}],"api_key":"random_key"}', ), @@ -392,7 +392,7 @@ public function testTimestamps(): void array( "distinctId" => "user-id", "event" => "integer-timestamp", - "timestamp" => (int)mktime(0, 0, 0, date('n'), 1, date('Y')), + "timestamp" => (int) mktime(0, 0, 0, date('n'), 1, date('Y')), ) ) ); @@ -402,7 +402,7 @@ public function testTimestamps(): void array( "distinctId" => "user-id", "event" => "string-integer-timestamp", - "timestamp" => (string)mktime(0, 0, 0, date('n'), 1, date('Y')), + "timestamp" => (string) mktime(0, 0, 0, date('n'), 1, date('Y')), ) ) ); diff --git a/test/assests/MockedResponses.php b/test/assests/MockedResponses.php index 092d6e0..669a73d 100644 --- a/test/assests/MockedResponses.php +++ b/test/assests/MockedResponses.php @@ -55,11 +55,11 @@ class MockedResponses "operator" => "exact", "type" => "person" ] - ], + ], "rollout_percentage" => 100 ] ] - ], + ], "deleted" => false, "active" => true, "is_simple_flag" => true, @@ -87,11 +87,11 @@ class MockedResponses "operator" => "exact", "type" => "person" ] - ], + ], "rollout_percentage" => 100 ] ] - ], + ], "deleted" => false, "active" => true, "is_simple_flag" => true, @@ -105,11 +105,11 @@ class MockedResponses "groups" => [ [ "properties" => [ - ], + ], "rollout_percentage" => 100 ] ] - ], + ], "deleted" => false, "active" => true, "is_simple_flag" => true, @@ -131,10 +131,10 @@ class MockedResponses "groups" => [ [ "properties" => [ - ], + ], ] ] - ], + ], "deleted" => false, "active" => true, "is_simple_flag" => true, @@ -156,11 +156,11 @@ class MockedResponses "groups" => [ [ "properties" => [ - ], + ], "rollout_percentage" => 100 ] ] - ], + ], "deleted" => false, "active" => true, "is_simple_flag" => true, @@ -174,11 +174,11 @@ class MockedResponses "groups" => [ [ "properties" => [ - ], + ], "rollout_percentage" => 100 ] ] - ], + ], "deleted" => false, "active" => false, "is_simple_flag" => true, @@ -189,8 +189,8 @@ class MockedResponses public const LOCAL_EVALUATION_WITH_COHORTS_REQUEST = [ 'flags' => [ - [ - 'id' => 2, + [ + 'id' => 2, 'name' => 'Beta Feature', 'key' => 'beta-feature', 'is_simple_flag' => false, @@ -216,57 +216,57 @@ class MockedResponses ], ], ], - ] + ] ], 'cohorts' => [ - '98' => [ - 'type' => 'OR', - 'values' => [ - ['key' => 'id', 'value' => 1, 'type' => 'cohort'], - ['key' => 'nation', 'operator' => 'exact', 'value' => ['UK'], 'type' => 'person'], + '98' => [ + 'type' => 'OR', + 'values' => [ + ['key' => 'id', 'value' => 1, 'type' => 'cohort'], + ['key' => 'nation', 'operator' => 'exact', 'value' => ['UK'], 'type' => 'person'], + ], ], - ], - '1' => [ - 'type' => 'AND', - 'values' => [ - ['key' => 'other', 'operator' => 'exact', 'value' => ['thing'], 'type' => 'person'], + '1' => [ + 'type' => 'AND', + 'values' => [ + ['key' => 'other', 'operator' => 'exact', 'value' => ['thing'], 'type' => 'person'], + ], ], - ], ] - + ]; public const LOCAL_EVALUATION_FOR_NEGATED_COHORTS_REQUEST = [ 'flags' => [ [ - 'id' => 2, - 'name' => 'Beta Feature', - 'key' => 'beta-feature', - 'is_simple_flag' => false, - 'active' => true, - 'filters' => [ - 'groups' => [ - [ - 'properties' => [ - [ - 'key' => 'region', - 'operator' => 'exact', - 'value' => ['USA'], - 'type' => 'person', - ], - [ - 'key' => 'id', - 'value' => 98, - 'operator' => null, - 'type' => 'cohort', - ], - ], - 'rollout_percentage' => 100, - ], - ], - ], + 'id' => 2, + 'name' => 'Beta Feature', + 'key' => 'beta-feature', + 'is_simple_flag' => false, + 'active' => true, + 'filters' => [ + 'groups' => [ + [ + 'properties' => [ + [ + 'key' => 'region', + 'operator' => 'exact', + 'value' => ['USA'], + 'type' => 'person', + ], + [ + 'key' => 'id', + 'value' => 98, + 'operator' => null, + 'type' => 'cohort', + ], + ], + 'rollout_percentage' => 100, + ], + ], + ], ] - ], + ], 'cohorts' => [ '98' => [ 'type' => 'OR', @@ -305,11 +305,11 @@ class MockedResponses "operator" => "exact", "type" => "group" ] - ], + ], "rollout_percentage" => 35 ] ] - ], + ], "deleted" => false, "active" => true, "is_simple_flag" => true, @@ -347,32 +347,33 @@ class MockedResponses "operator" => "exact", "type" => "person" ] - ], + ], "rollout_percentage" => 100 + ], + [ + "properties" => [ + [ + "key" => "email", + "value" => ["a@b.com"], + "operator" => "exact", + "type" => "person" + ] ], - [ - "properties" => [ - [ - "key" => "email", - "value" => ["a@b.com"], - "operator" => "exact", - "type" => "person" - ] - ], - "rollout_percentage" => 35 - ],[ - "properties" => [ - [ - "key" => "doesnt_matter", - "value" => ["1", "2"], - "operator" => "exact", - "type" => "person" - ] - ], - "rollout_percentage" => 0 + "rollout_percentage" => 35 + ], + [ + "properties" => [ + [ + "key" => "doesnt_matter", + "value" => ["1", "2"], + "operator" => "exact", + "type" => "person" ] + ], + "rollout_percentage" => 0 + ] ] - ], + ], "deleted" => false, "active" => true, "is_simple_flag" => true, @@ -400,7 +401,7 @@ class MockedResponses "operator" => "exact", "type" => "person" ] - ], + ], "rollout_percentage" => 100, "variant" => "second-variant" ], @@ -456,7 +457,7 @@ class MockedResponses "operator" => "exact", "type" => "person" ] - ], + ], "rollout_percentage" => 100, "variant" => "second-variant" ], @@ -468,7 +469,7 @@ class MockedResponses "operator" => "exact", "type" => "person" ] - ], + ], "rollout_percentage" => 100, "variant" => "first-variant" ], @@ -524,7 +525,7 @@ class MockedResponses "operator" => "exact", "type" => "person" ] - ], + ], "rollout_percentage" => 100, "variant" => "second???" ], @@ -583,7 +584,7 @@ class MockedResponses "operator" => "exact", "type" => "person" ] - ], + ], "rollout_percentage" => 100, "variant" => "second-variant" ], @@ -637,7 +638,7 @@ class MockedResponses "rollout_percentage" => 100 ] ] - ], + ], "ensure_experience_continuity" => true, "deleted" => false, "active" => true, @@ -666,17 +667,17 @@ class MockedResponses "operator" => null, "type" => "cohort" ] - ], - "rollout_percentage" => 100 - ], - ], ], + "rollout_percentage" => 100 + ], + ], + ], "deleted" => false, "active" => true, "is_simple_flag" => false, "rollout_percentage" => null - ], + ], [ "id" => 2, "name" => "feature 2", @@ -691,11 +692,11 @@ class MockedResponses "operator" => null, "type" => "person" ] - ], - "rollout_percentage" => 100 - ], - ], ], + "rollout_percentage" => 100 + ], + ], + ], "deleted" => false, "active" => true, "is_simple_flag" => false, @@ -721,7 +722,7 @@ class MockedResponses "rollout_percentage" => 0 ] ] - ], + ], "deleted" => false, "active" => true, "is_simple_flag" => true, @@ -745,7 +746,7 @@ class MockedResponses "rollout_percentage" => 100 ] ] - ], + ], "deleted" => false, "active" => true, "is_simple_flag" => true, @@ -770,7 +771,7 @@ class MockedResponses "rollout_percentage" => 45 ] ] - ], + ], "deleted" => false, "active" => true, "is_simple_flag" => true, @@ -824,7 +825,7 @@ class MockedResponses ] ] ] - ], + ], "deleted" => false, "active" => true, "is_simple_flag" => true, @@ -849,7 +850,7 @@ class MockedResponses "rollout_percentage" => 100 ] ] - ], + ], "deleted" => false, "active" => true, "is_simple_flag" => false, @@ -866,7 +867,7 @@ class MockedResponses "rollout_percentage" => 0 ] ] - ], + ], "deleted" => false, "active" => true, "is_simple_flag" => false, @@ -891,7 +892,7 @@ class MockedResponses "rollout_percentage" => 100 ] ] - ], + ], "deleted" => false, "active" => true, "is_simple_flag" => false, @@ -908,7 +909,7 @@ class MockedResponses "rollout_percentage" => 0 ] ] - ], + ], "deleted" => false, "active" => true, "is_simple_flag" => false, @@ -932,7 +933,7 @@ class MockedResponses "rollout_percentage" => 0 ] ] - ], + ], "deleted" => false, "active" => true, "is_simple_flag" => false,