Skip to content

Commit

Permalink
Porting getMinutesOn() and getHoursOn() to the model itself (rennokki#10
Browse files Browse the repository at this point in the history
)

* Ported getHoursOn() and getMinutesOn() directly into the schedule model.

* Updated the tests.

* Apply fixes from StyleCI (rennokki#11)
  • Loading branch information
rennokki authored Sep 7, 2018
1 parent 1262da1 commit 6c6becd
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Models/ScheduleModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@ public function model()
{
return $this->morphTo();
}

public function getHoursOn($dateOrDay): int
{
return $this->model()->first()->getHoursOn($dateOrDay);
}

public function getMinutesOn($dateOrDay): int
{
return $this->model()->first()->getMinutesOn($dateOrDay);
}
}
58 changes: 58 additions & 0 deletions tests/ScheduleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,11 @@ public function testIsAvailableOnExclusion()
$this->assertEquals($this->user->getHoursOn('2018-05-31'), 12);
$this->assertEquals($this->user->getHoursOn('2018-06-01'), 3);
$this->assertEquals($this->user->getHoursOn('2018-06-02'), 0);

$this->assertEquals($this->user->schedule()->first()->getHoursOn('2018-05-28'), 0);
$this->assertEquals($this->user->schedule()->first()->getHoursOn('2018-05-31'), 12);
$this->assertEquals($this->user->schedule()->first()->getHoursOn('2018-06-01'), 3);
$this->assertEquals($this->user->schedule()->first()->getHoursOn('2018-06-02'), 0);
}

public function testIsUnavailableOnExclusion()
Expand Down Expand Up @@ -400,6 +405,11 @@ public function testIsUnavailableOnExclusion()
$this->assertEquals($this->user->getHoursOn('2018-05-31'), 12);
$this->assertEquals($this->user->getHoursOn('2018-06-01'), 3);
$this->assertEquals($this->user->getHoursOn('2018-06-02'), 0);

$this->assertEquals($this->user->schedule()->first()->getHoursOn('2018-05-28'), 0);
$this->assertEquals($this->user->schedule()->first()->getHoursOn('2018-05-31'), 12);
$this->assertEquals($this->user->schedule()->first()->getHoursOn('2018-06-01'), 3);
$this->assertEquals($this->user->schedule()->first()->getHoursOn('2018-06-02'), 0);
}

public function testIsAvailableOnAtExclusion()
Expand Down Expand Up @@ -456,6 +466,14 @@ public function testGetHoursOn()
$this->assertEquals($this->user->getHoursOn('saturday'), 0);
$this->assertEquals($this->user->getHoursOn('sunday'), 0);

$this->assertEquals($this->user->schedule()->first()->getHoursOn('monday'), 4);
$this->assertEquals($this->user->schedule()->first()->getHoursOn('tuesday'), 8);
$this->assertEquals($this->user->schedule()->first()->getHoursOn('wednesday'), 1);
$this->assertEquals($this->user->schedule()->first()->getHoursOn('thursday'), 0);
$this->assertEquals($this->user->schedule()->first()->getHoursOn('friday'), 0);
$this->assertEquals($this->user->schedule()->first()->getHoursOn('saturday'), 0);
$this->assertEquals($this->user->schedule()->first()->getHoursOn('sunday'), 0);

$this->assertEquals($this->user->getHoursOn('2018-05-28'), 4);
$this->assertEquals($this->user->getHoursOn('2018-05-29'), 8);
$this->assertEquals($this->user->getHoursOn('2018-05-30'), 1);
Expand All @@ -464,13 +482,29 @@ public function testGetHoursOn()
$this->assertEquals($this->user->getHoursOn('2018-06-02'), 0);
$this->assertEquals($this->user->getHoursOn('2018-06-03'), 0);

$this->assertEquals($this->user->schedule()->first()->getHoursOn('2018-05-28'), 4);
$this->assertEquals($this->user->schedule()->first()->getHoursOn('2018-05-29'), 8);
$this->assertEquals($this->user->schedule()->first()->getHoursOn('2018-05-30'), 1);
$this->assertEquals($this->user->schedule()->first()->getHoursOn('2018-05-31'), 0);
$this->assertEquals($this->user->schedule()->first()->getHoursOn('2018-06-01'), 0);
$this->assertEquals($this->user->schedule()->first()->getHoursOn('2018-06-02'), 0);
$this->assertEquals($this->user->schedule()->first()->getHoursOn('2018-06-03'), 0);

$this->assertEquals($this->user->getHoursOn(Carbon::create(2018, 5, 28, 0, 0, 0)), 4);
$this->assertEquals($this->user->getHoursOn(Carbon::create(2018, 5, 29, 0, 0, 0)), 8);
$this->assertEquals($this->user->getHoursOn(Carbon::create(2018, 5, 30, 0, 0, 0)), 1);
$this->assertEquals($this->user->getHoursOn(Carbon::create(2018, 5, 31, 0, 0, 0)), 0);
$this->assertEquals($this->user->getHoursOn(Carbon::create(2018, 6, 1, 0, 0, 0)), 0);
$this->assertEquals($this->user->getHoursOn(Carbon::create(2018, 6, 2, 0, 0, 0)), 0);
$this->assertEquals($this->user->getHoursOn(Carbon::create(2018, 6, 3, 0, 0, 0)), 0);

$this->assertEquals($this->user->schedule()->first()->getHoursOn(Carbon::create(2018, 5, 28, 0, 0, 0)), 4);
$this->assertEquals($this->user->schedule()->first()->getHoursOn(Carbon::create(2018, 5, 29, 0, 0, 0)), 8);
$this->assertEquals($this->user->schedule()->first()->getHoursOn(Carbon::create(2018, 5, 30, 0, 0, 0)), 1);
$this->assertEquals($this->user->schedule()->first()->getHoursOn(Carbon::create(2018, 5, 31, 0, 0, 0)), 0);
$this->assertEquals($this->user->schedule()->first()->getHoursOn(Carbon::create(2018, 6, 1, 0, 0, 0)), 0);
$this->assertEquals($this->user->schedule()->first()->getHoursOn(Carbon::create(2018, 6, 2, 0, 0, 0)), 0);
$this->assertEquals($this->user->schedule()->first()->getHoursOn(Carbon::create(2018, 6, 3, 0, 0, 0)), 0);
}

public function testGetMinutesOn()
Expand All @@ -495,6 +529,14 @@ public function testGetMinutesOn()
$this->assertEquals($this->user->getMinutesOn('saturday'), 0);
$this->assertEquals($this->user->getMinutesOn('sunday'), 0);

$this->assertEquals($this->user->schedule()->first()->getMinutesOn('monday'), 4 * 60);
$this->assertEquals($this->user->schedule()->first()->getMinutesOn('tuesday'), 8 * 60);
$this->assertEquals($this->user->schedule()->first()->getMinutesOn('wednesday'), 1 * 60);
$this->assertEquals($this->user->schedule()->first()->getMinutesOn('thursday'), 0);
$this->assertEquals($this->user->schedule()->first()->getMinutesOn('friday'), 0);
$this->assertEquals($this->user->schedule()->first()->getMinutesOn('saturday'), 0);
$this->assertEquals($this->user->schedule()->first()->getMinutesOn('sunday'), 0);

$this->assertEquals($this->user->getMinutesOn('2018-05-28'), 0);
$this->assertEquals($this->user->getMinutesOn('2018-05-29'), 8 * 60);
$this->assertEquals($this->user->getMinutesOn('2018-05-30'), 1 * 60);
Expand All @@ -503,13 +545,29 @@ public function testGetMinutesOn()
$this->assertEquals($this->user->getMinutesOn('2018-06-02'), 0);
$this->assertEquals($this->user->getMinutesOn('2018-06-03'), 0);

$this->assertEquals($this->user->schedule()->first()->getMinutesOn('2018-05-28'), 0);
$this->assertEquals($this->user->schedule()->first()->getMinutesOn('2018-05-29'), 8 * 60);
$this->assertEquals($this->user->schedule()->first()->getMinutesOn('2018-05-30'), 1 * 60);
$this->assertEquals($this->user->schedule()->first()->getMinutesOn('2018-05-31'), 12 * 60);
$this->assertEquals($this->user->schedule()->first()->getMinutesOn('2018-06-01'), 3 * 60);
$this->assertEquals($this->user->schedule()->first()->getMinutesOn('2018-06-02'), 0);
$this->assertEquals($this->user->schedule()->first()->getMinutesOn('2018-06-03'), 0);

$this->assertEquals($this->user->getMinutesOn(Carbon::create(2018, 5, 28, 0, 0, 0)), 0);
$this->assertEquals($this->user->getMinutesOn(Carbon::create(2018, 5, 29, 0, 0, 0)), 8 * 60);
$this->assertEquals($this->user->getMinutesOn(Carbon::create(2018, 5, 30, 0, 0, 0)), 1 * 60);
$this->assertEquals($this->user->getMinutesOn(Carbon::create(2018, 5, 31, 0, 0, 0)), 12 * 60);
$this->assertEquals($this->user->getMinutesOn(Carbon::create(2018, 6, 1, 0, 0, 0)), 3 * 60);
$this->assertEquals($this->user->getMinutesOn(Carbon::create(2018, 6, 2, 0, 0, 0)), 0);
$this->assertEquals($this->user->getMinutesOn(Carbon::create(2018, 6, 3, 0, 0, 0)), 0);

$this->assertEquals($this->user->schedule()->first()->getMinutesOn(Carbon::create(2018, 5, 28, 0, 0, 0)), 0);
$this->assertEquals($this->user->schedule()->first()->getMinutesOn(Carbon::create(2018, 5, 29, 0, 0, 0)), 8 * 60);
$this->assertEquals($this->user->schedule()->first()->getMinutesOn(Carbon::create(2018, 5, 30, 0, 0, 0)), 1 * 60);
$this->assertEquals($this->user->schedule()->first()->getMinutesOn(Carbon::create(2018, 5, 31, 0, 0, 0)), 12 * 60);
$this->assertEquals($this->user->schedule()->first()->getMinutesOn(Carbon::create(2018, 6, 1, 0, 0, 0)), 3 * 60);
$this->assertEquals($this->user->schedule()->first()->getMinutesOn(Carbon::create(2018, 6, 2, 0, 0, 0)), 0);
$this->assertEquals($this->user->schedule()->first()->getMinutesOn(Carbon::create(2018, 6, 3, 0, 0, 0)), 0);
}

public function testDeleteExclusions()
Expand Down

0 comments on commit 6c6becd

Please sign in to comment.