summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2015-03-14 20:53:33 -0400
committerFrederic Guillot <fred@kanboard.net>2015-03-14 20:53:33 -0400
commit253996901a10918e3207d46839cdfdc90d200e72 (patch)
tree5e359471d8e7de963277c37c6ff9611181505975 /tests
parent4700139a86d1ef44eabe43edb5a4abff9c645811 (diff)
Calculate the time spent based on the timetable
Diffstat (limited to 'tests')
-rw-r--r--tests/units/DateParserTest.php28
-rw-r--r--tests/units/SubtaskTimeTrackingTest.php25
-rw-r--r--tests/units/TimetableTest.php123
3 files changed, 162 insertions, 14 deletions
diff --git a/tests/units/DateParserTest.php b/tests/units/DateParserTest.php
index 5828fc48..9403063b 100644
--- a/tests/units/DateParserTest.php
+++ b/tests/units/DateParserTest.php
@@ -6,6 +6,34 @@ use Model\DateParser;
class DateParserTest extends Base
{
+ public function testDateRange()
+ {
+ $d = new DateParser($this->container);
+
+ $this->assertTrue($d->withinDateRange(new DateTime('2015-03-14 15:30:00'), new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 16:00:00')));
+ $this->assertFalse($d->withinDateRange(new DateTime('2015-03-14 15:30:00'), new DateTime('2015-03-14 16:00:00'), new DateTime('2015-03-14 17:00:00')));
+ }
+
+ public function testRoundSeconds()
+ {
+ $d = new DateParser($this->container);
+ $this->assertEquals('16:30', date('H:i', $d->getRoundedSeconds(strtotime('16:28'))));
+ $this->assertEquals('16:00', date('H:i', $d->getRoundedSeconds(strtotime('16:02'))));
+ $this->assertEquals('16:15', date('H:i', $d->getRoundedSeconds(strtotime('16:14'))));
+ $this->assertEquals('17:00', date('H:i', $d->getRoundedSeconds(strtotime('16:58'))));
+ }
+
+ public function testGetHours()
+ {
+ $d = new DateParser($this->container);
+
+ $this->assertEquals(1, $d->getHours(new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 16:00:00')));
+ $this->assertEquals(2.5, $d->getHours(new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 17:30:00')));
+ $this->assertEquals(2.75, $d->getHours(new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 17:45:00')));
+ $this->assertEquals(3, $d->getHours(new DateTime('2015-03-14 14:57:00'), new DateTime('2015-03-14 17:58:00')));
+ $this->assertEquals(3, $d->getHours(new DateTime('2015-03-14 14:57:00'), new DateTime('2015-03-14 11:58:00')));
+ }
+
public function testValidDate()
{
$d = new DateParser($this->container);
diff --git a/tests/units/SubtaskTimeTrackingTest.php b/tests/units/SubtaskTimeTrackingTest.php
index 90650e42..e15e60da 100644
--- a/tests/units/SubtaskTimeTrackingTest.php
+++ b/tests/units/SubtaskTimeTrackingTest.php
@@ -176,38 +176,35 @@ class SubtaskTimeTrackingTest extends Base
$this->assertEquals(7, $s->create(array('title' => 'subtask #7', 'task_id' => 2)));
$this->assertEquals(8, $s->create(array('title' => 'subtask #8', 'task_id' => 2)));
- // Create a couple of time slots
- $now = time();
-
// Slot start before and finish inside the calendar time range
- $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 1, 'start' => $now - 86400, 'end' => $now + 3600));
+ $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 1, 'start' => strtotime('-1 day'), 'end' => strtotime('+1 hour')));
// Slot start inside time range and finish after the time range
- $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 2, 'start' => $now + 3600, 'end' => $now + 2*86400));
+ $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 2, 'start' => strtotime('+1 hour'), 'end' => strtotime('+2 days')));
// Start before time range and finish inside time range
- $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 3, 'start' => $now - 86400, 'end' => $now + 1.5*86400));
+ $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 3, 'start' => strtotime('-1 day'), 'end' => strtotime('+1.5 days')));
// Start and finish inside time range
- $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 4, 'start' => $now + 3600, 'end' => $now + 2*3600));
+ $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 4, 'start' => strtotime('+1 hour'), 'end' => strtotime('+2 hours')));
// Start and finish after the time range
- $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 5, 'start' => $now + 2*86400, 'end' => $now + 3*86400));
+ $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 5, 'start' => strtotime('+2 days'), 'end' => strtotime('+3 days')));
// Start and finish before the time range
- $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 6, 'start' => $now - 2*86400, 'end' => $now - 86400));
+ $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 6, 'start' => strtotime('-2 days'), 'end' => strtotime('-1 day')));
// Start before time range and not finished
- $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 7, 'start' => $now - 86400));
+ $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 7, 'start' => strtotime('-1 day')));
// Start inside time range and not finish
- $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 8, 'start' => $now + 3200));
+ $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 8, 'start' => strtotime('+3200 seconds')));
$timesheet = $st->getUserTimesheet(1);
$this->assertNotEmpty($timesheet);
$this->assertCount(8, $timesheet);
- $events = $st->getUserCalendarEvents(1, date('Y-m-d', $now), date('Y-m-d', $now + 86400));
+ $events = $st->getUserCalendarEvents(1, date('Y-m-d'), date('Y-m-d', strtotime('+2 day')));
$this->assertNotEmpty($events);
$this->assertCount(6, $events);
$this->assertEquals(1, $events[0]['subtask_id']);
@@ -217,14 +214,14 @@ class SubtaskTimeTrackingTest extends Base
$this->assertEquals(7, $events[4]['subtask_id']);
$this->assertEquals(8, $events[5]['subtask_id']);
- $events = $st->getProjectCalendarEvents(1, date('Y-m-d', $now), date('Y-m-d', $now + 86400));
+ $events = $st->getProjectCalendarEvents(1, date('Y-m-d'), date('Y-m-d', strtotime('+2 days')));
$this->assertNotEmpty($events);
$this->assertCount(3, $events);
$this->assertEquals(1, $events[0]['subtask_id']);
$this->assertEquals(2, $events[1]['subtask_id']);
$this->assertEquals(3, $events[2]['subtask_id']);
- $events = $st->getProjectCalendarEvents(2, date('Y-m-d', $now), date('Y-m-d', $now + 86400));
+ $events = $st->getProjectCalendarEvents(2, date('Y-m-d'), date('Y-m-d', strtotime('+2 days')));
$this->assertNotEmpty($events);
$this->assertCount(3, $events);
$this->assertEquals(4, $events[0]['subtask_id']);
diff --git a/tests/units/TimetableTest.php b/tests/units/TimetableTest.php
index 4b4dcc83..c88371f9 100644
--- a/tests/units/TimetableTest.php
+++ b/tests/units/TimetableTest.php
@@ -127,4 +127,127 @@ class TimetableTest extends Base
$this->assertEquals($friday->format('Y-m-d').' 13:00', $timetable[4][0]->format('Y-m-d H:i'));
$this->assertEquals($friday->format('Y-m-d').' 17:00', $timetable[4][1]->format('Y-m-d H:i'));
}
+
+ public function testClosestTimeSlot()
+ {
+ $w = new TimetableWeek($this->container);
+ $t = new Timetable($this->container);
+
+ $this->assertNotFalse($w->create(1, 1, '09:30', '12:00'));
+ $this->assertNotFalse($w->create(1, 1, '13:00', '17:00'));
+ $this->assertNotFalse($w->create(1, 2, '09:30', '12:00'));
+ $this->assertNotFalse($w->create(1, 2, '13:00', '17:00'));
+
+ $monday = new DateTime('next Monday');
+ $tuesday = new DateTime('next Tuesday');
+
+ $timetable = $t->calculate(1, new DateTime('next Monday'), new DateTime('next Monday + 6 days'));
+ $this->assertNotEmpty($timetable);
+ $this->assertCount(4, $timetable);
+
+ // Start to work before timetable
+ $date = new DateTime('next Monday');
+ $date->setTime(5, 02);
+
+ $slot = $t->findClosestTimeSlot($date, $timetable);
+ $this->assertNotEmpty($slot);
+ $this->assertEquals($monday->format('Y-m-d').' 09:30', $slot[0]->format('Y-m-d H:i'));
+ $this->assertEquals($monday->format('Y-m-d').' 12:00', $slot[1]->format('Y-m-d H:i'));
+
+ // Start to work at the end of the timeslot
+ $date = new DateTime('next Monday');
+ $date->setTime(12, 02);
+
+ $slot = $t->findClosestTimeSlot($date, $timetable);
+ $this->assertNotEmpty($slot);
+ $this->assertEquals($monday->format('Y-m-d').' 09:30', $slot[0]->format('Y-m-d H:i'));
+ $this->assertEquals($monday->format('Y-m-d').' 12:00', $slot[1]->format('Y-m-d H:i'));
+
+ // Start to work at lunch time
+ $date = new DateTime('next Monday');
+ $date->setTime(12, 32);
+
+ $slot = $t->findClosestTimeSlot($date, $timetable);
+ $this->assertNotEmpty($slot);
+ $this->assertEquals($monday->format('Y-m-d').' 13:00', $slot[0]->format('Y-m-d H:i'));
+ $this->assertEquals($monday->format('Y-m-d').' 17:00', $slot[1]->format('Y-m-d H:i'));
+
+ // Start to work early in the morning
+ $date = new DateTime('next Tuesday');
+ $date->setTime(8, 02);
+
+ $slot = $t->findClosestTimeSlot($date, $timetable);
+ $this->assertNotEmpty($slot);
+ $this->assertEquals($tuesday->format('Y-m-d').' 09:30', $slot[0]->format('Y-m-d H:i'));
+ $this->assertEquals($tuesday->format('Y-m-d').' 12:00', $slot[1]->format('Y-m-d H:i'));
+ }
+
+ public function testCalculateDuration()
+ {
+ $w = new TimetableWeek($this->container);
+ $t = new Timetable($this->container);
+
+ $this->assertNotFalse($w->create(1, 1, '09:30', '12:00'));
+ $this->assertNotFalse($w->create(1, 1, '13:00', '17:00'));
+ $this->assertNotFalse($w->create(1, 2, '09:30', '12:00'));
+ $this->assertNotFalse($w->create(1, 2, '13:00', '17:00'));
+
+ // Different day
+ $start = new DateTime('next Monday');
+ $start->setTime(16, 02);
+
+ $end = new DateTime('next Tuesday');
+ $end->setTime(10, 03);
+
+ $this->assertEquals(1.5, $t->calculateEffectiveDuration(1, $start, $end));
+
+ // Same time slot
+ $start = new DateTime('next Monday');
+ $start->setTime(16, 02);
+
+ $end = new DateTime('next Monday');
+ $end->setTime(17, 03);
+
+ $this->assertEquals(1, $t->calculateEffectiveDuration(1, $start, $end));
+
+ // Intermediate time slot
+ $start = new DateTime('next Monday');
+ $start->setTime(10, 02);
+
+ $end = new DateTime('next Tuesday');
+ $end->setTime(16, 03);
+
+ $this->assertEquals(11.5, $t->calculateEffectiveDuration(1, $start, $end));
+
+ // Different day
+ $start = new DateTime('next Monday');
+ $start->setTime(9, 02);
+
+ $end = new DateTime('next Tuesday');
+ $end->setTime(10, 03);
+
+ $this->assertEquals(7, $t->calculateEffectiveDuration(1, $start, $end));
+
+ // Start before first time slot
+ $start = new DateTime('next Monday');
+ $start->setTime(5, 32);
+
+ $end = new DateTime('next Tuesday');
+ $end->setTime(11, 17);
+
+ $this->assertEquals(8.25, $t->calculateEffectiveDuration(1, $start, $end));
+ }
+
+ public function testCalculateDurationWithEmptyTimetable()
+ {
+ $t = new Timetable($this->container);
+
+ $start = new DateTime('next Monday');
+ $start->setTime(16, 02);
+
+ $end = new DateTime('next Monday');
+ $end->setTime(17, 03);
+
+ $this->assertEquals(1, $t->calculateEffectiveDuration(1, $start, $end));
+ }
}