From 0ade9ef31a3384a6f3339650a694582f823cbd59 Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Sat, 18 May 2024 14:49:51 -0400 Subject: [PATCH] WIP --- config/ddd.php | 2 + src/Commands/DomainControllerMakeCommand.php | 36 ++++ src/Commands/DomainMigrateMakeCommand.php | 13 ++ src/Commands/DomainModelMakeCommand.php | 71 ++++++- src/Commands/DomainSeederMakeCommand.php | 13 ++ src/LaravelDDDServiceProvider.php | 3 + .../{MakeModelTest.php => Model/MakeTest.php} | 0 tests/Generator/Model/OptionsTest.php | 174 ++++++++++++++++++ 8 files changed, 311 insertions(+), 1 deletion(-) create mode 100644 src/Commands/DomainControllerMakeCommand.php create mode 100644 src/Commands/DomainMigrateMakeCommand.php create mode 100644 src/Commands/DomainSeederMakeCommand.php rename tests/Generator/{MakeModelTest.php => Model/MakeTest.php} (100%) create mode 100644 tests/Generator/Model/OptionsTest.php diff --git a/config/ddd.php b/config/ddd.php index 66d8901..2f967b2 100644 --- a/config/ddd.php +++ b/config/ddd.php @@ -56,6 +56,7 @@ 'job' => 'Jobs', 'listener' => 'Listeners', 'mail' => 'Mail', + 'migration' => 'Database\Migrations', 'notification' => 'Notifications', 'observer' => 'Observers', 'policy' => 'Policies', @@ -63,6 +64,7 @@ 'resource' => 'Resources', 'rule' => 'Rules', 'scope' => 'Scopes', + 'seeder' => 'Database\Seeders', 'trait' => '', ], diff --git a/src/Commands/DomainControllerMakeCommand.php b/src/Commands/DomainControllerMakeCommand.php new file mode 100644 index 0000000..6e1284e --- /dev/null +++ b/src/Commands/DomainControllerMakeCommand.php @@ -0,0 +1,36 @@ +parseModel($this->option('model')); + + // if (! class_exists($modelClass) && confirm("A {$modelClass} model does not exist. Do you want to generate it?", default: true)) { + // $this->call('make:model', ['name' => $modelClass]); + // } + + $replace = $this->buildFormRequestReplacements($replace, $modelClass); + + return array_merge($replace, [ + 'DummyFullModelClass' => $modelClass, + '{{ namespacedModel }}' => $modelClass, + '{{namespacedModel}}' => $modelClass, + 'DummyModelClass' => class_basename($modelClass), + '{{ model }}' => class_basename($modelClass), + '{{model}}' => class_basename($modelClass), + 'DummyModelVariable' => lcfirst(class_basename($modelClass)), + '{{ modelVariable }}' => lcfirst(class_basename($modelClass)), + '{{modelVariable}}' => lcfirst(class_basename($modelClass)), + ]); + } +} diff --git a/src/Commands/DomainMigrateMakeCommand.php b/src/Commands/DomainMigrateMakeCommand.php new file mode 100644 index 0000000..887f9e1 --- /dev/null +++ b/src/Commands/DomainMigrateMakeCommand.php @@ -0,0 +1,13 @@ +argument('name')); + $this->call(DomainFactoryMakeCommand::class, [ - 'name' => $this->getNameInput().'Factory', + 'name' => $factory.'Factory', + '--domain' => $this->domain->dotName, + '--model' => $this->qualifyClass($this->getNameInput()), + ]); + } + + // protected function createMigration() + // { + // $table = Str::snake(Str::pluralStudly(class_basename($this->argument('name')))); + + // if ($this->option('pivot')) { + // $table = Str::singular($table); + // } + + // $this->call('make:migration', [ + // 'name' => "create_{$table}_table", + // '--create' => $table, + // ]); + // } + + /** + * Create a seeder file for the model. + * + * @return void + */ + protected function createSeeder() + { + $seeder = Str::studly(class_basename($this->argument('name'))); + + $this->call(DomainSeederMakeCommand::class, [ + 'name' => "{$seeder}Seeder", + '--domain' => $this->domain->dotName, + ]); + } + + /** + * Create a controller for the model. + * + * @return void + */ + protected function createController() + { + $controller = Str::studly(class_basename($this->argument('name'))); + + $modelName = $this->qualifyClass($this->getNameInput()); + + $this->call(DomainControllerMakeCommand::class, array_filter([ + 'name' => "{$controller}Controller", + '--model' => $this->option('resource') || $this->option('api') ? $modelName : null, + '--api' => $this->option('api'), + '--requests' => $this->option('requests') || $this->option('all'), + '--test' => $this->option('test'), + '--pest' => $this->option('pest'), + ])); + } + + /** + * Create a policy file for the model. + * + * @return void + */ + protected function createPolicy() + { + $policy = Str::studly(class_basename($this->argument('name'))); + + $this->call(DomainPolicyMakeCommand::class, [ + 'name' => "{$policy}Policy", '--domain' => $this->domain->dotName, '--model' => $this->qualifyClass($this->getNameInput()), ]); diff --git a/src/Commands/DomainSeederMakeCommand.php b/src/Commands/DomainSeederMakeCommand.php new file mode 100644 index 0000000..6ee413d --- /dev/null +++ b/src/Commands/DomainSeederMakeCommand.php @@ -0,0 +1,13 @@ +version() >= 11) { diff --git a/tests/Generator/MakeModelTest.php b/tests/Generator/Model/MakeTest.php similarity index 100% rename from tests/Generator/MakeModelTest.php rename to tests/Generator/Model/MakeTest.php diff --git a/tests/Generator/Model/OptionsTest.php b/tests/Generator/Model/OptionsTest.php new file mode 100644 index 0000000..c6a19c7 --- /dev/null +++ b/tests/Generator/Model/OptionsTest.php @@ -0,0 +1,174 @@ +model($modelName); + +// $domainFactory = $domain->factory($factoryName); + +// $expectedModelPath = base_path($domainModel->path); + +// if (file_exists($expectedModelPath)) { +// unlink($expectedModelPath); +// } + +// $expectedFactoryPath = base_path($domainFactory->path); + +// if (file_exists($expectedFactoryPath)) { +// unlink($expectedFactoryPath); +// } + +// Artisan::call('ddd:model', [ +// 'name' => $modelName, +// '--domain' => $domain->dotName, +// '--factory' => true, +// ]); + +// $output = Artisan::output(); + +// expect($output)->toContainFilepath($domainModel->path); + +// expect(file_exists($expectedModelPath))->toBeTrue("Expecting model file to be generated at {$expectedModelPath}"); +// expect(file_exists($expectedFactoryPath))->toBeTrue("Expecting factory file to be generated at {$expectedFactoryPath}"); + +// expect(file_get_contents($expectedFactoryPath)) +// ->toContain("use {$domainModel->fullyQualifiedName};") +// ->toContain("protected \$model = {$modelName}::class;"); +// }); + +it('can generate domain model with options', function ($options, $objectType, $objectName, $expectedObjectPath) { + $domainName = 'World'; + $modelName = 'Record'; + + $domain = new Domain($domainName); + + $domainModel = $domain->model($modelName); + + $expectedModelPath = base_path($domainModel->path); + + if (file_exists($expectedModelPath)) { + unlink($expectedModelPath); + } + + if (file_exists($expectedObjectPath)) { + unlink($expectedObjectPath); + } + + $command = [ + 'ddd:model', [ + 'name' => $modelName, + '--domain' => $domain->dotName, + ...$options, + ], + ]; + + $this->artisan(...$command) + ->expectsOutputToContain(Path::normalize($domainModel->path)) + ->assertExitCode(0); + + $path = base_path($expectedObjectPath); + + expect(file_exists($path))->toBeTrue("Expecting {$objectType} to be generated at {$path}"); +})->with([ + '--factory' => [ + ['--factory' => true], + 'factory', + 'RecordFactory', + 'src/Domain/World/Database/Factories/RecordFactory.php', + ], + + '--seed' => [ + ['--seed' => true], + 'seeder', + 'RecordSeeder', + 'src/Domain/World/Database/Seeders/RecordSeeder.php', + ], + + '--policy' => [ + ['--policy' => true], + 'policy', + 'RecordPolicy', + 'src/Domain/World/Policies/RecordPolicy.php', + ], +]); + +it('can generate domain model with controller', function ($options, $objectType, $objectName, $expectedObjectPath) { + $domainName = 'World'; + $modelName = 'Record'; + + $domain = new Domain($domainName); + + $domainModel = $domain->model($modelName); + + $expectedModelPath = base_path($domainModel->path); + + if (file_exists($expectedModelPath)) { + unlink($expectedModelPath); + } + + if (file_exists($expectedObjectPath)) { + unlink($expectedObjectPath); + } + + $command = [ + 'ddd:model', [ + 'name' => $modelName, + '--domain' => $domain->dotName, + ...$options, + ], + ]; + + $this->artisan(...$command) + ->expectsOutputToContain(Path::normalize($domainModel->path)) + ->assertExitCode(0); + + // $output = Artisan::output(); + + // $outputPath = Path::normalize($domainModel->path); + + // expect($output)->toContainFilepath($domainModel->path); + + $path = base_path($expectedObjectPath); + + expect(file_exists($path))->toBeTrue("Expecting {$objectType} to be generated at {$path}"); + + $contents = file_get_contents($path); + dump($contents); +})->with([ + '--controller' => [ + ['--controller' => true], + 'controller', + 'RecordController', + 'app/Http/Controllers/RecordController.php', + ], + + '--controller --api' => [ + ['--controller' => true, '--api' => true], + 'controller', + 'RecordController', + 'app/Http/Controllers/RecordController.php', + ], + + '--controller --requests' => [ + ['--controller' => true, '--requests' => true], + 'controller', + 'RecordController', + 'app/Http/Controllers/RecordController.php', + ], +]);