Skip to content

Commit dcdbb00

Browse files
Basic Tests
1 parent 400c087 commit dcdbb00

File tree

9 files changed

+774
-9766
lines changed

9 files changed

+774
-9766
lines changed

composer.lock

Lines changed: 0 additions & 9733 deletions
This file was deleted.

stubs/web-controller.stub

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
namespace App\Infrastructure\UI\Web\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\View\View;
7+
use Illuminate\Http\RedirectResponse;
8+
9+
class {{ControllerName}} extends Controller
10+
{
11+
/**
12+
* Display a listing of the resource.
13+
*/
14+
public function index(): View
15+
{
16+
// Get {{domainVariable}} list logic here
17+
${{domainVariable}}s = collect(); // Replace with actual data
18+
19+
return view('{{domainVariable}}.index', compact('{{domainVariable}}s'));
20+
}
21+
22+
/**
23+
* Show the form for creating a new resource.
24+
*/
25+
public function create(): View
26+
{
27+
return view('{{domainVariable}}.create');
28+
}
29+
30+
/**
31+
* Store a newly created resource in storage.
32+
*/
33+
public function store(Request $request): RedirectResponse
34+
{
35+
// Validation logic here
36+
$validated = $request->validate([
37+
'name' => 'required|string|max:255',
38+
'description' => 'nullable|string',
39+
]);
40+
41+
// Create {{domainVariable}} logic here
42+
43+
return redirect()->route('{{domainVariable}}.index')
44+
->with('success', '{{DomainName}} created successfully.');
45+
}
46+
47+
/**
48+
* Display the specified resource.
49+
*/
50+
public function show(string $id): View
51+
{
52+
// Get specific {{domainVariable}} logic here
53+
${{domainVariable}} = null; // Replace with actual data
54+
55+
return view('{{domainVariable}}.show', compact('{{domainVariable}}'));
56+
}
57+
58+
/**
59+
* Show the form for editing the specified resource.
60+
*/
61+
public function edit(string $id): View
62+
{
63+
// Get specific {{domainVariable}} logic here
64+
${{domainVariable}} = null; // Replace with actual data
65+
66+
return view('{{domainVariable}}.edit', compact('{{domainVariable}}'));
67+
}
68+
69+
/**
70+
* Update the specified resource in storage.
71+
*/
72+
public function update(Request $request, string $id): RedirectResponse
73+
{
74+
// Validation logic here
75+
$validated = $request->validate([
76+
'name' => 'required|string|max:255',
77+
'description' => 'nullable|string',
78+
]);
79+
80+
// Update {{domainVariable}} logic here
81+
82+
return redirect()->route('{{domainVariable}}.index')
83+
->with('success', '{{DomainName}} updated successfully.');
84+
}
85+
86+
/**
87+
* Remove the specified resource from storage.
88+
*/
89+
public function destroy(string $id): RedirectResponse
90+
{
91+
// Delete {{domainVariable}} logic here
92+
93+
return redirect()->route('{{domainVariable}}.index')
94+
->with('success', '{{DomainName}} deleted successfully.');
95+
}
96+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
use Illuminate\Filesystem\Filesystem;
4+
use Illuminate\Support\Facades\File;
5+
use PlinCode\LaravelCleanArchitecture\Commands\InstallCleanArchitectureCommand;
6+
7+
describe('InstallCleanArchitectureCommand', function () {
8+
beforeEach(function () {
9+
$this->filesystem = new Filesystem;
10+
$this->command = new InstallCleanArchitectureCommand($this->filesystem);
11+
});
12+
13+
it('has correct command signature and description', function () {
14+
expect($this->command->getName())->toBe('clean-arch:install');
15+
expect($this->command->getDescription())->toBe('Install Clean Architecture structure in Laravel project');
16+
});
17+
18+
it('accepts force option', function () {
19+
$definition = $this->command->getDefinition();
20+
expect($definition->hasOption('force'))->toBeTrue();
21+
});
22+
23+
it('handles missing stub files gracefully', function () {
24+
$mockFilesystem = mock(Filesystem::class);
25+
$mockFilesystem->shouldReceive('isDirectory')->andReturn(false);
26+
$mockFilesystem->shouldReceive('makeDirectory')->andReturn(true);
27+
$mockFilesystem->shouldReceive('exists')->andReturn(false);
28+
29+
$command = new InstallCleanArchitectureCommand($mockFilesystem);
30+
31+
// Test that getStub method throws exception for missing files
32+
expect(function() use ($command) {
33+
$reflection = new ReflectionClass($command);
34+
$method = $reflection->getMethod('getStub');
35+
$method->setAccessible(true);
36+
$method->invoke($command, 'nonexistent');
37+
})->toThrow(\Exception::class, 'Stub file not found');
38+
});
39+
40+
it('has expected directory structure to create', function () {
41+
$reflection = new ReflectionClass($this->command);
42+
$method = $reflection->getMethod('createDirectoryStructure');
43+
$method->setAccessible(true);
44+
45+
// This test verifies the command structure without actually creating directories
46+
expect($method)->toBeInstanceOf(ReflectionMethod::class);
47+
});
48+
49+
it('can generate base classes', function () {
50+
$reflection = new ReflectionClass($this->command);
51+
$baseClassMethods = [
52+
'createBaseModel',
53+
'createBaseController',
54+
'createBaseAction',
55+
'createBaseService',
56+
'createBaseRequest',
57+
'createExceptionClasses'
58+
];
59+
60+
foreach ($baseClassMethods as $methodName) {
61+
$method = $reflection->getMethod($methodName);
62+
expect($method)->toBeInstanceOf(ReflectionMethod::class);
63+
}
64+
});
65+
66+
it('handles composer.json updates', function () {
67+
$reflection = new ReflectionClass($this->command);
68+
$method = $reflection->getMethod('updateComposerAutoload');
69+
70+
expect($method)->toBeInstanceOf(ReflectionMethod::class);
71+
});
72+
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
use Illuminate\Filesystem\Filesystem;
4+
use PlinCode\LaravelCleanArchitecture\Commands\MakeActionCommand;
5+
6+
describe('MakeActionCommand', function () {
7+
beforeEach(function () {
8+
$this->filesystem = new Filesystem;
9+
$this->command = new MakeActionCommand($this->filesystem);
10+
});
11+
12+
it('has correct command signature and description', function () {
13+
expect($this->command->getName())->toBe('clean-arch:make-action');
14+
expect($this->command->getDescription())->toBe('Create a new action in the specified domain');
15+
});
16+
17+
it('has required arguments', function () {
18+
$definition = $this->command->getDefinition();
19+
expect($definition->hasArgument('name'))->toBeTrue();
20+
expect($definition->hasArgument('domain'))->toBeTrue();
21+
});
22+
23+
it('accepts force option', function () {
24+
$definition = $this->command->getDefinition();
25+
expect($definition->hasOption('force'))->toBeTrue();
26+
});
27+
28+
it('can replace placeholders correctly', function () {
29+
$reflection = new ReflectionClass($this->command);
30+
$method = $reflection->getMethod('replacePlaceholders');
31+
$method->setAccessible(true);
32+
33+
$content = '{{ActionName}} in {{DomainName}} for {{PluralDomainName}}';
34+
$result = $method->invoke($this->command, $content, 'CreateUser', 'User');
35+
36+
expect($result)
37+
->toContain('CreateUserAction')
38+
->toContain('User')
39+
->toContain('Users')
40+
->not->toContain('{{ActionName}}')
41+
->not->toContain('{{DomainName}}');
42+
});
43+
44+
it('generates correct action paths', function () {
45+
// Test pluralization logic for action paths
46+
expect(\Illuminate\Support\Str::plural('User'))->toBe('Users');
47+
expect(\Illuminate\Support\Str::plural('Category'))->toBe('Categories');
48+
expect(\Illuminate\Support\Str::plural('ProductCategory'))->toBe('ProductCategories');
49+
});
50+
51+
it('handles getStub method', function () {
52+
$reflection = new ReflectionClass($this->command);
53+
$method = $reflection->getMethod('getStub');
54+
$method->setAccessible(true);
55+
56+
// Test with existing stub
57+
$result = $method->invoke($this->command, 'action');
58+
expect($result)->toBeString();
59+
expect($result)->toContain('<?php');
60+
});
61+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
use Illuminate\Filesystem\Filesystem;
4+
use PlinCode\LaravelCleanArchitecture\Commands\MakeControllerCommand;
5+
6+
describe('MakeControllerCommand', function () {
7+
beforeEach(function () {
8+
$this->filesystem = new Filesystem;
9+
$this->command = new MakeControllerCommand($this->filesystem);
10+
});
11+
12+
it('has correct command signature and description', function () {
13+
expect($this->command->getName())->toBe('clean-arch:make-controller');
14+
expect($this->command->getDescription())->toBe('Create a new controller in the Infrastructure layer');
15+
});
16+
17+
it('has required name argument', function () {
18+
$definition = $this->command->getDefinition();
19+
expect($definition->hasArgument('name'))->toBeTrue();
20+
});
21+
22+
it('has api and web options', function () {
23+
$definition = $this->command->getDefinition();
24+
expect($definition->hasOption('api'))->toBeTrue();
25+
expect($definition->hasOption('web'))->toBeTrue();
26+
expect($definition->hasOption('force'))->toBeTrue();
27+
});
28+
29+
it('can replace placeholders correctly', function () {
30+
$reflection = new ReflectionClass($this->command);
31+
$method = $reflection->getMethod('replacePlaceholders');
32+
$method->setAccessible(true);
33+
34+
$content = 'class {{ControllerName}} for {{DomainName}} and {{PluralDomainName}} with {{domainVariable}}';
35+
$result = $method->invoke($this->command, $content, 'User');
36+
37+
expect($result)
38+
->toContain('class UserController')
39+
->toContain('User')
40+
->toContain('Users')
41+
->toContain('user')
42+
->not->toContain('{{ControllerName}}')
43+
->not->toContain('{{DomainName}}');
44+
});
45+
46+
it('handles getStub method for API controller', function () {
47+
$reflection = new ReflectionClass($this->command);
48+
$method = $reflection->getMethod('getStub');
49+
$method->setAccessible(true);
50+
51+
// Test with existing stub
52+
$result = $method->invoke($this->command, 'controller');
53+
expect($result)->toBeString();
54+
expect($result)->toContain('<?php');
55+
});
56+
57+
it('handles getStub method for Web controller', function () {
58+
$reflection = new ReflectionClass($this->command);
59+
$method = $reflection->getMethod('getStub');
60+
$method->setAccessible(true);
61+
62+
// Test with existing stub
63+
$result = $method->invoke($this->command, 'web-controller');
64+
expect($result)->toBeString();
65+
expect($result)->toContain('<?php');
66+
});
67+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
use Illuminate\Filesystem\Filesystem;
4+
use PlinCode\LaravelCleanArchitecture\Commands\MakeServiceCommand;
5+
6+
describe('MakeServiceCommand', function () {
7+
beforeEach(function () {
8+
$this->filesystem = new Filesystem;
9+
$this->command = new MakeServiceCommand($this->filesystem);
10+
});
11+
12+
it('has correct command signature and description', function () {
13+
expect($this->command->getName())->toBe('clean-arch:make-service');
14+
expect($this->command->getDescription())->toBe('Create a new service in the Application layer');
15+
});
16+
17+
it('has required name argument', function () {
18+
$definition = $this->command->getDefinition();
19+
expect($definition->hasArgument('name'))->toBeTrue();
20+
});
21+
22+
it('accepts force option', function () {
23+
$definition = $this->command->getDefinition();
24+
expect($definition->hasOption('force'))->toBeTrue();
25+
});
26+
27+
it('can replace placeholders correctly', function () {
28+
$reflection = new ReflectionClass($this->command);
29+
$method = $reflection->getMethod('replacePlaceholders');
30+
$method->setAccessible(true);
31+
32+
$content = 'class {{DomainName}}Service for {{PluralDomainName}} with {{domainVariable}}';
33+
$result = $method->invoke($this->command, $content, 'User');
34+
35+
expect($result)
36+
->toContain('class UserService')
37+
->toContain('Users')
38+
->toContain('user')
39+
->not->toContain('{{DomainName}}')
40+
->not->toContain('{{PluralDomainName}}')
41+
->not->toContain('{{domainVariable}}');
42+
});
43+
44+
it('handles getStub method', function () {
45+
$reflection = new ReflectionClass($this->command);
46+
$method = $reflection->getMethod('getStub');
47+
$method->setAccessible(true);
48+
49+
// Test with existing stub
50+
$result = $method->invoke($this->command, 'service');
51+
expect($result)->toBeString();
52+
expect($result)->toContain('<?php');
53+
});
54+
});

0 commit comments

Comments
 (0)