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

Fix code styles and add database tests for overrides

parent eab91d85
......@@ -3,7 +3,6 @@
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
......
......@@ -17,8 +17,8 @@ use Source\UseCases\Doors\GetDoor\GetDoorUseCaseServiceProvider;
use Source\UseCases\Users\GetUser\GetUserUseCaseServiceProvider;
use Source\Gateways\DoorGroup\DoorGroupRepositoryServiceProvider;
use Source\Gateways\GroupUser\GroupUserRepositoryServiceProvider;
use Source\Gateways\Schedules\SchedulesRepositoryServiceProvider;
use Source\Gateways\Overrides\OverridesRepositoryServiceProvider;
use Source\Gateways\Schedules\SchedulesRepositoryServiceProvider;
use Source\UseCases\Groups\GetGroup\GetGroupUseCaseServiceProvider;
use Source\UseCases\Tokens\GetToken\GetTokenUseCaseServiceProvider;
use Source\UseCases\Doors\CreateDoor\CreateDoorUseCaseServiceProvider;
......
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateOverridesTable extends Migration
{
......
......@@ -3,7 +3,6 @@
namespace Source\Entities;
use Carbon\Carbon;
use InvalidArgumentException;
......
......@@ -6,6 +6,7 @@ namespace Source\Gateways\Overrides;
use Carbon\Carbon;
use Source\Sanitize\CastsTo;
use Source\Entities\Override;
use Illuminate\Database\Eloquent\Builder;
class DatabaseOverridesRepository implements OverridesRepository
{
......@@ -50,7 +51,11 @@ class DatabaseOverridesRepository implements OverridesRepository
{
$overrides = \App\Override::query()
->whereBetween('start', [$begin, $end])
->get()->values()->all();
->orWhereBetween('end', [$begin, $end])
->orWhere(static function (Builder $query) use ($begin, $end) {
$query->where('start', '<', $begin)
->where('end', '>', $end);
})->get()->values()->all();
return array_map(static function (\App\Override $override) {
return self::toOverride($override);
......@@ -89,6 +94,10 @@ class DatabaseOverridesRepository implements OverridesRepository
$o->setAttribute('start', $override->getStart());
$o->setAttribute('end', $override->getEnd());
if ($override->getCreatedAt()) {
$o->setCreatedAt($override->getCreatedAt());
}
if (!$o->save()) {
return null;
}
......
......@@ -5,8 +5,8 @@ namespace Source\Gateways\Overrides;
use Carbon\Carbon;
use Source\Entities\Override;
use Source\Gateways\Users\LocalUsersRepository;
use Source\Gateways\Doors\LocalDoorsRepository;
use Source\Gateways\Users\LocalUsersRepository;
class LocalOverridesRepository extends InMemoryOverridesRepository
{
......
......@@ -3,9 +3,9 @@
namespace Source\Gateways\Overrides;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
/**
* Service provider must be registered in AppServiceProvider
......@@ -24,7 +24,7 @@ class OverridesRepositoryServiceProvider extends ServiceProvider implements Defe
return new LocalOverridesRepository();
}
if(env('APP_ENV') === 'testing') {
if (env('APP_ENV') === 'testing') {
return new InMemoryOverridesRepository();
}
......
......@@ -49,6 +49,11 @@ class EntriesDatabaseTest extends DatabaseTestCase
return $this->doors->create(new Door(0, '', $name, new HashedSearchable($name)));
}
/**
* @param string $email
* @return \Source\Entities\User
* @throws \Source\Exceptions\EntityExistsException
*/
protected function createUser(string $email = ''): User
{
return $this->users->create(new User(
......
<?php
namespace Tests\Database;
use Carbon\Carbon;
use Source\Entities\Door;
use Source\Entities\User;
use Tests\DatabaseTestCase;
use Source\Entities\Override;
use Source\Entities\HashedSearchable;
use Source\Gateways\Doors\DatabaseDoorsRepository;
use Source\Gateways\Users\DatabaseUsersRepository;
use Source\Gateways\Overrides\DatabaseOverridesRepository;
class OverrideDatabaseTest extends DatabaseTestCase
{
/**
* @var \Source\Gateways\Overrides\DatabaseOverridesRepository
*/
protected DatabaseOverridesRepository $repository;
/**
* @var \Source\Gateways\Users\DatabaseUsersRepository
*/
protected DatabaseUsersRepository $users;
/**
* @var \Source\Gateways\Doors\DatabaseDoorsRepository
*/
protected DatabaseDoorsRepository $doors;
public function setUp(): void
{
parent::setUp();
$this->users = new DatabaseUsersRepository();
$this->doors = new DatabaseDoorsRepository();
$this->repository = new DatabaseOverridesRepository();
}
/**
* @param string $name
* @return \Source\Entities\Door
* @throws \Source\Exceptions\EntityExistsException
*/
protected function createDoor(string $name = ''): Door
{
return $this->doors->create(new Door(0, '', $name, new HashedSearchable($name)));
}
/**
* @param string $email
* @return \Source\Entities\User
* @throws \Source\Exceptions\EntityExistsException
*/
protected function createUser(string $email = ''): User
{
return $this->users->create(new User(
0,
'',
'',
'',
$email
));
}
/**
* @test
* @throws \Source\Exceptions\EntityExistsException
*/
public function it_can_create_and_find_overrides(): void
{
$d1 = $this->createDoor('d1');
$d2 = $this->createDoor('d2');
$u1 = $this->createUser('u1');
$this->repository->addOverride(new Override(
0,
$u1->getId(),
$d1->getId(),
Override::TYPE_LOCKED,
Carbon::now()->subHours(8),
Carbon::now()->subHours(5)
));
$this->repository->addOverride(new Override(
0,
$u1->getId(),
$d2->getId(),
Override::TYPE_LOCKED,
Carbon::now(),
Carbon::now()->addHour()
));
$this->assertCount(1, $this->repository->overrideHistoryForDoor($d1->getId()));
$this->assertCount(1, $this->repository->overrideHistoryBetween(Carbon::now()->addMinute(), Carbon::now()->addMinutes(2)));
}
/**
* @test
* @throws \Source\Exceptions\EntityExistsException
*/
public function it_gets_active_override_for_door(): void
{
$d1 = $this->createDoor('d1');
$u1 = $this->createUser('u1');
$this->repository->addOverride(new Override(
0,
$u1->getId(),
$d1->getId(),
Override::TYPE_LOCKED,
Carbon::now()->addMinutes(8),
Carbon::now()->addHours(2)
));
$this->repository->addOverride(new Override(
0,
$u1->getId(),
$d1->getId(),
Override::TYPE_LOCKED,
Carbon::now()->addMinute(),
Carbon::now()->addHour(),
Carbon::now()->subMinute()
));
$this->assertNull($this->repository->activeOverrideForDoor($d1->getId(), Carbon::now()));
$this->assertEquals(Override::TYPE_LOCKED, $this->repository->activeOverrideForDoor($d1->getId(), Carbon::now()->addMinutes(10))->getType());
}
}
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