Skip to content

Commit

Permalink
[11.x] Add HasFactory trait to make:model generation command usin…
Browse files Browse the repository at this point in the history
…g `--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>
  • Loading branch information
adel007gh and crynobone authored Nov 5, 2024
1 parent eb4d629 commit 488b367
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Console/ModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ protected function buildFactoryReplacements()
{
$replacements = [];

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

$factoryNamespace = '\\Database\\Factories\\'.$modelPath.'Factory';
Expand Down
27 changes: 27 additions & 0 deletions tests/Integration/Generators/ModelMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ModelMakeCommandTest extends TestCase
'app/Http/Controllers/BarController.php',
'database/factories/FooFactory.php',
'database/factories/Foo/BarFactory.php',
'database/migrations/*_create_foos_table.php',
'database/seeders/FooSeeder.php',
'tests/Feature/Models/FooTest.php',
];
Expand Down Expand Up @@ -144,6 +145,32 @@ public function testItCanGenerateModelFileWithFactoryOptionForDeepFolder()
$this->assertFilenameNotExists('database/seeders/Foo/BarSeeder.php');
}

public function testItGeneratesModelWithHasFactoryTraitWhenUsingAllOption()
{
$this->artisan('make:model', ['name' => 'Foo', '--all' => true])
->expectsQuestion('A App\Models\Foo model does not exist. Do you want to generate it?', false)
->assertExitCode(0);

$this->assertFileContains([
'namespace App\Models;',
'use Illuminate\Database\Eloquent\Factories\HasFactory;',
'use Illuminate\Database\Eloquent\Model;',
'class Foo extends Model',
'/** @use HasFactory<\Database\Factories\FooFactory> */',
'use HasFactory;',
], 'app/Models/Foo.php');

$this->assertFileNotContains([
'{{ factoryImport }}',
'{{ factory }}',
], 'app/Models/Foo.php');

$this->assertFilenameExists('app/Http/Controllers/FooController.php');
$this->assertFilenameExists('database/factories/FooFactory.php');
$this->assertFilenameExists('database/seeders/FooSeeder.php');
$this->assertMigrationFileExists('create_foos_table.php');
}

public function testItCanGenerateModelFileWithMigrationOption()
{
$this->artisan('make:model', ['name' => 'Foo', '--migration' => true])
Expand Down

0 comments on commit 488b367

Please sign in to comment.