Skip to content

Commit

Permalink
refactor(command): request structure directory
Browse files Browse the repository at this point in the history
  • Loading branch information
holiq committed Oct 21, 2023
1 parent ae793e2 commit 3935169
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
24 changes: 17 additions & 7 deletions src/Commands/Application/RequestMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,40 @@
namespace KoalaFacade\DiamondConsole\Commands\Application;

use Illuminate\Console\Command;
use KoalaFacade\DiamondConsole\Commands\Application\Concerns\InteractsWithConsoleInApplication;
use KoalaFacade\DiamondConsole\Commands\Concerns\HasArguments;
use KoalaFacade\DiamondConsole\Commands\Concerns\HasOptions;
use KoalaFacade\DiamondConsole\Commands\Concerns\InteractsWithConsole;
use KoalaFacade\DiamondConsole\Contracts\Console;
use KoalaFacade\DiamondConsole\DataTransferObjects\NamespaceData;
use KoalaFacade\DiamondConsole\DataTransferObjects\PlaceholderData;
use KoalaFacade\DiamondConsole\Support\Source;

class RequestMakeCommand extends Command implements Console
{
use HasArguments, HasOptions, InteractsWithConsoleInApplication;
use HasArguments, HasOptions, InteractsWithConsole;

protected $signature = 'application:make:request {name} {domain} {--force}';

protected $description = 'Create a new request';
protected $description = 'Create a new Request';

public function beforeCreate(): void
{
$this->info(string: 'Generating request file to your project');
$this->info(string: 'Generating action file to your project');
}

public function afterCreate(): void
{
$this->info(string: 'Successfully generate request file');
$this->info(string: 'Successfully generate action file');
}

public function getNamespace(): string
{
return Source::resolveNamespace(
data: new NamespaceData(
structures: Source::resolveApplicationPath() . '\\Http',
domainArgument: 'Requests\\' . $this->resolveDomainArgument(),
domainArgument: $this->resolveDomainArgument(),
structures: Source::resolveApplicationPath(),
nameArgument: $this->resolveNameArgument(),
endsWith: 'Requests',
)
);
}
Expand All @@ -43,4 +45,12 @@ public function getStubPath(): string
{
return Source::resolveStubForPath(name: 'application/request');
}

public function resolvePlaceholders(): PlaceholderData
{
return new PlaceholderData(
namespace: $this->getNamespace(),
class: $this->getClassName(),
);
}
}
41 changes: 20 additions & 21 deletions tests/Feature/Commands/Application/RequestMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
namespace Tests\Feature\Commands;

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use KoalaFacade\DiamondConsole\Exceptions\FileAlreadyExistException;

it(description: 'can generate new Request class')
->tap(function () {
$fileName = app_path(path: 'Http/Requests/User/StoreUserRequest.php');
$fileName = 'Requests/StoreUserRequest.php';

expect(value: File::exists(path: $fileName))->toBeFalse();
expect(value: fileExists(relativeFileName: $fileName, domain: 'User', prefix: applicationPath()))->toBeFalse();

Artisan::call(command: 'diamond:install');
Artisan::call(command: 'domain:install User');
Artisan::call(command: 'application:make:request StoreUserRequest User');

expect(value: File::exists(path: $fileName))->toBeTrue()
expect(value: fileExists(relativeFileName: $fileName, domain: 'User', prefix: applicationPath()))->toBeTrue()
->and(
value: Str::contains(
haystack: File::get(path: $fileName),
haystack: fileGet(relativeFileName: $fileName, domain: 'User', prefix: applicationPath()),
needles: ['{{ class }}', '{{ namespace }}']
)
)->toBeFalse();
Expand All @@ -28,17 +27,17 @@

it(description: 'can generate new Request class with separator')
->tap(function () {
$fileName = app_path(path: 'Http/Requests/User/Foo/BarRequest.php');
$fileName = 'Requests/Foo/BarRequest.php';

expect(value: File::exists(path: $fileName))->toBeFalse();
expect(value: fileExists(relativeFileName: $fileName, domain: 'User', prefix: applicationPath()))->toBeFalse();

Artisan::call(command: 'diamond:install');
Artisan::call(command: 'domain:install User');
Artisan::call(command: 'application:make:request Foo/BarRequest User');

expect(value: File::exists(path: $fileName))->toBeTrue()
expect(value: fileExists(relativeFileName: $fileName, domain: 'User', prefix: applicationPath()))->toBeTrue()
->and(
value: Str::contains(
haystack: File::get(path: $fileName),
haystack: fileGet(relativeFileName: $fileName, domain: 'User', prefix: applicationPath()),
needles: ['{{ class }}', '{{ namespace }}']
)
)->toBeFalse();
Expand All @@ -47,18 +46,18 @@

it(description: 'can force generate exists Request class')
->tap(function () {
$fileName = app_path(path: 'Http/Requests/User/StoreUserRequest.php');
$fileName = 'Requests/StoreUserRequest.php';

expect(value: File::exists(path: $fileName))->toBeFalse();
expect(value: fileExists(relativeFileName: $fileName, domain: 'User', prefix: applicationPath()))->toBeFalse();

Artisan::call(command: 'diamond:install');
Artisan::call(command: 'domain:install User');
Artisan::call(command: 'application:make:request StoreUserRequest User');
Artisan::call(command: 'application:make:request StoreUserRequest User --force');

expect(value: File::exists(path: $fileName))->toBeTrue()
expect(value: fileExists(relativeFileName: $fileName, domain: 'User', prefix: applicationPath()))->toBeTrue()
->and(
value: Str::contains(
haystack: File::get(path: $fileName),
haystack: fileGet(relativeFileName: $fileName, domain: 'User', prefix: applicationPath()),
needles: ['{{ class }}', '{{ namespace }}']
)
)->toBeFalse();
Expand All @@ -67,18 +66,18 @@

it(description: 'cannot generate the Request, if the Request already exists')
->tap(function () {
$fileName = app_path(path: 'Http/Requests/User/StoreUserRequest.php');
$fileName = 'Requests/StoreUserRequest.php';

expect(value: File::exists(path: $fileName))->toBeFalse();
expect(value: fileExists(relativeFileName: $fileName, domain: 'User', prefix: applicationPath()))->toBeFalse();

Artisan::call(command: 'diamond:install');
Artisan::call(command: 'domain:install User');
Artisan::call(command: 'application:make:request StoreUserRequest User');

expect(value: File::exists(path: $fileName))->toBeTrue();
expect(value: fileExists(relativeFileName: $fileName, domain: 'User', prefix: applicationPath()))->toBeTrue();

Artisan::call(command: 'application:make:request StoreUserRequest User');

expect(value: File::exists(path: $fileName))->toBeFalse();
expect(value: fileExists(relativeFileName: $fileName, domain: 'User', prefix: applicationPath()))->toBeFalse();
})
->group(groups: 'commands')
->throws(exception: FileAlreadyExistException::class);

0 comments on commit 3935169

Please sign in to comment.