### Define Signup Request Factory with Data and Files Source: https://github.com/worksome/request-factories/blob/main/README.md This example expands on the basic factory by filling the `definition` method with specific data for fields like phone, email, name, company, bio, and terms acceptance. It also demonstrates the `files` method to handle file uploads, such as a profile picture. ```php namespace Tests\RequestFactories; use Worksome\RequestFactories\RequestFactory; class SignupRequestFactory extends RequestFactory { public function definition(): array { return [ 'phone' => '01234567890', 'email' => 'foo@bar.com', 'name' => 'Luke Downing', 'company' => 'Worksome', 'bio' => $this->faker->words(300, true), 'accepts_terms_and_conditions' => true, ]; } public function files(): array { return [ 'profile_picture' => $this->file()->image('luke.png', 200, 200), ]; } } ``` -------------------------------- ### Bash: Install Request Factories Package Source: https://github.com/worksome/request-factories/blob/main/README.md This command installs the 'request-factories' package as a development dependency in a Laravel project using Composer. It's a standard way to add testing utilities to your project. ```bash composer require --dev worksome/request-factories ``` -------------------------------- ### PHP: Override User Signup Data with State Source: https://github.com/worksome/request-factories/blob/main/README.md This PHP example demonstrates how to override default user signup data using the 'state' method before faking the request. It shows how data passed directly to the HTTP method takes precedence over all factory-defined data. ```php it('can sign up a user with an international phone number', function () { SignupRequest::factory()->state(['name' => 'Oliver Nybroe', 'email' => 'oliver@worksome.com'])->fake(); $this->put('/users', ['email' => 'luke@worksome.com'])->assertValid(); }); ``` -------------------------------- ### Use `fake` Method on Request Factory Globally Source: https://github.com/worksome/request-factories/blob/main/README.md This example shows how to register a request factory globally using the `fake` method. This approach is useful when a single test primarily interacts with one type of request. It's important to call `fake` as the last method on the factory before making the request. ```php it('can sign up a user with an international phone number', function () { SignupRequestFactory::new()->fake(); $this->put('/users')->assertValid(); }); ``` -------------------------------- ### Chain State Modifications with `fakeRequest` Source: https://github.com/worksome/request-factories/blob/main/README.md This example shows how to chain factory state modifications, such as setting specific data fields like 'name', onto the `fakeRequest` method in Pest PHP tests. This allows for customized request data within the test. ```php it('can sign up a user with an international phone number', function () { $this->put('/users')->assertValid(); }) ->fakeRequest(SignupRequest::class) ->state(['name' => 'Jane Bloggs']); ``` -------------------------------- ### Escape Dots in State Method Source: https://github.com/worksome/request-factories/blob/main/UPGRADE.md This PHP snippet shows how to handle attribute keys containing dots in the `state` method of Request Factories. Due to the introduction of dot notation support, keys with dots must now be escaped. ```PHP $data = $factory->state(['worksome.co.uk' => 'Worksome UK'])->create(); ``` ```PHP $data = $factory->state(['worksome\.co\.uk' => 'Worksome UK'])->create(); ``` -------------------------------- ### Remove HasFactory Trait in FormRequest Source: https://github.com/worksome/request-factories/blob/main/UPGRADE.md This snippet demonstrates how to remove the `HasFactory` trait from a `FormRequest` class in PHP. The functionality is now achieved using macros on the `FormRequest` itself, simplifying production dependencies. ```PHP use Illuminate\Foundation\Http\FormRequest; use Worksome\RequestFactories\Concerns\HasFactory; class MyFormRequest extends FormRequest { use HasFactory; } ``` ```PHP use Illuminate\Foundation\Http\FormRequest; class MyFormRequest extends FormRequest { } ``` -------------------------------- ### Run Tests and Static Analysis (Bash) Source: https://github.com/worksome/request-factories/blob/main/README.md Details how to execute the project's test suite and static analysis checks using Composer scripts and Docker Compose. This ensures code quality and helps contributors set up the development environment. ```bash composer test ``` ```bash docker-compose run --rm composer install ``` ```bash docker-compose run --rm composer test ``` -------------------------------- ### Publish and Configure Request Factories (Bash/PHP) Source: https://github.com/worksome/request-factories/blob/main/README.md Provides instructions on how to publish the request factories configuration file using `php artisan vendor:publish` and then modify the `path` and `namespace` settings in `config/request-factories.php` to customize storage locations. ```bash php artisan vendor:publish --tag=request-factories ``` ```php return [ 'path' => base_path('request_factories'), 'namespace' => 'App\\RequestFactories', ]; ``` -------------------------------- ### Define Basic Signup Request Factory Source: https://github.com/worksome/request-factories/blob/main/README.md This snippet shows the basic structure of a `SignupRequestFactory` extending `RequestFactory`. The `definition` method is intended to return an array of data for the form request. ```php namespace Tests\RequestFactories; use Worksome\RequestFactories\RequestFactory; class SignupRequestFactory extends RequestFactory { public function definition(): array { return [ // 'email' => $this->faker->email, ]; } } ``` -------------------------------- ### Fake Multiple Routes in a Single Test (PHP) Source: https://github.com/worksome/request-factories/blob/main/README.md Demonstrates how to use `SignupRequest::fake()` and `ProfileRequest::fake()` to simulate multiple requests within a single test case. This allows for testing complex user flows that involve sequential actions. ```php it('allows a user to sign up and update their profile', function () { SignupRequest::fake(); post('/signup'); ProfileRequest::fake(); post('/profile')->assertValid(); }); ``` -------------------------------- ### PHP: Define dynamic properties using closures Source: https://github.com/worksome/request-factories/blob/main/README.md Explains how to use closures as property values within a factory's `definition` method. The closure receives all other properties, allowing for dynamic data generation based on other fields. ```php class SignupRequestFactory extends RequestFactory { public function definition(): array { return [ 'name' => 'Luke Downing', 'company' => 'Worksome', 'email' => fn ($properties) => Str::of($properties['name']) ->replace(' ', '.') ->append("@{$properties['company']}.com") ->lower() ->__toString(), // luke.downing@worksome.com ]; } } ``` -------------------------------- ### PHP: Test User Signup with International Phone Number Source: https://github.com/worksome/request-factories/blob/main/README.md This snippet demonstrates testing a user signup endpoint with an international phone number. It contrasts a verbose test requiring all form fields with a concise test using Request Factories, highlighting the reduction in boilerplate and improved readability. ```php it('can sign up a user with an international phone number', function () { $this->put('/users', [ 'phone' => '+375 154 767 1088', 'email' => 'foo@bar.com', 'name' => 'Luke Downing', 'company' => 'Worksome', 'bio' => 'Blah blah blah', 'profile_picture' => UploadedFile::fake()->image('luke.png', 200, 200), 'accepts_terms_and_conditions' => true, ]); expect(User::latest()->first()->phone)->toBe('+375 154 767 1088'); }); it('can sign up a user with an international phone number', function () { SignupRequest::fake(); $this->put('/users', ['phone' => '+375 154 767 1088']); expect(User::latest()->first()->phone)->toBe('+375 154 767 1088'); }); ``` -------------------------------- ### Use `create` Method in Tests Source: https://github.com/worksome/request-factories/blob/main/README.md This PHP code demonstrates how to use the `create` method on a request factory within a test. The `create` method returns an array of data, which can then be passed to HTTP testing methods like `put`. ```php it('can sign up a user with an international phone number', function () { $data = SignupRequest::factory()->create(['phone' => '+44 1234 567890']); $this->put('/users', $data)->assertValid(); }); ``` -------------------------------- ### PHP: Nest factories within factories Source: https://github.com/worksome/request-factories/blob/main/README.md Demonstrates how to nest one factory within another to reuse common sets of fields, such as address details. This promotes code reusability and reduces duplication. ```php class SignupRequestFactory extends RequestFactory { public function definition(): array { return [ 'name' => 'Luke Downing', 'company' => 'Worksome', 'address' => AddressRequestFactory::new(), ]; } } ``` -------------------------------- ### Use `fake` and `factory` on Form Requests Source: https://github.com/worksome/request-factories/blob/main/README.md This snippet illustrates how to use the `fake` and `factory` methods directly on a Form Request class, which are automatically registered via macros. This provides a convenient way to instantiate and use request factories without direct instantiation. ```php it('can sign up a user with an international phone number', function () { // Using the factory method... SignupRequest::factory()->fake(); // ...or using the fake method SignupRequest::fake(); $this->put('/users')->assertValid(); }); ``` -------------------------------- ### PHP: Integrate model factories with request factories Source: https://github.com/worksome/request-factories/blob/main/README.md Shows how to instantiate a model factory directly within a request factory definition. This is useful for generating related model data, like a user ID, without persisting unexpected models. ```php class StoreMovieController extends RequestFactory { public function definition(): array { return [ 'name' => 'My Cool Movie' 'owner_id' => User::factory(), ]; } } ``` -------------------------------- ### Bash: Generate Request Factory Artisan Command Source: https://github.com/worksome/request-factories/blob/main/README.md This command uses the Artisan CLI to generate a new Request Factory for a specified Form Request. It follows the convention of creating factories in the `tests/RequestFactories` directory with a `Factory` suffix. ```bash php artisan make:request-factory "App\Http\Requests\SignupRequest" php artisan make:request-factory SignupRequestFactory ``` -------------------------------- ### Use `fakeRequest` in Pest PHP Source: https://github.com/worksome/request-factories/blob/main/README.md This demonstrates the use of the `fakeRequest` higher-order method in Pest PHP for testing. It shows how to specify the Form Request class, the Request Factory class, or a closure returning a factory to mock the request. ```php // You can provide the form request FQCN... it('can sign up a user with an international phone number', function () { $this->put('/users')->assertValid(); })->fakeRequest(SignupRequest::class); // Or the request factory FQCN... it('can sign up a user with an international phone number', function () { $this->put('/users')->assertValid(); })->fakeRequest(SignupRequestFactory::class); // Or even a closure that returns a request factory... it('can sign up a user with an international phone number', function () { $this->put('/users')->assertValid(); })->fakeRequest(fn () => SignupRequest::factory()); ``` -------------------------------- ### PHP: Fake generic requests without form requests Source: https://github.com/worksome/request-factories/blob/main/README.md Explains how to use factories to fake generic requests when a specific `FormRequest` class is not used. This is applicable for controllers that handle data directly. ```php it('lets a guest sign up to the newsletter', function () { NewsletterSignupFactory::new()->fake(); post('/newsletter', ['email' => 'foo@bar.com'])->assertRedirect('/thanks'); }); ``` -------------------------------- ### PHP: Add state to a factory with a custom method Source: https://github.com/worksome/request-factories/blob/main/README.md Demonstrates how to add a custom state transformer method to a factory class. This allows for declarative methods to modify factory states, such as setting a profile picture. ```php class SignupRequestFactory extends RequestFactory { // After the definition... public function withOversizedProfilePicture(): static { return $this->state(['profile_picture' => $this->file()->image('profile.png', 2001, 2001)]) } } // In our test... it('does not allow profile pictures larger than 2000 pixels', function () { SignupRequest::factory()->withOversizedProfilePicture()->fake(); $this->put('/users')->assertInvalid(['profile_picture' => 'size']); }); ``` -------------------------------- ### PHP: Use dot-notation in state for nested data Source: https://github.com/worksome/request-factories/blob/main/README.md Shows how to use dot-notation within the `state` method to modify deeply nested keys in request data. This is useful for updating specific fields within complex structures like addresses. ```php it('requires a postcode with the first line of an address', function () { SignupRequest::factory()->state(['address.line_one' => '1 Test Street'])->fake(); $this->put('/users')->assertInvalid(['address.postcode' => 'required']); }); ``` -------------------------------- ### PHP: Resolve CouldNotLocateRequestFactoryException with $factory property Source: https://github.com/worksome/request-factories/blob/main/README.md Provides a solution for the `CouldNotLocateRequestFactoryException` by defining a static `$factory` property on the `FormRequest` class to explicitly link it to its corresponding factory. ```php class SignupRequest extends FormRequest { public static $factory = SignupRequestFactory::class; } ``` -------------------------------- ### PHP: Omit a property from a factory using 'without' Source: https://github.com/worksome/request-factories/blob/main/README.md Illustrates how to use the `without` method to remove a specific property from a factory's generated data. This is helpful when a field is conditionally required or should not be present. ```php it('requires an email address', function () { SignupRequest::factory()->without('email')->fake(); $this->put('/users')->assertInvalid(['email' => 'required']); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.