Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mpociot/laravel-test-factory-helper
Generate Laravel test factories from your existing models
https://github.com/mpociot/laravel-test-factory-helper
laravel laravel-5-package
Last synced: 4 days ago
JSON representation
Generate Laravel test factories from your existing models
- Host: GitHub
- URL: https://github.com/mpociot/laravel-test-factory-helper
- Owner: mpociot
- Created: 2016-03-26T23:41:49.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2024-01-18T04:49:12.000Z (about 1 year ago)
- Last Synced: 2025-02-06T11:52:25.661Z (5 days ago)
- Topics: laravel, laravel-5-package
- Language: PHP
- Size: 80.1 KB
- Stars: 935
- Watchers: 11
- Forks: 88
- Open Issues: 20
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-laravel-framework - Laravel Test Factory Generator - Generate Laravel test factories from your existing models (Popular Packages)
- awesome-laravel - Laravel Test Factory Generator - Generate Laravel test factories from your existing models (Popular Packages)
README
## Laravel Test Factory Generator
`php artisan generate:model-factory`
This package will generate [factories](https://laravel.com/docs/master/database-testing#writing-factories) from your existing models so you can get started with testing your Laravel application more quickly.
### Example output
#### Migration and Model
```php
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
```php
$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:
```bash
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.