### Install factory-payload via Composer Source: https://github.com/baconfy/factory-payload/blob/main/README.md Use this command to add the package to your project dependencies. ```bash composer require --dev baconfy/factory-payload ``` -------------------------------- ### Run project tests Source: https://github.com/baconfy/factory-payload/blob/main/README.md Executes the test suite using the Composer package manager. ```bash composer test ``` -------------------------------- ### Generate request payload with factory-payload Source: https://github.com/baconfy/factory-payload/blob/main/README.md Compares standard manual payload creation with the simplified factory-payload approach. ```php $response = $this->postJson(route('posts.store'), [ 'title' => fake()->sentence(), 'body' => fake()->paragraph(), ]); ``` ```php $response = $this->postJson(route('posts.store'), Post::factory()->payload()); ``` -------------------------------- ### Test payloads with Pest datasets Source: https://github.com/baconfy/factory-payload/blob/main/README.md Integrate factory payloads into Pest tests to validate multiple request scenarios. ```php it('rejects invalid post payloads', function (array $overrides, string $errorField): void { $response = $this->postJson( route('posts.store'), Post::factory()->payload($overrides) ); $response->assertStatus(422)->assertJsonValidationErrors([$errorField]); })->with([ 'missing title' => [['title' => ''], 'title'], 'missing body' => [['body' => ''], 'body'], 'title too long' => [['title' => str_repeat('a', 300)], 'title'], ]); ``` -------------------------------- ### Define payload attributes using #[PayloadAttributes] Source: https://github.com/baconfy/factory-payload/blob/main/README.md Apply the attribute directly to the factory class to specify which fields belong in the HTTP payload. ```php namespace Database\Factories; use App\Models\Post; use Baconfy\FactoryPayload\Attributes\PayloadAttributes; use Illuminate\Database\Eloquent\Factories\Factory; #[PayloadAttributes('title', 'body')] class PostFactory extends Factory { protected $model = Post::class; public function definition(): array { return [ 'title' => fake()->sentence(), 'body' => fake()->paragraph(), 'user_id' => User::factory(), 'published_at' => now(), ]; } } ``` -------------------------------- ### Generate payload from factory Source: https://github.com/baconfy/factory-payload/blob/main/README.md Extract the defined payload attributes from the factory instance. ```php $payload = Post::factory()->payload(); // ['title' => 'Lorem ipsum...', 'body' => 'Dolor sit amet...'] ``` -------------------------------- ### Define payload attributes using HasPayloadAttributes trait Source: https://github.com/baconfy/factory-payload/blob/main/README.md Use the trait and define the $payloadAttributes property within your factory class. ```php namespace Database\Factories; use App\Models\Post; use Baconfy\FactoryPayload\HasPayloadAttributes; use Illuminate\Database\Eloquent\Factories\Factory; class PostFactory extends Factory { use HasPayloadAttributes; protected $model = Post::class; /** * @var array */ protected array $payloadAttributes = ['title', 'body']; public function definition(): array { return [ 'title' => fake()->sentence(), 'body' => fake()->paragraph(), 'user_id' => User::factory(), 'published_at' => now(), ]; } } ``` -------------------------------- ### Override payload fields for validation testing Source: https://github.com/baconfy/factory-payload/blob/main/README.md Demonstrates how to pass an array to the payload method to override specific fields, useful for testing validation rules. ```php $response = $this->postJson( route('posts.store'), Post::factory()->payload(['title' => '']) ); $response->assertJsonValidationErrors(['title']); ``` -------------------------------- ### Resolve payload shape from DTO Source: https://github.com/baconfy/factory-payload/blob/main/README.md Use a DTO class to define the payload structure, either via a static keys method or public properties. ```php namespace App\Data; class PostCreateData { public static function keys(): array { return ['title', 'body']; } } ``` ```php $payload = Post::factory()->payload(PostCreateData::class); // ['title' => 'Lorem ipsum...', 'body' => 'Dolor sit amet...'] ``` ```php class PostUpdateData { public ?string $title = null; public ?string $body = null; } ``` ```php $payload = Post::factory()->payload(PostUpdateData::class); // ['title' => '...', 'body' => '...'] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.