From 1b2bb51b0885045d4e37078409d65697fe8a29d7 Mon Sep 17 00:00:00 2001 From: parsilver Date: Mon, 15 May 2023 17:48:40 +0700 Subject: [PATCH] Fix --- src/Http/Response.php | 10 +++++++--- tests/ResponseTest.php | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Http/Response.php b/src/Http/Response.php index fa2e952..265b1f3 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -15,7 +15,7 @@ class Response implements ResponseInterface protected $jsonDecoded; /** - * @var string + * @var string|null */ protected $content; @@ -26,7 +26,7 @@ public function __construct( protected PsrRequestInterface $request, protected PsrResponseInterface $response ) { - $this->content = $this->response->getBody()->getContents(); + // } /** @@ -42,6 +42,10 @@ public function statusCode(): int */ public function body(): string { + if (is_null($this->content)) { + $this->content = $this->response->getBody()->getContents(); + } + return $this->content; } @@ -69,7 +73,7 @@ public function isSuccessfull(): bool public function json(?string $key = null): mixed { if (is_null($this->jsonDecoded)) { - $this->jsonDecoded = @json_decode($this->content, true) ?: false; + $this->jsonDecoded = @json_decode($this->body(), true) ?: false; } if ($this->jsonDecoded === false) { diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index 4dba719..903f267 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -8,7 +8,7 @@ it('should return the psr response', function () { $response = new Response( \Mockery::mock(PsrRequestInterface::class), - \Mockery::mock(PsrResponseInterface::class) + \Mockery::mock(PsrResponseInterface::class), ); expect($response->getPsrResponse())->toBeInstanceOf(PsrResponseInterface::class);