Simple problems require simple solutions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

123 lines
4.5 KiB

<?php
namespace Tests\Unit;
use App\Enums\DurationModifier;
use App\Services\CalculateDuration;
use DateTimeImmutable;
use DateTimeInterface;
use PHPUnit\Framework\TestCase;
class CalculateDurationTest extends TestCase
{
public function testCanCalculateDurationBetweenTwoDates(): void
{
$start = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '2024-08-01T00:00:00+00:00');
$end = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '2024-08-17T00:00:00+00:00');
$calculateDuration = new CalculateDuration($start, $end);
$result = $calculateDuration->result();
$this->assertSame(17, $result->days);
$this->assertSame(2, $result->weeks);
$this->assertSame(12, $result->weekDays);
}
public function testCanCalculateDurationBetweenTwoDatesCloseToOverThreeWeeks(): void
{
$start = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '2024-08-01T00:00:00+00:00');
$end = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '2024-08-21T00:00:00+00:00');
$calculateDuration = new CalculateDuration($start, $end);
$result = $calculateDuration->result();
$this->assertSame(21, $result->days);
$this->assertSame(2, $result->weeks);
$this->assertSame(15, $result->weekDays);
}
public function testCanCalculateDurationBetweenTwoDatesInDifferentTimezones(): void
{
$start = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '2024-08-01T00:00:00-12:00');
$end = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '2024-08-21T00:00:00+14:00');
$calculateDuration = new CalculateDuration($start, $end);
$result = $calculateDuration->result();
$this->assertSame(20, $result->days);
$this->assertSame(2, $result->weeks);
$this->assertSame(14, $result->weekDays);
}
public function testCanCalculateDurationBetweenAndDisplayAsSeconds(): void
{
$start = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '2024-08-01T00:00:00+00:00');
$end = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '2024-08-8T00:00:00+00:00');
$calculateDuration = new CalculateDuration($start, $end);
$result = $calculateDuration->result(DurationModifier::Second);
// 8 * 60 * 60 * 24
$this->assertSame(691200, $result->days);
// 5 * 60 * 60 * 24
$this->assertSame(518400, $result->weekDays);
// 1 * 7 * 60 * 60 * 24
$this->assertSame(604800, $result->weeks);
}
public function testCanCalculateDurationBetweenAndDisplayAsMinutes(): void
{
$start = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '2024-08-01T00:00:00+00:00');
$end = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '2024-08-8T00:00:00+00:00');
$calculateDuration = new CalculateDuration($start, $end);
$result = $calculateDuration->result(DurationModifier::Minute);
// 8 * 60 * 24
$this->assertSame(11520, $result->days);
// 5 * 60 * 24
$this->assertSame(8640, $result->weekDays);
// 1 * 7 * 60 * 24
$this->assertSame(10080, $result->weeks);
}
public function testCanCalculateDurationBetweenAndDisplayAsHours(): void
{
$start = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '2024-08-01T00:00:00+00:00');
$end = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '2024-08-8T00:00:00+00:00');
$calculateDuration = new CalculateDuration($start, $end);
$result = $calculateDuration->result(DurationModifier::Hour);
// 8 * 24
$this->assertSame(192, $result->days);
// 5 * 24
$this->assertSame(144, $result->weekDays);
// 1 * 7 * 24
$this->assertSame(168, $result->weeks);
}
public function testCanCalculateDurationBetweenAndDisplayAsYears(): void
{
$start = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '2024-08-01T00:00:00+00:00');
$end = DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '2024-08-8T00:00:00+00:00');
$calculateDuration = new CalculateDuration($start, $end);
$result = $calculateDuration->result(DurationModifier::Year);
// 8 * 0.00273973
$this->assertSame(0.02192, $result->days);
// 5 * 0.00273973
$this->assertSame(0.01644, $result->weekDays);
// 1 * 7 * 0.00273973
$this->assertSame(0.01918, $result->weeks);
}
}