chore: complete update
This commit is contained in:
54
tests/Framework/DateTime/ClockInitializerTest.php
Normal file
54
tests/Framework/DateTime/ClockInitializerTest.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Framework\DateTime;
|
||||
|
||||
use App\Framework\DateTime\Clock;
|
||||
use App\Framework\DateTime\ClockInitializer;
|
||||
use App\Framework\DateTime\FrozenClock;
|
||||
use App\Framework\DateTime\SystemClock;
|
||||
|
||||
class ClockInitializerTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testDefaultInitialization(): void
|
||||
{
|
||||
$initializer = new ClockInitializer();
|
||||
$clock = $initializer();
|
||||
|
||||
$this->assertInstanceOf(SystemClock::class, $clock);
|
||||
$this->assertEquals('UTC', $clock->now()->getTimezone()->getName());
|
||||
}
|
||||
|
||||
public function testCustomTimezone(): void
|
||||
{
|
||||
$initializer = new ClockInitializer('Europe/Berlin');
|
||||
$clock = $initializer();
|
||||
|
||||
$this->assertInstanceOf(SystemClock::class, $clock);
|
||||
$this->assertEquals('Europe/Berlin', $clock->now()->getTimezone()->getName());
|
||||
}
|
||||
|
||||
public function testFrozenClockInitialization(): void
|
||||
{
|
||||
$initializer = new ClockInitializer(
|
||||
useFrozenClock: true,
|
||||
frozenTime: '2021-01-01 12:00:00'
|
||||
);
|
||||
$clock = $initializer();
|
||||
|
||||
$this->assertInstanceOf(FrozenClock::class, $clock);
|
||||
$this->assertEquals('2021-01-01 12:00:00', $clock->now()->format('Y-m-d H:i:s'));
|
||||
}
|
||||
|
||||
public function testFrozenClockWithCustomTimezone(): void
|
||||
{
|
||||
$initializer = new ClockInitializer(
|
||||
timezone: 'Europe/Berlin',
|
||||
useFrozenClock: true,
|
||||
frozenTime: '2021-01-01 12:00:00'
|
||||
);
|
||||
$clock = $initializer();
|
||||
|
||||
$this->assertInstanceOf(FrozenClock::class, $clock);
|
||||
$this->assertEquals('Europe/Berlin', $clock->now()->getTimezone()->getName());
|
||||
}
|
||||
}
|
||||
101
tests/Framework/DateTime/DateRangeTest.php
Normal file
101
tests/Framework/DateTime/DateRangeTest.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Framework\DateTime;
|
||||
|
||||
use App\Framework\DateTime\DateRange;
|
||||
|
||||
class DateRangeTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testConstructorWithValidDates(): void
|
||||
{
|
||||
$start = new \DateTimeImmutable('2021-01-01');
|
||||
$end = new \DateTimeImmutable('2021-01-31');
|
||||
|
||||
$range = new DateRange($start, $end);
|
||||
|
||||
$this->assertSame($start, $range->getStart());
|
||||
$this->assertSame($end, $range->getEnd());
|
||||
}
|
||||
|
||||
public function testConstructorWithInvalidDatesThrowsException(): void
|
||||
{
|
||||
$start = new \DateTimeImmutable('2021-01-31');
|
||||
$end = new \DateTimeImmutable('2021-01-01');
|
||||
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
new DateRange($start, $end);
|
||||
}
|
||||
|
||||
public function testFromStrings(): void
|
||||
{
|
||||
$range = DateRange::fromStrings('2021-01-01', '2021-01-31');
|
||||
|
||||
$this->assertEquals('2021-01-01', $range->getStart()->format('Y-m-d'));
|
||||
$this->assertEquals('2021-01-31', $range->getEnd()->format('Y-m-d'));
|
||||
}
|
||||
|
||||
public function testContains(): void
|
||||
{
|
||||
$range = DateRange::fromStrings('2021-01-01', '2021-01-31');
|
||||
|
||||
$inside = new \DateTimeImmutable('2021-01-15');
|
||||
$onStart = new \DateTimeImmutable('2021-01-01');
|
||||
$onEnd = new \DateTimeImmutable('2021-01-31');
|
||||
$before = new \DateTimeImmutable('2020-12-31');
|
||||
$after = new \DateTimeImmutable('2021-02-01');
|
||||
|
||||
$this->assertTrue($range->contains($inside));
|
||||
$this->assertTrue($range->contains($onStart));
|
||||
$this->assertTrue($range->contains($onEnd));
|
||||
$this->assertFalse($range->contains($before));
|
||||
$this->assertFalse($range->contains($after));
|
||||
}
|
||||
|
||||
public function testContainsRange(): void
|
||||
{
|
||||
$mainRange = DateRange::fromStrings('2021-01-01', '2021-01-31');
|
||||
|
||||
$inside = DateRange::fromStrings('2021-01-10', '2021-01-20');
|
||||
$sameRange = DateRange::fromStrings('2021-01-01', '2021-01-31');
|
||||
$overlapping = DateRange::fromStrings('2021-01-15', '2021-02-15');
|
||||
$outside = DateRange::fromStrings('2021-02-01', '2021-02-28');
|
||||
|
||||
$this->assertTrue($mainRange->containsRange($inside));
|
||||
$this->assertTrue($mainRange->containsRange($sameRange));
|
||||
$this->assertFalse($mainRange->containsRange($overlapping));
|
||||
$this->assertFalse($mainRange->containsRange($outside));
|
||||
}
|
||||
|
||||
public function testOverlaps(): void
|
||||
{
|
||||
$mainRange = DateRange::fromStrings('2021-01-01', '2021-01-31');
|
||||
|
||||
$inside = DateRange::fromStrings('2021-01-10', '2021-01-20');
|
||||
$overlappingStart = DateRange::fromStrings('2020-12-15', '2021-01-15');
|
||||
$overlappingEnd = DateRange::fromStrings('2021-01-15', '2021-02-15');
|
||||
$outside = DateRange::fromStrings('2021-02-01', '2021-02-28');
|
||||
|
||||
$this->assertTrue($mainRange->overlaps($inside));
|
||||
$this->assertTrue($mainRange->overlaps($overlappingStart));
|
||||
$this->assertTrue($mainRange->overlaps($overlappingEnd));
|
||||
$this->assertFalse($mainRange->overlaps($outside));
|
||||
}
|
||||
|
||||
public function testGetDuration(): void
|
||||
{
|
||||
$range = DateRange::fromStrings('2021-01-01', '2021-01-31');
|
||||
$duration = $range->getDuration();
|
||||
|
||||
$this->assertInstanceOf(\DateInterval::class, $duration);
|
||||
$this->assertEquals(30, $duration->days);
|
||||
}
|
||||
|
||||
public function testGetDurationInSeconds(): void
|
||||
{
|
||||
$range = DateRange::fromStrings('2021-01-01', '2021-01-02');
|
||||
$seconds = $range->getDurationInSeconds();
|
||||
|
||||
// Ein Tag hat 86400 Sekunden
|
||||
$this->assertEquals(86400, $seconds);
|
||||
}
|
||||
}
|
||||
64
tests/Framework/DateTime/DateTimeFormatterTest.php
Normal file
64
tests/Framework/DateTime/DateTimeFormatterTest.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
138
tests/Framework/DateTime/DateTimeTest.php
Normal file
138
tests/Framework/DateTime/DateTimeTest.php
Normal file
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Framework\DateTime;
|
||||
|
||||
use App\Framework\DateTime\DateTime;
|
||||
use App\Framework\DateTime\Exceptions\InvalidDateTimeException;
|
||||
use App\Framework\DateTime\Exceptions\InvalidTimezoneException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DateTimeTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
// Sicherheitsvorkehrung: Default-Timezone vor jedem Test zurücksetzen
|
||||
if (method_exists(DateTime::class, 'setDefaultTimezone')) {
|
||||
DateTime::setDefaultTimezone('UTC');
|
||||
}
|
||||
}
|
||||
|
||||
public function testNowReturnsCurrentDateTime(): void
|
||||
{
|
||||
$now = DateTime::now();
|
||||
|
||||
$this->assertInstanceOf(\DateTimeImmutable::class, $now);
|
||||
$this->assertEquals('UTC', $now->getTimezone()->getName());
|
||||
$this->assertLessThanOrEqual(2, abs(time() - $now->getTimestamp()));
|
||||
}
|
||||
|
||||
public function testFromTimestamp(): void
|
||||
{
|
||||
$timestamp = 1609459200; // 2021-01-01 00:00:00 UTC
|
||||
$date = DateTime::fromTimestamp($timestamp);
|
||||
|
||||
$this->assertEquals($timestamp, $date->getTimestamp());
|
||||
$this->assertEquals('UTC', $date->getTimezone()->getName());
|
||||
}
|
||||
|
||||
public function testFromString(): void
|
||||
{
|
||||
$date = DateTime::fromString('2021-01-01 12:00:00');
|
||||
|
||||
$this->assertEquals('2021-01-01 12:00:00', $date->format('Y-m-d H:i:s'));
|
||||
$this->assertEquals('UTC', $date->getTimezone()->getName());
|
||||
}
|
||||
|
||||
public function testFromFormat(): void
|
||||
{
|
||||
$date = DateTime::fromFormat('01/01/2021', 'd/m/Y');
|
||||
|
||||
$this->assertEquals('2021-01-01', $date->format('Y-m-d'));
|
||||
}
|
||||
|
||||
public function testFromDateTime(): void
|
||||
{
|
||||
$originalDate = new \DateTime('2021-01-01 12:00:00');
|
||||
$immutableDate = DateTime::fromDateTime($originalDate);
|
||||
|
||||
$this->assertInstanceOf(\DateTimeImmutable::class, $immutableDate);
|
||||
$this->assertEquals($originalDate->format('Y-m-d H:i:s'), $immutableDate->format('Y-m-d H:i:s'));
|
||||
}
|
||||
|
||||
public function testCreateInterval(): void
|
||||
{
|
||||
$interval = DateTime::createInterval('P1D');
|
||||
|
||||
$this->assertInstanceOf(\DateInterval::class, $interval);
|
||||
$this->assertEquals(1, $interval->d);
|
||||
}
|
||||
|
||||
public function testCreateTimezone(): void
|
||||
{
|
||||
$timezone = DateTime::createTimezone('Europe/Berlin');
|
||||
|
||||
$this->assertInstanceOf(\DateTimeZone::class, $timezone);
|
||||
$this->assertEquals('Europe/Berlin', $timezone->getName());
|
||||
}
|
||||
|
||||
public function testInvalidTimezoneThrowsException(): void
|
||||
{
|
||||
$this->expectException(InvalidTimezoneException::class);
|
||||
DateTime::createTimezone('Invalid/Timezone');
|
||||
}
|
||||
|
||||
public function testInvalidDateTimeThrowsException(): void
|
||||
{
|
||||
$this->expectException(InvalidDateTimeException::class);
|
||||
DateTime::fromString('not a date');
|
||||
}
|
||||
|
||||
public function testInvalidFormatThrowsException(): void
|
||||
{
|
||||
$this->expectException(InvalidDateTimeException::class);
|
||||
DateTime::fromFormat('2021-01-01', 'invalid format');
|
||||
}
|
||||
|
||||
public function testInvalidIntervalThrowsException(): void
|
||||
{
|
||||
$this->expectException(InvalidDateTimeException::class);
|
||||
DateTime::createInterval('invalid interval');
|
||||
}
|
||||
|
||||
public function testSetDefaultTimezone(): void
|
||||
{
|
||||
DateTime::setDefaultTimezone('Europe/Berlin');
|
||||
$now = DateTime::now();
|
||||
|
||||
$this->assertEquals('Europe/Berlin', $now->getTimezone()->getName());
|
||||
}
|
||||
|
||||
public function testToday(): void
|
||||
{
|
||||
$today = DateTime::today();
|
||||
$expected = (new \DateTimeImmutable('today'))->format('Y-m-d');
|
||||
|
||||
$this->assertEquals($expected, $today->format('Y-m-d'));
|
||||
$this->assertEquals('00:00:00', $today->format('H:i:s'));
|
||||
}
|
||||
|
||||
public function testTomorrow(): void
|
||||
{
|
||||
$tomorrow = DateTime::tomorrow();
|
||||
$expected = (new \DateTimeImmutable('tomorrow'))->format('Y-m-d');
|
||||
|
||||
$this->assertEquals($expected, $tomorrow->format('Y-m-d'));
|
||||
$this->assertEquals('00:00:00', $tomorrow->format('H:i:s'));
|
||||
}
|
||||
|
||||
public function testYesterday(): void
|
||||
{
|
||||
$yesterday = DateTime::yesterday();
|
||||
$expected = (new \DateTimeImmutable('yesterday'))->format('Y-m-d');
|
||||
|
||||
$this->assertEquals($expected, $yesterday->format('Y-m-d'));
|
||||
$this->assertEquals('00:00:00', $yesterday->format('H:i:s'));
|
||||
}
|
||||
}
|
||||
82
tests/Framework/DateTime/FrozenClockTest.php
Normal file
82
tests/Framework/DateTime/FrozenClockTest.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Framework\DateTime;
|
||||
|
||||
use App\Framework\DateTime\FrozenClock;
|
||||
|
||||
class FrozenClockTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testNowReturnsFrozenTime(): void
|
||||
{
|
||||
$frozenTime = new \DateTimeImmutable('2021-01-01 00:00:00');
|
||||
$clock = new FrozenClock($frozenTime);
|
||||
|
||||
$this->assertEquals($frozenTime, $clock->now());
|
||||
|
||||
// Überprüfen, dass die Zeit eingefroren ist
|
||||
sleep(1);
|
||||
$this->assertEquals($frozenTime, $clock->now());
|
||||
}
|
||||
|
||||
public function testFromTimestampReturnsCorrectDateTime(): void
|
||||
{
|
||||
$clock = new FrozenClock('2021-01-01 00:00:00');
|
||||
$timestamp = 1609459200; // 2021-01-01 00:00:00 UTC
|
||||
$date = $clock->fromTimestamp($timestamp);
|
||||
|
||||
$this->assertInstanceOf(\DateTimeImmutable::class, $date);
|
||||
$this->assertEquals($timestamp, $date->getTimestamp());
|
||||
}
|
||||
|
||||
public function testFromStringReturnsCorrectDateTime(): void
|
||||
{
|
||||
$clock = new FrozenClock('2021-01-01 00:00:00');
|
||||
$date = $clock->fromString('2021-02-01 00:00:00');
|
||||
|
||||
$this->assertInstanceOf(\DateTimeImmutable::class, $date);
|
||||
$this->assertEquals('2021-02-01', $date->format('Y-m-d'));
|
||||
$this->assertEquals('00:00:00', $date->format('H:i:s'));
|
||||
}
|
||||
|
||||
public function testSetToChangesTime(): void
|
||||
{
|
||||
$clock = new FrozenClock('2021-01-01 00:00:00');
|
||||
$newTime = new \DateTimeImmutable('2021-02-01 00:00:00');
|
||||
|
||||
$clock->setTo($newTime);
|
||||
$this->assertEquals($newTime, $clock->now());
|
||||
|
||||
$clock->setTo('2021-03-01 00:00:00');
|
||||
$this->assertEquals('2021-03-01', $clock->now()->format('Y-m-d'));
|
||||
}
|
||||
|
||||
public function testMoveForwardAdvancesTime(): void
|
||||
{
|
||||
$clock = new FrozenClock('2021-01-01 00:00:00');
|
||||
|
||||
$clock->moveForward('P1D'); // 1 Tag vorwärts
|
||||
$this->assertEquals('2021-01-02', $clock->now()->format('Y-m-d'));
|
||||
|
||||
$clock->moveForward('PT1H'); // 1 Stunde vorwärts
|
||||
$this->assertEquals('2021-01-02 01:00:00', $clock->now()->format('Y-m-d H:i:s'));
|
||||
}
|
||||
|
||||
public function testMoveBackwardReversesTime(): void
|
||||
{
|
||||
$clock = new FrozenClock('2021-01-10 10:00:00');
|
||||
|
||||
$clock->moveBackward('P5D'); // 5 Tage zurück
|
||||
$this->assertEquals('2021-01-05', $clock->now()->format('Y-m-d'));
|
||||
|
||||
$clock->moveBackward('PT5H'); // 5 Stunden zurück
|
||||
$this->assertEquals('2021-01-05 05:00:00', $clock->now()->format('Y-m-d H:i:s'));
|
||||
}
|
||||
|
||||
public function testCustomTimezone(): void
|
||||
{
|
||||
$clock = new FrozenClock('2021-01-01 00:00:00', 'Europe/Berlin');
|
||||
$now = $clock->now();
|
||||
|
||||
$this->assertEquals('Europe/Berlin', $now->getTimezone()->getName());
|
||||
}
|
||||
}
|
||||
66
tests/Framework/DateTime/SystemClockTest.php
Normal file
66
tests/Framework/DateTime/SystemClockTest.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Framework\DateTime;
|
||||
|
||||
use App\Framework\DateTime\SystemClock;
|
||||
|
||||
class SystemClockTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testNowReturnsCurrentDateTime(): void
|
||||
{
|
||||
$clock = new SystemClock();
|
||||
$now = $clock->now();
|
||||
|
||||
$this->assertInstanceOf(\DateTimeImmutable::class, $now);
|
||||
$this->assertEquals('UTC', $now->getTimezone()->getName());
|
||||
|
||||
// Überprüfen, dass das Datum innerhalb von 2 Sekunden liegt (für Testlaufzeit)
|
||||
$this->assertLessThanOrEqual(2, abs(time() - $now->getTimestamp()));
|
||||
}
|
||||
|
||||
public function testFromTimestampReturnsCorrectDateTime(): void
|
||||
{
|
||||
$clock = new SystemClock();
|
||||
$timestamp = 1609459200; // 2021-01-01 00:00:00 UTC
|
||||
$date = $clock->fromTimestamp($timestamp);
|
||||
|
||||
$this->assertInstanceOf(\DateTimeImmutable::class, $date);
|
||||
$this->assertEquals($timestamp, $date->getTimestamp());
|
||||
$this->assertEquals('UTC', $date->getTimezone()->getName());
|
||||
}
|
||||
|
||||
public function testFromStringReturnsCorrectDateTime(): void
|
||||
{
|
||||
$clock = new SystemClock();
|
||||
$date = $clock->fromString('2021-01-01 00:00:00');
|
||||
|
||||
$this->assertInstanceOf(\DateTimeImmutable::class, $date);
|
||||
$this->assertEquals('2021-01-01', $date->format('Y-m-d'));
|
||||
$this->assertEquals('00:00:00', $date->format('H:i:s'));
|
||||
}
|
||||
|
||||
public function testFromStringWithFormatReturnsCorrectDateTime(): void
|
||||
{
|
||||
$clock = new SystemClock();
|
||||
$date = $clock->fromString('01/01/2021', 'd/m/Y');
|
||||
|
||||
$this->assertInstanceOf(\DateTimeImmutable::class, $date);
|
||||
$this->assertEquals('2021-01-01', $date->format('Y-m-d'));
|
||||
}
|
||||
|
||||
public function testFromStringWithInvalidFormatThrowsException(): void
|
||||
{
|
||||
$clock = new SystemClock();
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
$clock->fromString('not a date', 'Y-m-d');
|
||||
}
|
||||
|
||||
public function testClockWithCustomTimezone(): void
|
||||
{
|
||||
$clock = new SystemClock('Europe/Berlin');
|
||||
$now = $clock->now();
|
||||
|
||||
$this->assertEquals('Europe/Berlin', $now->getTimezone()->getName());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user