Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/Services/CodePath/CodePath.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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'
)
)
;
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/CommandAddActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/CommandControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/CommandDtoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/CommandEditActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/CommandRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions tests/Feature/CommandSummaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
68 changes: 68 additions & 0 deletions tests/Feature/DirectoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace DevLnk\LaravelCodeBuilder\Tests\Feature;

use DevLnk\LaravelCodeBuilder\Tests\TestCase;
use Illuminate\Filesystem\Filesystem;
use PHPUnit\Framework\Attributes\Test;

final class DirectoryTest extends TestCase
{
private string $app_path = '';

private string $base_path = '';

private Filesystem $filesystem;

public function setUp(): void
{
parent::setUp();

$this->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();
}
}