From 1d7e000619be1ffe24e4fee93f11a1fcf5af841e Mon Sep 17 00:00:00 2001 From: Evgeniy Yurchenko <46298617+JeRabix@users.noreply.github.com> Date: Sun, 13 Oct 2024 18:12:21 +0300 Subject: [PATCH] fix: make model command with deep folder path - has incorrect import HasFactory class path (#53142) --- .../Foundation/Console/ModelMakeCommand.php | 4 ++- .../Generators/ModelMakeCommandTest.php | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Console/ModelMakeCommand.php b/src/Illuminate/Foundation/Console/ModelMakeCommand.php index 9717a9d8eaa..e2f8de510fb 100644 --- a/src/Illuminate/Foundation/Console/ModelMakeCommand.php +++ b/src/Illuminate/Foundation/Console/ModelMakeCommand.php @@ -252,7 +252,9 @@ protected function buildFactoryReplacements() $replacements = []; if ($this->option('factory')) { - $factoryNamespace = '\\Database\\Factories\\'.Str::studly($this->argument('name')).'Factory'; + $modelPath = str($this->argument('name'))->studly()->replace('/', '\\')->toString(); + + $factoryNamespace = '\\Database\\Factories\\'.$modelPath.'Factory'; $factoryCode = << */ diff --git a/tests/Integration/Generators/ModelMakeCommandTest.php b/tests/Integration/Generators/ModelMakeCommandTest.php index 78e8f059681..776aa6c9674 100644 --- a/tests/Integration/Generators/ModelMakeCommandTest.php +++ b/tests/Integration/Generators/ModelMakeCommandTest.php @@ -10,6 +10,7 @@ class ModelMakeCommandTest extends TestCase 'app/Http/Controllers/FooController.php', 'app/Http/Controllers/BarController.php', 'database/factories/FooFactory.php', + 'database/factories/Foo/BarFactory.php', 'database/seeders/FooSeeder.php', 'tests/Feature/Models/FooTest.php', ]; @@ -119,6 +120,30 @@ public function testItCanGenerateModelFileWithFactoryOption() $this->assertFilenameNotExists('database/seeders/FooSeeder.php'); } + public function testItCanGenerateModelFileWithFactoryOptionForDeepFolder() + { + $this->artisan('make:model', ['name' => 'Foo/Bar', '--factory' => true]) + ->assertExitCode(0); + + $this->assertFileContains([ + 'namespace App\Models\Foo;', + 'use Illuminate\Database\Eloquent\Factories\HasFactory;', + 'use Illuminate\Database\Eloquent\Model;', + 'class Bar extends Model', + '/** @use HasFactory<\Database\Factories\Foo\BarFactory> */', + 'use HasFactory;', + ], 'app/Models/Foo/Bar.php'); + + $this->assertFileNotContains([ + '{{ factoryImport }}', + '{{ factory }}', + ], 'app/Models/Foo/Bar.php'); + + $this->assertFilenameNotExists('app/Http/Controllers/Foo/BarController.php'); + $this->assertFilenameExists('database/factories/Foo/BarFactory.php'); + $this->assertFilenameNotExists('database/seeders/Foo/BarSeeder.php'); + } + public function testItCanGenerateModelFileWithMigrationOption() { $this->artisan('make:model', ['name' => 'Foo', '--migration' => true])