| 1 |
<?php |
| 2 |
|
| 3 |
namespace PostHog\Test; |
| 4 |
|
| 5 |
use PHPUnit\Framework\TestCase; |
| 6 |
use PostHog\Client; |
| 7 |
|
| 8 |
class ConsumerFileTest extends TestCase |
| 9 |
{ |
| 10 |
private $client; |
| 11 |
private $filename = "/tmp/posthog.log"; |
| 12 |
|
| 13 |
public function setUp(): void |
| 14 |
{ |
| 15 |
date_default_timezone_set("UTC"); |
| 16 |
if (file_exists($this->filename())) { |
| 17 |
unlink($this->filename()); |
| 18 |
} |
| 19 |
|
| 20 |
$this->client = new Client( |
| 21 |
"BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg", |
| 22 |
array( |
| 23 |
"consumer" => "file", |
| 24 |
"filename" => $this->filename, |
| 25 |
) |
| 26 |
); |
| 27 |
} |
| 28 |
|
| 29 |
public function tearDown(): void |
| 30 |
{ |
| 31 |
if (file_exists($this->filename)) { |
| 32 |
unlink($this->filename); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
public function testCapture(): void |
| 37 |
{ |
| 38 |
self::assertTrue( |
| 39 |
$this->client->capture( |
| 40 |
array( |
| 41 |
"distinctId" => "some-user", |
| 42 |
"event" => "File PHP Event - Microtime", |
| 43 |
"timestamp" => time(), |
| 44 |
) |
| 45 |
) |
| 46 |
); |
| 47 |
$this->checkWritten("capture"); |
| 48 |
} |
| 49 |
|
| 50 |
public function testIdentify(): void |
| 51 |
{ |
| 52 |
self::assertTrue( |
| 53 |
$this->client->identify( |
| 54 |
array( |
| 55 |
"distinctId" => "Calvin", |
| 56 |
"properties" => array( |
| 57 |
"loves_php" => false, |
| 58 |
"type" => "posthog.log", |
| 59 |
"birthday" => time(), |
| 60 |
), |
| 61 |
) |
| 62 |
) |
| 63 |
); |
| 64 |
$this->checkWritten("identify"); |
| 65 |
} |
| 66 |
|
| 67 |
public function testAlias(): void |
| 68 |
{ |
| 69 |
self::assertTrue( |
| 70 |
$this->client->alias( |
| 71 |
array( |
| 72 |
"alias" => "previous-id", |
| 73 |
"distinctId" => "user-id", |
| 74 |
) |
| 75 |
) |
| 76 |
); |
| 77 |
|
| 78 |
$this->checkWritten("alias"); |
| 79 |
} |
| 80 |
|
| 81 |
public function testSend(): void |
| 82 |
{ |
| 83 |
for ($i = 0; $i < 200; ++$i) { |
| 84 |
$this->client->capture( |
| 85 |
array( |
| 86 |
"distinctId" => "distinctId", |
| 87 |
"event" => "event", |
| 88 |
) |
| 89 |
); |
| 90 |
} |
| 91 |
exec("php send.php --apiKey BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg --file /tmp/posthog.log", $output); |
| 92 |
self::assertSame("sent 200 from 200 requests successfully", trim(implode('', $output))); |
| 93 |
self::assertFileDoesNotExist($this->filename()); |
| 94 |
} |
| 95 |
|
| 96 |
public function testProductionProblems(): void |
| 97 |
{ |
| 98 |
// Open to a place where we should not have write access. |
| 99 |
$client = new Client( |
| 100 |
"BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg", |
| 101 |
array( |
| 102 |
"consumer" => "file", |
| 103 |
"filename" => "/dev/x/xxxxxxx", |
| 104 |
) |
| 105 |
); |
| 106 |
|
| 107 |
$captured = $client->capture(array("distinctId" => "some-user", "event" => "my event")); |
| 108 |
self::assertFalse($captured); |
| 109 |
} |
| 110 |
|
| 111 |
private function checkWritten($type): void |
| 112 |
{ |
| 113 |
exec("wc -l " . $this->filename, $output); |
| 114 |
$out = trim($output[0]); |
| 115 |
self::assertSame($out, "1 " . $this->filename); |
| 116 |
$str = file_get_contents($this->filename); |
| 117 |
$json = json_decode(trim($str)); |
| 118 |
self::assertSame($type, $json->type); |
| 119 |
unlink($this->filename); |
| 120 |
} |
| 121 |
|
| 122 |
public function filename(): string |
| 123 |
{ |
| 124 |
return '/tmp/posthog.log'; |
| 125 |
} |
| 126 |
} |
| 127 |
|