Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/Consumer/LibCurl.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,24 @@ public function flushBatch($messages)
$payload = gzencode($payload);
}

return $this->httpClient->sendRequest(
$shouldVerify = $this->options['verify_batch_events_request'] ?? true;
$response = $this->httpClient->sendRequest(
'/batch/',
$payload,
[
// Send user agent in the form of {library_name}/{library_version} as per RFC 7231.
"User-Agent: {$messages[0]['library']}/{$messages[0]['library_version']}",
],
[
'shouldVerify' => $this->options['verify_batch_events_request'] ?? true,
'shouldVerify' => $shouldVerify,
]
)->getResponse();
);

if (!$shouldVerify) {
return $response->getResponse() !== false;
}

// Keep batch success semantics aligned with HttpClient retry handling and the Socket consumer.
return $response->getResponseCode() === 200;
}
}
4 changes: 2 additions & 2 deletions lib/Consumer/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function flushBatch($batch)
$socket = $this->createSocket();

if (!$socket) {
return;
return false;
}

$payload = $this->payload($batch);
Expand Down Expand Up @@ -166,7 +166,7 @@ private function makeRequest($socket, $req, $retry = true)
$socket = $this->createSocket();
}

return true;
return false;
}

/**
Expand Down
23 changes: 23 additions & 0 deletions test/ConsumerSocketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,29 @@ public function testShortTimeout(): void
$client->__destruct();
}

public function testBatchSizeOneConnectionErrorReturnsFalse(): void
{
$client = new Client(
"x",
array(
"batch_size" => 1,
"consumer" => "socket",
"host" => "invalid.invalid",
"ssl" => false,
"timeout" => 0.01,
)
);

self::assertFalse(
$client->capture(
array(
"distinctId" => "some-user",
"event" => "Socket PHP Event",
)
)
);
}

public function testProductionProblems(): void
{
$client = new Client(
Expand Down
4 changes: 4 additions & 0 deletions test/MockedHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public function sendRequest(string $path, ?string $payload, array $extraHeaders
);
}

if ($path === "/batch/") {
return new HttpResponse('{"status":1}', 200);
}

return parent::sendRequest($path, $payload, $extraHeaders, $requestOptions);
}
}
24 changes: 24 additions & 0 deletions test/PostHogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,30 @@ public function testAlias(): void
);
}

public function testAliasReturnsBooleanWhenBatchFlushesImmediately(): void
{
$httpClient = new MockedHttpClient("app.posthog.com");
$client = new Client(
self::FAKE_API_KEY,
[
"debug" => true,
"batch_size" => 1,
],
$httpClient
);
PostHog::init(null, null, $client);

$result = PostHog::alias(
array(
"alias" => "previous-id",
"distinctId" => "user-id",
)
);

$this->assertIsBool($result);
$this->assertTrue($result);
}

public function testTimestamps(): void
{
self::assertTrue(
Expand Down
Loading