mpociot / laravel-test-factory-helper
从您的现有模型生成Laravel测试工厂
v2.1.0
2020-03-02 16:04 UTC
Requires
- php: ^7.2.5
- doctrine/dbal: ^2.9
- illuminate/console: ^6.0|^7.0
- illuminate/filesystem: ^6.0|^7.0
- illuminate/support: ^6.0|^7.0
README
php artisan generate:model-factory
此包将从您的现有模型生成工厂,以便您能更快地开始测试您的Laravel应用程序。
示例输出
迁移和模型
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->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), ]; });
安装
使用以下命令通过composer安装此包
composer require --dev mpociot/laravel-test-factory-helper
使用方法
要一次生成多个工厂,请运行以下artisan命令
php artisan generate:model-factory
此命令将在您的应用程序中查找所有模型并创建测试工厂。默认情况下,此操作不会覆盖任何现有模型工厂。您可以使用force选项强制覆盖现有模型工厂。
要为特定的模型或多个模型生成工厂,请运行以下artisan命令
php artisan generate:model-factory User Team
默认情况下,此命令将在app
文件夹下搜索模型。如果您的模型位于不同的文件夹中,例如app/Models
,您可以使用--dir
选项指定此文件夹。在这种情况下,请运行以下artisan命令
php artisan generate:model-factory --dir app/Models -- User Team
许可证
Laravel Test Factory Helper是免费软件,受MIT许可证的许可。