ldc-boutique-cnc-sable/tests/Integration/WithApp/App/Models/DiscountTest.php

87 lines
3.8 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Models;
use App\Models\Discount;
use Domain\Enums\DiscountType;
use Domain\Models\ValueObjects\DiscountValue;
use Illuminate\Support\Carbon;
describe('Discount model', function (): void {
it('filters published discounts including those with null start or end dates', function (): void {
// GIVEN
Carbon::setTestNow('2026-06-15 12:00:00');
// 1. Permanent discount (no start, no end) -> published
$permanent = new Discount();
$permanent->type = DiscountType::PERCENT;
$permanent->value = DiscountValue::fromPercentage(10);
$permanent->is_old_price_displayed = true;
$permanent->date_start = null;
$permanent->date_end = null;
$permanent->save();
// 2. Immediate discount with end date in future -> published
$immediate = new Discount();
$immediate->type = DiscountType::PERCENT;
$immediate->value = DiscountValue::fromPercentage(15);
$immediate->is_old_price_displayed = true;
$immediate->date_start = null;
$immediate->date_end = Carbon::parse('2026-06-20 23:59:59');
$immediate->save();
// 3. Started discount with no end date -> published
$ongoing = new Discount();
$ongoing->type = DiscountType::PERCENT;
$ongoing->value = DiscountValue::fromPercentage(20);
$ongoing->is_old_price_displayed = true;
$ongoing->date_start = Carbon::parse('2026-06-10 00:00:00');
$ongoing->date_end = null;
$ongoing->save();
// 4. Bounded active discount -> published
$bounded = new Discount();
$bounded->type = DiscountType::PERCENT;
$bounded->value = DiscountValue::fromPercentage(25);
$bounded->is_old_price_displayed = true;
$bounded->date_start = Carbon::parse('2026-06-10 00:00:00');
$bounded->date_end = Carbon::parse('2026-06-20 23:59:59');
$bounded->save();
// 5. Future discount -> not published
$future = new Discount();
$future->type = DiscountType::PERCENT;
$future->value = DiscountValue::fromPercentage(30);
$future->is_old_price_displayed = true;
$future->date_start = Carbon::parse('2026-06-20 00:00:00');
$future->date_end = Carbon::parse('2026-06-25 23:59:59');
$future->save();
// 6. Expired discount -> not published
$expired = new Discount();
$expired->type = DiscountType::PERCENT;
$expired->value = DiscountValue::fromPercentage(35);
$expired->is_old_price_displayed = true;
$expired->date_start = Carbon::parse('2026-06-01 00:00:00');
$expired->date_end = Carbon::parse('2026-06-10 23:59:59');
$expired->save();
// WHEN
$publishedDiscounts = Discount::query()->published()->get();
// THEN
$publishedIds = $publishedDiscounts->pluck('id')->all();
expect($publishedIds)->toContain($permanent->id)
->and($publishedIds)->toContain($immediate->id)
->and($publishedIds)->toContain($ongoing->id)
->and($publishedIds)->toContain($bounded->id)
->and($publishedIds)->not->toContain($future->id)
->and($publishedIds)->not->toContain($expired->id);
Carbon::setTestNow();
});
});