| 1 |
<?php |
| 2 |
|
| 3 |
namespace PostHog\Test; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use PHPUnit\Framework\TestCase; |
| 7 |
use PostHog\Client; |
| 8 |
use PostHog\PostHog; |
| 9 |
use PostHog\Test\Assets\MockedResponses; |
| 10 |
|
| 11 |
class PostHogTest extends TestCase |
| 12 |
{ |
| 13 |
public function setUp(): void |
| 14 |
{ |
| 15 |
date_default_timezone_set("UTC"); |
| 16 |
$this->http_client = new MockedHttpClient("app.posthog.com"); |
| 17 |
$this->client = new Client( |
| 18 |
"test-key", |
| 19 |
[ |
| 20 |
"debug" => true |
| 21 |
], |
| 22 |
$this->http_client |
| 23 |
); |
| 24 |
PostHog::init(null, null, $this->client); |
| 25 |
} |
| 26 |
|
| 27 |
public function testInitWithParamApiKey(): void |
| 28 |
{ |
| 29 |
$this->expectNotToPerformAssertions(); |
| 30 |
PostHog::init("BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg", array("debug" => true)); |
| 31 |
} |
| 32 |
|
| 33 |
public function testInitWithEnvApiKey(): void |
| 34 |
{ |
| 35 |
$this->expectNotToPerformAssertions(); |
| 36 |
putenv(PostHog::ENV_API_KEY . "=BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg"); |
| 37 |
PostHog::init(null, array("debug" => true)); |
| 38 |
|
| 39 |
// Clear the environment variable |
| 40 |
putenv(PostHog::ENV_API_KEY); |
| 41 |
} |
| 42 |
|
| 43 |
public function testInitThrowsExceptionWithNoApiKey(): void |
| 44 |
{ |
| 45 |
$this->expectException(Exception::class); |
| 46 |
$this->expectExceptionMessage("PostHog::init() requires an apiKey"); |
| 47 |
PostHog::init(null); |
| 48 |
} |
| 49 |
|
| 50 |
public function testCapture(): void |
| 51 |
{ |
| 52 |
self::assertTrue( |
| 53 |
PostHog::capture( |
| 54 |
array( |
| 55 |
"distinctId" => "john", |
| 56 |
"event" => "Module PHP Event", |
| 57 |
) |
| 58 |
) |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
public function testIdentify(): void |
| 63 |
{ |
| 64 |
self::assertTrue( |
| 65 |
PostHog::identify( |
| 66 |
array( |
| 67 |
"distinctId" => "doe", |
| 68 |
"properties" => array( |
| 69 |
"loves_php" => false, |
| 70 |
"birthday" => time(), |
| 71 |
), |
| 72 |
) |
| 73 |
) |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
public function testIsFeatureEnabled() |
| 78 |
{ |
| 79 |
$this->assertFalse(PostHog::isFeatureEnabled('having_fun', 'user-id')); |
| 80 |
$this->assertEquals( |
| 81 |
$this->http_client->calls, |
| 82 |
array( |
| 83 |
0 => array( |
| 84 |
"path" => "/decide/", |
| 85 |
"payload" => '{"api_key":"test-key","distinct_id":"user-id"}', |
| 86 |
) |
| 87 |
) |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
public function testIsFeatureEnabledDefault() |
| 92 |
{ |
| 93 |
$this->assertTrue(PostHog::isFeatureEnabled('having_fun', 'user-id', true)); |
| 94 |
} |
| 95 |
|
| 96 |
public function testIsFeatureEnabledGroups() |
| 97 |
{ |
| 98 |
$this->assertFalse(PostHog::isFeatureEnabled('having_fun', 'user-id', false, array("company" => "id:5"))); |
| 99 |
|
| 100 |
$this->assertEquals( |
| 101 |
$this->http_client->calls, |
| 102 |
array( |
| 103 |
0 => array( |
| 104 |
"path" => "/decide/", |
| 105 |
"payload" => '{"api_key":"test-key","distinct_id":"user-id","groups":{"company":"id:5"}}', |
| 106 |
) |
| 107 |
) |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
public function testFetchEnabledFeatureFlags() |
| 112 |
{ |
| 113 |
$this->assertIsArray(PostHog::fetchEnabledFeatureFlags('user-id')); |
| 114 |
} |
| 115 |
|
| 116 |
public function testEmptyProperties(): void |
| 117 |
{ |
| 118 |
self::assertTrue( |
| 119 |
PostHog::identify( |
| 120 |
array( |
| 121 |
"distinctId" => "empty-properties", |
| 122 |
) |
| 123 |
) |
| 124 |
); |
| 125 |
|
| 126 |
self::assertTrue( |
| 127 |
PostHog::capture( |
| 128 |
array( |
| 129 |
"distinctId" => "user-id", |
| 130 |
"event" => "empty-properties", |
| 131 |
) |
| 132 |
) |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
public function testEmptyArrayProperties(): void |
| 137 |
{ |
| 138 |
self::assertTrue( |
| 139 |
PostHog::identify( |
| 140 |
array( |
| 141 |
"distinctId" => "empty-properties", |
| 142 |
"properties" => array(), |
| 143 |
) |
| 144 |
) |
| 145 |
); |
| 146 |
|
| 147 |
self::assertTrue( |
| 148 |
PostHog::capture( |
| 149 |
array( |
| 150 |
"distinctId" => "user-id", |
| 151 |
"event" => "empty-properties", |
| 152 |
"properties" => array(), |
| 153 |
) |
| 154 |
) |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
public function testAlias(): void |
| 159 |
{ |
| 160 |
self::assertTrue( |
| 161 |
PostHog::alias( |
| 162 |
array( |
| 163 |
"alias" => "previous-id", |
| 164 |
"distinctId" => "user-id", |
| 165 |
) |
| 166 |
) |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
public function testTimestamps(): void |
| 171 |
{ |
| 172 |
self::assertTrue( |
| 173 |
PostHog::capture( |
| 174 |
array( |
| 175 |
"distinctId" => "user-id", |
| 176 |
"event" => "integer-timestamp", |
| 177 |
"timestamp" => (int)mktime(0, 0, 0, date('n'), 1, date('Y')), |
| 178 |
) |
| 179 |
) |
| 180 |
); |
| 181 |
|
| 182 |
self::assertTrue( |
| 183 |
PostHog::capture( |
| 184 |
array( |
| 185 |
"distinctId" => "user-id", |
| 186 |
"event" => "string-integer-timestamp", |
| 187 |
"timestamp" => (string)mktime(0, 0, 0, date('n'), 1, date('Y')), |
| 188 |
) |
| 189 |
) |
| 190 |
); |
| 191 |
|
| 192 |
self::assertTrue( |
| 193 |
PostHog::capture( |
| 194 |
array( |
| 195 |
"distinctId" => "user-id", |
| 196 |
"event" => "iso8630-timestamp", |
| 197 |
"timestamp" => date(DATE_ATOM, mktime(0, 0, 0, date('n'), 1, date('Y'))), |
| 198 |
) |
| 199 |
) |
| 200 |
); |
| 201 |
|
| 202 |
self::assertTrue( |
| 203 |
PostHog::capture( |
| 204 |
array( |
| 205 |
"distinctId" => "user-id", |
| 206 |
"event" => "iso8601-timestamp", |
| 207 |
"timestamp" => date(DATE_ATOM, mktime(0, 0, 0, date('n'), 1, date('Y'))), |
| 208 |
) |
| 209 |
) |
| 210 |
); |
| 211 |
|
| 212 |
self::assertTrue( |
| 213 |
PostHog::capture( |
| 214 |
array( |
| 215 |
"distinctId" => "user-id", |
| 216 |
"event" => "strtotime-timestamp", |
| 217 |
"timestamp" => strtotime('1 week ago'), |
| 218 |
) |
| 219 |
) |
| 220 |
); |
| 221 |
} |
| 222 |
|
| 223 |
public function testGroupIdentify(): void |
| 224 |
{ |
| 225 |
self::assertTrue( |
| 226 |
PostHog::groupIdentify( |
| 227 |
array( |
| 228 |
"groupType" => "company", |
| 229 |
"groupKey" => "id:5", |
| 230 |
"properties" => array( |
| 231 |
"foo" => "bar" |
| 232 |
) |
| 233 |
) |
| 234 |
) |
| 235 |
); |
| 236 |
|
| 237 |
self::assertTrue( |
| 238 |
PostHog::groupIdentify( |
| 239 |
array( |
| 240 |
"groupType" => "company", |
| 241 |
"groupKey" => "id:5", |
| 242 |
) |
| 243 |
) |
| 244 |
); |
| 245 |
} |
| 246 |
|
| 247 |
public function testGroupIdentifyValidation(): void |
| 248 |
{ |
| 249 |
try { |
| 250 |
Posthog::groupIdentify(array()); |
| 251 |
} catch (Exception $e) { |
| 252 |
$this->assertEquals("PostHog::groupIdentify() expects a groupType", $e->getMessage()); |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
|