ldc-cocotte/tests/Integration/WithApp/App/Actions/Common/ChangeModelOrderTest.php

69 lines
1.8 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
use App\Actions\Common\ChangeModelOrder;
use App\Actions\Common\ChangeModelOrderInput;
use App\Contracts\ActionInput;
use App\Models\Tag;
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);
});
});