Skip to content

Commit 699fd21

Browse files
authored
Merge pull request #9 from dev-lnk/fix
Fix dirs and tests
2 parents 13bd0d5 + 7bf694c commit 699fd21

File tree

8 files changed

+93
-7
lines changed

8 files changed

+93
-7
lines changed

src/Services/CodePath/CodePath.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,45 @@ public function initPaths(CodeStructure $codeStructure, string $path, bool $isGe
2626
{
2727
$genPath = $isGenerationDir ? base_path($path) : '';
2828

29+
$namespace = implode('/', array_map(
30+
fn ($dir) => str($dir)->camel()->ucfirst()->value(),
31+
explode("/", $path)
32+
));
33+
2934
$this
3035
->setPath(
3136
new ModelPath(
3237
$codeStructure->entity()->ucFirstSingular() . '.php',
3338
$genPath ? $genPath . "/Models" : app_path('Models'),
34-
$genPath ? 'App\\' . str_replace('/', '\\', $path) . '\\Models' : 'App\\Models'
39+
$genPath ? str_replace('/', '\\', $namespace) . '\\Models' : 'App\\Models'
3540
)
3641
)
3742
->setPath(
3843
new AddActionPath(
3944
'Add' . $codeStructure->entity()->ucFirstSingular() . 'Action.php',
4045
$genPath ? $genPath . "/Actions" : app_path('Actions'),
41-
$genPath ? 'App\\' . str_replace('/', '\\', $path) . '\\Actions' : 'App\\Actions'
46+
$genPath ? str_replace('/', '\\', $namespace) . '\\Actions' : 'App\\Actions'
4247
)
4348
)
4449
->setPath(
4550
new EditActionPath(
4651
'Edit' . $codeStructure->entity()->ucFirstSingular() . 'Action.php',
4752
$genPath ? $genPath . "/Actions" : app_path('Actions'),
48-
$genPath ? 'App\\' . str_replace('/', '\\', $path) . '\\Actions' : 'App\\Actions'
53+
$genPath ? str_replace('/', '\\', $namespace) . '\\Actions' : 'App\\Actions'
4954
)
5055
)
5156
->setPath(
5257
new RequestPath(
5358
$codeStructure->entity()->ucFirstSingular() . 'Request.php',
5459
$genPath ? $genPath . "/Http/Requests" : app_path('Http/Requests'),
55-
$genPath ? 'App\\' . str_replace('/', '\\', $path) . '\\Http\\Requests' : 'App\\Http\\Requests'
60+
$genPath ? str_replace('/', '\\', $namespace) . '\\Http\\Requests' : 'App\\Http\\Requests'
5661
)
5762
)
5863
->setPath(
5964
new ControllerPath(
6065
$codeStructure->entity()->ucFirstSingular() . 'Controller.php',
6166
$genPath ? $genPath . "/Http/Controllers" : app_path('Http/Controllers'),
62-
$genPath ? 'App\\' . str_replace('/', '\\', $path) . '\\Http\\Controllers' : 'App\\Http\\Controllers'
67+
$genPath ? str_replace('/', '\\', $namespace) . '\\Http\\Controllers' : 'App\\Http\\Controllers'
6368
)
6469
)
6570
->setPath(
@@ -80,7 +85,7 @@ public function initPaths(CodeStructure $codeStructure, string $path, bool $isGe
8085
new DTOPath(
8186
$codeStructure->entity()->ucFirstSingular() . 'DTO.php',
8287
$genPath ? $genPath . "/DTO" : app_path('DTO'),
83-
$genPath ? 'App\\' . str_replace('/', '\\', $path) . '\\DTOs' : 'App\\DTOs'
88+
$genPath ? str_replace('/', '\\', $namespace) . '\\DTO' : 'App\\DTO'
8489
)
8590
)
8691
;

tests/Feature/CommandAddActionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function testProduct()
3333
$this->assertFileExists($this->path . 'AddProductAction.php');
3434

3535
$file = $this->filesystem->get($this->path . 'AddProductAction.php');
36+
$this->assertStringContainsString('namespace App\Actions;', $file);
3637
$this->assertStringContainsString('use App\Models\Product;', $file);
3738
$this->assertStringContainsString('final class AddProductAction', $file);
3839
$this->assertStringContainsString('public function handle(array $data): Product', $file);

tests/Feature/CommandControllerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function testProduct()
3333
$this->assertFileExists($this->path . 'ProductController.php');
3434

3535
$file = $this->filesystem->get($this->path . 'ProductController.php');
36+
$this->assertStringContainsString('namespace App\Http\Controllers;', $file);
3637
$this->assertStringContainsString('class ProductController extends Controller', $file);
3738
$this->assertStringContainsString(
3839
'public function edit(string $id, ProductRequest $request, EditProductAction $action): RedirectResponse',

tests/Feature/CommandDtoTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function testProduct()
3333
$this->assertFileExists($this->path . 'ProductDTO.php');
3434

3535
$file = $this->filesystem->get($this->path . 'ProductDTO.php');
36+
$this->assertStringContainsString('namespace App\DTO;', $file);
3637
$this->assertStringContainsString('use App\Models\Product;', $file);
3738
$this->assertStringContainsString('use App\Http\Requests\ProductRequest;', $file);
3839
$this->assertStringContainsString('private int $id', $file);

tests/Feature/CommandEditActionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testProduct()
3333
$this->assertFileExists($this->path . 'EditProductAction.php');
3434

3535
$file = $this->filesystem->get($this->path . 'EditProductAction.php');
36-
36+
$this->assertStringContainsString('namespace App\Actions;', $file);
3737
$this->assertStringContainsString('use App\Models\Product;', $file);
3838
$this->assertStringContainsString('final class EditProductAction', $file);
3939
$this->assertStringContainsString('public function handle(int $id, array $data): Product', $file);

tests/Feature/CommandRequestTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function testProduct()
3333
$this->assertFileExists($this->path . 'ProductRequest.php');
3434

3535
$file = $this->filesystem->get($this->path . 'ProductRequest.php');
36+
$this->assertStringContainsString('namespace App\Http\Requests;', $file);
3637
$this->assertStringContainsString('class ProductRequest extends FormRequest', $file);
3738
$this->assertStringContainsString("'id' => ['int', 'nullable']", $file);
3839
$this->assertStringContainsString("'title' => ['string', 'nullable']", $file);

tests/Feature/CommandSummaryTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ public function testProductGenerationPath()
7575
$this->routePath = app_path('Generation/routes/');
7676
$this->DTOPath = app_path('Generation/DTO/');
7777

78+
$controller = $this->filesystem->get($this->controllerPath . 'ProductController.php');
79+
$this->assertStringContainsString('namespace App\Generation\Http\Controllers;', $controller);
80+
$this->assertStringContainsString('use App\Generation\Http\Requests\ProductRequest;', $controller);
81+
82+
$dto = $this->filesystem->get($this->DTOPath . 'ProductDTO.php');
83+
$this->assertStringContainsString('namespace App\Generation\DTO;', $dto);
84+
$this->assertStringContainsString('use App\Generation\Models\Product;', $dto);
85+
$this->assertStringContainsString('use App\Generation\Http\Requests\ProductRequest;', $dto);
86+
7887
$this->assertFileExists($this->actionPath . 'AddProductAction.php');
7988
$this->assertFileExists($this->actionPath . 'EditProductAction.php');
8089
$this->assertFileExists($this->controllerPath . 'ProductController.php');

tests/Feature/DirectoryTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DevLnk\LaravelCodeBuilder\Tests\Feature;
6+
7+
use DevLnk\LaravelCodeBuilder\Tests\TestCase;
8+
use Illuminate\Filesystem\Filesystem;
9+
use PHPUnit\Framework\Attributes\Test;
10+
11+
final class DirectoryTest extends TestCase
12+
{
13+
private string $app_path = '';
14+
15+
private string $base_path = '';
16+
17+
private Filesystem $filesystem;
18+
19+
public function setUp(): void
20+
{
21+
parent::setUp();
22+
23+
$this->filesystem = new Filesystem();
24+
25+
$this->app_path = app_path('generation_dir/DTO/');
26+
27+
$this->base_path = base_path('generation_dir/DTO/');
28+
}
29+
30+
#[Test]
31+
public function testAppGenerationDir()
32+
{
33+
$this->artisan('code:build product --DTO')
34+
->expectsQuestion('Table', 'products')
35+
->expectsQuestion('Where to generate the result?', 'app/generation_dir');
36+
37+
$this->assertFileExists($this->app_path . 'ProductDTO.php');
38+
39+
$file = $this->filesystem->get($this->app_path . 'ProductDTO.php');
40+
$this->assertStringContainsString('namespace App\GenerationDir\DTO;', $file);
41+
$this->assertStringContainsString('use App\GenerationDir\Models\Product;', $file);
42+
$this->assertStringContainsString('use App\GenerationDir\Http\Requests\ProductRequest;', $file);
43+
}
44+
45+
#[Test]
46+
public function testBaseGenerationDir()
47+
{
48+
$this->artisan('code:build product --DTO')
49+
->expectsQuestion('Table', 'products')
50+
->expectsQuestion('Where to generate the result?', 'generation_dir');
51+
52+
$this->assertFileExists($this->base_path . 'ProductDTO.php');
53+
54+
$file = $this->filesystem->get($this->base_path . 'ProductDTO.php');
55+
$this->assertStringContainsString('namespace GenerationDir\DTO;', $file);
56+
$this->assertStringContainsString('use GenerationDir\Models\Product;', $file);
57+
$this->assertStringContainsString('use GenerationDir\Http\Requests\ProductRequest;', $file);
58+
}
59+
60+
public function tearDown(): void
61+
{
62+
$this->filesystem->delete($this->app_path . 'ProductDTO.php');
63+
64+
$this->filesystem->delete($this->base_path . 'ProductDTO.php');
65+
66+
parent::tearDown();
67+
}
68+
}

0 commit comments

Comments
 (0)