Skip to content

Commit c64a3fa

Browse files
committed
Add factory option for the model generation
1 parent affbee7 commit c64a3fa

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

readme.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ Laravel 5.2 - 5.3 : v3.0.3
1111
Laravel 5.4 : v4.1.9
1212
Laravel 5.5 - 5.8 : v5.0.0+
1313
Laravel 6.0 : v5.1.0+
14-
Laravel 7.0 : v6.0
15-
Laravel 8.0 : v7.0+
14+
Laravel 7.0 : v6.x
15+
Laravel 8.0 : v7.x
16+
Laravel 9.0 : v8.x
1617
```
1718

1819
## Commands
@@ -42,10 +43,11 @@ php artisan generate:exception
4243
```
4344

4445
### Option for all the commands
45-
`--force` This will override the existing file, if it exists.
46+
- `--force` This will override the existing file, if it exists.
47+
- `--test` This will also generate a test file.
4648

4749
### Option for all the commands, except `views` and `migration:pivot`
48-
`--plain` This will use the .plain stub of the command (generate an empty controller)
50+
- `--plain` This will use the .plain stub of the command (generate an empty controller)
4951

5052
### Customization
5153
This is for all except the `migration` and `migration:pivot` commands
@@ -123,6 +125,8 @@ Run `php artisan` command to see the new commands in the `generate:*` section
123125
php artisan generate:model bar
124126
php artisan generate:model foo.bar --plain
125127
php artisan generate:model bar --force
128+
php artisan generate:model bar --factory
129+
php artisan generate:model bar --migration
126130
php artisan generate:model bar --migration --schema="title:string, body:text"
127131
```
128132

src/Commands/ModelCommand.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,17 @@ public function handle()
3939

4040
if ($this->option('migration')) {
4141
$this->call('generate:migration', [
42-
'name' => $this->getMigrationName(),
43-
'--model' => false,
42+
'name' => $this->getMigrationName(),
43+
'--model' => false,
4444
'--schema' => $this->option('schema')
4545
]);
4646
}
47+
48+
if ($this->option('factory')) {
49+
$this->call('generate:factory', [
50+
'name' => $this->getArgumentNameOnly(),
51+
]);
52+
}
4753
}
4854

4955
/**
@@ -70,6 +76,7 @@ protected function getOptions()
7076
{
7177
return array_merge([
7278
['migration', 'm', InputOption::VALUE_NONE, 'Create a new migration file as well.'],
79+
['factory', 'f', InputOption::VALUE_NONE, 'Create a new factory file as well.'],
7380
['schema', 's', InputOption::VALUE_OPTIONAL, 'Optional schema to be attached to the migration', null],
7481
], parent::getOptions());
7582
}

tests/ModelCommandTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,20 @@ public function generate_model_with_migration()
3434
$this->assertFileExists('app/Models/UserComment.php');
3535
$this->assertFileExists('database/migrations/'. date('Y_m_d_His') .'_create_user_comments_table.php');
3636
}
37+
38+
/** @test */
39+
public function generate_model_with_option_factory()
40+
{
41+
$this->artisan('generate:model Comment --factory');
42+
$this->assertFileExists('app/Models/Comment.php');
43+
$this->assertFileExists('database/factories/CommentFactory.php');
44+
}
45+
46+
/** @test */
47+
public function generate_model_with_option_test()
48+
{
49+
$this->artisan('generate:model Post --test');
50+
$this->assertFileExists('app/Models/Post.php');
51+
$this->assertFileExists('tests/Unit/Models/PostTest.php');
52+
}
3753
}

0 commit comments

Comments
 (0)