Skip to content

Commit

Permalink
Merge pull request #231 from Sammyjo20/fix/support-null-mock-responses
Browse files Browse the repository at this point in the history
(V1) Fix | Use of `null` in MockResponse data
  • Loading branch information
Sammyjo20 committed Jun 6, 2023
2 parents 5e7d03d + 215a7af commit 38ecead
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/Http/MockResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ public function getFormattedData(): mixed
return json_encode($data);
}

if (empty($data)) {
return null;
}

return $data;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Traits/CollectsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/Traits/CollectsHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) ?? '';
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/MockResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
});

0 comments on commit 38ecead

Please sign in to comment.