Skip to content

Commit

Permalink
26 domain factories need model property (#27)
Browse files Browse the repository at this point in the history
* Ensure domain factories set the $model property.
  • Loading branch information
JasperTey authored Aug 15, 2023
1 parent c371cf5 commit 6809b95
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
49 changes: 28 additions & 21 deletions src/Commands/MakeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
18 changes: 18 additions & 0 deletions stubs/factory.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace {{ namespace }};

use Illuminate\Database\Eloquent\Factories\Factory;
use {{ namespacedModel }};

class {{ class }} extends Factory
{
protected $model = {{ model }}::class;

public function definition()
{
return [
//
];
}
}
15 changes: 12 additions & 3 deletions tests/Generator/MakeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
use Lunarstorm\LaravelDDD\Support\Domain;
use Lunarstorm\LaravelDDD\Tests\Fixtures\Enums\Feature;

it('can generate domain factories', function ($domainPath, $domainRoot) {
Expand All @@ -12,6 +13,8 @@
$modelName = Str::studly(fake()->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
Expand All @@ -37,14 +40,20 @@
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',
'Factories',
$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');
3 changes: 2 additions & 1 deletion tests/Generator/MakeModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 6809b95

Please sign in to comment.