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
5 changes: 5 additions & 0 deletions .changeset/quiet-lions-batch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'posthog-php': patch
---

Keep the default batch size when invalid non-positive values are configured.
4 changes: 2 additions & 2 deletions lib/QueueConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public function __construct($apiKey, $options = array())
$this->max_queue_size = $options["max_queue_size"];
}

if (isset($options["batch_size"])) {
$this->batch_size = $options["batch_size"];
if (isset($options["batch_size"]) && (int) $options["batch_size"] > 0) {
$this->batch_size = (int) $options["batch_size"];
}

if (isset($options["maximum_backoff_duration"])) {
Expand Down
48 changes: 48 additions & 0 deletions test/PostHogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ public static function disabledClientNoRequestCases(): array
];
}

public static function queuedBatchSizeCases(): array
{
return [
'default' => [["debug" => true]],
'zero' => [["debug" => true, "batch_size" => 0]],
'negative' => [["debug" => true, "batch_size" => -1]],
];
}

public static function facadeNoOpBeforeInitCases(): array
{
return [
Expand Down Expand Up @@ -368,6 +377,45 @@ public function testClientWithBlankApiKeyDoesNotSendRequests(?string $apiKey, st
$this->assertSame([], $httpClient->calls ?? []);
}

/**
* @dataProvider queuedBatchSizeCases
*/
public function testCapturesStayQueuedUntilFlush(array $options): void
{
$httpClient = new MockedHttpClient("app.posthog.com");
$client = new Client(self::FAKE_API_KEY, $options, $httpClient, null, false);

$this->assertTrue($client->capture([
"distinctId" => "john",
"event" => "Module PHP Event",
]));
$this->assertSame(0, count($httpClient->calls ?? []));

$this->assertTrue($client->flush());
$this->assertSame(1, count($httpClient->calls ?? []));
$this->assertSame('/batch/', $httpClient->calls[0]['path']);
}

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

$this->assertTrue($client->capture([
"distinctId" => "john",
"event" => "Module PHP Event",
]));
$this->assertSame(1, count($httpClient->calls ?? []));
$this->assertSame('/batch/', $httpClient->calls[0]['path']);
}


/**
* @dataProvider facadeNoOpBeforeInitCases
*/
Expand Down
Loading