95 lines
3.1 KiB
PHP
95 lines
3.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Integration\WithApp\App\Cqs\Actions\Slides;
|
|
|
|
use App\Contracts\ActionInput;
|
|
use App\Contracts\ActionInputEmpty;
|
|
use App\Cqs\Actions\Slides\UpdateSlidesStatus;
|
|
use App\Models\Slide;
|
|
use Illuminate\Support\Facades\Date;
|
|
use Psr\Log\NullLogger;
|
|
|
|
describe('UpdateSlidesStatus action', function (): void {
|
|
it('publishes slides past date_start and unpublishes slides past date_end while preserving scheduling dates', function (): void {
|
|
// GIVEN
|
|
Date::setTestNow('2026-11-14 10:00:00');
|
|
$publishStart = Date::now()->addHour();
|
|
$publishEnd = Date::now()->addDay();
|
|
$unpublishStart = Date::now()->subHour();
|
|
$unpublishEnd = Date::now()->addHour();
|
|
|
|
$toPublish = Slide::factory()->create([
|
|
'date_start' => $publishStart,
|
|
'date_end' => $publishEnd,
|
|
]);
|
|
$toUnpublish = Slide::factory()->create([
|
|
'date_start' => $unpublishStart,
|
|
'date_end' => $unpublishEnd,
|
|
]);
|
|
|
|
expect($toPublish->status)->toBeFalse()
|
|
->and($toUnpublish->status)->toBeTrue();
|
|
|
|
Date::setTestNow('2026-11-14 12:00:00');
|
|
$action = new UpdateSlidesStatus(new NullLogger());
|
|
|
|
// WHEN
|
|
$action(new ActionInputEmpty());
|
|
|
|
// THEN
|
|
$toPublish->refresh();
|
|
$toUnpublish->refresh();
|
|
|
|
expect($toPublish->status)->toBeTrue()
|
|
->and($toPublish->date_start?->format('Y-m-d H:i:s'))->toBe($publishStart->format('Y-m-d H:i:s'))
|
|
->and($toPublish->date_end?->format('Y-m-d H:i:s'))->toBe($publishEnd->format('Y-m-d H:i:s'))
|
|
->and($toUnpublish->status)->toBeFalse()
|
|
->and($toUnpublish->date_start?->format('Y-m-d H:i:s'))->toBe($unpublishStart->format('Y-m-d H:i:s'))
|
|
->and($toUnpublish->date_end?->format('Y-m-d H:i:s'))->toBe($unpublishEnd->format('Y-m-d H:i:s'));
|
|
|
|
Date::setTestNow();
|
|
});
|
|
|
|
it('does not re-publish an already expired unpublished slide on subsequent runs', function (): void {
|
|
// GIVEN
|
|
Date::setTestNow('2026-11-14 10:00:00');
|
|
$expiredSlide = Slide::factory()->create([
|
|
'date_start' => Date::now()->subDays(3),
|
|
'date_end' => Date::now()->subDay(),
|
|
]);
|
|
|
|
expect($expiredSlide->status)->toBeFalse();
|
|
|
|
$action = new UpdateSlidesStatus(new NullLogger());
|
|
|
|
// WHEN
|
|
$action(new ActionInputEmpty());
|
|
|
|
// THEN
|
|
$expiredSlide->refresh();
|
|
|
|
expect($expiredSlide->status)->toBeFalse()
|
|
->and($expiredSlide->date_start)->not->toBeNull()
|
|
->and($expiredSlide->date_end)->not->toBeNull();
|
|
|
|
Date::setTestNow();
|
|
});
|
|
|
|
it('throws when an invalid input is passed', function (): void {
|
|
// GIVEN
|
|
$action = new UpdateSlidesStatus(new NullLogger());
|
|
$invalidInput = new class () implements ActionInput
|
|
{
|
|
};
|
|
|
|
// WHEN
|
|
expect(
|
|
/** @phpstan-ignore argument.type */
|
|
fn () => $action($invalidInput)
|
|
)
|
|
// THEN
|
|
->toThrow(\InvalidArgumentException::class);
|
|
});
|
|
});
|