Commit 082d4234 authored by Jacob Priddy's avatar Jacob Priddy 👌

add tests for new override architecture

parent b9445689
Pipeline #13198 failed with stages
in 1 minute and 12 seconds
......@@ -108,23 +108,18 @@ class SplittableDate
$prev = array_shift($set);
$noMerge = true;
foreach ($set as $item) {
if ($item->getBegin() <= $prev->getEnd()) {
if ($prev->getEnd() < $item->getEnd()) {
$prev->setEnd($item->getEnd());
}
$noMerge = true;
} else {
$merged[] = $prev;
$noMerge = false;
$prev = $item;
}
}
if ($noMerge) {
$merged[] = $prev;
}
$merged[] = $prev;
return $merged;
}
......
......@@ -18,7 +18,7 @@ interface DoorScheduleRepository
* @param string $doorId
* @param \Carbon\Carbon $begin
* @param \Carbon\Carbon $end
* @param int|null $type
* @param int $type
* @return \Source\Entities\Schedule[]
*/
public function getSchedulesForDoorBetween(string $doorId, Carbon $begin, Carbon $end, int $type = Schedule::TYPE_OPEN_MODE): array;
......
......@@ -10,7 +10,7 @@ interface AccessUseCase
{
/**
* @param \Source\Entities\Door|null $door
* @param string|null $doorcode
* @param string $doorcode
* @param \Carbon\Carbon $date
* @throws \Source\Exceptions\AuthenticationException
* @throws \Source\Exceptions\AuthorizationException
......
......@@ -126,4 +126,16 @@ class SplittableDateTest extends TestCase
$toSplit = new SplittableDate(Carbon::now()->subMinute(), Carbon::now()->addMinute());
$this->assertEquals([], $toSplit->split([$toSplit]));
}
/** @test */
public function test_merge_end_on_no_merge(): void
{
$d1 = new SplittableDate(Carbon::now()->addMinute(), Carbon::now()->addMinutes(3));
$d2 = new SplittableDate(Carbon::now()->addMinutes(5), Carbon::now()->addMinutes(10));
$result = SplittableDate::mergeSet([$d1, $d2]);
$this->assertCount(2, $result);
$this->assertEquals($d1, $result[0]);
$this->assertEquals($d2, $result[1]);
}
}
......@@ -7,6 +7,7 @@ use Carbon\Carbon;
use Source\Entities\Override;
use Source\Entities\Schedule;
use PHPUnit\Framework\TestCase;
use Source\Entities\SplittableDate;
use Tests\Doubles\RecurrenceSetStub;
use Source\UseCases\Door\ScheduleEvents\ResponseModel;
use Source\UseCases\Door\ScheduleEvents\ScheduleEvents;
......@@ -81,7 +82,7 @@ class ScheduleEventsTest extends TestCase
*/
public function it_adds_override_events(): void
{
$override = $this->overrides->addOverride(new Override(
$o1 = $this->overrides->addOverride(new Override(
1,
'ree',
1,
......@@ -91,10 +92,21 @@ class ScheduleEventsTest extends TestCase
Carbon::now()->addMinute()
));
$this->handleTest('1', Carbon::now(), Carbon::now()->addSeconds(30));
$o2 = $this->overrides->addOverride(new Override(
1,
'ree',
1,
1,
Override::TYPE_OPEN,
Carbon::now()->addseconds(40),
Carbon::now()->addMinutes(10)
));
$this->handleTest('1', Carbon::now(), Carbon::now()->addSeconds(50));
self::assertCount(1, $this->response->getOpenEvents());
self::assertEquals($override->getBegin(), $this->response->getOpenEvents()[0]->getBegin());
self::assertEquals($o1->getBegin(), $this->response->getOpenEvents()[0]->getBegin());
self::assertEquals($o2->getEnd(), $this->response->getOpenEvents()[0]->getEnd());
}
/**
......@@ -162,6 +174,67 @@ class ScheduleEventsTest extends TestCase
self::assertEquals($this->rset->occurrences[0]->addRealSeconds(60 * 20), $this->response->getOpenEvents()[1]->getEnd());
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_chops_a_schedule_with_multiple_overrides(): void
{
$this->rset->occurrences = [
Carbon::now()
];
$o1 = $this->overrides->addOverride(new Override(
1,
'',
1,
1,
Override::TYPE_LOCKED,
Carbon::now()->addMinute(),
Carbon::now()->addMinutes(3)
));
$o2 = $this->overrides->addOverride(new Override(
1,
'',
1,
1,
Override::TYPE_LOCKED,
Carbon::now()->addMinutes(5),
Carbon::now()->addMinutes(10)
));
$this->overrides->addOverride(new Override(
1,
'',
1,
2,
Override::TYPE_LOCKED,
Carbon::now()->addMinutes(2),
Carbon::now()->addMinutes(6)
));
$this->doorSchedules->attachScheduleToDoor('1', new Schedule(
1,
Schedule::TYPE_OPEN_MODE,
'',
60 * 20,
'desc',
Carbon::now()->addSeconds(10)
));
$this->handleTest('1', Carbon::now()->subMinute(), Carbon::now()->addMinutes(30));
self::assertCount(3, $this->response->getOpenEvents());
self::assertEquals($this->rset->occurrences[0], $this->response->getOpenEvents()[0]->getBegin());
self::assertEquals($o1->getBegin(), $this->response->getOpenEvents()[0]->getEnd());
self::assertEquals($o1->getEnd(), $this->response->getOpenEvents()[1]->getBegin());
self::assertEquals($o2->getBegin(), $this->response->getOpenEvents()[1]->getEnd());
self::assertEquals($o2->getEnd(), $this->response->getOpenEvents()[2]->getBegin());
self::assertEquals($this->rset->occurrences[0]->addRealSeconds(60 * 20), $this->response->getOpenEvents()[2]->getEnd());
}
/**
* @test
*/
......
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