[8.x] Clear recorded calls when calling Http::fake() #40194
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR aims to clear the
$recorded
array onIlluminate\Http\Client\Factory
class.Background
Currently, i'm creating a test to ensure that i will never login to an api while i still have a valid bearer token. The token management is handled with cache, with the token being stored in cache for
now() + $access_token['expires_in']
seconds. It works fine, but in my tests, in order to ensure that i'll only call the login api the token is already expired, i mock thelogin
response usingHttp::fake()
, freeze the time (using spatie's test-time package), then call the login api, receive and store the token in cache. Then, i callHttp::fake()
one more time (to forget recorded calls) and call the api again, with time freezed and token in cache yet. Now, as i haven't called the login api (due to token being in cache), i want to assert that nothing was sent usingHttp::assertNothingSent()
, but the problem is: The$recorded
property are never cleared when i callfake
, and the assertion fails.Here's some code snippets to illustrate what i'm explaining:
Solution
As a proposed solution, i've added a call to clear the
$recorded
array within thefake
method: