135 lines
3.7 KiB
PHP
135 lines
3.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
use App\Actions\Common\ChangeModelOrderPosition;
|
||
|
|
use App\Actions\Common\ChangeModelOrderPositionInput;
|
||
|
|
use App\Contracts\ActionInput;
|
||
|
|
use App\Models\Tag;
|
||
|
|
|
||
|
|
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);
|
||
|
|
});
|
||
|
|
});
|