Commit 9ca4a709 authored by Jacob Priddy's avatar Jacob Priddy 👌

add tests for date split and merging

parent 5304af80
Pipeline #13196 canceled with stages
in 38 seconds
<?php
namespace Tests\Unit\Source\Entities;
use Carbon\Carbon;
use PHPUnit\Framework\TestCase;
use Source\Entities\SplittableDate;
class SplittableDateTest extends TestCase
{
/** @test */
public function it_tests_engulfed_ranges(): void
{
$d1 = new SplittableDate(Carbon::now()->subMinute(), Carbon::now()->addHour());
$d2 = new SplittableDate(Carbon::now(), Carbon::now()->addMinute());
$this->assertTrue($d1->engulfs($d2));
$this->assertFalse($d2->engulfs($d1));
}
/** @test */
public function it_tests_engulfed_ranges_when_equal(): void
{
$s = Carbon::now();
$e = $s->clone()->addMinute();
$d1 = new SplittableDate($s, $e);
$d2 = new SplittableDate($s, $e);
$this->assertTrue($d1->engulfs($d2));
$this->assertTrue($d2->engulfs($d1));
}
/** @test */
public function it_tests_overlap_detection(): void
{
$d1 = new SplittableDate(Carbon::now()->addDay(), Carbon::now()->addYear());
$d2 = new SplittableDate(Carbon::now(), Carbon::now()->addHour());
$this->assertTrue($d1->hasNoOverlapWith($d2));
$this->assertTrue($d2->hasNoOverlapWith($d1));
$d1 = new SplittableDate(Carbon::now()->subDay(), Carbon::now()->addYear());
$d2 = new SplittableDate(Carbon::now(), Carbon::now()->addHour());
$this->assertFalse($d1->hasNoOverlapWith($d2));
$this->assertFalse($d2->hasNoOverlapWith($d1));
$d1 = new SplittableDate(Carbon::now(), Carbon::now()->addMinute());
$d2 = new SplittableDate(Carbon::now()->subMinute(), Carbon::now());
$this->assertFalse($d1->hasNoOverlapWith($d2));
$this->assertFalse($d2->hasNoOverlapWith($d1));
}
/** @test */
public function it_tests_date_set_merging(): void
{
// |--- d1 ---|
// |---d2-------------|
// |---d3---|
// |-----d4---|
// |---d5---|
// |---d6---|
$d1 = new SplittableDate(Carbon::now()->subDay(), Carbon::now()->subHours(20));
$d2 = new SplittableDate(Carbon::now(), Carbon::now()->addHour());
$sameStart = Carbon::now()->addMinute();
$d3 = new SplittableDate($sameStart, Carbon::now()->addMinutes(10));
$d4 = new SplittableDate($sameStart, Carbon::now()->addMinutes(20));
$d5 = new SplittableDate(Carbon::now()->addYear(), Carbon::now()->addYears(3));
$d6 = new SplittableDate(Carbon::now()->addYears(2), Carbon::now()->addYears(10));
// Test null case
$this->assertEquals([], SplittableDate::mergeSet([]));
// Test case with just 1 in it
$result = SplittableDate::mergeSet([$d1]);
$this->assertCount(1, $result);
$this->assertEquals($d1, $result[0]);
$result = SplittableDate::mergeSet([$d1, $d2, $d3, $d4, $d5, $d6]);
$this->assertCount(3, $result);
$this->assertEquals($d1, $result[0]);
$this->assertEquals($d2, $result[1]);
$this->assertEquals($d5->getBegin(), $result[2]->getBegin());
$this->assertEquals($d6->getEnd(), $result[2]->getEnd());
}
/** @test */
public function it_tests_date_block_splitting(): void
{
$toSplit = new SplittableDate(Carbon::now()->subHour(), Carbon::now()->addHour());
$leftNoOverlap = new SplittableDate(Carbon::now()->subWeek(), Carbon::now()->subDay());
$leftOverlap = new SplittableDate(Carbon::now()->subHours(2), Carbon::now()->subMinutes(50));
$midBlockLarge = new SplittableDate(Carbon::now()->subMinutes(30), Carbon::now()->addMinutes(2));
$midBlockSmall = new SplittableDate(Carbon::now()->subMinute(), Carbon::now()->addMinute());
$rightMid = new SplittableDate(Carbon::now()->addMinutes(30), Carbon::now()->addMinutes(40));
$rightOverlap = new SplittableDate(Carbon::now()->addMinutes(50), Carbon::now()->addHours(2));
$rightNoOverlap = new SplittableDate(Carbon::now()->addDay(), Carbon::now()->addWeek());
$splits = $toSplit->split([
$leftNoOverlap, $leftOverlap, $midBlockLarge, $midBlockSmall, $rightMid, $rightOverlap, $rightNoOverlap,
]);
$this->assertCount(3, $splits);
$this->assertEquals($leftOverlap->getEnd(), $splits[0]->getBegin());
$this->assertEquals($midBlockLarge->getBegin(), $splits[0]->getEnd());
$this->assertEquals($midBlockLarge->getEnd(), $splits[1]->getBegin());
$this->assertEquals($rightMid->getBegin(), $splits[1]->getEnd());
$this->assertEquals($rightMid->getEnd(), $splits[2]->getBegin());
$this->assertEquals($rightOverlap->getBegin(), $splits[2]->getEnd());
}
/** @test */
public function test_split_with_engulfs(): void
{
$toSplit = new SplittableDate(Carbon::now()->subMinute(), Carbon::now()->addMinute());
$this->assertEquals([], $toSplit->split([$toSplit]));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment