1. Go to this page and download the library: Download worksome/request-factories library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
worksome / request-factories example snippets
it('can sign up a user with an international phone number', function () {
$this->put('/users', [
'phone' => '+375 154 767 1088',
'email' => '[email protected]', 🙄
'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');
});
namespace Tests\RequestFactories;
use Worksome\RequestFactories\RequestFactory;
class SignupRequestFactory extends RequestFactory
{
public function definition(): array
{
return [
// 'email' => $this->faker->email,
];
}
}
namespace Tests\RequestFactories;
use Worksome\RequestFactories\RequestFactory;
class SignupRequestFactory extends RequestFactory
{
public function definition(): array
{
return [
'phone' => '01234567890',
'email' => '[email protected]',
'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),
];
}
}
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();
});
it('can sign up a user with an international phone number', function () {
SignupRequestFactory::new()->fake();
$this->put('/users')->assertValid();
});
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();
});
// 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());
it('can sign up a user with an international phone number', function () {
$this->put('/users')->assertValid();
})
->fakeRequest(SignupRequest::class)
->state(['name' => 'Jane Bloggs']);
it('can sign up a user with an international phone number', function () {
SignupRequest::factory()->state(['name' => 'Oliver Nybroe', 'email' => '[email protected]'])->fake();
$this->put('/users', ['email' => '[email protected]'])->assertValid();
});
// In our factory...
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']);
});
it(' SignupRequest::factory()->state(['address.line_one' => '1 Test Street'])->fake();
$this->put('/users')->assertInvalid(['address.postcode' => '
class SignupRequestFactory extends RequestFactory
{
public function definition(): array
{
return [
'name' => 'Luke Downing',
'company' => 'Worksome',
'address' => AddressRequestFactory::new(),
];
}
}
class StoreMovieController extends RequestFactory
{
public function definition(): array
{
return [
'name' => 'My Cool Movie'
'owner_id' => User::factory(),
];
}
}
it('lets a guest sign up to the newsletter', function () {
NewsletterSignupFactory::new()->fake();
post('/newsletter', ['email' => '[email protected]'])->assertRedirect('/thanks');
});
class SignupRequest extends FormRequest
{
public static $factory = SignupRequestFactory::class;
}
it('allows a user to sign up and update their profile', function () {
SignupRequest::fake();
post('/signup');
ProfileRequest::fake();
post('/profile')->assertValid();
});