Skip to content

Commit

Permalink
Normalize factory class names (#28)
Browse files Browse the repository at this point in the history
* Normalize factory class names:
- ensure they always have a Factory suffix

* Update changelog.

* Laravel 9/Pest compatibility.
  • Loading branch information
JasperTey authored Aug 15, 2023
1 parent 6809b95 commit 8c95778
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/Commands/MakeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('\\', '/')
Expand All @@ -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());
Expand All @@ -86,6 +99,7 @@ protected function preparePlaceholders(): array
return [
'namespacedModel' => $namespacedModel,
'model' => class_basename($namespacedModel),
'factory' => $this->getFactoryName(),
];
}

Expand Down
2 changes: 1 addition & 1 deletion stubs/factory.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 4 additions & 1 deletion tests/Generator/MakeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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();

0 comments on commit 8c95778

Please sign in to comment.