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),