From c746985ca7c45b955c59db3bfdec4986feab906a Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Wed, 17 Jun 2026 18:37:42 +0200 Subject: [PATCH 1/2] fix: validate batch size option --- .changeset/quiet-lions-batch.md | 5 +++ lib/QueueConsumer.php | 4 +- test/PostHogTest.php | 68 +++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 .changeset/quiet-lions-batch.md diff --git a/.changeset/quiet-lions-batch.md b/.changeset/quiet-lions-batch.md new file mode 100644 index 0000000..a5f617c --- /dev/null +++ b/.changeset/quiet-lions-batch.md @@ -0,0 +1,5 @@ +--- +'posthog-php': patch +--- + +Keep the default batch size when invalid non-positive values are configured. diff --git a/lib/QueueConsumer.php b/lib/QueueConsumer.php index 9c5bc42..9828fc6 100644 --- a/lib/QueueConsumer.php +++ b/lib/QueueConsumer.php @@ -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"])) { diff --git a/test/PostHogTest.php b/test/PostHogTest.php index 130f5d4..088e564 100644 --- a/test/PostHogTest.php +++ b/test/PostHogTest.php @@ -101,6 +101,14 @@ public static function disabledClientNoRequestCases(): array ]; } + public static function invalidBatchSizeCases(): array + { + return [ + 'zero' => [0], + 'negative' => [-1], + ]; + } + public static function facadeNoOpBeforeInitCases(): array { return [ @@ -368,6 +376,66 @@ public function testClientWithBlankApiKeyDoesNotSendRequests(?string $apiKey, st $this->assertSame([], $httpClient->calls ?? []); } + public function testLowVolumeCapturesStayQueuedUntilFlush(): void + { + $httpClient = new MockedHttpClient("app.posthog.com"); + $client = new Client(self::FAKE_API_KEY, ["debug" => true], $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 invalidBatchSizeCases + */ + public function testInvalidBatchSizeUsesDefault(int $batchSize): void + { + $httpClient = new MockedHttpClient("app.posthog.com"); + $client = new Client( + self::FAKE_API_KEY, + ["debug" => true, "batch_size" => $batchSize], + $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']); + } + /** * @dataProvider facadeNoOpBeforeInitCases */ From 0fee8e04ea9a84008f9e70c56707d44eb167cc3e Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Wed, 17 Jun 2026 18:41:53 +0200 Subject: [PATCH 2/2] address pr review feedback --- test/PostHogTest.php | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/test/PostHogTest.php b/test/PostHogTest.php index 088e564..b35b254 100644 --- a/test/PostHogTest.php +++ b/test/PostHogTest.php @@ -101,11 +101,12 @@ public static function disabledClientNoRequestCases(): array ]; } - public static function invalidBatchSizeCases(): array + public static function queuedBatchSizeCases(): array { return [ - 'zero' => [0], - 'negative' => [-1], + 'default' => [["debug" => true]], + 'zero' => [["debug" => true, "batch_size" => 0]], + 'negative' => [["debug" => true, "batch_size" => -1]], ]; } @@ -376,10 +377,13 @@ public function testClientWithBlankApiKeyDoesNotSendRequests(?string $apiKey, st $this->assertSame([], $httpClient->calls ?? []); } - public function testLowVolumeCapturesStayQueuedUntilFlush(): void + /** + * @dataProvider queuedBatchSizeCases + */ + public function testCapturesStayQueuedUntilFlush(array $options): void { $httpClient = new MockedHttpClient("app.posthog.com"); - $client = new Client(self::FAKE_API_KEY, ["debug" => true], $httpClient, null, false); + $client = new Client(self::FAKE_API_KEY, $options, $httpClient, null, false); $this->assertTrue($client->capture([ "distinctId" => "john", @@ -411,30 +415,6 @@ public function testBatchSizeOneFlushesImmediately(): void $this->assertSame('/batch/', $httpClient->calls[0]['path']); } - /** - * @dataProvider invalidBatchSizeCases - */ - public function testInvalidBatchSizeUsesDefault(int $batchSize): void - { - $httpClient = new MockedHttpClient("app.posthog.com"); - $client = new Client( - self::FAKE_API_KEY, - ["debug" => true, "batch_size" => $batchSize], - $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']); - } /** * @dataProvider facadeNoOpBeforeInitCases