This repository has been archived! Use Shift's Factory Generator to utilize the new class factories of Laravel 8.
Factories are a great concept and I really love to use them for testing purposes, but it sucks to write every needed column name and associated faker methods by hand. This package aims to make the process less painful by providing a command to prefill your factories!
After creating a factory with the artisan cli you end up having something like this:
<?php
use Faker\Generator as Faker;
$factory->define(Model::class, function (Faker $faker) {
return [
//
];
});
With laravel-factory-prefill
you can just skip the previous command call and instead execute php artisan factory:prefill Habit
.
composer require naoray/laravel-factory-prefill --dev
After running php artisan migrate
you are good to go. If you want the factory:prefill
command to notice the model relations, you should implement the methods first!
Tip: If you also want the realtionships to be loaded automatically, you have to define the methods in the models.
To generate factories for all models run
php artisan factory:all
To prefill factories from models outside of the app/
directory just add the -P
flag and provide the path.
php artisan factory:all --path=Some/Other/Path
you can also append the --realpath
option to indicate that the given path is a pre-resolved absolut path.
To fill a single factory you can either run php artisan factory:prefill model_name
or php artisan factory:all model_name
.
To prefill factories from models outside of the App/
namespace just add the -O
flag and provide the full path in the model name.
php artisan factory:prefill "Some\Other\Namespace\ModelName" -O
By default nullable
columns are ignored. If you want to also add nullable
columns to your factory includ the flag -N
or --allow-nullable
.
php artisan factory:prefill ModelName -N
or
php artisan factory:all -N
Run the tests with:
vendor/bin/phpunit
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security-related issues, please email krishan.koenig@googlemail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.