<?php

namespace App\Tests\Unit\Service\Utils;

use App\Service\Utils\DatesUtils;
use PHPUnit\Framework\TestCase;

class DatesUtilsTest extends TestCase
{
    public function tearDown(): void
    {
        DatesUtils::clearFakeDatetime();
    }

    /**
     * @see DatesUtils::isNowInInterval()
     *
     * @noinspection PhpRedundantOptionalArgumentInspection
     */
    public function testIsNowInInterval(): void
    {
        DatesUtils::setFakeDatetime('2023-01-01');
        $this->assertTrue(DatesUtils::isNowInInterval(new \DateTime('2020-01-02'), new \DateTime('2025-01-02')));
        $this->assertTrue(DatesUtils::isNowInInterval(new \DateTime('2020-01-02'), null));
    }

    /**
     * @see DatesUtils::isNowInInterval()
     */
    public function testIsNowInIntervalFalse(): void
    {
        DatesUtils::setFakeDatetime('2023-01-01');
        $this->assertFalse(DatesUtils::isNowInInterval(new \DateTime('2019-01-02'), new \DateTime('2020-01-02')));
    }

    /**
     * @see DatesUtils::new()
     */
    public function testNew(): void
    {
        self::assertEquals('2021-01-01', DatesUtils::new('2021-01-01')->format('Y-m-d'));

        $now = new \DateTime();

        self::assertEquals(
            $now->format('Y-m-d'),
            DatesUtils::new()->format('Y-m-d')
        );

        DatesUtils::setFakeDatetime('2021-01-01');

        /* @noinspection PhpRedundantOptionalArgumentInspection */
        self::assertEquals('2021-01-01', DatesUtils::new('now')->format('Y-m-d'));

        DatesUtils::clearFakeDatetime();

        $now = new \DateTime();

        self::assertEquals(
            $now->format('Y-m-d'),
            DatesUtils::new()->format('Y-m-d')
        );
    }

    public function testGetDaysSinceLastUpdate(): void
    {
        DatesUtils::setFakeDatetime('2023-01-21');

        $this->assertEquals(
            DatesUtils::daysSince(new \DateTime('2023-01-01')),
            20
        );
    }
}
