4,000 emails/month for free | Mailtrap sends real emails now!

Laravel Test Factory Helper

mpociot/laravel-test-factory-helper image

Laravel Test Factory Helper stats

Downloads
574.4K
Stars
929
Open Issues
20
Forks
89

View on GitHub →

Generate Laravel test factories from your existing models

Laravel Test Factory Generator

php artisan generate:model-factory

This package will generate factories from your existing models so you can get started with testing your Laravel application more quickly.

Example output

Migration and Model

Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('username');
$table->string('email')->unique();
$table->string('password', 60);
$table->integer('company_id');
$table->rememberToken();
$table->timestamps();
});
 
class User extends Model {
public function company()
{
return $this->belongsTo(Company::class);
}
}

Factory Result

$factory->define(App\User::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'username' => $faker->userName,
'email' => $faker->safeEmail,
'password' => bcrypt($faker->password),
'company_id' => factory(App\Company::class),
'remember_token' => Str::random(10),
];
});

Install

Require this package with composer using the following command:

composer require --dev mpociot/laravel-test-factory-helper

Usage

To generate multiple factories at once, run the artisan command:

php artisan generate:model-factory

This command will find all models within your application and create test factories. By default, this will not overwrite any existing model factories. You can force overwriting existing model factories by using the --force option.

To generate a factory for specific model or models, run the artisan command:

php artisan generate:model-factory User Team

By default, this command will search under the app folder for models. If your models are within a different folder, for example app/Models, you can specify this using --dir option. In this case, run the artisan command:

php artisan generate:model-factory --dir app/Models -- User Team

License

The Laravel Test Factory Helper is free software licensed under the MIT license.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Mpociot Laravel Test Factory Helper Related Articles

Automatically Create Model Factories image

Automatically Create Model Factories

Read article
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit