psr-mock/http
psr-mock/http is a tiny PHP package for mocking PSR-7 HTTP messages in tests. Create predictable request/response objects without a full HTTP client, helping you unit test middleware, clients, and services with minimal setup.
symfony/http-foundation or nyholm/psr7), reducing friction in middleware, route handler, and HTTP client testing.HttpFoundation mocks or Guzzle clients) for unit tests, improving speed and determinism.Illuminate\Http\Middleware) by providing mock ServerRequest/Response objects with preconfigured attributes.nyholm/psr7).Request facade methods like input(), ajax()), requiring wrappers or extensions.HttpTests, RefreshDatabase) when combined with Mockery or PHPUnit.Request features could diverge from upstream updates.Request facade methods (e.g., old(), session())? If so, how will these be bridged?createMock(ServerRequestInterface::class)), or supplement them for specific scenarios?Symfony\Component\HttpFoundation\File\UploadedFile) that this package doesn’t cover?Illuminate\Http\Request/Response.DatabaseMigrations trait).psr-mock/http and compare:
// Before (Manual Mock)
$request = $this->createMock(ServerRequestInterface::class);
$request->method('getUri')->willReturn(new Uri('/test'));
$request->method('getServerParams')->willReturn([]);
// After (psr-mock/http)
$request = (new MockRequest())
->withUri('/test')
->withServerParams([]);
tests/TestHelpers) to extend mocks with Laravel-specific methods:
class LaravelRequest extends MockRequest {
public function withInput(array $input): self {
return $this->withParsedBody($input);
}
}
nyholm/psr7) or symfony/http-foundation.Request/Response.createTestRequest()) with package instances.dd().var_dump($request->getBody()->getContents()) for inspection.HttpFoundation for unsupported cases.TESTING.md guide with:
--parallel).| Risk | Mitigation | Detection |
|---|---|---|
| Over-mocking | Enforce a rule: "No mocks for external services" (use HTTP clients instead). | Code reviews, test naming conventions. |
| Laravel-specific gaps | Maintain a LaravelRequest wrapper class. |
Test coverage reports. |
| Test flakiness | Pair with integration tests (e.g., HttpTestCase). |
CI test stability metrics. |
| Package abandonment | Fork or create a Laravel-specific version if needed. | Monitor GitHub activity. |
| CI slowdowns | Cache mock instances or use Pest’s --min-threads. |
CI benchmarking. |
MockRequest::withJsonBody()).How can I help you explore Laravel packages today?