Skip to content

Commit

Permalink
refactor(command): resource structure directory
Browse files Browse the repository at this point in the history
  • Loading branch information
holiq committed Oct 21, 2023
1 parent 3935169 commit 251b134
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 37 deletions.
30 changes: 15 additions & 15 deletions src/Commands/Application/ResourceMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,59 @@
namespace KoalaFacade\DiamondConsole\Commands\Application;

use Illuminate\Console\Command;
use Illuminate\Support\Str;
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 ResourceMakeCommand extends Command implements Console
{
use HasArguments, HasOptions, InteractsWithConsoleInApplication;
use HasArguments, HasOptions, InteractsWithConsole;

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

protected $description = 'Create a new resource';
protected $description = 'Create a new Resource';

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

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

public function getNamespace(): string
{
return Source::resolveNamespace(
data: new NamespaceData(
structures: Source::resolveApplicationPath() . '\\Http',
domainArgument: 'Resources\\' . $this->resolveDomainArgument(),
domainArgument: $this->resolveDomainArgument(),
structures: Source::resolveApplicationPath(),
nameArgument: $this->resolveNameArgument(),
endsWith: 'Resources',
)
);
}

public function getStubPath(): string
{
$stub = 'application/resource';
$stubs = 'application/resource';

if ($this->resolveModelOption()) {
$stub .= '-model';
$stubs .= '-model';
}

return Source::resolveStubForPath(name: $stub);
return Source::resolveStubForPath(name: $stubs);
}

public function resolvePlaceholders(): PlaceholderData
{
return new PlaceholderData(
namespace: Str::ucfirst(string: $this->getNamespace()),
namespace: $this->getNamespace(),
class: $this->getClassName(),
model: $this->resolveModelOption(),
modelNamespace: $this->resolveModelNamespace()
Expand All @@ -67,10 +67,10 @@ public function resolveModelNamespace(): ?string
if ($this->resolveModelOption()) {
$namespace = Source::resolveNamespace(
data: new NamespaceData(
structures: Source::resolveDomainPath(),
domainArgument: 'Shared\\' . $this->resolveDomainArgument(),
domainArgument: $this->resolveDomainArgument(),
structures: Source::resolveInfrastructurePath(),
nameArgument: $this->resolveModelOption(),
endsWith: 'Models\\' . $this->resolveModelOption(),
endsWith: 'Database\\Models',
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion stubs/application/resource-model.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace {{ namespace }};

use {{ modelNamespace }};
use {{ modelNamespace }}\{{ model }};
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
Expand Down
41 changes: 20 additions & 21 deletions tests/Feature/Commands/Application/ResourceMakeCommandTest.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 Resource class')
->tap(function () {
$fileName = app_path(path: 'Http/Resources/User/UserResource.php');
$fileName = 'Resources/UserResource.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:resource UserResource User --model=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 }}', '{{ model }}', '{{ modelNamespace }}']
)
)->toBeFalse();
Expand All @@ -28,17 +27,17 @@

it(description: 'can generate new Resource class with separator')
->tap(function () {
$fileName = app_path(path: 'Http/Resources/User/Foo/BarResource.php');
$fileName = 'Resources/Foo/BarResource.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:resource Foo/BarResource User --model=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 }}', '{{ model }}', '{{ modelNamespace }}']
)
)->toBeFalse();
Expand All @@ -47,18 +46,18 @@

it(description: 'can force generate exists Resource class')
->tap(function () {
$fileName = app_path(path: 'Http/Resources/User/UserResource.php');
$fileName = 'Resources/UserResource.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:resource UserResource User --model=User');
Artisan::call(command: 'application:make:resource UserResource User --model=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 }}', '{{ model }}', '{{ modelNamespace }}']
)
)->toBeFalse();
Expand All @@ -67,18 +66,18 @@

it(description: 'cannot generate the Resource, if the Resource already exists')
->tap(function () {
$fileName = app_path(path: 'Http/Resources/User/UserResource.php');
$fileName = 'Resources/UserResource.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:resource UserResource User --model=User');

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

Artisan::call(command: 'application:make:resource UserResource User --model=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 251b134

Please sign in to comment.