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 e45f7e8..b1fa52e 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 f52bb95..abbe64c 100644 --- a/test/PostHogTest.php +++ b/test/PostHogTest.php @@ -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 [ @@ -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 */