diff --git a/src/Services/CodePath/CodePath.php b/src/Services/CodePath/CodePath.php index b48924f..2116400 100644 --- a/src/Services/CodePath/CodePath.php +++ b/src/Services/CodePath/CodePath.php @@ -26,40 +26,45 @@ public function initPaths(CodeStructure $codeStructure, string $path, bool $isGe { $genPath = $isGenerationDir ? base_path($path) : ''; + $namespace = implode('/', array_map( + fn ($dir) => str($dir)->camel()->ucfirst()->value(), + explode("/", $path) + )); + $this ->setPath( new ModelPath( $codeStructure->entity()->ucFirstSingular() . '.php', $genPath ? $genPath . "/Models" : app_path('Models'), - $genPath ? 'App\\' . str_replace('/', '\\', $path) . '\\Models' : 'App\\Models' + $genPath ? str_replace('/', '\\', $namespace) . '\\Models' : 'App\\Models' ) ) ->setPath( new AddActionPath( 'Add' . $codeStructure->entity()->ucFirstSingular() . 'Action.php', $genPath ? $genPath . "/Actions" : app_path('Actions'), - $genPath ? 'App\\' . str_replace('/', '\\', $path) . '\\Actions' : 'App\\Actions' + $genPath ? str_replace('/', '\\', $namespace) . '\\Actions' : 'App\\Actions' ) ) ->setPath( new EditActionPath( 'Edit' . $codeStructure->entity()->ucFirstSingular() . 'Action.php', $genPath ? $genPath . "/Actions" : app_path('Actions'), - $genPath ? 'App\\' . str_replace('/', '\\', $path) . '\\Actions' : 'App\\Actions' + $genPath ? str_replace('/', '\\', $namespace) . '\\Actions' : 'App\\Actions' ) ) ->setPath( new RequestPath( $codeStructure->entity()->ucFirstSingular() . 'Request.php', $genPath ? $genPath . "/Http/Requests" : app_path('Http/Requests'), - $genPath ? 'App\\' . str_replace('/', '\\', $path) . '\\Http\\Requests' : 'App\\Http\\Requests' + $genPath ? str_replace('/', '\\', $namespace) . '\\Http\\Requests' : 'App\\Http\\Requests' ) ) ->setPath( new ControllerPath( $codeStructure->entity()->ucFirstSingular() . 'Controller.php', $genPath ? $genPath . "/Http/Controllers" : app_path('Http/Controllers'), - $genPath ? 'App\\' . str_replace('/', '\\', $path) . '\\Http\\Controllers' : 'App\\Http\\Controllers' + $genPath ? str_replace('/', '\\', $namespace) . '\\Http\\Controllers' : 'App\\Http\\Controllers' ) ) ->setPath( @@ -80,7 +85,7 @@ public function initPaths(CodeStructure $codeStructure, string $path, bool $isGe new DTOPath( $codeStructure->entity()->ucFirstSingular() . 'DTO.php', $genPath ? $genPath . "/DTO" : app_path('DTO'), - $genPath ? 'App\\' . str_replace('/', '\\', $path) . '\\DTOs' : 'App\\DTOs' + $genPath ? str_replace('/', '\\', $namespace) . '\\DTO' : 'App\\DTO' ) ) ; diff --git a/tests/Feature/CommandAddActionTest.php b/tests/Feature/CommandAddActionTest.php index 5c03ce4..d16ada0 100644 --- a/tests/Feature/CommandAddActionTest.php +++ b/tests/Feature/CommandAddActionTest.php @@ -33,6 +33,7 @@ public function testProduct() $this->assertFileExists($this->path . 'AddProductAction.php'); $file = $this->filesystem->get($this->path . 'AddProductAction.php'); + $this->assertStringContainsString('namespace App\Actions;', $file); $this->assertStringContainsString('use App\Models\Product;', $file); $this->assertStringContainsString('final class AddProductAction', $file); $this->assertStringContainsString('public function handle(array $data): Product', $file); diff --git a/tests/Feature/CommandControllerTest.php b/tests/Feature/CommandControllerTest.php index 003e97e..df806ee 100644 --- a/tests/Feature/CommandControllerTest.php +++ b/tests/Feature/CommandControllerTest.php @@ -33,6 +33,7 @@ public function testProduct() $this->assertFileExists($this->path . 'ProductController.php'); $file = $this->filesystem->get($this->path . 'ProductController.php'); + $this->assertStringContainsString('namespace App\Http\Controllers;', $file); $this->assertStringContainsString('class ProductController extends Controller', $file); $this->assertStringContainsString( 'public function edit(string $id, ProductRequest $request, EditProductAction $action): RedirectResponse', diff --git a/tests/Feature/CommandDtoTest.php b/tests/Feature/CommandDtoTest.php index fd4697a..946a6c3 100644 --- a/tests/Feature/CommandDtoTest.php +++ b/tests/Feature/CommandDtoTest.php @@ -33,6 +33,7 @@ public function testProduct() $this->assertFileExists($this->path . 'ProductDTO.php'); $file = $this->filesystem->get($this->path . 'ProductDTO.php'); + $this->assertStringContainsString('namespace App\DTO;', $file); $this->assertStringContainsString('use App\Models\Product;', $file); $this->assertStringContainsString('use App\Http\Requests\ProductRequest;', $file); $this->assertStringContainsString('private int $id', $file); diff --git a/tests/Feature/CommandEditActionTest.php b/tests/Feature/CommandEditActionTest.php index 992fe54..ee32f86 100644 --- a/tests/Feature/CommandEditActionTest.php +++ b/tests/Feature/CommandEditActionTest.php @@ -33,7 +33,7 @@ public function testProduct() $this->assertFileExists($this->path . 'EditProductAction.php'); $file = $this->filesystem->get($this->path . 'EditProductAction.php'); - + $this->assertStringContainsString('namespace App\Actions;', $file); $this->assertStringContainsString('use App\Models\Product;', $file); $this->assertStringContainsString('final class EditProductAction', $file); $this->assertStringContainsString('public function handle(int $id, array $data): Product', $file); diff --git a/tests/Feature/CommandRequestTest.php b/tests/Feature/CommandRequestTest.php index ccd87c5..65d4aec 100644 --- a/tests/Feature/CommandRequestTest.php +++ b/tests/Feature/CommandRequestTest.php @@ -33,6 +33,7 @@ public function testProduct() $this->assertFileExists($this->path . 'ProductRequest.php'); $file = $this->filesystem->get($this->path . 'ProductRequest.php'); + $this->assertStringContainsString('namespace App\Http\Requests;', $file); $this->assertStringContainsString('class ProductRequest extends FormRequest', $file); $this->assertStringContainsString("'id' => ['int', 'nullable']", $file); $this->assertStringContainsString("'title' => ['string', 'nullable']", $file); diff --git a/tests/Feature/CommandSummaryTest.php b/tests/Feature/CommandSummaryTest.php index 9bf0fa8..38c1622 100644 --- a/tests/Feature/CommandSummaryTest.php +++ b/tests/Feature/CommandSummaryTest.php @@ -75,6 +75,15 @@ public function testProductGenerationPath() $this->routePath = app_path('Generation/routes/'); $this->DTOPath = app_path('Generation/DTO/'); + $controller = $this->filesystem->get($this->controllerPath . 'ProductController.php'); + $this->assertStringContainsString('namespace App\Generation\Http\Controllers;', $controller); + $this->assertStringContainsString('use App\Generation\Http\Requests\ProductRequest;', $controller); + + $dto = $this->filesystem->get($this->DTOPath . 'ProductDTO.php'); + $this->assertStringContainsString('namespace App\Generation\DTO;', $dto); + $this->assertStringContainsString('use App\Generation\Models\Product;', $dto); + $this->assertStringContainsString('use App\Generation\Http\Requests\ProductRequest;', $dto); + $this->assertFileExists($this->actionPath . 'AddProductAction.php'); $this->assertFileExists($this->actionPath . 'EditProductAction.php'); $this->assertFileExists($this->controllerPath . 'ProductController.php'); diff --git a/tests/Feature/DirectoryTest.php b/tests/Feature/DirectoryTest.php new file mode 100644 index 0000000..c334858 --- /dev/null +++ b/tests/Feature/DirectoryTest.php @@ -0,0 +1,68 @@ +filesystem = new Filesystem(); + + $this->app_path = app_path('generation_dir/DTO/'); + + $this->base_path = base_path('generation_dir/DTO/'); + } + + #[Test] + public function testAppGenerationDir() + { + $this->artisan('code:build product --DTO') + ->expectsQuestion('Table', 'products') + ->expectsQuestion('Where to generate the result?', 'app/generation_dir'); + + $this->assertFileExists($this->app_path . 'ProductDTO.php'); + + $file = $this->filesystem->get($this->app_path . 'ProductDTO.php'); + $this->assertStringContainsString('namespace App\GenerationDir\DTO;', $file); + $this->assertStringContainsString('use App\GenerationDir\Models\Product;', $file); + $this->assertStringContainsString('use App\GenerationDir\Http\Requests\ProductRequest;', $file); + } + + #[Test] + public function testBaseGenerationDir() + { + $this->artisan('code:build product --DTO') + ->expectsQuestion('Table', 'products') + ->expectsQuestion('Where to generate the result?', 'generation_dir'); + + $this->assertFileExists($this->base_path . 'ProductDTO.php'); + + $file = $this->filesystem->get($this->base_path . 'ProductDTO.php'); + $this->assertStringContainsString('namespace GenerationDir\DTO;', $file); + $this->assertStringContainsString('use GenerationDir\Models\Product;', $file); + $this->assertStringContainsString('use GenerationDir\Http\Requests\ProductRequest;', $file); + } + + public function tearDown(): void + { + $this->filesystem->delete($this->app_path . 'ProductDTO.php'); + + $this->filesystem->delete($this->base_path . 'ProductDTO.php'); + + parent::tearDown(); + } +}