diff --git a/src/Commands/DomainMigrationMakeCommand.php b/src/Commands/DomainMigrationMakeCommand.php index b5473a4..7484299 100644 --- a/src/Commands/DomainMigrationMakeCommand.php +++ b/src/Commands/DomainMigrationMakeCommand.php @@ -7,9 +7,13 @@ class DomainMigrationMakeCommand extends MigrateMakeCommand { + /* use ResolvesDomainFromInput { ResolvesDomainFromInput::getPath as getDomainPath; } + */ + use ResolvesDomainFromInput; + protected $signature = 'ddd:migration {name : The name of the migration} {--domain= : test} @@ -23,9 +27,15 @@ class DomainMigrationMakeCommand extends MigrateMakeCommand protected function getMigrationPath() { - $name = $this->input->getArgument('name'); + //dd(dirname($this->getPath('test'))); + return dirname($this->getPath('test')); + + + dump('mmmm', $this->getDomainPath(''), dirname($this->getDomainPath(''))); + - return $this->getDomainPath($name); + dump('mmmm', $this->getDomainPath(''), dirname($this->getDomainPath(''))); + return dirname($this->getDomainPath('')); } protected function guessObjectType(): string diff --git a/src/Commands/DomainModelMakeCommand.php b/src/Commands/DomainModelMakeCommand.php index 17e53f4..b33b773 100644 --- a/src/Commands/DomainModelMakeCommand.php +++ b/src/Commands/DomainModelMakeCommand.php @@ -27,10 +27,9 @@ protected function getOptions() ['migration', 'm', InputOption::VALUE_NONE, 'Create a new migration for the domain model'], ['test', 't', InputOption::VALUE_NONE, 'Generate an accompanying PHPUnit test for the model'], // TDOD ['pest', 'tpa', InputOption::VALUE_NONE, 'Generate an accompanying Pest test for the model'], // TDOD - ['seed', 's', InputOption::VALUE_NONE, 'Create a new seeder for the model'], // TDOD - ['policy', 'p', InputOption::VALUE_NONE, 'Create a new seeder for the model'], // TDOD - ['all', 'a', InputOption::VALUE_NONE, 'Generate a migration, seeder, factory, and policy classes for the model'], // TDOD - + ['seed', 's', InputOption::VALUE_NONE, 'Create a new seeder for the model'], + ['policy', 'p', InputOption::VALUE_NONE, 'Create a new seeder for the model'], + ['all', 'a', InputOption::VALUE_NONE, 'Generate a migration, seeder, factory, and policy classes for the model'], ]; } diff --git a/src/Commands/DomainProviderMakeCommand.php b/src/Commands/DomainProviderMakeCommand.php index dcc2c8a..9c27a6a 100644 --- a/src/Commands/DomainProviderMakeCommand.php +++ b/src/Commands/DomainProviderMakeCommand.php @@ -10,4 +10,9 @@ class DomainProviderMakeCommand extends ProviderMakeCommand use ResolvesDomainFromInput; protected $name = 'ddd:provider'; + + protected function getStub() + { + return $this->resolveStubPath('/stubs/provider.stub'); + } } diff --git a/src/Support/Domain.php b/src/Support/Domain.php index 0abc48e..b92d71a 100644 --- a/src/Support/Domain.php +++ b/src/Support/Domain.php @@ -209,4 +209,9 @@ public function seeder(string $name): DomainObject { return $this->object('seed', $name); } + + public function migration(string $name): DomainObject + { + return $this->object('migration', $name); + } } diff --git a/tests/Generator/MakeModelTest.php b/tests/Generator/MakeModelTest.php index 8772f20..5b99f9b 100644 --- a/tests/Generator/MakeModelTest.php +++ b/tests/Generator/MakeModelTest.php @@ -2,9 +2,27 @@ 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; + +function expectGeneratedDomainFile($output, $fileName, $domain, $type) +{ + $domainObject = $domain->$type($fileName); + $expectedFilePath = base_path($domainObject->path); + + if (file_exists($expectedFilePath)) { + unlink($expectedFilePath); + } + + expect($output)->toContainFilepath($expectedFilePath); + + expect(file_exists($expectedFilePath))->toBeTrue("Expecting file to be generated at {$expectedFilePath}"); + + return $expectedFilePath; +} + it('can generate domain models', function ($domainPath, $domainRoot) { Config::set('ddd.domain_path', $domainPath); Config::set('ddd.domain_namespace', $domainRoot); @@ -52,11 +70,70 @@ $domain = new Domain($domainName, $subdomain); - $factoryName = "{$modelName}Factory"; + $domainModel = $domain->model($modelName); + + Artisan::call('ddd:model', [ + 'name' => $modelName, + '--domain' => $domain->dotName, + '--factory' => true, + ]); + + $output = Artisan::output(); + + $expectedModelPath = expectGeneratedDomainFile($output, $modelName, $domain, 'model'); + $expectedFactoryPath = expectGeneratedDomainFile($output, "{$modelName}Factory", $domain, 'factory'); + + expect($output)->toContainFilepath($domainModel->path); + + expect(file_exists($expectedModelPath))->toBeTrue("Expecting model file to be generated at {$expectedModelPath}"); + + expect(file_get_contents($expectedFactoryPath)) + ->toContain("use {$domainModel->fullyQualifiedName};") + ->toContain("protected \$model = {$modelName}::class;"); + +})->with('domainPaths')->with('domainSubdomain'); + +it('can generate a domain model with seeder', function ($domainPath, $domainRoot, $domainName, $subdomain) { + Config::set('ddd.domain_path', $domainPath); + + $modelName = 'Record'; + + $domain = new Domain($domainName, $subdomain); + + $domainModel = $domain->model($modelName); + + Artisan::call('ddd:model', [ + 'name' => $modelName, + '--domain' => $domain->dotName, + '--seed' => true, + ]); + + $output = Artisan::output(); + + $expectedModelPath = expectGeneratedDomainFile($output, $modelName, $domain, 'model'); + $expectedSeederPath = expectGeneratedDomainFile($output, "{$modelName}Seeder", $domain, 'seeder'); + + expect(file_exists($expectedModelPath))->toBeTrue("Expecting model file to be generated at {$expectedModelPath}"); + expect(file_exists($expectedSeederPath))->toBeTrue("Expecting seeder file to be generated at {$expectedSeederPath}"); + + expect(file_get_contents($expectedSeederPath)) + ->toContain("class {$modelName}Seeder extends Seeder") + ->toContain("public function run(): void"); +})->with('domainPaths')->with('domainSubdomain'); + +it('can generate a domain model with migration', function ($domainPath, $domainRoot, $domainName, $subdomain) { + Config::set('ddd.domain_path', $domainPath); + + $modelName = 'Record'; + + $domain = new Domain($domainName, $subdomain); + + $migrationName = Str::snake(Str::pluralStudly(class_basename($modelName))); + //$migrationName = "{$modelName}Factory"; $domainModel = $domain->model($modelName); - $domainFactory = $domain->factory($factoryName); + $domainMigration = $domain->migration($migrationName); $expectedModelPath = base_path($domainModel->path); @@ -64,20 +141,55 @@ unlink($expectedModelPath); } - $expectedFactoryPath = base_path($domainFactory->path); + $expectedMigrationPath = base_path($domainMigration->path); - if (file_exists($expectedFactoryPath)) { - unlink($expectedFactoryPath); + if (file_exists($expectedMigrationPath)) { + unlink($expectedMigrationPath); } Artisan::call('ddd:model', [ 'name' => $modelName, '--domain' => $domain->dotName, - '--factory' => true, + '--migration' => true, ]); $output = Artisan::output(); + //dd(scandir(dirname(base_path($expectedMigrationPath)))); + + expect($output)->toContainFilepath($domainModel->path); + + expect(file_exists($expectedModelPath))->toBeTrue("Expecting model file to be generated at {$expectedModelPath}"); + expect(file_exists($expectedMigrationPath))->toBeTrue("Expecting factory file to be generated at {$expectedMigrationPath}"); + + expect(file_get_contents($expectedMigrationPath)) + ->toContain("use {$domainModel->fullyQualifiedName};") + ->toContain("protected \$model = {$modelName}::class;"); +})->with('domainPaths')->with('domainSubdomain'); + + + +it('can generate a domain model with all', function ($domainPath, $domainRoot, $domainName, $subdomain) { + Config::set('ddd.domain_path', $domainPath); + + $modelName = 'Record'; + + $domain = new Domain($domainName, $subdomain); + + $domainModel = $domain->model($modelName); + + Artisan::call('ddd:model', [ + 'name' => $modelName, + '--domain' => $domain->dotName, + '--all' => true, + ]); + + $output = Artisan::output(); + + $expectedModelPath = expectGeneratedDomainFile($output, $modelName, $domain, 'model'); + $expectedFactoryPath = expectGeneratedDomainFile($output, "{$modelName}Factory", $domain, 'factory'); + $expectedSeederPath = expectGeneratedDomainFile($output, "{$modelName}Seeder", $domain, 'seeder'); + expect($output)->toContainFilepath($domainModel->path); expect(file_exists($expectedModelPath))->toBeTrue("Expecting model file to be generated at {$expectedModelPath}");