refactor(tags): implement CRUD actions for tags
- Added CreateTag, UpdateTag, and DeleteTag actions. - Introduced CreateTagInput and UpdateTagInput for input validation. - Implemented ActionExecutor for executing actions with logging. - Added tests for all new actions and input validation.
This commit is contained in:
parent
209b7e0193
commit
98e580d327
26 changed files with 772 additions and 56 deletions
34
app/Actions/Tags/CreateTag.php
Normal file
34
app/Actions/Tags/CreateTag.php
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Actions\Tags;
|
||||
|
||||
use App\Contracts\Action;
|
||||
use App\Contracts\ActionInput;
|
||||
use App\Models\Tag;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/**
|
||||
* @implements \App\Contracts\Action<\App\Actions\Tags\CreateTagInput, \App\Models\Tag>
|
||||
*/
|
||||
final class CreateTag implements Action
|
||||
{
|
||||
/**
|
||||
* @param \App\Actions\Tags\CreateTagInput $input
|
||||
*/
|
||||
#[\Override]
|
||||
public function __invoke(ActionInput $input): Tag
|
||||
{
|
||||
Assert::isInstanceOf($input, CreateTagInput::class);
|
||||
|
||||
$tag = new Tag();
|
||||
$tag->fill([
|
||||
'label' => $input->label,
|
||||
'status' => $input->status,
|
||||
]);
|
||||
$tag->saveOrFail();
|
||||
|
||||
return $tag;
|
||||
}
|
||||
}
|
||||
19
app/Actions/Tags/CreateTagInput.php
Normal file
19
app/Actions/Tags/CreateTagInput.php
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Actions\Tags;
|
||||
|
||||
use App\Contracts\ActionInput;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
final readonly class CreateTagInput implements ActionInput
|
||||
{
|
||||
public function __construct(
|
||||
public string $label,
|
||||
public bool $status,
|
||||
) {
|
||||
Assert::stringNotEmpty(trim($this->label), 'Tag label cannot be empty.');
|
||||
Assert::maxLength($this->label, 255);
|
||||
}
|
||||
}
|
||||
26
app/Actions/Tags/DeleteTag.php
Normal file
26
app/Actions/Tags/DeleteTag.php
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Actions\Tags;
|
||||
|
||||
use App\Contracts\Action;
|
||||
use App\Contracts\ActionInput;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/**
|
||||
* @implements \App\Contracts\Action<\App\Actions\Tags\DeleteTagInput, void>
|
||||
*/
|
||||
final class DeleteTag implements Action
|
||||
{
|
||||
/**
|
||||
* @param \App\Actions\Tags\DeleteTagInput $input
|
||||
*/
|
||||
#[\Override]
|
||||
public function __invoke(ActionInput $input): void
|
||||
{
|
||||
Assert::isInstanceOf($input, DeleteTagInput::class);
|
||||
|
||||
$input->tag->deleteOrFail();
|
||||
}
|
||||
}
|
||||
16
app/Actions/Tags/DeleteTagInput.php
Normal file
16
app/Actions/Tags/DeleteTagInput.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Actions\Tags;
|
||||
|
||||
use App\Contracts\ActionInput;
|
||||
use App\Models\Tag;
|
||||
|
||||
final readonly class DeleteTagInput implements ActionInput
|
||||
{
|
||||
public function __construct(
|
||||
public Tag $tag,
|
||||
) {
|
||||
}
|
||||
}
|
||||
34
app/Actions/Tags/UpdateTag.php
Normal file
34
app/Actions/Tags/UpdateTag.php
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Actions\Tags;
|
||||
|
||||
use App\Contracts\Action;
|
||||
use App\Contracts\ActionInput;
|
||||
use App\Models\Tag;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/**
|
||||
* @implements \App\Contracts\Action<\App\Actions\Tags\UpdateTagInput, \App\Models\Tag>
|
||||
*/
|
||||
final class UpdateTag implements Action
|
||||
{
|
||||
/**
|
||||
* @param \App\Actions\Tags\UpdateTagInput $input
|
||||
*/
|
||||
#[\Override]
|
||||
public function __invoke(ActionInput $input): Tag
|
||||
{
|
||||
Assert::isInstanceOf($input, UpdateTagInput::class);
|
||||
|
||||
$tag = $input->tag;
|
||||
$tag->fill([
|
||||
'label' => $input->label,
|
||||
'status' => $input->status,
|
||||
]);
|
||||
$tag->saveOrFail();
|
||||
|
||||
return $tag;
|
||||
}
|
||||
}
|
||||
21
app/Actions/Tags/UpdateTagInput.php
Normal file
21
app/Actions/Tags/UpdateTagInput.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Actions\Tags;
|
||||
|
||||
use App\Contracts\ActionInput;
|
||||
use App\Models\Tag;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
final readonly class UpdateTagInput implements ActionInput
|
||||
{
|
||||
public function __construct(
|
||||
public Tag $tag,
|
||||
public string $label,
|
||||
public bool $status,
|
||||
) {
|
||||
Assert::stringNotEmpty(trim($this->label), 'Tag label cannot be empty.');
|
||||
Assert::maxLength($this->label, 255);
|
||||
}
|
||||
}
|
||||
18
app/Contracts/Action.php
Normal file
18
app/Contracts/Action.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Contracts;
|
||||
|
||||
/**
|
||||
* @template TInput of \App\Contracts\ActionInput
|
||||
* @template TResult
|
||||
*/
|
||||
interface Action
|
||||
{
|
||||
/**
|
||||
* @param TInput $input
|
||||
* @return TResult
|
||||
*/
|
||||
public function __invoke(ActionInput $input);
|
||||
}
|
||||
18
app/Contracts/ActionExecutor.php
Normal file
18
app/Contracts/ActionExecutor.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Contracts;
|
||||
|
||||
interface ActionExecutor
|
||||
{
|
||||
/**
|
||||
* @template TInput of \App\Contracts\ActionInput
|
||||
* @template TResult
|
||||
*
|
||||
* @param \App\Contracts\Action<TInput, TResult> $action
|
||||
* @param TInput $input
|
||||
* @return TResult
|
||||
*/
|
||||
public function execute(Action $action, ActionInput $input): mixed;
|
||||
}
|
||||
9
app/Contracts/ActionInput.php
Normal file
9
app/Contracts/ActionInput.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Contracts;
|
||||
|
||||
interface ActionInput
|
||||
{
|
||||
}
|
||||
12
app/Contracts/Query.php
Normal file
12
app/Contracts/Query.php
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Contracts;
|
||||
|
||||
/**
|
||||
* @template TResult
|
||||
*/
|
||||
interface Query
|
||||
{
|
||||
}
|
||||
55
app/Dto/TagDto.php
Normal file
55
app/Dto/TagDto.php
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Dto;
|
||||
|
||||
use App\Models\Tag;
|
||||
use Illuminate\Contracts\Routing\UrlRoutable;
|
||||
|
||||
final readonly class TagDto implements UrlRoutable
|
||||
{
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public string $label,
|
||||
public string $slug,
|
||||
public int $order,
|
||||
public bool $status,
|
||||
) {
|
||||
}
|
||||
|
||||
public static function fromModel(Tag $tag): self
|
||||
{
|
||||
return new self(
|
||||
id: $tag->id,
|
||||
label: $tag->label,
|
||||
slug: $tag->slug,
|
||||
order: $tag->order,
|
||||
status: $tag->status,
|
||||
);
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function getRouteKey(): mixed
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function getRouteKeyName(): string
|
||||
{
|
||||
return 'id';
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function resolveRouteBinding($value, $field = null): mixed
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function resolveChildRouteBinding($childType, $value, $field): mixed
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -4,16 +4,22 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Http\Controllers\Bo;
|
||||
|
||||
use App\Actions\Tags\CreateTag;
|
||||
use App\Actions\Tags\CreateTagInput;
|
||||
use App\Actions\Tags\DeleteTag;
|
||||
use App\Actions\Tags\DeleteTagInput;
|
||||
use App\Actions\Tags\UpdateTag;
|
||||
use App\Actions\Tags\UpdateTagInput;
|
||||
use App\Contracts\ActionExecutor;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Bo\Tags\StoreTagRequest;
|
||||
use App\Http\Requests\Bo\Tags\UpdateTagRequest;
|
||||
use App\Models\Tag;
|
||||
use App\Services\DataTableService;
|
||||
use App\Queries\Tags\GetPaginatedTagsQuery;
|
||||
use App\Traits\Controllers\ChangesModelOrder;
|
||||
use App\Traits\Controllers\TogglesModelStatus;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class TagsController extends Controller
|
||||
|
|
@ -21,15 +27,16 @@ class TagsController extends Controller
|
|||
use ChangesModelOrder;
|
||||
use TogglesModelStatus;
|
||||
|
||||
public function index(DataTableService $dataTable): View
|
||||
public function __construct(
|
||||
private readonly ActionExecutor $executor,
|
||||
) {
|
||||
}
|
||||
|
||||
public function index(GetPaginatedTagsQuery $query): View
|
||||
{
|
||||
$this->authorize('viewAny', Tag::class);
|
||||
|
||||
$query = Tag::query()->with('products.range');
|
||||
|
||||
$dataTable->sortQuery(Tag::class, $query);
|
||||
|
||||
$tags = $dataTable->paginateQuery($query);
|
||||
$tags = $query->execute();
|
||||
|
||||
return view('bo.tags.index', compact('tags'));
|
||||
}
|
||||
|
|
@ -41,27 +48,22 @@ class TagsController extends Controller
|
|||
return view('bo.tags.create');
|
||||
}
|
||||
|
||||
public function store(StoreTagRequest $request): RedirectResponse
|
||||
public function store(StoreTagRequest $request, CreateTag $action): RedirectResponse
|
||||
{
|
||||
$this->authorize('create', Tag::class);
|
||||
|
||||
return DB::transaction(function () use ($request) {
|
||||
$tag = new Tag();
|
||||
$tag->fill($request->only(
|
||||
$tag->getFillable()
|
||||
));
|
||||
if ($tag->saveOrFail()) {
|
||||
return to_route('bo.tags.edit', $tag)
|
||||
->with('success', trans('crud.messages.has_been_created', [
|
||||
'model' => Str::ucfirst(trans_choice('models.classes.tags', 1)),
|
||||
]));
|
||||
}
|
||||
$tag = $this->executor->execute(
|
||||
$action,
|
||||
new CreateTagInput(
|
||||
label: $request->string('label')->value(),
|
||||
status: $request->boolean('status'),
|
||||
),
|
||||
);
|
||||
|
||||
return to_route('bo.tags.create')
|
||||
->with('error', trans('crud.messages.cannot_be_created', [
|
||||
'model' => Str::ucfirst(trans_choice('models.classes.tags', 1)),
|
||||
]));
|
||||
});
|
||||
return to_route('bo.tags.edit', $tag)
|
||||
->with('success', trans('crud.messages.has_been_created', [
|
||||
'model' => Str::ucfirst(trans_choice('models.classes.tags', 1)),
|
||||
]));
|
||||
}
|
||||
|
||||
public function edit(Tag $tag): View
|
||||
|
|
@ -71,43 +73,34 @@ class TagsController extends Controller
|
|||
return view('bo.tags.edit', compact('tag'));
|
||||
}
|
||||
|
||||
public function update(UpdateTagRequest $request, Tag $tag): RedirectResponse
|
||||
public function update(UpdateTagRequest $request, Tag $tag, UpdateTag $action): RedirectResponse
|
||||
{
|
||||
$this->authorize('update', $tag);
|
||||
|
||||
return DB::transaction(function () use ($request, $tag) {
|
||||
$tag->fill($request->only(
|
||||
$tag->getFillable()
|
||||
));
|
||||
$this->executor->execute(
|
||||
$action,
|
||||
new UpdateTagInput(
|
||||
tag: $tag,
|
||||
label: $request->string('label')->value(),
|
||||
status: $request->boolean('status'),
|
||||
),
|
||||
);
|
||||
|
||||
if ($tag->saveOrFail()) {
|
||||
return to_route('bo.tags.edit', $tag)
|
||||
->with('success', trans('crud.messages.has_been_updated', [
|
||||
'model' => Str::ucfirst(trans_choice('models.classes.tags', 1)),
|
||||
]));
|
||||
}
|
||||
|
||||
return to_route('bo.tags.edit', $tag)
|
||||
->with('error', trans('crud.messages.cannot_be_updated', [
|
||||
'model' => Str::ucfirst(trans_choice('models.classes.tags', 1)),
|
||||
]));
|
||||
});
|
||||
return to_route('bo.tags.edit', $tag)
|
||||
->with('success', trans('crud.messages.has_been_updated', [
|
||||
'model' => Str::ucfirst(trans_choice('models.classes.tags', 1)),
|
||||
]));
|
||||
}
|
||||
|
||||
public function destroy(Tag $tag): RedirectResponse
|
||||
public function destroy(Tag $tag, DeleteTag $action): RedirectResponse
|
||||
{
|
||||
$this->authorize('delete', $tag);
|
||||
|
||||
return DB::transaction(function () use ($tag) {
|
||||
if ($tag->deleteOrFail()) {
|
||||
return to_route('bo.tags.index')
|
||||
->with('warning', trans('crud.messages.has_been_deleted', ['model' => Str::ucfirst(trans_choice('models.classes.tags', 1))]));
|
||||
}
|
||||
$this->executor->execute($action, new DeleteTagInput(tag: $tag));
|
||||
|
||||
return to_route('bo.tags.edit', $tag)
|
||||
->with('error', trans('crud.messages.cannot_be_deleted', [
|
||||
'model' => Str::ucfirst(trans_choice('models.classes.tags', 1)),
|
||||
]));
|
||||
});
|
||||
return to_route('bo.tags.index')
|
||||
->with('warning', trans('crud.messages.has_been_deleted', [
|
||||
'model' => Str::ucfirst(trans_choice('models.classes.tags', 1)),
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Policies\Backend;
|
||||
|
||||
use App\Dto\TagDto;
|
||||
use App\Models\Customer;
|
||||
use App\Models\Tag;
|
||||
use App\Models\User;
|
||||
|
|
@ -20,7 +21,7 @@ class TagPolicy
|
|||
return $user->role->isAtLeastEqualsTo(UserRole::Admin);
|
||||
}
|
||||
|
||||
public function view(User | Customer $user, Tag $_): bool
|
||||
public function view(User | Customer $user, Tag | TagDto $_): bool
|
||||
{
|
||||
if ($user instanceof Customer) {
|
||||
return false;
|
||||
|
|
@ -38,7 +39,7 @@ class TagPolicy
|
|||
return $user->role->isAtLeastEqualsTo(UserRole::Admin);
|
||||
}
|
||||
|
||||
public function update(User | Customer $user, Tag $_): bool
|
||||
public function update(User | Customer $user, Tag | TagDto $_): bool
|
||||
{
|
||||
if ($user instanceof Customer) {
|
||||
return false;
|
||||
|
|
@ -47,7 +48,7 @@ class TagPolicy
|
|||
return $user->role->isAtLeastEqualsTo(UserRole::Admin);
|
||||
}
|
||||
|
||||
public function delete(User | Customer $user, Tag $_): bool
|
||||
public function delete(User | Customer $user, Tag | TagDto $_): bool
|
||||
{
|
||||
if ($user instanceof Customer) {
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Contracts\ActionExecutor;
|
||||
use App\Contracts\Directories\CustomerDirectory;
|
||||
use App\Contracts\Directories\EmployeeDirectory;
|
||||
use App\Contracts\Directories\OrderDirectory;
|
||||
|
|
@ -18,6 +19,7 @@ use App\Directories\EloquentProductDirectory;
|
|||
use App\Repositories\EloquentEmployeeRepository;
|
||||
use App\Repositories\EloquentOrderRepository;
|
||||
use App\Repositories\EloquentUserRepository;
|
||||
use App\Services\ActionExecutorService;
|
||||
use Domain\Models\ValueObjects\Pagination;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Collection;
|
||||
|
|
@ -56,6 +58,7 @@ class AppServiceProvider extends ServiceProvider
|
|||
$this->app->bind(EmployeeDirectory::class, EloquentEmployeeDirectory::class);
|
||||
$this->app->bind(OrderDirectory::class, EloquentOrderDirectory::class);
|
||||
$this->app->bind(ProductDirectory::class, EloquentProductDirectory::class);
|
||||
$this->app->bind(ActionExecutor::class, ActionExecutorService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -4,7 +4,9 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Dto\TagDto;
|
||||
use App\Models\Customer;
|
||||
use App\Policies\Backend\TagPolicy;
|
||||
use App\Policies\Frontend\BasketPolicy;
|
||||
use App\Services\CrmService;
|
||||
use Domain\Models\ValueObjects\BasketProduct;
|
||||
|
|
@ -28,6 +30,7 @@ class AuthServiceProvider extends ServiceProvider
|
|||
protected $policies = [
|
||||
// * Policies Exceptions
|
||||
BasketProduct::class => BasketPolicy::class,
|
||||
TagDto::class => TagPolicy::class,
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
|
|||
36
app/Queries/Tags/GetPaginatedTagsQuery.php
Normal file
36
app/Queries/Tags/GetPaginatedTagsQuery.php
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Queries\Tags;
|
||||
|
||||
use App\Contracts\Query;
|
||||
use App\Dto\TagDto;
|
||||
use App\Models\Tag;
|
||||
use App\Services\DataTableService;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
|
||||
/**
|
||||
* @implements \App\Contracts\Query<\Illuminate\Contracts\Pagination\LengthAwarePaginator<int, \App\Dto\TagDto>>
|
||||
*/
|
||||
final readonly class GetPaginatedTagsQuery implements Query
|
||||
{
|
||||
public function __construct(
|
||||
private DataTableService $dataTable,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator<int, \App\Dto\TagDto>
|
||||
*/
|
||||
public function execute(): LengthAwarePaginator
|
||||
{
|
||||
$query = Tag::query()->with('products.range');
|
||||
|
||||
$this->dataTable->sortQuery(Tag::class, $query);
|
||||
|
||||
$paginator = $this->dataTable->paginateQuery($query);
|
||||
|
||||
return $paginator->through(fn (Tag $tag): TagDto => TagDto::fromModel($tag));
|
||||
}
|
||||
}
|
||||
59
app/Services/ActionExecutorService.php
Normal file
59
app/Services/ActionExecutorService.php
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Contracts\Action;
|
||||
use App\Contracts\ActionExecutor;
|
||||
use App\Contracts\ActionInput;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
final class ActionExecutorService implements ActionExecutor
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ?LoggerInterface $logger = null,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @template TInput of \App\Contracts\ActionInput
|
||||
* @template TResult
|
||||
*
|
||||
* @param \App\Contracts\Action<TInput, TResult> $action
|
||||
* @param TInput $input
|
||||
* @return TResult
|
||||
*/
|
||||
#[\Override]
|
||||
public function execute(Action $action, ActionInput $input): mixed
|
||||
{
|
||||
$actionClass = $action::class;
|
||||
$actionName = class_basename($actionClass);
|
||||
|
||||
$this->logger?->info("[Action] Executing {$actionName}", [
|
||||
'action' => $actionClass,
|
||||
'input' => $input::class,
|
||||
]);
|
||||
|
||||
try {
|
||||
/** @var TResult */
|
||||
return DB::transaction(function () use ($action, $input, $actionName): mixed {
|
||||
$result = $action($input);
|
||||
|
||||
$this->logger?->info("[Action] Succeeded {$actionName}");
|
||||
|
||||
return $result;
|
||||
});
|
||||
} catch (\Throwable $e) {
|
||||
$this->logger?->error("[Action] Failed {$actionName}: {$e->getMessage()}", [
|
||||
'action' => $actionClass,
|
||||
'input' => $input::class,
|
||||
'message' => $e->getMessage(),
|
||||
'exception' => $e,
|
||||
]);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,9 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
use App\Contracts\Action;
|
||||
use App\Contracts\ActionInput;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
describe('application coding rules', function (): void {
|
||||
arch('No Domain Entities are used in Controllers')
|
||||
|
|
@ -24,4 +27,27 @@ describe('application coding rules', function (): void {
|
|||
->classes()
|
||||
->toBeFinal()
|
||||
->toBeReadonly();
|
||||
|
||||
arch('App Actions implement App\Contracts\Action and Inputs implement ActionInput', function (): void {
|
||||
$path = __DIR__ . '/../../app/Actions';
|
||||
$files = (new Finder())->files()->in($path)->name('*.php');
|
||||
|
||||
foreach ($files as $file) {
|
||||
$subPath = $file->getRelativePathname();
|
||||
$className = 'App\\Actions\\' . str_replace(['/', '.php'], ['\\', ''], $subPath);
|
||||
$reflection = new \ReflectionClass($className);
|
||||
|
||||
if (str_ends_with($className, 'Input')) {
|
||||
expect($reflection->implementsInterface(ActionInput::class))
|
||||
->toBeTrue("{$className} must implement App\Contracts\ActionInput");
|
||||
} else {
|
||||
expect($reflection->implementsInterface(Action::class))
|
||||
->toBeTrue("{$className} must implement App\Contracts\Action");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
arch('App Queries implement App\Contracts\Query')
|
||||
->expect('App\Queries')
|
||||
->toImplement('App\Contracts\Query');
|
||||
});
|
||||
|
|
|
|||
|
|
@ -48,6 +48,10 @@ describe('naming conventions in app', function (): void {
|
|||
arch('services have Service suffix')
|
||||
->expect("App\Services")
|
||||
->toHaveSuffix('Service');
|
||||
|
||||
arch('queries have Query suffix')
|
||||
->expect("App\Queries")
|
||||
->toHaveSuffix('Query');
|
||||
});
|
||||
|
||||
describe('naming conventions in domain', function (): void {
|
||||
|
|
|
|||
46
tests/Integration/WithApp/App/Actions/Tags/CreateTagTest.php
Normal file
46
tests/Integration/WithApp/App/Actions/Tags/CreateTagTest.php
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Actions\Tags\CreateTag;
|
||||
use App\Actions\Tags\CreateTagInput;
|
||||
use App\Contracts\ActionInput;
|
||||
use App\Models\Tag;
|
||||
|
||||
describe('CreateTag action', function (): void {
|
||||
it('creates and persists a tag in database', function (): void {
|
||||
// GIVEN
|
||||
$action = new CreateTag();
|
||||
$input = new CreateTagInput(label: 'Offre Spéciale', status: true);
|
||||
|
||||
// WHEN
|
||||
$tag = $action($input);
|
||||
|
||||
// THEN
|
||||
expect($tag)->toBeInstanceOf(Tag::class)
|
||||
->and($tag->id)->toBeInt()
|
||||
->and($tag->label)->toBe('Offre Spéciale')
|
||||
->and($tag->status)->toBeTrue();
|
||||
|
||||
$fresh = Tag::query()->find($tag->id);
|
||||
expect($fresh)->not->toBeNull()
|
||||
->and($fresh?->label)->toBe('Offre Spéciale')
|
||||
->and($fresh?->status)->toBeTrue();
|
||||
});
|
||||
|
||||
it('throws when an invalid input is passed', function (): void {
|
||||
// GIVEN
|
||||
$action = new CreateTag();
|
||||
$invalidInput = new class () implements ActionInput
|
||||
{
|
||||
};
|
||||
|
||||
// WHEN
|
||||
expect(
|
||||
/** @phpstan-ignore argument.type */
|
||||
fn () => $action($invalidInput)
|
||||
)
|
||||
// THEN
|
||||
->toThrow(\InvalidArgumentException::class);
|
||||
});
|
||||
});
|
||||
39
tests/Integration/WithApp/App/Actions/Tags/DeleteTagTest.php
Normal file
39
tests/Integration/WithApp/App/Actions/Tags/DeleteTagTest.php
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Actions\Tags\DeleteTag;
|
||||
use App\Actions\Tags\DeleteTagInput;
|
||||
use App\Contracts\ActionInput;
|
||||
use App\Models\Tag;
|
||||
|
||||
describe('DeleteTag action', function (): void {
|
||||
it('deletes an existing tag from database', function (): void {
|
||||
// GIVEN
|
||||
$tag = Tag::factory()->create();
|
||||
$action = new DeleteTag();
|
||||
$input = new DeleteTagInput(tag: $tag);
|
||||
|
||||
// WHEN
|
||||
$action($input);
|
||||
|
||||
// THEN
|
||||
expect(Tag::query()->find($tag->id))->toBeNull();
|
||||
});
|
||||
|
||||
it('throws when an invalid input is passed', function (): void {
|
||||
// GIVEN
|
||||
$action = new DeleteTag();
|
||||
$invalidInput = new class () implements ActionInput
|
||||
{
|
||||
};
|
||||
|
||||
// WHEN
|
||||
expect(
|
||||
/** @phpstan-ignore argument.type */
|
||||
fn () => $action($invalidInput)
|
||||
)
|
||||
// THEN
|
||||
->toThrow(\InvalidArgumentException::class);
|
||||
});
|
||||
});
|
||||
46
tests/Integration/WithApp/App/Actions/Tags/UpdateTagTest.php
Normal file
46
tests/Integration/WithApp/App/Actions/Tags/UpdateTagTest.php
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Actions\Tags\UpdateTag;
|
||||
use App\Actions\Tags\UpdateTagInput;
|
||||
use App\Contracts\ActionInput;
|
||||
use App\Models\Tag;
|
||||
|
||||
describe('UpdateTag action', function (): void {
|
||||
it('updates an existing tag in database', function (): void {
|
||||
// GIVEN
|
||||
$tag = Tag::factory()->create(['label' => 'Ancien Label', 'status' => false]);
|
||||
$action = new UpdateTag();
|
||||
$input = new UpdateTagInput(tag: $tag, label: 'Nouveau Label', status: true);
|
||||
|
||||
// WHEN
|
||||
$updatedTag = $action($input);
|
||||
|
||||
// THEN
|
||||
expect($updatedTag->id)->toBe($tag->id)
|
||||
->and($updatedTag->label)->toBe('Nouveau Label')
|
||||
->and($updatedTag->status)->toBeTrue();
|
||||
|
||||
$fresh = Tag::query()->find($tag->id);
|
||||
expect($fresh)->not->toBeNull()
|
||||
->and($fresh?->label)->toBe('Nouveau Label')
|
||||
->and($fresh?->status)->toBeTrue();
|
||||
});
|
||||
|
||||
it('throws when an invalid input is passed', function (): void {
|
||||
// GIVEN
|
||||
$action = new UpdateTag();
|
||||
$invalidInput = new class () implements ActionInput
|
||||
{
|
||||
};
|
||||
|
||||
// WHEN
|
||||
expect(
|
||||
/** @phpstan-ignore argument.type */
|
||||
fn () => $action($invalidInput)
|
||||
)
|
||||
// THEN
|
||||
->toThrow(\InvalidArgumentException::class);
|
||||
});
|
||||
});
|
||||
|
|
@ -0,0 +1,86 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Contracts\Action;
|
||||
use App\Contracts\ActionInput;
|
||||
use App\Services\ActionExecutorService;
|
||||
use Psr\Log\AbstractLogger;
|
||||
|
||||
describe('ActionExecutorService', function (): void {
|
||||
it('executes action in transaction and logs success', function (): void {
|
||||
// GIVEN
|
||||
$logger = new class () extends AbstractLogger
|
||||
{
|
||||
/** @var array<int, string> */
|
||||
public array $logs = [];
|
||||
|
||||
#[\Override]
|
||||
public function log($level, string | \Stringable $message, array $context = []): void
|
||||
{
|
||||
$this->logs[] = (string) $message;
|
||||
}
|
||||
};
|
||||
|
||||
$action = new class () implements Action
|
||||
{
|
||||
#[\Override]
|
||||
public function __invoke(ActionInput $input): string
|
||||
{
|
||||
return 'done';
|
||||
}
|
||||
};
|
||||
|
||||
$input = new class () implements ActionInput
|
||||
{
|
||||
};
|
||||
$executor = new ActionExecutorService($logger);
|
||||
|
||||
// WHEN
|
||||
$result = $executor->execute($action, $input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBe('done')
|
||||
->and($logger->logs)->toHaveCount(2)
|
||||
->and($logger->logs[0])->toContain('Executing')
|
||||
->and($logger->logs[1])->toContain('Succeeded');
|
||||
});
|
||||
|
||||
it('logs error and rethrows when action fails', function (): void {
|
||||
// GIVEN
|
||||
$logger = new class () extends AbstractLogger
|
||||
{
|
||||
/** @var array<int, string> */
|
||||
public array $logs = [];
|
||||
|
||||
#[\Override]
|
||||
public function log($level, string | \Stringable $message, array $context = []): void
|
||||
{
|
||||
$this->logs[] = (string) $message;
|
||||
}
|
||||
};
|
||||
|
||||
$action = new class () implements Action
|
||||
{
|
||||
#[\Override]
|
||||
public function __invoke(ActionInput $input): never
|
||||
{
|
||||
throw new \RuntimeException('Database failure');
|
||||
}
|
||||
};
|
||||
|
||||
$input = new class () implements ActionInput
|
||||
{
|
||||
};
|
||||
$executor = new ActionExecutorService($logger);
|
||||
|
||||
// WHEN
|
||||
expect(fn () => $executor->execute($action, $input))
|
||||
// THEN
|
||||
->toThrow(\RuntimeException::class, 'Database failure');
|
||||
|
||||
expect($logger->logs)->toHaveCount(2)
|
||||
->and($logger->logs[0])->toContain('Executing')
|
||||
->and($logger->logs[1])->toContain('Failed');
|
||||
});
|
||||
});
|
||||
44
tests/Unit/App/Actions/Tags/CreateTagInputTest.php
Normal file
44
tests/Unit/App/Actions/Tags/CreateTagInputTest.php
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Actions\Tags\CreateTagInput;
|
||||
|
||||
describe('CreateTagInput', function (): void {
|
||||
it('can be instantiated with valid data', function (): void {
|
||||
// GIVEN
|
||||
$label = 'Nouveauté';
|
||||
$status = true;
|
||||
|
||||
// WHEN
|
||||
$input = new CreateTagInput(label: $label, status: $status);
|
||||
|
||||
// THEN
|
||||
expect($input->label)->toBe($label)
|
||||
->and($input->status)->toBe($status);
|
||||
});
|
||||
});
|
||||
|
||||
describe('CreateTagInput validation rules', function (): void {
|
||||
it('throws when label is empty or whitespace only', function (string $invalidLabel): void {
|
||||
// GIVEN
|
||||
$status = true;
|
||||
$label = $invalidLabel;
|
||||
|
||||
// WHEN
|
||||
expect(fn () => new CreateTagInput(label: $label, status: $status))
|
||||
// THEN
|
||||
->toThrow(\InvalidArgumentException::class);
|
||||
})->with(['', ' ', "\t\n"]);
|
||||
|
||||
it('throws when label exceeds 255 characters', function (): void {
|
||||
// GIVEN
|
||||
$status = true;
|
||||
$label = str_repeat('a', 256);
|
||||
|
||||
// WHEN
|
||||
expect(fn () => new CreateTagInput(label: $label, status: $status))
|
||||
// THEN
|
||||
->toThrow(\InvalidArgumentException::class);
|
||||
});
|
||||
});
|
||||
19
tests/Unit/App/Actions/Tags/DeleteTagInputTest.php
Normal file
19
tests/Unit/App/Actions/Tags/DeleteTagInputTest.php
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Actions\Tags\DeleteTagInput;
|
||||
use App\Models\Tag;
|
||||
|
||||
describe('DeleteTagInput', function (): void {
|
||||
it('can be instantiated with valid data', function (): void {
|
||||
// GIVEN
|
||||
$tag = new Tag();
|
||||
|
||||
// WHEN
|
||||
$input = new DeleteTagInput(tag: $tag);
|
||||
|
||||
// THEN
|
||||
expect($input->tag)->toBe($tag);
|
||||
});
|
||||
});
|
||||
49
tests/Unit/App/Actions/Tags/UpdateTagInputTest.php
Normal file
49
tests/Unit/App/Actions/Tags/UpdateTagInputTest.php
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Actions\Tags\UpdateTagInput;
|
||||
use App\Models\Tag;
|
||||
|
||||
describe('UpdateTagInput', function (): void {
|
||||
it('can be instantiated with valid data', function (): void {
|
||||
// GIVEN
|
||||
$tag = new Tag();
|
||||
$label = 'Nouveauté';
|
||||
$status = false;
|
||||
|
||||
// WHEN
|
||||
$input = new UpdateTagInput(tag: $tag, label: $label, status: $status);
|
||||
|
||||
// THEN
|
||||
expect($input->tag)->toBe($tag)
|
||||
->and($input->label)->toBe($label)
|
||||
->and($input->status)->toBe($status);
|
||||
});
|
||||
});
|
||||
|
||||
describe('UpdateTagInput validation rules', function (): void {
|
||||
it('throws when label is empty or whitespace only', function (string $invalidLabel): void {
|
||||
// GIVEN
|
||||
$tag = new Tag();
|
||||
$status = true;
|
||||
$label = $invalidLabel;
|
||||
|
||||
// WHEN
|
||||
expect(fn () => new UpdateTagInput(tag: $tag, label: $label, status: $status))
|
||||
// THEN
|
||||
->toThrow(\InvalidArgumentException::class);
|
||||
})->with(['', ' ', "\t\n"]);
|
||||
|
||||
it('throws when label exceeds 255 characters', function (): void {
|
||||
// GIVEN
|
||||
$tag = new Tag();
|
||||
$status = true;
|
||||
$label = str_repeat('a', 256);
|
||||
|
||||
// WHEN
|
||||
expect(fn () => new UpdateTagInput(tag: $tag, label: $label, status: $status))
|
||||
// THEN
|
||||
->toThrow(\InvalidArgumentException::class);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Reference in a new issue