- Can I use psr-mock/http to test Laravel middleware like auth or CORS?
- Yes, this package generates valid PSR-7 `ServerRequestInterface` and `ResponseInterface` objects, which Laravel middleware expects. You can mock headers, query params, and cookies to simulate real requests for middleware testing. For example, test CORS by setting the `Origin` header or validate auth middleware with custom user claims in the request.
- How do I install psr-mock/http in a Laravel project?
- Run `composer require psr-mock/http` in your project directory. The package has no Laravel-specific dependencies and works alongside existing PSR-7 implementations like `nyholm/psr7` or `symfony/http-foundation`. No additional configuration is needed for basic usage.
- Does this package support Laravel’s Request facade methods like `input()` or `ajax()`?
- No, this package strictly follows PSR-7 standards and doesn’t include Laravel-specific methods. You’ll need to create a wrapper class (e.g., extending `MockRequest`) to add Laravel-specific functionality like `withInput()` or `withAjax()`. Example: `return $this->withParsedBody($input)->withHeader('X-Requested-With', 'XMLHttpRequest');`
- Will psr-mock/http work with Laravel’s built-in HTTP tests (e.g., `HttpTests`)?
- Yes, it integrates seamlessly with Laravel’s testing tools. Use it to replace manual mocks in unit tests while keeping `HttpTests` for integration-level validation. For example, mock a `ServerRequest` for a controller test, then use Laravel’s `HttpTests` to verify the full response lifecycle.
- Can I use this package to test API responses with JSON payloads?
- Absolutely. The package lets you set custom body streams, including JSON payloads. For example, create a mock response with `->withBody(json_encode(['data' => 'test']))` and set the `Content-Type: application/json` header. This is ideal for testing API controllers or middleware that transform responses.
- What Laravel versions does psr-mock/http support?
- This package is PSR-7 agnostic, so it works with any Laravel version (5.5+) that uses PSR-7-compliant HTTP components. Laravel 8+ uses `symfony/http-foundation` under the hood, which is fully compatible. For older versions, ensure your PSR-7 adapter (e.g., `nyholm/psr7`) is up to date.
- How does this compare to manually creating mocks with PHPUnit’s `createMock()`?
- This package is more ergonomic and readable. Instead of manually stubbing methods like `getUri()`, `getServerParams()`, or `getBody()`, you chain fluent methods like `->withUri('/test')->withServerParams([])`. It reduces boilerplate and makes tests easier to maintain, especially for complex request/response scenarios.
- Are there any performance concerns using mocks in Laravel tests?
- No, this package is lightweight and designed for speed. Mocks eliminate the need for external HTTP servers or clients, making tests execute faster. However, avoid overusing mocks for integration tests—pair them with Laravel’s `HttpTests` or Dusk for full-stack validation to catch edge cases like chunked encoding or timeouts.
- Can I use psr-mock/http with PestPHP for cleaner test syntax?
- Yes, the package’s fluent API works well with PestPHP. For example, use `it('tests a middleware', fn() => $this->mockRequest()->withUri('/test')->passesThrough(Middleware::class))` to create concise, readable tests. Pest’s syntax complements the package’s simplicity.
- What if I need to test Laravel-specific features like file uploads (UploadedFile)?
- This package doesn’t cover Laravel-specific classes like `UploadedFile`. For those cases, you’ll need to manually mock them using PHPUnit or combine this package with Laravel’s `createUploadedFile()` helper. Example: `$request = (new MockRequest())->withUploadedFiles(['file' => UploadedFile::fake()->image('test.jpg')]);`