<?php

declare(strict_types=1);

/*
 * Chill is a software for social workers
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Chill\DocStoreBundle\Tests\Entity;

use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Entity\StoredObjectLock;
use Chill\DocStoreBundle\Entity\StoredObjectLockMethodEnum;
use PHPUnit\Framework\TestCase;

/**
 * @internal
 *
 * @covers \Chill\DocStoreBundle\Entity\StoredObjectLock
 */
class StoredObjectLockTest extends TestCase
{
    public function testIsActiveAtWithNoExpiration(): void
    {
        $storedObject = new StoredObject();
        $now = new \DateTimeImmutable();
        $lock = new StoredObjectLock(
            $storedObject,
            StoredObjectLockMethodEnum::WOPI,
            $now,
            'token',
            null
        );

        self::assertTrue($lock->isActiveAt($now));
        self::assertTrue($lock->isActiveAt($now->modify('+1 year')));
    }

    public function testIsActiveAtWithFutureExpiration(): void
    {
        $storedObject = new StoredObject();
        $now = new \DateTimeImmutable();
        $expireAt = $now->modify('+1 hour');
        $lock = new StoredObjectLock(
            $storedObject,
            StoredObjectLockMethodEnum::WOPI,
            $now,
            'token',
            $expireAt
        );

        self::assertTrue($lock->isActiveAt($now));
        self::assertTrue($lock->isActiveAt($now->modify('+30 minutes')));
    }

    public function testIsActiveAtWithPastExpiration(): void
    {
        $storedObject = new StoredObject();
        $now = new \DateTimeImmutable();
        $expireAt = $now->modify('-1 hour');
        $lock = new StoredObjectLock(
            $storedObject,
            StoredObjectLockMethodEnum::WOPI,
            $now->modify('-2 hours'),
            'token',
            $expireAt
        );

        self::assertFalse($lock->isActiveAt($now));
        self::assertFalse($lock->isActiveAt($expireAt));
        self::assertFalse($lock->isActiveAt($expireAt->modify('+1 second')));
    }
}
