From 3ac95cc6f82a6b7985ac8a87490da35b13989265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Tue, 29 Sep 2026 14:51:46 +0200 Subject: [PATCH] refactor(common): implement generic actions for controller traits (order, status, star) --- app/Actions/Common/ChangeModelOrder.php | 66 ++++++++++ app/Actions/Common/ChangeModelOrderInput.php | 22 ++++ app/Actions/Common/ToggleModelStar.php | 32 +++++ app/Actions/Common/ToggleModelStarInput.php | 20 +++ app/Actions/Common/ToggleModelStatus.php | 32 +++++ app/Actions/Common/ToggleModelStatusInput.php | 20 +++ app/Http/Requests/Bo/ChangeOrderRequest.php | 7 ++ app/Interfaces/ModelWithOrder.php | 12 ++ app/Models/Product.php | 5 +- app/Models/Range.php | 8 +- app/Models/Slide.php | 9 +- app/Models/Tag.php | 5 +- app/Providers/RouteServiceProvider.php | 2 + app/Traits/Controllers/ChangesModelOrder.php | 82 +++---------- app/Traits/Controllers/TogglesModelStar.php | 28 ++--- app/Traits/Controllers/TogglesModelStatus.php | 44 +++---- app/Traits/Models/HasOrder.php | 21 ++++ .../Controllers/Bo/ProductsControllerTest.php | 116 ++++++++++++++++++ .../Controllers/Bo/TagsControllerTest.php | 39 +++--- .../Actions/Common/ChangeModelOrderTest.php | 68 ++++++++++ .../Actions/Common/ToggleModelStarTest.php | 77 ++++++++++++ .../Actions/Common/ToggleModelStatusTest.php | 45 +++++++ .../Common/ChangeModelOrderInputTest.php | 31 +++++ .../Common/ToggleModelStarInputTest.php | 29 +++++ .../Common/ToggleModelStatusInputTest.php | 29 +++++ 25 files changed, 716 insertions(+), 133 deletions(-) create mode 100644 app/Actions/Common/ChangeModelOrder.php create mode 100644 app/Actions/Common/ChangeModelOrderInput.php create mode 100644 app/Actions/Common/ToggleModelStar.php create mode 100644 app/Actions/Common/ToggleModelStarInput.php create mode 100644 app/Actions/Common/ToggleModelStatus.php create mode 100644 app/Actions/Common/ToggleModelStatusInput.php create mode 100644 app/Interfaces/ModelWithOrder.php create mode 100644 app/Traits/Models/HasOrder.php create mode 100644 tests/Integration/WithApp/App/Actions/Common/ChangeModelOrderTest.php create mode 100644 tests/Integration/WithApp/App/Actions/Common/ToggleModelStarTest.php create mode 100644 tests/Integration/WithApp/App/Actions/Common/ToggleModelStatusTest.php create mode 100644 tests/Unit/App/Actions/Common/ChangeModelOrderInputTest.php create mode 100644 tests/Unit/App/Actions/Common/ToggleModelStarInputTest.php create mode 100644 tests/Unit/App/Actions/Common/ToggleModelStatusInputTest.php diff --git a/app/Actions/Common/ChangeModelOrder.php b/app/Actions/Common/ChangeModelOrder.php new file mode 100644 index 00000000..a44fa440 --- /dev/null +++ b/app/Actions/Common/ChangeModelOrder.php @@ -0,0 +1,66 @@ + + */ +final class ChangeModelOrder implements Action +{ + private const int MAX_ORDER_VALUE = 4294967295; + + /** + * @param \App\Actions\Common\ChangeModelOrderInput $input + */ + #[\Override] + public function __invoke(ActionInput $input): bool + { + Assert::isInstanceOf($input, ChangeModelOrderInput::class); + + $model = $input->model; + $goUp = $input->direction === SortDirection::Ascending; + $currentOrder = $model->getOrder(); + + /** @var (Model&ModelWithOrder)|null $adjacentModel */ + $adjacentModel = $model::query() + ->where('order', $goUp ? '<' : '>', $currentOrder) + ->orderBy('order', $goUp ? SortDirection::Descending : SortDirection::Ascending) + ->first(); + + if ($adjacentModel === null) { + return false; + } + + $this->permuteModelOrders($model, $adjacentModel); + + return true; + } + + private function permuteModelOrders(Model & ModelWithOrder $model1, Model & ModelWithOrder $model2): void + { + $order1 = $model1->getOrder(); + $order2 = $model2->getOrder(); + + // Deactivate timestamps to avoid updating updated_at + $model1->timestamps = false; + $model2->timestamps = false; + + $model1->setOrder(self::MAX_ORDER_VALUE); + $model1->saveQuietly(); + + $model2->setOrder($order1); + $model2->saveQuietly(); + + $model1->setOrder($order2); + $model1->saveQuietly(); + } +} diff --git a/app/Actions/Common/ChangeModelOrderInput.php b/app/Actions/Common/ChangeModelOrderInput.php new file mode 100644 index 00000000..a175026b --- /dev/null +++ b/app/Actions/Common/ChangeModelOrderInput.php @@ -0,0 +1,22 @@ +model, Model::class); + Assert::isInstanceOf($this->model, ModelWithOrder::class); + } +} diff --git a/app/Actions/Common/ToggleModelStar.php b/app/Actions/Common/ToggleModelStar.php new file mode 100644 index 00000000..9ec546a7 --- /dev/null +++ b/app/Actions/Common/ToggleModelStar.php @@ -0,0 +1,32 @@ + + */ +final class ToggleModelStar implements Action +{ + /** + * @param \App\Actions\Common\ToggleModelStarInput $input + */ + #[\Override] + public function __invoke(ActionInput $input): Model & ModelWithStar + { + Assert::isInstanceOf($input, ToggleModelStarInput::class); + + $model = $input->model; + $model->toggleStar(); + $model->saveOrFail(); + + return $model; + } +} diff --git a/app/Actions/Common/ToggleModelStarInput.php b/app/Actions/Common/ToggleModelStarInput.php new file mode 100644 index 00000000..0ce672da --- /dev/null +++ b/app/Actions/Common/ToggleModelStarInput.php @@ -0,0 +1,20 @@ +model, Model::class); + Assert::isInstanceOf($this->model, ModelWithStar::class); + } +} diff --git a/app/Actions/Common/ToggleModelStatus.php b/app/Actions/Common/ToggleModelStatus.php new file mode 100644 index 00000000..038d3484 --- /dev/null +++ b/app/Actions/Common/ToggleModelStatus.php @@ -0,0 +1,32 @@ + + */ +final class ToggleModelStatus implements Action +{ + /** + * @param \App\Actions\Common\ToggleModelStatusInput $input + */ + #[\Override] + public function __invoke(ActionInput $input): Model & ModelWithStatus + { + Assert::isInstanceOf($input, ToggleModelStatusInput::class); + + $model = $input->model; + $model->toggleStatus(); + $model->saveOrFail(); + + return $model; + } +} diff --git a/app/Actions/Common/ToggleModelStatusInput.php b/app/Actions/Common/ToggleModelStatusInput.php new file mode 100644 index 00000000..deab9ef8 --- /dev/null +++ b/app/Actions/Common/ToggleModelStatusInput.php @@ -0,0 +1,20 @@ +model, Model::class); + Assert::isInstanceOf($this->model, ModelWithStatus::class); + } +} diff --git a/app/Http/Requests/Bo/ChangeOrderRequest.php b/app/Http/Requests/Bo/ChangeOrderRequest.php index 5a3f5f76..e45ba779 100644 --- a/app/Http/Requests/Bo/ChangeOrderRequest.php +++ b/app/Http/Requests/Bo/ChangeOrderRequest.php @@ -42,4 +42,11 @@ class ChangeOrderRequest extends FormRequest 'action' => ['required', 'string', 'in:up,down'], ]; } + + public function getSortDirection(): \SortDirection + { + return strval($this->route('action')) === 'up' + ? \SortDirection::Ascending + : \SortDirection::Descending; + } } diff --git a/app/Interfaces/ModelWithOrder.php b/app/Interfaces/ModelWithOrder.php new file mode 100644 index 00000000..1e839844 --- /dev/null +++ b/app/Interfaces/ModelWithOrder.php @@ -0,0 +1,12 @@ + */ use HasFactory; + use HasOrder; + use HasStatus; + #[\Override] protected static function booted() { diff --git a/app/Models/Tag.php b/app/Models/Tag.php index 71c98112..09a5dde2 100644 --- a/app/Models/Tag.php +++ b/app/Models/Tag.php @@ -4,8 +4,10 @@ declare(strict_types=1); namespace App\Models; +use App\Interfaces\ModelWithOrder; use App\Interfaces\ModelWithStatus; use App\Lib\Helpers\ToolboxHelper; +use App\Traits\Models\HasOrder; use App\Traits\Models\HasStatus; use Illuminate\Database\Eloquent\Attributes\Fillable; use Illuminate\Database\Eloquent\Attributes\WithoutTimestamps; @@ -33,11 +35,12 @@ use Illuminate\Support\Str; 'status', ])] #[WithoutTimestamps] -class Tag extends Model implements ModelWithStatus +class Tag extends Model implements ModelWithOrder, ModelWithStatus { /** @use HasFactory<\Database\Factories\TagFactory> */ use HasFactory; + use HasOrder; use HasStatus; #[\Override] diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index 23718629..52cbe44d 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -6,6 +6,7 @@ namespace App\Providers; use App\Dto\ProductWithPricingDto; use App\Models\Discount; +use App\Models\Faq; use App\Models\Product; use App\Models\ProductInco; use App\Models\Range; @@ -90,6 +91,7 @@ class RouteServiceProvider extends ServiceProvider }); Route::model('inco', ProductInco::class); + Route::model('faq', Faq::class); Route::model('tag', Tag::class); Route::model('slide', Slide::class); Route::model('range', Range::class); diff --git a/app/Traits/Controllers/ChangesModelOrder.php b/app/Traits/Controllers/ChangesModelOrder.php index cfb184e6..a80a53c4 100644 --- a/app/Traits/Controllers/ChangesModelOrder.php +++ b/app/Traits/Controllers/ChangesModelOrder.php @@ -4,90 +4,36 @@ declare(strict_types=1); namespace App\Traits\Controllers; +use App\Actions\Common\ChangeModelOrder; +use App\Actions\Common\ChangeModelOrderInput; +use App\Contracts\ActionExecutor; use App\Http\Requests\Bo\ChangeOrderRequest; +use App\Interfaces\ModelWithOrder; use App\Services\FlashMessageService; use Illuminate\Database\Eloquent\Model; use Illuminate\Http\RedirectResponse; -use Illuminate\Support\Collection; -use Illuminate\Support\Facades\DB; -use SortDirection; trait ChangesModelOrder { - private const MODEL_ATTRIBUTE_NAME = 'order'; - - private const MAX_ORDER_VALUE = 4294967295; - public function changeOrder( + Model & ModelWithOrder $model, ChangeOrderRequest $request, + ChangeModelOrder $action, + ActionExecutor $executor, FlashMessageService $flashMessageService ): RedirectResponse { - /** @var Model|null $model */ - $model = Collection::make(request()->route()?->parameters()) - ->first(fn ($param) => $param instanceof Model); + $direction = $request->getSortDirection(); - if ($model === null && method_exists($this, 'getModelClass')) { - $modelClass = $this->getModelClass(); - /** @var Model $modelInstance */ - $modelInstance = app($modelClass); + $changed = $executor->execute($action, new ChangeModelOrderInput($model, $direction)); - $routeParams = request()->route()?->parameters() ?? []; - unset($routeParams['action']); - - $identifier = reset($routeParams); - - if (is_string($identifier) || is_int($identifier)) { - $model = $modelInstance->resolveRouteBinding($identifier); - } - } - - if ($model === null || ! $model instanceof Model || ! array_key_exists(self::MODEL_ATTRIBUTE_NAME, $model->getAttributes())) { - throw new \InvalidArgumentException('Model must exists and extend Model and have an order attribute'); - } - - return DB::transaction(function () use ($model, $request, $flashMessageService) { - $goUp = $request->boolean('direction'); - - $adjacentModel = $model->newQuery() - ->where( - self::MODEL_ATTRIBUTE_NAME, - $goUp ? '<' : '>', - $model->getAttribute(self::MODEL_ATTRIBUTE_NAME) - ) - ->orderBy(self::MODEL_ATTRIBUTE_NAME, $goUp ? - SortDirection::Descending : SortDirection::Ascending) - ->first(); - - if (! $adjacentModel) { - $flashMessageService->error(trans('crud.messages.order_not_changed')); - - return back(); - } - - $this->permuteModelOrders($model, $adjacentModel); - - $flashMessageService->success(trans('crud.messages.order_changed')); + if (! $changed) { + $flashMessageService->error(trans('crud.messages.order_not_changed')); return back(); - }); - } + } - private function permuteModelOrders(Model $model1, Model $model2): void - { - $order1 = $model1->getAttribute(self::MODEL_ATTRIBUTE_NAME); - $order2 = $model2->getAttribute(self::MODEL_ATTRIBUTE_NAME); + $flashMessageService->success(trans('crud.messages.order_changed')); - // * Deactivate timestamps to avoid updating `updated_at` - $model1->timestamps = false; - $model2->timestamps = false; - - $model1->setAttribute(self::MODEL_ATTRIBUTE_NAME, self::MAX_ORDER_VALUE); - $model1->saveQuietly(); - - $model2->setAttribute(self::MODEL_ATTRIBUTE_NAME, $order1); - $model2->saveQuietly(); - - $model1->setAttribute(self::MODEL_ATTRIBUTE_NAME, $order2); - $model1->saveQuietly(); + return back(); } } diff --git a/app/Traits/Controllers/TogglesModelStar.php b/app/Traits/Controllers/TogglesModelStar.php index 9d06d945..a1f152c1 100644 --- a/app/Traits/Controllers/TogglesModelStar.php +++ b/app/Traits/Controllers/TogglesModelStar.php @@ -4,10 +4,12 @@ declare(strict_types=1); namespace App\Traits\Controllers; +use App\Actions\Common\ToggleModelStar; +use App\Actions\Common\ToggleModelStarInput; +use App\Contracts\ActionExecutor; use App\Interfaces\ModelWithStar; use Illuminate\Database\Eloquent\Model; use Illuminate\Http\RedirectResponse; -use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; /** @@ -18,23 +20,17 @@ trait TogglesModelStar /** * Toggles model star */ - public function toggleStar(Model & ModelWithStar $model): RedirectResponse - { + public function toggleStar( + Model & ModelWithStar $model, + ToggleModelStar $action, + ActionExecutor $executor + ): RedirectResponse { $this->authorize('toggleStar', $model); - try { - return DB::transaction(function () use ($model) { - $model->toggleStar(); - $model->saveOrFail(); + $executor->execute($action, new ToggleModelStarInput($model)); - return back()->with('success', trans('crud.messages.has_been_updated', [ - 'model' => Str::ucfirst(trans_choice('models.classes.' . $model->getTable(), 1)), - ])); - }); - } catch (\Throwable) { - return back()->with('error', trans('crud.messages.cannot_be_updated', [ - 'model' => Str::ucfirst(trans_choice('models.classes.' . $model->getTable(), 1)), - ])); - } + return back()->with('success', trans('crud.messages.has_been_updated', [ + 'model' => Str::ucfirst(trans_choice('models.classes.' . $model->getTable(), 1)), + ])); } } diff --git a/app/Traits/Controllers/TogglesModelStatus.php b/app/Traits/Controllers/TogglesModelStatus.php index d5c8e0e6..2305720d 100644 --- a/app/Traits/Controllers/TogglesModelStatus.php +++ b/app/Traits/Controllers/TogglesModelStatus.php @@ -4,47 +4,33 @@ declare(strict_types=1); namespace App\Traits\Controllers; +use App\Actions\Common\ToggleModelStatus; +use App\Actions\Common\ToggleModelStatusInput; +use App\Contracts\ActionExecutor; use App\Interfaces\ModelWithStatus; use Illuminate\Database\Eloquent\Model; use Illuminate\Http\RedirectResponse; -use Illuminate\Support\Collection; -use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; -/** @mixin \Illuminate\Routing\Controller */ +/** + * @mixin \Illuminate\Routing\Controller + */ trait TogglesModelStatus { /** * Toggles model status */ - public function toggleStatus(): RedirectResponse - { - /** @var Model|null $model */ - $model = Collection::make(request()->route()?->parameters()) - ->first(fn ($param) => $param instanceof Model); - - if ($model === null) { - throw new \InvalidArgumentException('No model found in route parameters'); - } - - if (! $model instanceof ModelWithStatus || ! $model instanceof Model) { - throw new \InvalidArgumentException('Model must implement ModelWithStatus and extend Model'); - } - + public function toggleStatus( + Model & ModelWithStatus $model, + ToggleModelStatus $action, + ActionExecutor $executor + ): RedirectResponse { $this->authorize('toggleStatus', $model); - return DB::transaction(function () use ($model) { - $model->toggleStatus(); + $executor->execute($action, new ToggleModelStatusInput($model)); - if ($model->saveOrFail()) { - return back()->with('success', trans('crud.messages.has_been_updated', [ - 'model' => Str::ucfirst(trans_choice('models.classes.' . $model->getTable(), 1)), - ])); - } - - return back()->with('error', trans('crud.messages.cannot_be_updated', [ - 'model' => Str::ucfirst(trans_choice('models.classes.' . $model->getTable(), 1)), - ])); - }); + return back()->with('success', trans('crud.messages.has_been_updated', [ + 'model' => Str::ucfirst(trans_choice('models.classes.' . $model->getTable(), 1)), + ])); } } diff --git a/app/Traits/Models/HasOrder.php b/app/Traits/Models/HasOrder.php new file mode 100644 index 00000000..bbc7dcfd --- /dev/null +++ b/app/Traits/Models/HasOrder.php @@ -0,0 +1,21 @@ +getAttribute('order'); + + return is_numeric($order) ? (int) $order : 0; + } + + public function setOrder(int $order): void + { + $this->setAttribute('order', $order); + } +} diff --git a/tests/Feature/App/Http/Controllers/Bo/ProductsControllerTest.php b/tests/Feature/App/Http/Controllers/Bo/ProductsControllerTest.php index b6c8b661..63e0d2b9 100644 --- a/tests/Feature/App/Http/Controllers/Bo/ProductsControllerTest.php +++ b/tests/Feature/App/Http/Controllers/Bo/ProductsControllerTest.php @@ -235,6 +235,122 @@ describe('ProductsController', function () { expect(Product::find($product->id))->toBeNull(); }); + it('can toggle star', function (): void { + /** @var \Tests\TestCase $this */ + // GIVEN + $range = new Range(); + $range->name = 'Test Range ' . uniqid(); + $range->status = true; + $range->order = random_int(1, 9999); + $range->picture_alt = 'alt'; + $range->picture_title = 'title'; + $range->save(); + + $product = new Product([ + 'range_id' => $range->id, + 'name' => 'Test Product ' . uniqid(), + 'brand' => ProductBrand::loue, + 'invoicing_unit' => InvoicingUnit::PCE, + 'sell_unit' => SellUnit::PCE, + 'order_unit' => OrderUnit::PCE, + 'tax_rate' => TaxRate::TVA_20, + 'description' => 'Test description', + 'status' => true, + ]); + $product->price = Money::EUR(1000); + $product->short_description = 'Short test description'; + $product->picture_alt = 'test alt'; + $product->picture_title = 'test title'; + $product->picture_large_alt = 'test alt'; + $product->picture_large_title = 'test title'; + $product->star = false; + $product->save(); + + // WHEN + $response = $this->patch("/bo/products/{$product->slug}/toggle-star"); + + // THEN + $product->refresh(); + expect($product->star)->toBeTrue(); + $response->assertRedirect(); + }); + + it('can toggle status', function (): void { + /** @var \Tests\TestCase $this */ + // GIVEN + $range = new Range(); + $range->name = 'Test Range ' . uniqid(); + $range->status = true; + $range->order = random_int(1, 9999); + $range->picture_alt = 'alt'; + $range->picture_title = 'title'; + $range->save(); + + $product = new Product([ + 'range_id' => $range->id, + 'name' => 'Test Product ' . uniqid(), + 'brand' => ProductBrand::loue, + 'invoicing_unit' => InvoicingUnit::PCE, + 'sell_unit' => SellUnit::PCE, + 'order_unit' => OrderUnit::PCE, + 'tax_rate' => TaxRate::TVA_20, + 'description' => 'Test description', + 'status' => true, + ]); + $product->price = Money::EUR(1000); + $product->short_description = 'Short test description'; + $product->picture_alt = 'test alt'; + $product->picture_title = 'test title'; + $product->picture_large_alt = 'test alt'; + $product->picture_large_title = 'test title'; + $product->save(); + + // WHEN + $response = $this->patch("/bo/products/{$product->slug}/toggle-status"); + + // THEN + $product->refresh(); + expect($product->status)->toBeFalse(); + $response->assertRedirect(); + }); + + it('can change order', function (): void { + /** @var \Tests\TestCase $this */ + // GIVEN + $range = new Range(); + $range->name = 'Test Range ' . uniqid(); + $range->status = true; + $range->order = random_int(1, 9999); + $range->picture_alt = 'alt'; + $range->picture_title = 'title'; + $range->save(); + + $product = new Product([ + 'range_id' => $range->id, + 'name' => 'Test Product ' . uniqid(), + 'brand' => ProductBrand::loue, + 'invoicing_unit' => InvoicingUnit::PCE, + 'sell_unit' => SellUnit::PCE, + 'order_unit' => OrderUnit::PCE, + 'tax_rate' => TaxRate::TVA_20, + 'description' => 'Test description', + 'status' => true, + ]); + $product->price = Money::EUR(1000); + $product->short_description = 'Short test description'; + $product->picture_alt = 'test alt'; + $product->picture_title = 'test title'; + $product->picture_large_alt = 'test alt'; + $product->picture_large_title = 'test title'; + $product->save(); + + // WHEN + $response = $this->patch("/bo/products/{$product->slug}/up"); + + // THEN + expect($response->status())->toBe(302); + }); + describe('authorization rejections', function () { it('redirects unauthenticated guest to login', function () { /** @var \Tests\TestCase $this */ diff --git a/tests/Feature/App/Http/Controllers/Bo/TagsControllerTest.php b/tests/Feature/App/Http/Controllers/Bo/TagsControllerTest.php index 08a758f9..2e71e3dc 100644 --- a/tests/Feature/App/Http/Controllers/Bo/TagsControllerTest.php +++ b/tests/Feature/App/Http/Controllers/Bo/TagsControllerTest.php @@ -6,15 +6,16 @@ use App\Models\Tag; use App\Models\User; use Domain\Enums\UserRole; -describe('TagsController', function () { - - beforeEach(function () { +describe('TagsController', function (): void { + beforeEach(function (): void { + /** @var \Tests\TestCase $this */ // GIVEN - $this->user = User::factory()->create(['role' => UserRole::SuperAdmin]); - $this->actingAs($this->user, 'backend'); + $user = User::factory()->create(['role' => UserRole::SuperAdmin]); + $this->actingAs($user, 'backend'); }); - it('can access index', function () { + it('can access index', function (): void { + /** @var \Tests\TestCase $this */ // WHEN $response = $this->get('/bo/tags'); @@ -23,7 +24,8 @@ describe('TagsController', function () { $response->assertViewIs('bo.tags.index'); }); - it('can access create', function () { + it('can access create', function (): void { + /** @var \Tests\TestCase $this */ // WHEN $response = $this->get('/bo/tags/create'); @@ -32,7 +34,8 @@ describe('TagsController', function () { $response->assertViewIs('bo.tags.create'); }); - it('can store a tag', function () { + it('can store a tag', function (): void { + /** @var \Tests\TestCase $this */ // GIVEN $data = [ 'label' => 'New Tag', @@ -43,12 +46,12 @@ describe('TagsController', function () { $response = $this->post('/bo/tags', $data); // THEN - $tag = Tag::where('label', 'New Tag')->first(); - expect($tag)->not->toBeNull(); + $tag = Tag::query()->where('label', 'New Tag')->firstOrFail(); $response->assertRedirect("/bo/tags/{$tag->id}/edit"); }); - it('can access edit', function () { + it('can access edit', function (): void { + /** @var \Tests\TestCase $this */ // GIVEN $tag = Tag::factory()->create(); @@ -60,7 +63,8 @@ describe('TagsController', function () { $response->assertViewIs('bo.tags.edit'); }); - it('can update a tag', function () { + it('can update a tag', function (): void { + /** @var \Tests\TestCase $this */ // GIVEN $tag = Tag::factory()->create(); @@ -79,7 +83,8 @@ describe('TagsController', function () { $response->assertRedirect("/bo/tags/{$tag->id}/edit"); }); - it('can delete a tag', function () { + it('can delete a tag', function (): void { + /** @var \Tests\TestCase $this */ // GIVEN $tag = Tag::factory()->create(); @@ -87,11 +92,12 @@ describe('TagsController', function () { $response = $this->delete("/bo/tags/{$tag->id}"); // THEN - expect(Tag::find($tag->id))->toBeNull(); + expect(Tag::query()->find($tag->id))->toBeNull(); $response->assertRedirect('/bo/tags'); }); - it('can toggle status', function () { + it('can toggle status', function (): void { + /** @var \Tests\TestCase $this */ // GIVEN $tag = Tag::factory()->create(); $tag->status = true; @@ -106,7 +112,8 @@ describe('TagsController', function () { $response->assertRedirect(); }); - it('can change order', function () { + it('can change order', function (): void { + /** @var \Tests\TestCase $this */ // GIVEN $tag = Tag::factory()->create(); diff --git a/tests/Integration/WithApp/App/Actions/Common/ChangeModelOrderTest.php b/tests/Integration/WithApp/App/Actions/Common/ChangeModelOrderTest.php new file mode 100644 index 00000000..e0016cb2 --- /dev/null +++ b/tests/Integration/WithApp/App/Actions/Common/ChangeModelOrderTest.php @@ -0,0 +1,68 @@ +create(); + $tag1->setOrder(1); + $tag1->save(); + + $tag2 = Tag::factory()->create(); + $tag2->setOrder(2); + $tag2->save(); + + $action = new ChangeModelOrder(); + $input = new ChangeModelOrderInput($tag2, \SortDirection::Ascending); + + // WHEN + $result = $action($input); + + // THEN + expect($result)->toBeTrue(); + + $fresh1 = Tag::query()->find($tag1->id); + $fresh2 = Tag::query()->find($tag2->id); + + expect($fresh1?->getOrder())->toBe(2) + ->and($fresh2?->getOrder())->toBe(1); + }); + + it('returns false when no adjacent model exists', function (): void { + // GIVEN + $tag = Tag::factory()->create(); + $tag->setOrder(1); + $tag->save(); + + $action = new ChangeModelOrder(); + $input = new ChangeModelOrderInput($tag, \SortDirection::Ascending); + + // WHEN + $result = $action($input); + + // THEN + expect($result)->toBeFalse(); + }); + + it('throws when an invalid input is passed', function (): void { + // GIVEN + $action = new ChangeModelOrder(); + $invalidInput = new class () implements ActionInput + { + }; + + // WHEN + expect( + /** @phpstan-ignore argument.type */ + fn () => $action($invalidInput) + ) + // THEN + ->toThrow(\InvalidArgumentException::class); + }); +}); diff --git a/tests/Integration/WithApp/App/Actions/Common/ToggleModelStarTest.php b/tests/Integration/WithApp/App/Actions/Common/ToggleModelStarTest.php new file mode 100644 index 00000000..67680f7a --- /dev/null +++ b/tests/Integration/WithApp/App/Actions/Common/ToggleModelStarTest.php @@ -0,0 +1,77 @@ +name = 'Test Range ' . uniqid(); + $range->status = true; + $range->order = random_int(1, 9999); + $range->picture_alt = 'alt'; + $range->picture_title = 'title'; + $range->save(); + + $product = new Product([ + 'range_id' => $range->id, + 'name' => 'Test Product ' . uniqid(), + 'brand' => ProductBrand::loue, + 'invoicing_unit' => InvoicingUnit::PCE, + 'sell_unit' => SellUnit::PCE, + 'order_unit' => OrderUnit::PCE, + 'tax_rate' => TaxRate::TVA_20, + 'description' => 'Test description', + 'status' => true, + ]); + $product->price = Money::EUR(1000); + $product->short_description = 'Short test description'; + $product->picture_alt = 'test alt'; + $product->picture_title = 'test title'; + $product->picture_large_alt = 'test alt'; + $product->picture_large_title = 'test title'; + $product->star = false; + $product->save(); + + $action = new ToggleModelStar(); + $input = new ToggleModelStarInput($product); + + // WHEN + $result = $action($input); + + // THEN + expect($result->getAttribute('star'))->toBeTrue(); + + $fresh = Product::query()->find($product->id); + assert($fresh instanceof Product); + expect($fresh->getAttribute('star'))->toBeTrue(); + }); + + it('throws when an invalid input is passed', function (): void { + // GIVEN + $action = new ToggleModelStar(); + $invalidInput = new class () implements ActionInput + { + }; + + // WHEN + expect( + /** @phpstan-ignore argument.type */ + fn () => $action($invalidInput) + ) + // THEN + ->toThrow(\InvalidArgumentException::class); + }); +}); diff --git a/tests/Integration/WithApp/App/Actions/Common/ToggleModelStatusTest.php b/tests/Integration/WithApp/App/Actions/Common/ToggleModelStatusTest.php new file mode 100644 index 00000000..cbdcac8b --- /dev/null +++ b/tests/Integration/WithApp/App/Actions/Common/ToggleModelStatusTest.php @@ -0,0 +1,45 @@ +create([ + 'status' => true, + ]); + + $action = new ToggleModelStatus(); + $input = new ToggleModelStatusInput($tag); + + // WHEN + $result = $action($input); + + // THEN + expect($result->getAttribute('status'))->toBeFalse(); + + $fresh = Tag::query()->find($tag->id); + expect($fresh?->getAttribute('status'))->toBeFalse(); + }); + + it('throws when an invalid input is passed', function (): void { + // GIVEN + $action = new ToggleModelStatus(); + $invalidInput = new class () implements ActionInput + { + }; + + // WHEN + expect( + /** @phpstan-ignore argument.type */ + fn () => $action($invalidInput) + ) + // THEN + ->toThrow(\InvalidArgumentException::class); + }); +}); diff --git a/tests/Unit/App/Actions/Common/ChangeModelOrderInputTest.php b/tests/Unit/App/Actions/Common/ChangeModelOrderInputTest.php new file mode 100644 index 00000000..529b7b61 --- /dev/null +++ b/tests/Unit/App/Actions/Common/ChangeModelOrderInputTest.php @@ -0,0 +1,31 @@ +model)->toBe($mock) + ->and($input->direction)->toBe(\SortDirection::Ascending); + }); +}); diff --git a/tests/Unit/App/Actions/Common/ToggleModelStarInputTest.php b/tests/Unit/App/Actions/Common/ToggleModelStarInputTest.php new file mode 100644 index 00000000..5bfdc681 --- /dev/null +++ b/tests/Unit/App/Actions/Common/ToggleModelStarInputTest.php @@ -0,0 +1,29 @@ +model)->toBe($mock); + }); +}); diff --git a/tests/Unit/App/Actions/Common/ToggleModelStatusInputTest.php b/tests/Unit/App/Actions/Common/ToggleModelStatusInputTest.php new file mode 100644 index 00000000..c7535aff --- /dev/null +++ b/tests/Unit/App/Actions/Common/ToggleModelStatusInputTest.php @@ -0,0 +1,29 @@ +model)->toBe($mock); + }); +});