Skip to content

Commit

Permalink
fix: make model command with deep folder path - has incorrect import …
Browse files Browse the repository at this point in the history
…HasFactory class path (#53142)
  • Loading branch information
JeRabix authored Oct 13, 2024
1 parent 964ef32 commit 1d7e000
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Illuminate/Foundation/Console/ModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<EOT
/** @use HasFactory<$factoryNamespace> */
Expand Down
25 changes: 25 additions & 0 deletions tests/Integration/Generators/ModelMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
Expand Down Expand Up @@ -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])
Expand Down

0 comments on commit 1d7e000

Please sign in to comment.