Files
michaelschiemer/tests/Framework/DateTime/DateTimeFormatterTest.php

65 lines
2.0 KiB
PHP

<?php
namespace Tests\Framework\DateTime;
use App\Framework\DateTime\DateTimeFormatter;
class DateTimeFormatterTest extends \PHPUnit\Framework\TestCase
{
private \DateTimeImmutable $sampleDate;
private DateTimeFormatter $formatter;
protected function setUp(): void
{
$this->sampleDate = new \DateTimeImmutable('2021-01-01 12:34:56', new \DateTimeZone('UTC'));
$this->formatter = new DateTimeFormatter();
}
public function testFormatIso8601(): void
{
$formatted = $this->formatter->formatIso8601($this->sampleDate);
$this->assertEquals('2021-01-01T12:34:56+00:00', $formatted);
}
public function testFormatSql(): void
{
$formatted = $this->formatter->formatSql($this->sampleDate);
$this->assertEquals('2021-01-01 12:34:56', $formatted);
}
public function testFormatDate(): void
{
$formatted = $this->formatter->formatDate($this->sampleDate);
$this->assertEquals('2021-01-01', $formatted);
}
public function testFormatTime(): void
{
$formatted = $this->formatter->formatTime($this->sampleDate);
$this->assertEquals('12:34:56', $formatted);
}
public function testCustomFormat(): void
{
$formatted = $this->formatter->format($this->sampleDate, 'd.m.Y H:i');
$this->assertEquals('01.01.2021 12:34', $formatted);
}
public function testWithCustomTimezone(): void
{
$formatter = new DateTimeFormatter('Europe/Berlin');
$formatted = $formatter->format($this->sampleDate, 'Y-m-d H:i:s T');
// UTC 12:34:56 sollte in Berlin 13:34:56 sein (während Standardzeit/Winterzeit)
$this->assertEquals('2021-01-01 13:34:56 CET', $formatted);
}
public function testWithDateTimeObject(): void
{
$dateTime = new \DateTime('2021-01-01 12:34:56', new \DateTimeZone('UTC'));
$formatted = $this->formatter->formatIso8601($dateTime);
$this->assertEquals('2021-01-01T12:34:56+00:00', $formatted);
}
}