<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

use App\Localization\CarbonExtensions;

use Carbon\Carbon;

class CarbonExtensionsTest extends TestCase
{
    public function testToUserLocalConvertsUTCToUserLocalTime()
    {
        $nowInUtc = Carbon::now();

        $nowInLocalTime = CarbonExtensions::toUserLocal($nowInUtc);

        // ensure the correct timezone is set.
        $this->assertTrue($nowInLocalTime->tzName === 'Asia/Manila');
        $this->assertFalse($nowInLocalTime->utc);
        
        // when carbon compares dates, utc is used. so now we ensured, the timezone gets set
        // properly and the datetime is not modified.
        $this->assertTrue($nowInUtc->eq($nowInLocalTime));
    }

    public function testToUserLocalCanHandleStrings()
    {
        $nowInUtc = Carbon::now();

        $nowInLocalTime = CarbonExtensions::toUserLocal($nowInUtc->toDateTimeString());

         // ensure the correct timezone is set.
        $this->assertTrue($nowInLocalTime->tzName === 'Asia/Manila');
        $this->assertFalse($nowInLocalTime->utc);
        
        // when carbon compares dates, utc is used. so now we ensured, the timezone gets set
        // properly and the datetime is not modified.
        $this->assertTrue($nowInUtc->eq($nowInLocalTime));       
    }

    public function testToUtcConvertsFromUserLocalTimeToUTC()
    {
        $nowInLocalTime = Carbon::now()->setTimezone('Asia/Manila');

        $nowInUtc = CarbonExtensions::toUtc($nowInLocalTime);

        $this->assertTrue($nowInUtc->utc);

        $this->assertTrue($nowInLocalTime->eq($nowInUtc));
    }
}
