Commit 8a27bdd7 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Sort status response events by begin date. I'm bad committing straight to master...

parent 0a8918fa
......@@ -27,7 +27,7 @@ class LocalSchedulesRepository extends InMemorySchedulesRepository
LocalGroupsRepository::getComputerScienceMajorGroup()->getId(),
Schedule::TYPE_OPEN_MODE,
// Open mode for 10 mintes, not open mode for 10 minutes
'RRULE:FREQ=MINUTELY;INTERVAL=20',
'RRULE:FREQ=SECONDLY;INTERVAL=20',
1000 * 60 * 10,
'Computer science lab open mode schedule, every 20 minutes for 10 minutes',
Carbon::now()
......
......@@ -11,12 +11,21 @@ class JsonPresenter extends BasePresenter implements Presenter
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$events = $responseModel->getEvents();
// Sort them by start date
usort($events, static function (OpenEvent $a, OpenEvent $b) {
return $a->cmp($b);
});
// No more than 256
$this->viewModel['events'] = array_map(function (OpenEvent $event) {
return [
'begins_at' => $this->formatDateTime($event->getBegin()),
'ends_at' => $this->formatDateTime($event->getEnd()),
];
}, $responseModel->getEvents());
}, $events);
}
/** @inheritDoc */
......
......@@ -42,18 +42,11 @@ class OpenEvent
}
/**
* @param \Carbon\Carbon $begin
* @param \Source\UseCases\Door\StatusResponse\OpenEvent $other
* @return int
*/
public function setBegin(Carbon $begin): void
public function cmp(OpenEvent $other): int
{
$this->begin = $begin;
}
/**
* @param \Carbon\Carbon $end
*/
public function setEnd(Carbon $end): void
{
$this->end = $end;
return $other->getBegin()->diffInRealMilliseconds($this->getBegin(), false);
}
}
<?php
namespace Tests\Unit\Source\UseCases\Door\StatusResponse;
use Carbon\Carbon;
use PHPUnit\Framework\TestCase;
use Source\UseCases\Door\StatusResponse\OpenEvent;
use Source\UseCases\Door\StatusResponse\JsonPresenter;
use Source\UseCases\Door\StatusResponse\ResponseModel;
class StatusResponsePresenterTest extends TestCase
{
/**
* @var \Source\UseCases\Door\StatusResponse\ResponseModel
*/
protected ResponseModel $responseModel;
/**
* @var \Source\UseCases\Door\StatusResponse\JsonPresenter
*/
protected JsonPresenter $presenter;
/**
* @var array
*/
protected array $response;
public function setUp(): void
{
parent::setUp();
$this->responseModel = new ResponseModel();
$this->presenter = new JsonPresenter();
}
protected function handleTest(): void
{
$this->presenter->present($this->responseModel);
$this->response = $this->presenter->getViewModel();
}
/**
* @test
*/
public function it_presents_open_mode_events(): void
{
$date = new Carbon('2020-05-19 16:45:25');
$date->setTimezone('Asia/Ho_Chi_Minh');
$this->responseModel->addEvent(new OpenEvent($date->clone()->subDay(), $date));
$this->handleTest();
$events = $this->response['events'];
$this->assertCount(1, $events);
$this->assertCount(2, $events[0]);
$this->assertEquals('2020-05-18T23:45:25+07:00', $events[0]['begins_at']);
$this->assertEquals('2020-05-19T23:45:25+07:00', $events[0]['ends_at']);
}
/**
* @test
* @throws \Exception
*/
public function it_sorts_out_of_order_events(): void
{
$date = Carbon::now();
$this->responseModel->addEvent(new OpenEvent($date->clone()->subDay(), $date->clone()->subMinute()));
$this->responseModel->addEvent(new OpenEvent($date->clone()->addDay(), $date->clone()->addDays(2)));
$this->handleTest();
$events = $this->response['events'];
$this->assertCount(2, $events);
$first = new Carbon($events[0]['begins_at']);
$second = new Carbon($events[1]['begins_at']);
$this->assertTrue($first < $second);
}
/**
* @test
* @throws \Exception
*/
public function it_sorts_in_order_events(): void
{
$date = Carbon::now();
$this->responseModel->addEvent(new OpenEvent($date->clone()->addDay(), $date->clone()->addDays(2)));
$this->responseModel->addEvent(new OpenEvent($date->clone()->subDay(), $date->clone()->subMinute()));
$this->handleTest();
$events = $this->response['events'];
$this->assertCount(2, $events);
$first = new Carbon($events[0]['begins_at']);
$second = new Carbon($events[1]['begins_at']);
$this->assertTrue($first < $second);
}
}
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