| 1 |
<?php |
| 2 |
|
| 3 |
namespace PostHog\Test; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use PHPUnit\Framework\TestCase; |
| 7 |
use PostHog\Client; |
| 8 |
use RuntimeException; |
| 9 |
|
| 10 |
class ConsumerSocketTest extends TestCase |
| 11 |
{ |
| 12 |
public function setUp(): void |
| 13 |
{ |
| 14 |
date_default_timezone_set("UTC"); |
| 15 |
} |
| 16 |
|
| 17 |
public function testCapture(): void |
| 18 |
{ |
| 19 |
$client = new Client( |
| 20 |
"BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg", |
| 21 |
array( |
| 22 |
"consumer" => "socket", |
| 23 |
) |
| 24 |
); |
| 25 |
self::assertTrue( |
| 26 |
$client->capture( |
| 27 |
array( |
| 28 |
"distinctId" => "some-user", |
| 29 |
"event" => "Socket PHP Event", |
| 30 |
) |
| 31 |
) |
| 32 |
); |
| 33 |
$client->__destruct(); |
| 34 |
} |
| 35 |
|
| 36 |
public function testIdentify(): void |
| 37 |
{ |
| 38 |
$client = new Client( |
| 39 |
"BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg", |
| 40 |
array( |
| 41 |
"consumer" => "socket", |
| 42 |
) |
| 43 |
); |
| 44 |
self::assertTrue( |
| 45 |
$client->identify( |
| 46 |
array( |
| 47 |
"distinctId" => "Calvin", |
| 48 |
"properties" => array( |
| 49 |
"loves_php" => false, |
| 50 |
"birthday" => time(), |
| 51 |
), |
| 52 |
) |
| 53 |
) |
| 54 |
); |
| 55 |
$client->__destruct(); |
| 56 |
} |
| 57 |
|
| 58 |
public function testShortTimeout(): void |
| 59 |
{ |
| 60 |
$client = new Client( |
| 61 |
"BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg", |
| 62 |
array( |
| 63 |
"timeout" => 0.01, |
| 64 |
"consumer" => "socket", |
| 65 |
) |
| 66 |
); |
| 67 |
|
| 68 |
self::assertTrue( |
| 69 |
$client->capture( |
| 70 |
array( |
| 71 |
"distinctId" => "some-user", |
| 72 |
"event" => "Socket PHP Event", |
| 73 |
) |
| 74 |
) |
| 75 |
); |
| 76 |
|
| 77 |
self::assertTrue( |
| 78 |
$client->identify( |
| 79 |
array( |
| 80 |
"distinctId" => "some-user", |
| 81 |
"properties" => array(), |
| 82 |
) |
| 83 |
) |
| 84 |
); |
| 85 |
|
| 86 |
$client->__destruct(); |
| 87 |
} |
| 88 |
|
| 89 |
public function testProductionProblems(): void |
| 90 |
{ |
| 91 |
$client = new Client( |
| 92 |
"x", |
| 93 |
array( |
| 94 |
"consumer" => "socket", |
| 95 |
"error_handler" => function () { |
| 96 |
throw new Exception("Was called"); |
| 97 |
}, |
| 98 |
) |
| 99 |
); |
| 100 |
|
| 101 |
// Shouldn't error out without debug on. |
| 102 |
$client->capture(array("user_id" => "some-user", "event" => "Production Problems")); |
| 103 |
$client->__destruct(); |
| 104 |
self::assertTrue(true); |
| 105 |
} |
| 106 |
|
| 107 |
public function testLargeMessage(): void |
| 108 |
{ |
| 109 |
$options = array( |
| 110 |
"debug" => true, |
| 111 |
"consumer" => "socket", |
| 112 |
); |
| 113 |
|
| 114 |
$client = new Client("BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg", $options); |
| 115 |
|
| 116 |
$big_property = ""; |
| 117 |
|
| 118 |
for ($i = 0; $i < 10000; ++$i) { |
| 119 |
$big_property .= "a"; |
| 120 |
} |
| 121 |
|
| 122 |
self::assertTrue( |
| 123 |
$client->capture( |
| 124 |
array( |
| 125 |
"distinctId" => "some-user", |
| 126 |
"event" => "Super Large PHP Event", |
| 127 |
"properties" => array("big_property" => $big_property), |
| 128 |
) |
| 129 |
) |
| 130 |
); |
| 131 |
|
| 132 |
$client->__destruct(); |
| 133 |
} |
| 134 |
|
| 135 |
public function testConnectionError(): void |
| 136 |
{ |
| 137 |
$this->expectException('RuntimeException'); |
| 138 |
$client = new Client( |
| 139 |
"x", |
| 140 |
array( |
| 141 |
"consumer" => "socket", |
| 142 |
"host" => "t.posthog.comcomcom", |
| 143 |
"error_handler" => function ($errno, $errmsg) { |
| 144 |
throw new RuntimeException($errmsg, $errno); |
| 145 |
}, |
| 146 |
) |
| 147 |
); |
| 148 |
|
| 149 |
$client->capture(array("user_id" => "some-user", "event" => "Event")); |
| 150 |
$client->__destruct(); |
| 151 |
} |
| 152 |
} |
| 153 |
|