2019-04-22 13:59:04 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit;
|
|
|
|
|
2019-04-23 00:41:32 +02:00
|
|
|
use App\Utils\Traits\MakesDates;
|
2019-04-22 13:59:04 +02:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2019-04-23 00:41:32 +02:00
|
|
|
* @covers App\Utils\Traits\MakesDates
|
2019-04-22 13:59:04 +02:00
|
|
|
*/
|
2019-04-23 00:41:32 +02:00
|
|
|
class MakesDatesTest extends TestCase
|
2019-04-22 13:59:04 +02:00
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
use MakesDates;
|
2019-04-22 13:59:04 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
/*
|
|
|
|
* tests may break with daylight savings changes ( as PHP handle DST under the hood )
|
|
|
|
* these tests are to confirm the timezone conversions
|
|
|
|
* work as expected at this point in time.
|
|
|
|
*/
|
2019-04-22 13:59:04 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function testConvertClientDateToUTCDateTimeTickOverSameDay()
|
|
|
|
{
|
|
|
|
$date_src = '2007-04-19 23:59';
|
|
|
|
$client_timezone = 'Europe/Amsterdam'; // +1 UTC
|
|
|
|
$date_time = new \DateTime($date_src, new \DateTimeZone($client_timezone));
|
2019-04-22 13:59:04 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$utc_date = $this->createUtcDate($date_time, $client_timezone);
|
2019-04-22 14:17:52 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals('2007-04-19 21:59', $date_time->format('Y-m-d H:i'));
|
|
|
|
}
|
2019-04-22 14:17:52 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function testConvertClientDateToUTCDateTimeSameDay()
|
|
|
|
{
|
|
|
|
$date_src = '2007-04-19 21:59';
|
|
|
|
$client_timezone = 'Europe/Amsterdam'; // +1 UTC
|
|
|
|
$date_time = new \DateTime($date_src, new \DateTimeZone($client_timezone));
|
2019-04-22 14:17:52 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$utc_date = $this->createUtcDate($date_time, $client_timezone);
|
2019-04-22 14:17:52 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals($utc_date->format('Y-m-d'), '2007-04-19');
|
|
|
|
}
|
2019-04-22 14:17:52 +02:00
|
|
|
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function testConvertClientDateToUTCDateTimeTickOverNextDay()
|
|
|
|
{
|
|
|
|
$date_src = '2007-04-19 23:59';
|
|
|
|
$client_timezone = 'Atlantic/Cape_Verde'; // -1 UTC
|
|
|
|
$date_time = new \DateTime($date_src, new \DateTimeZone($client_timezone));
|
2019-04-22 14:17:52 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$utc_date = $this->createUtcDate($date_time, $client_timezone);
|
2019-04-22 14:17:52 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals('2007-04-20 00:59', $date_time->format('Y-m-d H:i'));
|
|
|
|
}
|
2019-08-28 01:19:54 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function testConvertClientDateToUTCDateTimeSameDayDiffTimeZone()
|
|
|
|
{
|
|
|
|
$date_src = '2007-04-19 22:59';
|
|
|
|
$client_timezone = 'Atlantic/Cape_Verde'; // -1 UTC
|
|
|
|
$date_time = new \DateTime($date_src, new \DateTimeZone($client_timezone));
|
2019-08-28 01:19:54 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$utc_date = $this->createUtcDate($date_time, $client_timezone);
|
2019-08-28 01:19:54 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals('2007-04-19 23:59', $date_time->format('Y-m-d H:i'));
|
|
|
|
}
|
2019-08-28 01:19:54 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function testCreateClientDate()
|
|
|
|
{
|
|
|
|
$client_date_src = '2007-04-19 22:59';
|
|
|
|
$client_timezone = 'Atlantic/Cape_Verde'; // -1 UTC
|
|
|
|
$date_time = new \DateTime($client_date_src, new \DateTimeZone($client_timezone));
|
2019-08-28 02:58:13 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$utc_date = $this->createUtcDate($date_time, $client_timezone);
|
|
|
|
$client_date = $this->createClientDate($utc_date, $client_timezone);
|
2019-08-28 02:58:13 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals('2007-04-19 22:59', $client_date->format('Y-m-d H:i'));
|
|
|
|
}
|
2019-08-28 02:58:13 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function testCreateClientDateWithFormat()
|
|
|
|
{
|
|
|
|
$client_date_src = '2007-04-19';
|
|
|
|
$client_timezone = 'Atlantic/Cape_Verde'; // -1 UTC
|
|
|
|
$date_time = new \DateTime($client_date_src, new \DateTimeZone($client_timezone));
|
2019-08-28 02:58:13 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$utc_date = $this->createUtcDate($date_time, $client_timezone);
|
|
|
|
$client_date = $this->createClientDate($utc_date, $client_timezone);
|
2019-04-22 14:17:52 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals('2007-04-19', $client_date->format('Y-m-d'));
|
|
|
|
}
|
|
|
|
}
|