From 24f2b8268c5147dd93c167ac0ec2586d4ccb9555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jost=20Vo=CC=88lker?= Date: Tue, 28 Jan 2025 16:59:48 +0100 Subject: [PATCH] fix: pass size to content cb in MockRequestBuilder --- src/HttpClientMock/MockRequestBuilderFactory.php | 2 +- tests/HttpClientMock/MockRequestBuilderFactoryTest.php | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/HttpClientMock/MockRequestBuilderFactory.php b/src/HttpClientMock/MockRequestBuilderFactory.php index 49c64c7..c9afeeb 100644 --- a/src/HttpClientMock/MockRequestBuilderFactory.php +++ b/src/HttpClientMock/MockRequestBuilderFactory.php @@ -132,7 +132,7 @@ private function processBody( } if (is_callable($body)) { - $mockRequestBuilder->content((string) $body()); + $mockRequestBuilder->content((string) $body((int) $contentLength)); return; } diff --git a/tests/HttpClientMock/MockRequestBuilderFactoryTest.php b/tests/HttpClientMock/MockRequestBuilderFactoryTest.php index eeded8d..3d0b5d7 100644 --- a/tests/HttpClientMock/MockRequestBuilderFactoryTest.php +++ b/tests/HttpClientMock/MockRequestBuilderFactoryTest.php @@ -9,6 +9,8 @@ use PHPUnit\Framework\TestCase; use Psr\Http\Message\StreamInterface; +use function sprintf; + #[CoversClass(MockRequestBuilderFactory::class)] final class MockRequestBuilderFactoryTest extends TestCase { @@ -52,13 +54,17 @@ public function testBuildsRequestWithJsonInBody(): void public function testBuildsRequestWithCallableInBody(): void { + $size = 1; $body = $this->getMockBuilder(StreamInterface::class)->getMock(); - $body->method('read') + $body + ->expects(self::once()) + ->method('read') + ->with($size) ->willReturn('{"foo": "bar"}'); $options = [ 'headers' => [ - 'Content-Length: 1', + sprintf('Content-Length: %d', $size), 'Content-Type: application/json', ], 'body' => static fn (int $size) => $body->read($size),