diff --git a/CHANGELOG.md b/CHANGELOG.md index 137adc9..e111ca6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ 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. + ## [0.6.0] - 2023-08-14 ### Added - Ability to generate domain model factories, in a few ways: diff --git a/src/Commands/MakeFactory.php b/src/Commands/MakeFactory.php index 174c17d..08b9da3 100644 --- a/src/Commands/MakeFactory.php +++ b/src/Commands/MakeFactory.php @@ -2,7 +2,6 @@ namespace Lunarstorm\LaravelDDD\Commands; -use Illuminate\Database\Console\Factories\FactoryMakeCommand; use Lunarstorm\LaravelDDD\Support\Domain; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; @@ -62,32 +61,40 @@ protected function getRelativeDomainNamespace(): string return ''; } - public function handle() + protected function getPath($name) { - $domain = $this->getDomain(); - $nameWithDomain = $domain.'/'.$this->getNameInput(); - $model = $this->option('model'); + $name = str($name) + ->replaceFirst($this->rootNamespace(), '') + ->replace('\\', '/') + ->ltrim('/') + ->append('.php') + ->toString(); + + return base_path('database/factories/'.$name); + } - // Generate the factory using the native factory generator - $this->call(FactoryMakeCommand::class, [ - 'name' => $nameWithDomain, - '--model' => $model ?: false, - ]); + protected function preparePlaceholders(): array + { + $domain = new Domain($this->getDomain()); - // Correct the namespaced model reference inside the generated factory. - $pathToFactory = base_path("database/factories/{$nameWithDomain}.php"); + $name = $this->getNameInput(); - $contents = file_get_contents($pathToFactory); + $namespacedModel = $this->option('model') + ? $domain->namespacedModel($this->option('model')) + : $domain->namespacedModel($this->guessModelName($name)); - $domainHelper = new Domain($domain); - $domainNamespacedModel = $domainHelper->namespacedModel($model); + return [ + 'namespacedModel' => $namespacedModel, + 'model' => class_basename($namespacedModel), + ]; + } - $contents = str_replace( - "App\\{$domainNamespacedModel}", - $domainNamespacedModel, - $contents - ); + protected function guessModelName($name) + { + if (str_ends_with($name, 'Factory')) { + $name = substr($name, 0, -7); + } - file_put_contents($pathToFactory, $contents); + return (new Domain($this->getDomain()))->namespacedModel($name); } } diff --git a/stubs/factory.php.stub b/stubs/factory.php.stub new file mode 100644 index 0000000..01753f8 --- /dev/null +++ b/stubs/factory.php.stub @@ -0,0 +1,18 @@ +word()); $domain = Str::studly(fake()->word()); $factoryName = "{$modelName}Factory"; + $domainHelper = new Domain($domain); + $namespacedModel = $domainHelper->namespacedModel($modelName); // Domain factories are expected to be generated in: // database/factories/{Domain}/{Factory}.php @@ -37,7 +40,9 @@ fn ($output) => $output->toContain($relativePath), ); - expect(file_exists($expectedFactoryPath))->toBeTrue(); + expect(file_exists($expectedFactoryPath))->toBeTrue( + "Expected factory to be generated in {$expectedFactoryPath}" + ); $expectedNamespace = implode('\\', [ 'Database', @@ -45,6 +50,10 @@ $domain, ]); - expect(file_get_contents($expectedFactoryPath)) - ->toContain("namespace {$expectedNamespace};"); + $contents = file_get_contents($expectedFactoryPath); + + expect($contents) + ->toContain("namespace {$expectedNamespace};") + ->toContain("use {$namespacedModel};") + ->toContain("protected \$model = {$modelName}::class;"); })->with('domainPaths'); diff --git a/tests/Generator/MakeModelTest.php b/tests/Generator/MakeModelTest.php index d9c221f..040e1f5 100644 --- a/tests/Generator/MakeModelTest.php +++ b/tests/Generator/MakeModelTest.php @@ -97,7 +97,8 @@ ]); expect(file_get_contents($expectedFactoryPath)) - ->toContain($expectedNamespacedModel); + ->toContain("use {$expectedNamespacedModel};") + ->toContain("protected \$model = {$modelName}::class;"); })->with('domainPaths'); it('normalizes generated model to pascal case', function ($given, $normalized) {