From 215a7af5d65bb68ac7bdb6d4fc3a4c7c8d1d69de Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Tue, 6 Jun 2023 20:49:39 +0100 Subject: [PATCH] (V1) Fix used in MockResponse data --- src/Http/MockResponse.php | 4 ++++ src/Traits/CollectsData.php | 2 +- src/Traits/CollectsHeaders.php | 6 +++--- tests/Unit/MockResponseTest.php | 9 +++++++++ 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Http/MockResponse.php b/src/Http/MockResponse.php index 7d43409d..ef2c8c9a 100644 --- a/src/Http/MockResponse.php +++ b/src/Http/MockResponse.php @@ -148,6 +148,10 @@ public function getFormattedData(): mixed return json_encode($data); } + if (empty($data)) { + return null; + } + return $data; } diff --git a/src/Traits/CollectsData.php b/src/Traits/CollectsData.php index 8c5b0f41..18771446 100644 --- a/src/Traits/CollectsData.php +++ b/src/Traits/CollectsData.php @@ -76,7 +76,7 @@ public function addData(string $data, $value): static } /** - * Get all data or filter with a key. + * Get all data or filter with a key. * * @param string|null $key * @return mixed diff --git a/src/Traits/CollectsHeaders.php b/src/Traits/CollectsHeaders.php index 7f89e503..c08114c1 100644 --- a/src/Traits/CollectsHeaders.php +++ b/src/Traits/CollectsHeaders.php @@ -96,12 +96,12 @@ public function getHeaders(string $key = null): mixed /** * Get an individual header * - * @param string|null $key - * @return array + * @param string $key + * @return string */ public function getHeader(string $key): string { - return $this->getHeaders($key); + return $this->getHeaders($key) ?? ''; } /** diff --git a/tests/Unit/MockResponseTest.php b/tests/Unit/MockResponseTest.php index 6a2b5537..22adfcf2 100644 --- a/tests/Unit/MockResponseTest.php +++ b/tests/Unit/MockResponseTest.php @@ -68,3 +68,12 @@ expect($response)->customCastMethod()->toBeInstanceOf(UserData::class); expect($response)->foo()->toBe('bar'); }); + +test('a mock response can have null for the data', function () { + $mockClient = new MockClient([MockResponse::make(null, 200)]); + $request = new UserRequest(); + + $response = $request->send($mockClient); + + expect($response->body())->toEqual(''); +});