Skip to content

Commit 6c706fe

Browse files
authored
Merge pull request #28 from jasonmccreary/streamline
Update and streamline package for common use case
2 parents 3d122e7 + b3727f8 commit 6c706fe

File tree

4 files changed

+115
-140
lines changed

4 files changed

+115
-140
lines changed

README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
## Laravel Test Factory Generator
22

3-
`php artisan test-factory-helper:generate`
3+
`php artisan generate:model-factory`
44

5-
This package helps you generate model factories from your existing models / database structure to get started with testing your Laravel application even faster.
5+
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.
66

77
### Example output
88

@@ -37,7 +37,7 @@ $factory->define(App\User::class, function (Faker\Generator $faker) {
3737
'email' => $faker->safeEmail,
3838
'password' => bcrypt($faker->password),
3939
'company_id' => factory(App\Company::class)->create()->id,
40-
'remember_token' => str_random(10),
40+
'remember_token' => Str::random(10),
4141
];
4242
});
4343
```
@@ -48,25 +48,24 @@ $factory->define(App\User::class, function (Faker\Generator $faker) {
4848
Require this package with composer using the following command:
4949

5050
```bash
51-
composer require mpociot/laravel-test-factory-helper
52-
```
53-
Go to your `config/app.php` and add the service provider:
54-
55-
```php
56-
Mpociot\LaravelTestFactoryHelper\TestFactoryHelperServiceProvider::class
51+
composer require --dev mpociot/laravel-test-factory-helper
5752
```
5853

5954
### Usage
6055

61-
Just call the artisan command:
56+
To generate multiple factories at once, run the artisan command:
57+
58+
`php artisan generate:model-factory`
59+
60+
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.
6261

63-
`php artisan test-factory-helper:generate`
62+
To generate a factory for specific model or models, run the artisan command:
6463

65-
This command will look for all models in your "app" folder (configurable by using the `--dir` option) and create test factories and save them in your `database/factories/ModelFactory.php`.
64+
`php artisan generate:model-factory User Team`
6665

67-
The output filename is also configurable by using the `--filename` option.
66+
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:
6867

69-
By default, the command will only append new models and doesn't modify the existing content of your factories file. To rewrite the file, use the `--reset` option.
68+
`php artisan generate:model-factory --dir app/Models -- User Team`
7069

7170
### License
7271

composer.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
"keywords": ["Laravel", "Tests", "Factory"],
55
"license": "MIT",
66
"require": {
7-
"php": ">=5.6.0",
8-
"illuminate/support": "^5.0",
9-
"illuminate/console": "^5.0",
10-
"illuminate/filesystem": "^5.0",
11-
"symfony/class-loader": "^2.3|3.*",
7+
"php": ">=7.0",
8+
"illuminate/support": "^5.5",
9+
"illuminate/console": "^5.5",
10+
"illuminate/filesystem": "^5.5",
1211
"doctrine/dbal": "~2.3"
1312
},
1413
"autoload": {

resources/views/factory.blade.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
/* @@var $factory \Illuminate\Database\Eloquent\Factory */
12

2-
$factory->define({{$reflection->getName()}}::class, function (Faker\Generator $faker) {
3+
use Faker\Generator as Faker;
4+
5+
$factory->define({{ $reflection->getName() }}::class, function (Faker $faker) {
36
return [
47
@foreach($properties as $name => $property)
5-
'{{$name}}' => @if($property['faker']){!!$property['type']!!}@else'{{$property['type']}}'@endif,
8+
'{{$name}}' => {!! $property !!},
69
@endforeach
710
];
811
});
9-

0 commit comments

Comments
 (0)