missionx-co / laravel-better-http-fake
A package that provides improved helpers for faking Laravel HTTP client requests.
0.0.3
2025-05-05 15:51 UTC
Requires
- php: ^8.2
- illuminate/contracts: ^10.0||^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- spatie/invade: ^2.1
This package is auto-updated.
Last update: 2025-05-05 15:52:02 UTC
README
A package that provides improved helpers for faking Laravel HTTP client requests.
Current Laravel HTTP Fake Workflow
Example Service Class
class Service { public function createInstance($data) { return $this->client()->post('/instances', $data)->json(); } public function client(): PendingRequest { return Http::baseUrl('https://service.com') ->withToken(config('services.service.api_key')); } }
Testing Steps (Without This Package)
1. Fake the response:
Http::fake([ 'https://service.com/instances' => Http::response(['key' => 'value'], 204) ]);
2. Trigger the request
(new Service)->createInstance(['bodyKey' => 'bodyValue']);
3. Manually verify the request:
Http::assertSent(function($request) { return $request->url() == "https://service.com/instances" && $request->method() == 'POST' && $request->data() == ['bodyKey' => 'bodyValue']; });
With Laravel Better Http Fake
Basic Usage
(new Service)->client() ->shouldMakePostRequestTo('instances') ->withData(['bodyKey' => 'bodyValue']) // Verify payload ->andRespondWith(['key' => 'value']); // Fake response (new Service)->createInstance($body); // Execute
Key Features
1. Skip Request Verification
->doNotAssertSent(); // Only fake the response, skip checks
2. Multiple Responses (Sequence)
->andRespondWithSequence( Http::sequence([ Http::response([]), // First response Http::response([]), // Second response ]) );
3. Headers Validation
->withHeaders(['Header-Key' => 'Header-Value']); // Verify headers
4. File Upload Verification
->withFile('photo', 'profile.jpg'); // Verify file uploads
5. Test Multiple HTTP Verbs
Supports all common HTTP methods with dedicated methods:
// GET Request ->shouldMakeGetRequestTo('users/1') // PUT Request ->shouldMakePutRequestTo('users/1') // PATCH Request ->shouldMakePatchRequestTo('profile') // DELETE Request ->shouldMakeDeleteRequestTo('posts/123')
Why Use This Package?
- Simpler tests: Combine faking + assertions in one chain
- Clearer syntax: Reads like plain English instructions
- Fewer mistakes: Automatic request validation by default
- Time saver: No more separate setup/verification steps