Commit d32f7c4b authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

add tests for status response

parent d02c0188
......@@ -39,8 +39,11 @@ class InMemoryOverridesRepository implements OverridesRepository
public function activeOverrideForDoorBetween(string $doorId, Carbon $begin, Carbon $end): ?Override
{
$overrides = array_filter($this->overrides, static function (Override $override) use ($doorId, $begin, $end) {
return $override->hasDoorIdOf($doorId) && ($override->isActiveForDate($begin) || $override->isActiveForDate($end));
return $override->hasDoorIdOf($doorId) &&
($override->isActiveForDate($begin) || $override->isActiveForDate($end) ||
($begin->isBefore($override->getStart()) && $end->isAfter($override->getEnd())));
});
return array_shift($overrides);
}
......
......@@ -83,7 +83,6 @@ class StatusResponse implements StatusResponseUseCase
if ($override && $override->hasTypeOf(Override::TYPE_OPEN)) {
$response->addEvent(new OpenEvent($override->getStart(), $override->getEnd()));
$override = null;
}
foreach ($openSchedules as $schedule) {
......@@ -94,8 +93,9 @@ class StatusResponse implements StatusResponseUseCase
}
foreach ($this->rset->occurrencesBetween($begin, $end) as $eventStart) {
if ($override) {
$eventEnd = $eventStart->clone()->addRealMilliseconds($schedule->getDuration());
$eventEnd = $eventStart->clone()->addRealMilliseconds($schedule->getDuration());
$override = $this->overrides->activeOverrideForDoorBetween($doorId, $eventStart, $eventEnd);
if ($override && $override->hasTypeOf(Override::TYPE_LOCKED)) {
// If the beginning of the schedule overlaps with the override
$overlapBegin = $override->isActiveForDate($eventStart);
// If the end of the schedule overlaps with the override
......@@ -105,10 +105,12 @@ class StatusResponse implements StatusResponseUseCase
// Override is enclosed by the event, break up the open event
$response->addEvent(new OpenEvent($eventStart, $override->getStart()));
$response->addEvent(new OpenEvent($override->getEnd(), $eventEnd));
} else {
// override does not overlap with event add whole event range
$response->addEvent(new OpenEvent($eventStart, $eventEnd));
continue;
}
// override does not overlap with event add whole event range
$response->addEvent(new OpenEvent($eventStart, $eventEnd));
continue;
}
if ($overlapBegin && $overlapEnd) {
......@@ -117,13 +119,13 @@ class StatusResponse implements StatusResponseUseCase
}
if ($overlapBegin) {
$response->addEvent(new OpenEvent($override->getEnd(), $schedule->getEnd()));
} else {
$response->addEvent(new OpenEvent($schedule->getStart(), $override->getStart()));
$response->addEvent(new OpenEvent($override->getEnd(), $eventEnd));
continue;
}
$response->addEvent(new OpenEvent($eventStart, $override->getStart()));
} else {
// No override to worry about, add the open event.
$eventEnd = $eventStart->clone()->addRealMilliseconds($schedule->getDuration());
$response->addEvent(new OpenEvent($eventStart, $eventEnd));
}
}
......
......@@ -8,6 +8,7 @@ use Carbon\Carbon;
use Source\Entities\Override;
use Source\Entities\Schedule;
use PHPUnit\Framework\TestCase;
use Tests\Doubles\RecurrenceSetStub;
use Source\UseCases\Door\StatusResponse\ResponseModel;
use Source\UseCases\Door\StatusResponse\StatusResponse;
use Source\Gateways\Overrides\InMemoryOverridesRepository;
......@@ -40,13 +41,19 @@ class StatusResponseTest extends TestCase
*/
protected ResponseModel $response;
/**
* @var \Tests\Doubles\RecurrenceSetStub
*/
protected RecurrenceSetStub $rset;
public function setUp(): void
{
parent::setUp();
$this->doorSchedules = new InMemoryDoorScheduleRepository();
$this->overrides = new InMemoryOverridesRepository();
$this->useCase = new StatusResponse($this->doorSchedules, $this->overrides);
$this->rset = new RecurrenceSetStub();
$this->useCase = new StatusResponse($this->doorSchedules, $this->overrides, $this->rset);
$this->presenter = new PresenterStub();
}
......@@ -65,6 +72,7 @@ class StatusResponseTest extends TestCase
$this->handleTest('1', Carbon::now(), Carbon::now()->addDay());
$this->assertEquals([], $this->response->getEvents());
$this->assertTrue($this->presenter->wasPresenterCalled());
}
/**
......@@ -93,12 +101,210 @@ class StatusResponseTest extends TestCase
*/
public function it_adds_schedules_without_overrides(): void
{
$this->rset->occurrences = [
Carbon::now()
];
$this->doorSchedules->attachScheduleToDoor('1', new Schedule(
1,
1,
Schedule::TYPE_OPEN_MODE,
'',
1234,
'desc',
Carbon::now()->addSeconds(10)
));
$this->handleTest('1', Carbon::now(), Carbon::now()->addMinutes(10));
$this->assertEquals($this->rset->occurrences[0]->clone()->addRealMilliseconds(1234), $this->response->getEvents()[0]->getEnd());
}
/**
* @test
*/
public function it_chops_a_schedule_in_two_when_override_is_in_the_middle(): void
{
$this->rset->occurrences = [
Carbon::now()
];
$o = $this->overrides->addOverride(new Override(
1,
'',
1,
1,
Override::TYPE_LOCKED,
Carbon::now()->addMinute(),
Carbon::now()->addMinutes(10)
));
$this->doorSchedules->attachScheduleToDoor('1', new Schedule(
1,
1,
Schedule::TYPE_OPEN_MODE,
'',
// 20 minutes
1000 * 60 * 20,
'desc',
Carbon::now()->addSeconds(10)
));
$this->handleTest('1', Carbon::now(), Carbon::now()->addMinutes(20));
$this->assertCount(2, $this->response->getEvents());
$this->assertEquals($this->rset->occurrences[0], $this->response->getEvents()[0]->getBegin());
$this->assertEquals($o->getStart(), $this->response->getEvents()[0]->getEnd());
$this->assertEquals($o->getEnd(), $this->response->getEvents()[1]->getBegin());
$this->assertEquals($this->rset->occurrences[0]->addRealMilliseconds(1000 * 60 * 20), $this->response->getEvents()[1]->getEnd());
}
/**
* @test
*/
public function it_gets_overrides_outside_of_date(): void
{
$this->rset->occurrences = [
Carbon::now()
];
$this->overrides->addOverride(new Override(
1,
'',
1,
1,
Override::TYPE_LOCKED,
Carbon::now()->addMinute(),
Carbon::now()->addMinutes(10)
));
$this->doorSchedules->attachScheduleToDoor('1', new Schedule(
1,
1,
Schedule::TYPE_OPEN_MODE,
'',
''
// 20 minutes
1000 * 60 * 20,
'desc',
Carbon::now()->addSeconds(10)
));
$this->handleTest('1', Carbon::now(), Carbon::now()->addSeconds(20));
// It being split means it sees the override
$this->assertCount(2, $this->response->getEvents());
}
/**
* @test
*/
public function it_only_splits_on_locked_override(): void
{
$this->rset->occurrences = [
Carbon::now()
];
$this->overrides->addOverride(new Override(
1,
'',
1,
1,
Override::TYPE_OPEN,
Carbon::now()->addMinute(),
Carbon::now()->addMinutes(10)
));
$this->doorSchedules->attachScheduleToDoor('1', new Schedule(
1,
1,
Schedule::TYPE_OPEN_MODE,
'',
// 20 minutes
1000 * 60 * 20,
'desc',
Carbon::now()->addSeconds(10)
));
$this->handleTest('1', Carbon::now(), Carbon::now()->addSeconds(20));
// If it didn't split, it did not see the override
$this->assertCount(1, $this->response->getEvents());
}
/**
* @test
*/
public function it_cuts_the_end(): void
{
$start = Carbon::now();
$this->rset->occurrences = [
$start
];
$o = $this->overrides->addOverride(new Override(
1,
'',
1,
1,
Override::TYPE_LOCKED,
Carbon::now()->addMinute(),
Carbon::now()->addMinutes(10)
));
$this->doorSchedules->attachScheduleToDoor('1', new Schedule(
1,
1,
Schedule::TYPE_OPEN_MODE,
'',
// 10 minutes
1000 * 60 * 10,
'desc',
Carbon::now()->addSeconds(10)
));
$this->handleTest('1', Carbon::now(), Carbon::now()->addMinutes(20));
$this->assertCount(1, $this->response->getEvents());
$event = $this->response->getEvents()[0];
$this->assertEquals($start, $event->getBegin());
$this->assertEquals($o->getStart(), $event->getEnd());
}
/**
* @test
*/
public function it_cuts_the_beginning(): void
{
$start = Carbon::now();
$this->rset->occurrences = [
$start
];
$o = $this->overrides->addOverride(new Override(
1,
'',
1,
1,
Override::TYPE_LOCKED,
Carbon::now()->subMinute(),
Carbon::now()->addMinute()
));
$this->doorSchedules->attachScheduleToDoor('1', new Schedule(
1,
1,
Schedule::TYPE_OPEN_MODE,
'',
// 10 minutes
1000 * 60 * 10,
'desc',
Carbon::now()->addSeconds(10)
));
$this->handleTest('1', Carbon::now(), Carbon::now()->addMinutes(20));
$this->assertCount(1, $this->response->getEvents());
$event = $this->response->getEvents()[0];
$this->assertEquals($o->getEnd(), $event->getBegin());
$this->assertEquals($start->addRealMilliseconds(1000 * 60 * 10), $event->getEnd());
}
}
Supports Markdown
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