<?php

namespace Tests\Unit\Purchase;

use App\Domains\Purchase\Exceptions\PurchaseExpiredException;
use App\Domains\Purchase\Models\Purchase;
use App\Domains\Purchase\Services\PurchaseStateGuard;
use Tests\TestCase;

class PurchaseStateGuardTest extends TestCase
{
    private PurchaseStateGuard $guard;

    protected function setUp(): void
    {
        parent::setUp();

        $this->guard = new PurchaseStateGuard;
    }

    public function test_it_rejects_a_purchase_with_expired_status(): void
    {
        $purchase = (new Purchase)->forceFill([
            'status' => Purchase::STATUS_EXPIRED,
            'expires_at' => null,
        ]);

        $this->expectException(PurchaseExpiredException::class);

        $this->guard->assertNotExpired($purchase);
    }

    public function test_it_rejects_an_active_purchase_when_its_deadline_has_passed(): void
    {
        $purchase = (new Purchase)->forceFill([
            'status' => Purchase::STATUS_PENDING_PAYMENT,
            'expires_at' => now()->subMinute(),
        ]);

        $this->expectException(PurchaseExpiredException::class);

        $this->guard->assertNotExpired($purchase);
    }

    public function test_it_does_not_treat_a_paid_purchase_as_expired_by_time(): void
    {
        $purchase = (new Purchase)->forceFill([
            'status' => Purchase::STATUS_PAID,
            'expires_at' => now()->subMinute(),
        ]);

        $this->guard->assertNotExpired($purchase);

        $this->assertTrue(true);
    }
}
