Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Feat: factory generic in model #52855

Open
wants to merge 4 commits into
base: 11.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/Illuminate/Foundation/Console/ModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,46 @@ protected function getDefaultNamespace($rootNamespace)
return is_dir(app_path('Models')) ? $rootNamespace.'\\Models' : $rootNamespace;
}

/**
* Build the class with the given name.
*
* @param string $name
* @return string
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected function buildClass($name)
{
$replace = [];

if ($this->option('factory')) {
$replace['{{ factoryDocBlock }}'] = $this->buildFactoryReplacements();
} else {
$replace["\n {{ factoryDocBlock }}"] = '';
}

$class = str_replace(
array_keys($replace), array_values($replace), parent::buildClass($name)
);

return $class;
}

/**
* Build the replacements for a factory doc block.
*
* @return string
*/
protected function buildFactoryReplacements()
{
$factory = Str::studly($this->argument('name')).'Factory';
$factoryNameSpace = '\\Database\\Factories\\'.$factory;

return <<<EOT
/** @use HasFactory<$factoryNameSpace> */
EOT;
}

/**
* Get the console command options.
*
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Foundation/Console/stubs/model.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ use Illuminate\Database\Eloquent\Model;

class {{ class }} extends Model
{
{{ factoryDocBlock }}
use HasFactory;
}
7 changes: 7 additions & 0 deletions tests/Integration/Generators/ModelMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public function testItCanGenerateModelFile()
'class Foo extends Model',
], 'app/Models/Foo.php');

$this->assertFileDoesNotContains([
'{{ factoryDocBlock }}',
'/** @use HasFactory<\Database\Factories\FooFactory> */',
], 'app/Models/Foo.php');

$this->assertFilenameNotExists('app/Http/Controllers/FooController.php');
$this->assertFilenameNotExists('database/factories/FooFactory.php');
$this->assertFilenameNotExists('database/seeders/FooSeeder.php');
Expand Down Expand Up @@ -96,6 +101,8 @@ public function testItCanGenerateModelFileWithFactoryOption()
'namespace App\Models;',
'use Illuminate\Database\Eloquent\Model;',
'class Foo extends Model',
'/** @use HasFactory<\Database\Factories\FooFactory> */',
'use HasFactory;',
], 'app/Models/Foo.php');

$this->assertFilenameNotExists('app/Http/Controllers/FooController.php');
Expand Down