2026-09-29 14:51:46 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-11-16 14:21:17 +01:00
|
|
|
namespace Tests\Integration\WithApp\App\Cqs\Actions\Common;
|
|
|
|
|
|
2026-09-29 14:51:46 +02:00
|
|
|
use App\Contracts\ActionInput;
|
2026-11-10 14:28:00 +01:00
|
|
|
use App\Cqs\Actions\Common\ChangeModelOrder;
|
|
|
|
|
use App\Cqs\Actions\Common\ChangeModelOrderInput;
|
2026-10-29 19:04:46 +01:00
|
|
|
use App\Models\Product;
|
|
|
|
|
use App\Models\Range;
|
2026-09-29 14:51:46 +02:00
|
|
|
use App\Models\Tag;
|
2026-10-29 19:04:46 +01:00
|
|
|
use Domain\Enums\ProductBrand;
|
|
|
|
|
use Domain\Enums\SellUnit;
|
|
|
|
|
use Domain\Enums\TaxRate;
|
|
|
|
|
use Domain\Enums\Units\InvoicingUnit;
|
|
|
|
|
use Domain\Enums\Units\OrderUnit;
|
|
|
|
|
use Money\Money;
|
|
|
|
|
|
|
|
|
|
function createChangeModelOrderProduct(int $order): Product
|
|
|
|
|
{
|
|
|
|
|
$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->saveOrFail();
|
|
|
|
|
|
|
|
|
|
$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->order = $order;
|
|
|
|
|
$product->star = false;
|
|
|
|
|
$product->saveOrFail();
|
|
|
|
|
|
|
|
|
|
return $product;
|
|
|
|
|
}
|
2026-09-29 14:51:46 +02:00
|
|
|
|
|
|
|
|
describe('ChangeModelOrder action', function (): void {
|
|
|
|
|
it('permutes order between adjacent models', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$tag1 = Tag::factory()->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);
|
|
|
|
|
});
|
2026-10-29 19:04:46 +01:00
|
|
|
|
|
|
|
|
it('takes into account soft-deleted models when permuting order', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$product1 = createChangeModelOrderProduct(1);
|
|
|
|
|
$product2 = createChangeModelOrderProduct(2);
|
|
|
|
|
$product3 = createChangeModelOrderProduct(3);
|
|
|
|
|
|
|
|
|
|
$product2->delete();
|
|
|
|
|
|
|
|
|
|
$action = new ChangeModelOrder();
|
|
|
|
|
$input = new ChangeModelOrderInput($product1, \SortDirection::Descending);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$result = $action($input);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($result)->toBeTrue();
|
|
|
|
|
|
|
|
|
|
$fresh1 = Product::query()->withTrashed()->find($product1->id);
|
|
|
|
|
$fresh2 = Product::query()->withTrashed()->find($product2->id);
|
|
|
|
|
$fresh3 = Product::query()->withTrashed()->find($product3->id);
|
|
|
|
|
|
|
|
|
|
expect($fresh1?->getOrder())->toBe(2)
|
|
|
|
|
->and($fresh2?->getOrder())->toBe(1)
|
|
|
|
|
->and($fresh3?->getOrder())->toBe(3);
|
|
|
|
|
});
|
2026-09-29 14:51:46 +02:00
|
|
|
});
|