Download the PHP package baconfy/factory-payload without Composer
On this page you can find all versions of the php package baconfy/factory-payload. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package factory-payload
Why?
When testing HTTP endpoints, you need request payloads that match the shape your endpoint expects, not the shape your model stores.
The simple case
Without this package:
With this package:
One line. Self-documenting. Reusable across every test that hits this endpoint.
When you need overrides
Sometimes you need to control specific fields, for example to test validation or assert against known values:
Overrides always pass through, even if the field isn't part of the model's stored attributes (useful for things like password_confirmation).
Installation
Requires PHP 8.3+ and Laravel 11, 12 or 13.
Usage
This package supports three equivalent ways to declare which attributes belong in the HTTP payload. Choose the style that best matches your project.
Using #[PayloadAttributes]
Declare the payload attributes directly on the factory class:
Using HasPayloadAttributes
Add the HasPayloadAttributes trait and define $payloadAttributes on your factory:
Both examples produce the same payload in your tests:
Notice how user_id and published_at are automatically excluded because they belong to persistence, not to the HTTP request.
Using a DTO class
If your project already declares request shapes through Data Transfer Objects (DTOs), you can resolve the payload shape directly from the DTO class. Useful when you have multiple endpoints (create, update, etc.) sharing the same model.
The DTO resolution follows these rules:
- If the class has a static
keys(): arraymethod, its return value is used as the whitelist (compatible withspatie/laravel-dataand similar libraries). - Otherwise, falls back to the class's public properties via Reflection:
If the class doesn't exist, an InvalidArgumentException is thrown with a clear message.
Note: When passing a DTO class, overrides are not supported in the same call. If you need both, use the array form:
payload(['title' => 'custom']).
With Pest datasets
Test multiple invalid scenarios in one go:
Behavior
The behavior below applies to all three ways of declaring payload attributes:
| Scenario | Result |
|---|---|
| No payload attributes declared | Returns only the overrides |
#[PayloadAttributes] or $payloadAttributes declared |
Filters raw() by whitelist, then merges overrides |
| Override key exists in whitelist | Override wins |
| Override key not in whitelist | Override still passes through |
Factory has count() set |
payload() still returns a single array |
DTO class passed to payload() |
Resolves shape from keys() or public properties |
| Invalid DTO class passed | Throws InvalidArgumentException |
Testing
Credits
- Renato Dehnhardt
- Josh Donnell — for adding the
#[PayloadAttributes]attribute support - All contributors
License
See LICENSE for details.