Skip to content

Commit

Permalink
Update README.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDoctor0 committed Aug 14, 2022
1 parent 19ae15a commit 07dfb3f
Showing 1 changed file with 66 additions and 50 deletions.
116 changes: 66 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
[![Packagist](https://img.shields.io/packagist/dt/TheDoctor0/laravel-factory-generator.svg)](https://packagist.org/packages/TheDoctor0/laravel-factory-generator)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/TheDoctor0/laravel-factory-generator/blob/master/LICENSE.md)

This package will generate [factories](https://laravel.com/docs/master/database-testing#writing-factories) from your existing models.
[![Banner](https://banners.beyondco.de/Laravel%20Factory%20Generator.png?theme=light&packageManager=composer+require&packageName=thedoctor0%2Flaravel-factory-generator+--dev&pattern=architect&style=style_1&description=Automatically+generate+test+factories+for+all+your+models&md=1&showWatermark=1&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg)]()

That way you can get started with testing your Laravel or Lumen application more quickly!
Automatically generate [factories](https://laravel.com/docs/master/database-testing#writing-factories) from your existing models.

It is a forked version of [mpociot/laravel-test-factory-helper](https://github.com/mpociot/laravel-test-factory-helper) package.
It will allow you to write tests containing your models much faster.

## Installation

Expand All @@ -18,57 +18,13 @@ You can install the package via composer:
composer require thedoctor0/laravel-factory-generator --dev
```

For Lumen it is also required to register `FactoryGeneratorServiceProvider`.

## Usage

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

`php artisan generate:factory`

This command will find all models within your application and create test factories.

---

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

`php artisan generate:factory User Company`

---

By default, generation will not overwrite any existing model factories.

You can _force_ overwriting existing model factories by using the `--force` option:

`php artisan generate:factory --force`

---

By default, it will search recursively for models under the `app/Models` (Laravel 8.x and up) or `app` for (Laravel 6.x and 7.x).

If your models are within a different folder, you can specify this using `--dir` option.

In this case, run the artisan command:

`php artisan generate:factory --dir app/Models`

---

If your models are within a different namespace, you can specify it using `--namespace` option.

You just need to execute this artisan command:

`php artisan generate:factory --dir vendor/package/src/Models --namespace CustomNamespace\\Models`

---

By default, your model directory structure is not taken into account, even though it has subdirectories.

You can reflect it to `database/factories` directory by using the `--recursive` option:

`php artisan generate:factory --recursive`

---
It will find all models and generate test factories based on the database structure and model relations.

### Example

Expand Down Expand Up @@ -98,6 +54,12 @@ class User extends Model {
For Laravel 6.x and 7.x:

```php
<?php

declare(strict_types=1);

use Faker\Generator as Faker;

$factory->define(App\User::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
Expand All @@ -112,7 +74,19 @@ $factory->define(App\User::class, function (Faker\Generator $faker) {

For Laravel 8.x and up:
```php
class UserFactory extends Factory
<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Models\Contact;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends Factory<\App\Models\User>
*/
final class UserFactory extends Factory
{
/**
* The name of the factory's corresponding model.
Expand Down Expand Up @@ -140,6 +114,48 @@ class UserFactory extends Factory
}
```

## Advanced usage

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

`php artisan generate:factory User Company`

---

By default, generation will not overwrite any existing model factories.

You can _force_ overwriting existing model factories by using the `--force` option:

`php artisan generate:factory --force`

---

By default, it will search recursively for models under the `app/Models` (Laravel 8.x and up) or `app` for (Laravel 6.x and 7.x).

If your models are within a different folder, you can specify this using `--dir` option.

In this case, run the artisan command:

`php artisan generate:factory --dir app/Models`

---

If your models are within a different namespace, you can specify it using `--namespace` option.

You just need to execute this artisan command:

`php artisan generate:factory --dir vendor/package/src/Models --namespace CustomNamespace\\Models`

---

By default, your model directory structure is not taken into account, even though it has subdirectories.

You can reflect it to `database/factories` directory by using the `--recursive` option:

`php artisan generate:factory --recursive`

---

## License

The MIT License (MIT). Please see [license file](LICENSE.md) for more information.

0 comments on commit 07dfb3f

Please sign in to comment.