204 lines
6.1 KiB
PHP
204 lines
6.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Integration\WithApp\App\Cqs\Actions\Common;
|
|
|
|
use App\Contracts\ActionInput;
|
|
use App\Cqs\Actions\Common\ChangeModelOrderPosition;
|
|
use App\Cqs\Actions\Common\ChangeModelOrderPositionInput;
|
|
use App\Models\Product;
|
|
use App\Models\Range;
|
|
use App\Models\Tag;
|
|
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 createChangeModelOrderPositionProduct(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;
|
|
}
|
|
|
|
describe('ChangeModelOrderPosition action', function (): void {
|
|
it('shifts orders when moving a model up to target position', function (): void {
|
|
// GIVEN
|
|
$tag1 = Tag::factory()->create();
|
|
$tag1->setOrder(1);
|
|
$tag1->save();
|
|
|
|
$tag2 = Tag::factory()->create();
|
|
$tag2->setOrder(2);
|
|
$tag2->save();
|
|
|
|
$tag3 = Tag::factory()->create();
|
|
$tag3->setOrder(3);
|
|
$tag3->save();
|
|
|
|
$action = new ChangeModelOrderPosition();
|
|
$input = new ChangeModelOrderPositionInput($tag3, 1);
|
|
|
|
// WHEN
|
|
$result = $action($input);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
|
|
$fresh1 = Tag::query()->find($tag1->id);
|
|
$fresh2 = Tag::query()->find($tag2->id);
|
|
$fresh3 = Tag::query()->find($tag3->id);
|
|
|
|
expect($fresh3?->getOrder())->toBe(1)
|
|
->and($fresh1?->getOrder())->toBe(2)
|
|
->and($fresh2?->getOrder())->toBe(3);
|
|
});
|
|
|
|
it('shifts orders when moving a model down to target position', function (): void {
|
|
// GIVEN
|
|
$tag1 = Tag::factory()->create();
|
|
$tag1->setOrder(1);
|
|
$tag1->save();
|
|
|
|
$tag2 = Tag::factory()->create();
|
|
$tag2->setOrder(2);
|
|
$tag2->save();
|
|
|
|
$tag3 = Tag::factory()->create();
|
|
$tag3->setOrder(3);
|
|
$tag3->save();
|
|
|
|
$action = new ChangeModelOrderPosition();
|
|
$input = new ChangeModelOrderPositionInput($tag1, 3);
|
|
|
|
// WHEN
|
|
$result = $action($input);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
|
|
$fresh1 = Tag::query()->find($tag1->id);
|
|
$fresh2 = Tag::query()->find($tag2->id);
|
|
$fresh3 = Tag::query()->find($tag3->id);
|
|
|
|
expect($fresh2?->getOrder())->toBe(1)
|
|
->and($fresh3?->getOrder())->toBe(2)
|
|
->and($fresh1?->getOrder())->toBe(3);
|
|
});
|
|
|
|
it('returns true without modification when target order is identical to current order', function (): void {
|
|
// GIVEN
|
|
$tag = Tag::factory()->create();
|
|
$tag->setOrder(2);
|
|
$tag->save();
|
|
|
|
$action = new ChangeModelOrderPosition();
|
|
$input = new ChangeModelOrderPositionInput($tag, 2);
|
|
|
|
// WHEN
|
|
$result = $action($input);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
$fresh = Tag::query()->find($tag->id);
|
|
expect($fresh?->getOrder())->toBe(2);
|
|
});
|
|
|
|
it('clamps target order to maximum existing order when target exceeds max', function (): void {
|
|
// GIVEN
|
|
$tag1 = Tag::factory()->create();
|
|
$tag1->setOrder(1);
|
|
$tag1->save();
|
|
|
|
$tag2 = Tag::factory()->create();
|
|
$tag2->setOrder(2);
|
|
$tag2->save();
|
|
|
|
$action = new ChangeModelOrderPosition();
|
|
$input = new ChangeModelOrderPositionInput($tag1, 100);
|
|
|
|
// WHEN
|
|
$result = $action($input);
|
|
|
|
// THEN
|
|
expect($result)->toBeTrue();
|
|
|
|
$fresh1 = Tag::query()->find($tag1->id);
|
|
$fresh2 = Tag::query()->find($tag2->id);
|
|
|
|
expect($fresh2?->getOrder())->toBe(1)
|
|
->and($fresh1?->getOrder())->toBe(2);
|
|
});
|
|
|
|
it('throws when an invalid input is passed', function (): void {
|
|
// GIVEN
|
|
$action = new ChangeModelOrderPosition();
|
|
$invalidInput = new class () implements ActionInput
|
|
{
|
|
};
|
|
|
|
// WHEN
|
|
expect(
|
|
/** @phpstan-ignore argument.type */
|
|
fn () => $action($invalidInput)
|
|
)
|
|
// THEN
|
|
->toThrow(\InvalidArgumentException::class);
|
|
});
|
|
|
|
it('shifts soft-deleted models when moving a model across them', function (): void {
|
|
// GIVEN
|
|
$product1 = createChangeModelOrderPositionProduct(1);
|
|
$product2 = createChangeModelOrderPositionProduct(2);
|
|
$product3 = createChangeModelOrderPositionProduct(3);
|
|
|
|
$product2->delete();
|
|
|
|
$action = new ChangeModelOrderPosition();
|
|
$input = new ChangeModelOrderPositionInput($product1, 3);
|
|
|
|
// 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(3)
|
|
->and($fresh2?->getOrder())->toBe(1)
|
|
->and($fresh3?->getOrder())->toBe(2);
|
|
});
|
|
});
|