Skip to content

Commit 488b367

Browse files
adel007ghcrynobone
andauthored
[11.x] Add HasFactory trait to make:model generation command using --all options (#53391)
* Add HasFactory support to model generation * Add test for model generation with HasFactory trait * wip * Delete tests/Feature/ModelMakeCommandTest.php * Update ModelMakeCommandTest.php * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> --------- Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> Co-authored-by: Mior Muhammad Zaki <crynobone@gmail.com>
1 parent eb4d629 commit 488b367

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/Illuminate/Foundation/Console/ModelMakeCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ protected function buildFactoryReplacements()
251251
{
252252
$replacements = [];
253253

254-
if ($this->option('factory')) {
254+
if ($this->option('factory') || $this->option('all')) {
255255
$modelPath = str($this->argument('name'))->studly()->replace('/', '\\')->toString();
256256

257257
$factoryNamespace = '\\Database\\Factories\\'.$modelPath.'Factory';

tests/Integration/Generators/ModelMakeCommandTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class ModelMakeCommandTest extends TestCase
1111
'app/Http/Controllers/BarController.php',
1212
'database/factories/FooFactory.php',
1313
'database/factories/Foo/BarFactory.php',
14+
'database/migrations/*_create_foos_table.php',
1415
'database/seeders/FooSeeder.php',
1516
'tests/Feature/Models/FooTest.php',
1617
];
@@ -144,6 +145,32 @@ public function testItCanGenerateModelFileWithFactoryOptionForDeepFolder()
144145
$this->assertFilenameNotExists('database/seeders/Foo/BarSeeder.php');
145146
}
146147

148+
public function testItGeneratesModelWithHasFactoryTraitWhenUsingAllOption()
149+
{
150+
$this->artisan('make:model', ['name' => 'Foo', '--all' => true])
151+
->expectsQuestion('A App\Models\Foo model does not exist. Do you want to generate it?', false)
152+
->assertExitCode(0);
153+
154+
$this->assertFileContains([
155+
'namespace App\Models;',
156+
'use Illuminate\Database\Eloquent\Factories\HasFactory;',
157+
'use Illuminate\Database\Eloquent\Model;',
158+
'class Foo extends Model',
159+
'/** @use HasFactory<\Database\Factories\FooFactory> */',
160+
'use HasFactory;',
161+
], 'app/Models/Foo.php');
162+
163+
$this->assertFileNotContains([
164+
'{{ factoryImport }}',
165+
'{{ factory }}',
166+
], 'app/Models/Foo.php');
167+
168+
$this->assertFilenameExists('app/Http/Controllers/FooController.php');
169+
$this->assertFilenameExists('database/factories/FooFactory.php');
170+
$this->assertFilenameExists('database/seeders/FooSeeder.php');
171+
$this->assertMigrationFileExists('create_foos_table.php');
172+
}
173+
147174
public function testItCanGenerateModelFileWithMigrationOption()
148175
{
149176
$this->artisan('make:model', ['name' => 'Foo', '--migration' => true])

0 commit comments

Comments
 (0)