| 1 |
<?php |
| 2 |
|
| 3 |
namespace LinnoSDK\Telemetry\Tests\Drivers; |
| 4 |
|
| 5 |
use LinnoSDK\Telemetry\Drivers\PostHogDriver; |
| 6 |
use Mockery; |
| 7 |
use PostHog\PostHog; |
| 8 |
use PHPUnit\Framework\TestCase; |
| 9 |
|
| 10 |
class PostHogDriverTest extends TestCase |
| 11 |
{ |
| 12 |
protected function tearDown(): void |
| 13 |
{ |
| 14 |
Mockery::close(); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* @runInSeparateProcess |
| 19 |
* @preserveGlobalState disabled |
| 20 |
*/ |
| 21 |
public function testSendReturnsTrueOnSuccess() |
| 22 |
{ |
| 23 |
$driver = new PostHogDriver('host'); |
| 24 |
$driver->setApiKey('key'); |
| 25 |
|
| 26 |
Mockery::mock('alias:' . PostHog::class) |
| 27 |
->shouldReceive('init')->once() |
| 28 |
->shouldReceive('capture')->once() |
| 29 |
->shouldReceive('flush')->once(); |
| 30 |
|
| 31 |
$this->assertTrue($driver->send('event', [])); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* @runInSeparateProcess |
| 36 |
* @preserveGlobalState disabled |
| 37 |
*/ |
| 38 |
public function testSendReturnsFalseOnFailure() |
| 39 |
{ |
| 40 |
$driver = new PostHogDriver('host'); |
| 41 |
$driver->setApiKey('key'); |
| 42 |
|
| 43 |
Mockery::mock('alias:' . PostHog::class) |
| 44 |
->shouldReceive('init')->once()->andThrow(new \Exception('error')); |
| 45 |
|
| 46 |
$this->assertFalse($driver->send('event', [])); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @runInSeparateProcess |
| 51 |
* @preserveGlobalState disabled |
| 52 |
*/ |
| 53 |
public function testGetLastErrorReturnsErrorMessage() |
| 54 |
{ |
| 55 |
$driver = new PostHogDriver('host'); |
| 56 |
$driver->setApiKey('key'); |
| 57 |
|
| 58 |
Mockery::mock('alias:' . PostHog::class) |
| 59 |
->shouldReceive('init')->once()->andThrow(new \Exception('error message')); |
| 60 |
|
| 61 |
$driver->send('event', []); |
| 62 |
|
| 63 |
$this->assertEquals('error message', $driver->getLastError()); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @runInSeparateProcess |
| 68 |
* @preserveGlobalState disabled |
| 69 |
*/ |
| 70 |
public function testSendWithMalformedHostUrl() |
| 71 |
{ |
| 72 |
$driver = new PostHogDriver('malformed-url'); |
| 73 |
$driver->setApiKey('key'); |
| 74 |
|
| 75 |
Mockery::mock('alias:' . PostHog::class) |
| 76 |
->shouldReceive('init')->once()->andThrow(new \Exception('cURL error')); |
| 77 |
|
| 78 |
$this->assertFalse($driver->send('event', [])); |
| 79 |
$this->assertStringContainsString('cURL error', $driver->getLastError()); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @runInSeparateProcess |
| 84 |
* @preserveGlobalState disabled |
| 85 |
*/ |
| 86 |
public function testSendWithLargePayload() |
| 87 |
{ |
| 88 |
$driver = new PostHogDriver('host'); |
| 89 |
$driver->setApiKey('key'); |
| 90 |
|
| 91 |
Mockery::mock('alias:' . PostHog::class) |
| 92 |
->shouldReceive('init')->once() |
| 93 |
->shouldReceive('capture')->once() |
| 94 |
->shouldReceive('flush')->once(); |
| 95 |
|
| 96 |
$largePayload = array_fill(0, 1000, 'a'); |
| 97 |
$this->assertTrue($driver->send('event', ['large_payload' => $largePayload])); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* @runInSeparateProcess |
| 102 |
* @preserveGlobalState disabled |
| 103 |
*/ |
| 104 |
public function testSendWithNonJsonErrorResponse() |
| 105 |
{ |
| 106 |
$driver = new PostHogDriver('host'); |
| 107 |
$driver->setApiKey('key'); |
| 108 |
|
| 109 |
Mockery::mock('alias:' . PostHog::class) |
| 110 |
->shouldReceive('init')->once()->andThrow(new \Exception('not a json response')); |
| 111 |
|
| 112 |
$this->assertFalse($driver->send('event', [])); |
| 113 |
$this->assertEquals('not a json response', $driver->getLastError()); |
| 114 |
} |
| 115 |
} |
| 116 |
|