diff --git a/src/Console/Commands/ModuleMakeTestCommand.php b/src/Console/Commands/ModuleMakeTestCommand.php index 0b45f3d..48ca8ab 100644 --- a/src/Console/Commands/ModuleMakeTestCommand.php +++ b/src/Console/Commands/ModuleMakeTestCommand.php @@ -16,14 +16,15 @@ class ModuleMakeTestCommand extends ModuleMakerCommand {name : The name of the test} {--module= : Name of module migration should belong to} {--u|unit : Create a unit test} - {--p|pest : Create a Pest test}'; + {--p|pest : Create a Pest test} + {--view : Create a view test}'; /** * The console command description. * * @var string */ - protected $description = 'Generate resource for a module'; + protected $description = 'Generate test resource for a module'; /** * The type of class being generated. @@ -41,11 +42,26 @@ public function handle() $testType = 'Feature'; $prefix = 'test'; $type = ''; + if ($this->option('unit')) { $type = 'unit.'; $testType = 'Unit'; } + if ($this->option('view')) { + $filename = collect(explode('/', $filename)) + ->map(fn ($name) => ucwords($name)) + ->join('/'); + + $filename = "View/{$filename}Test"; + $type = 'view.'; + $testType = 'Feature'; + + if ($this->option('pest')) { + $prefix = 'pest'; + } + } + $name = $this->qualifyClass($module.'\\'.$folder.'\\'.$testType.'\\'.$filename); if ($this->files->exists($path = $this->getPath($name))) { diff --git a/src/Console/Commands/ModuleMakeViewCommand.php b/src/Console/Commands/ModuleMakeViewCommand.php new file mode 100644 index 0000000..c9946ca --- /dev/null +++ b/src/Console/Commands/ModuleMakeViewCommand.php @@ -0,0 +1,87 @@ +getModuleInput(); + $filename = $this->getNameInput(); + $folder = $this->getFolderPath(); + + $name = $this->qualifyClass($module.'\\'.$folder.'\\'.$filename); + + if ($this->files->exists($path = $this->getPath($name, 'blade.php'))) { + $this->logFileExist($name); + + return true; + } + + $type = ''; + + $this->setStubFile("view.{$type}"); + $this->makeDirectory($path); + $this->files->put($path, $this->buildClass($name)); + + $this->logFileCreated($name); + + $folder = 'Tests/Feature'; + if ($this->option('pest')) { + $this->call( + ModuleMakeTestCommand::class, + [ + 'name' => ($filename), + '--module' => $module, + '--pest' => true, + '--view' => true, + ] + ); + $type = 'pest.'; + } + + if ($this->option('test')) { + $this->call( + ModuleMakeTestCommand::class, + [ + 'name' => $filename, + '--module' => $module, + '--view' => true, + ] + ); + $type = 'test.'; + } + + return true; + } + + protected function getFolderPath(): string + { + return 'Views'; + } +} diff --git a/src/Console/Commands/ModuleMakerCommand.php b/src/Console/Commands/ModuleMakerCommand.php index 72a15a8..23a6114 100644 --- a/src/Console/Commands/ModuleMakerCommand.php +++ b/src/Console/Commands/ModuleMakerCommand.php @@ -14,6 +14,8 @@ abstract class ModuleMakerCommand extends GeneratorCommand */ protected $currentStub = __DIR__.'/templates/'; + protected ?string $module = null; + /** * Get the stub file for the generator. * @@ -26,11 +28,11 @@ protected function getStub() public function getModuleInput(): string { - if (! $module = $this->option('module')) { - $module = $this->ask('What is the name of the module?'); + if (! $this->module = $this->option('module')) { + $this->module = $this->ask('What is the name of the module?'); } - return $module; + return $this->module; } /** @@ -46,6 +48,7 @@ protected function buildClass($name) $stub = $this->files->get($this->getStub()); return $this->replaceName($stub, $this->getNameInput()) + ->replaceModuleName($stub, $this->module) ->replaceNamespace($stub, $name) ->replaceClass($stub, $name); } @@ -119,15 +122,42 @@ protected function replaceName(&$stub, $name) { $title = $name; - $stub = str_replace(search: 'SampleTitle', replace: strtolower($name), subject: $stub); - $stub = str_replace(search: 'SampleViewTitle', replace: strtolower(Str::snake($title, '-')), subject: $stub); - $stub = str_replace(search: 'SampleUCtitle', replace: ucfirst(Str::studly($name)), subject: $stub); + $stub = str_replace( + search: [ + 'SampleTitle', + 'SampleViewTitle', + 'SampleUCtitle', + '{{viewFile}}', + ], + replace: [ + strtolower($name), + strtolower(Str::snake($title, '-')), + ucfirst(Str::studly($name)), + strtolower(Str::snake(str_replace(search: '/', replace: '.', subject: $title), '-')), + ], + subject: $stub + ); $stub = $this->removePrefixFromRoutes($stub); return $this; } + protected function replaceModuleName(&$stub, $name) + { + $stub = str_replace( + search: [ + '{{ moduleName }}', + '{{moduleName}}', + 'moduleName', + ], + replace: strtolower($name), + subject: $stub + ); + + return $this; + } + /** * Remove prefix from routes when there its not a module group * @@ -317,11 +347,11 @@ protected function logFileExist(string $path) * @param string $name * @return string */ - protected function getPath($name) + protected function getPath($name, string $fileExtension = 'php') { $name = Str::replaceFirst($this->rootNamespace(), '', $name); - return $this->getModuleRootPath().'/'.str_replace('\\', '/', $name).'.php'; + return $this->getModuleRootPath().'/'.str_replace('\\', '/', $name).".{$fileExtension}"; } protected function getModuleRootPath(): string diff --git a/src/Console/Commands/templates/pest.view.sample b/src/Console/Commands/templates/pest.view.sample new file mode 100644 index 0000000..303772c --- /dev/null +++ b/src/Console/Commands/templates/pest.view.sample @@ -0,0 +1,9 @@ +view('{{moduleName}}::{{viewFile}}', [ + // + ]); + + $contents->assertSee(''); +}); diff --git a/src/Console/Commands/templates/test.view.sample b/src/Console/Commands/templates/test.view.sample new file mode 100644 index 0000000..8656db5 --- /dev/null +++ b/src/Console/Commands/templates/test.view.sample @@ -0,0 +1,20 @@ +view('{{moduleName}}::{{viewFile}}', [ + // + ]); + + $contents->assertSee(''); + } +} diff --git a/src/Console/Commands/templates/view-component.sample b/src/Console/Commands/templates/view-component.sample new file mode 100644 index 0000000..95cf525 --- /dev/null +++ b/src/Console/Commands/templates/view-component.sample @@ -0,0 +1,26 @@ + + + + + {{-- {{ __('{module}::messages.welcome') }} --}} + \ No newline at end of file diff --git a/src/ModularizeServiceProvider.php b/src/ModularizeServiceProvider.php index df486e7..41422b3 100644 --- a/src/ModularizeServiceProvider.php +++ b/src/ModularizeServiceProvider.php @@ -20,6 +20,7 @@ use NorbyBaru\Modularize\Console\Commands\ModuleMakeRequestCommand; use NorbyBaru\Modularize\Console\Commands\ModuleMakeResourceCommand; use NorbyBaru\Modularize\Console\Commands\ModuleMakeTestCommand; +use NorbyBaru\Modularize\Console\Commands\ModuleMakeViewCommand; class ModularizeServiceProvider extends ServiceProvider { @@ -206,7 +207,7 @@ private function autoloadViews(string $moduleRootPath, string $module): void private function autoloadTranslations(string $moduleRootPath, string $module): void { - $path = "{$moduleRootPath}/{$module}/Translations"; + $path = "{$moduleRootPath}/{$module}/Lang"; if ($this->files->isDirectory(directory: $path)) { $this->loadTranslationsFrom( @@ -246,6 +247,7 @@ protected function registerMakeCommand() ModuleMakeResourceCommand::class, ModuleMakeRequestCommand::class, ModuleMakeTestCommand::class, + ModuleMakeViewCommand::class, ]); } }