missionx-co/laravel-better-http-fake
最新稳定版本:0.0.4
Composer 安装命令:
composer require missionx-co/laravel-better-http-fake
包简介
A package that provides improved helpers for faking Laravel HTTP client requests.
README 文档
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
统计信息
- 总下载量: 15
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-05