From 8c95778714687242c1a51af35ad917f331e9c708 Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Mon, 14 Aug 2023 21:08:36 -0400 Subject: [PATCH] Normalize factory class names (#28) * Normalize factory class names: - ensure they always have a Factory suffix * Update changelog. * Laravel 9/Pest compatibility. --- CHANGELOG.md | 3 ++- src/Commands/MakeFactory.php | 14 ++++++++++++++ stubs/factory.php.stub | 2 +- tests/Generator/MakeFactoryTest.php | 5 ++++- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e111ca6..4c822f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,8 @@ All notable changes to `laravel-ddd` will be documented in this file. ## [0.6.1] - 2023-08-14 ### Fixed -- Ensure that generated domain factories set the `protected $model` property. +- Ensure generated domain factories set the `protected $model` property. +- Ensure generated factory classes are always suffixed by `Factory`. ## [0.6.0] - 2023-08-14 ### Added diff --git a/src/Commands/MakeFactory.php b/src/Commands/MakeFactory.php index 08b9da3..235986e 100644 --- a/src/Commands/MakeFactory.php +++ b/src/Commands/MakeFactory.php @@ -63,6 +63,10 @@ protected function getRelativeDomainNamespace(): string protected function getPath($name) { + if (! str_ends_with($name, 'Factory')) { + $name .= 'Factory'; + } + $name = str($name) ->replaceFirst($this->rootNamespace(), '') ->replace('\\', '/') @@ -73,6 +77,15 @@ protected function getPath($name) return base_path('database/factories/'.$name); } + protected function getFactoryName() + { + $name = $this->getNameInput(); + + return str_ends_with($name, 'Factory') + ? substr($name, 0, -7) + : $name; + } + protected function preparePlaceholders(): array { $domain = new Domain($this->getDomain()); @@ -86,6 +99,7 @@ protected function preparePlaceholders(): array return [ 'namespacedModel' => $namespacedModel, 'model' => class_basename($namespacedModel), + 'factory' => $this->getFactoryName(), ]; } diff --git a/stubs/factory.php.stub b/stubs/factory.php.stub index 01753f8..ee27ef5 100644 --- a/stubs/factory.php.stub +++ b/stubs/factory.php.stub @@ -5,7 +5,7 @@ namespace {{ namespace }}; use Illuminate\Database\Eloquent\Factories\Factory; use {{ namespacedModel }}; -class {{ class }} extends Factory +class {{ factory }}Factory extends Factory { protected $model = {{ model }}::class; diff --git a/tests/Generator/MakeFactoryTest.php b/tests/Generator/MakeFactoryTest.php index 753d233..d3417b2 100644 --- a/tests/Generator/MakeFactoryTest.php +++ b/tests/Generator/MakeFactoryTest.php @@ -33,7 +33,7 @@ expect(file_exists($expectedFactoryPath))->toBeFalse(); - Artisan::call("ddd:factory {$domain} {$factoryName}"); + Artisan::call("ddd:factory {$domain} {$modelName}"); expect(Artisan::output())->when( Feature::IncludeFilepathInGeneratorCommandOutput->exists(), @@ -55,5 +55,8 @@ expect($contents) ->toContain("namespace {$expectedNamespace};") ->toContain("use {$namespacedModel};") + ->toContain("class {$factoryName} extends Factory") ->toContain("protected \$model = {$modelName}::class;"); })->with('domainPaths'); + +it('normalizes factory classes with Factory suffix')->markTestIncomplete();