From e0a0e2efd6452fadb15608890a238900886e112f Mon Sep 17 00:00:00 2001 From: Giso Stallenberg Date: Wed, 17 Nov 2021 15:35:23 +0100 Subject: [PATCH] Cast the stream to string instead of getting the contents PHP FIG PSR-7 describes getContents as "Returns the *remaining* contents in a string", while the interface description mentions "[...] including serialization of the entire stream to a string.". For some implementations of PSR-7 the stream is not rewinded and thus getContents turns out to be an empty string in some cases. Also see https://github.com/php-fig/http-message/blob/efd67d1dc14a7ef4fc4e518e7dee91c271d524e4/src/StreamInterface.php#L136-L143 and https://github.com/Nyholm/psr7/issues/135#issuecomment-546593859. --- src/Results.php | 2 +- tests/ClientTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Results.php b/src/Results.php index 98d869f..4bdfc43 100644 --- a/src/Results.php +++ b/src/Results.php @@ -40,7 +40,7 @@ class Results public function __construct(ResponseInterface $response, $asArray = false) { $this->responseObject = $response; - $this->responseBody = $this->responseObject->getBody()->getContents(); + $this->responseBody = (string) $this->responseObject->getBody(); $this->results = json_decode($this->responseBody, $asArray); // Check if any errors exist, and throw exception if they do diff --git a/tests/ClientTest.php b/tests/ClientTest.php index c24e941..a2d52e3 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -79,7 +79,7 @@ public function testConstructClient() /** @var Request $firstRequest */ $firstRequest = $container[0]['request']; - $this->assertEquals('{"query":"query_string","variables":{}}', $firstRequest->getBody()->getContents()); + $this->assertEquals('{"query":"query_string","variables":{}}', (string) $firstRequest->getBody()); $this->assertSame('POST', $firstRequest->getMethod()); /** @var Request $thirdRequest */ @@ -92,7 +92,7 @@ public function testConstructClient() /** @var Request $secondRequest */ $secondRequest = $container[2]['request']; - $this->assertEquals('{"query":"query_string","variables":{"name":"val"}}', $secondRequest->getBody()->getContents()); + $this->assertEquals('{"query":"query_string","variables":{"name":"val"}}', (string) $secondRequest->getBody()); /** @var Request $fourthRequest */ $fourthRequest = $container[3]['request'];