summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2015-02-08 13:46:38 -0500
committerFrederic Guillot <fred@kanboard.net>2015-02-08 13:46:38 -0500
commit813b7c4c0036a73c3bb5ef31f98e83cce2630e9e (patch)
tree2374d6e4f07cda8f56f32b2997f3dc900ebbaa1d
parent696cd9535cc622e477dde10eac24b947c351dc87 (diff)
Add unit test for subtask time tracking
-rw-r--r--app/Model/SubtaskTimeTracking.php27
-rw-r--r--composer.lock8
-rw-r--r--tests/units/Base.php2
-rw-r--r--tests/units/SubtaskTimeTrackingTest.php154
4 files changed, 185 insertions, 6 deletions
diff --git a/app/Model/SubtaskTimeTracking.php b/app/Model/SubtaskTimeTracking.php
index 3104dc20..5b58dfe2 100644
--- a/app/Model/SubtaskTimeTracking.php
+++ b/app/Model/SubtaskTimeTracking.php
@@ -44,6 +44,21 @@ class SubtaskTimeTracking extends Base
}
/**
+ * Get all recorded time slots for a given user
+ *
+ * @access public
+ * @param integer $user_id User id
+ * @return array
+ */
+ public function getUserTimesheet($user_id)
+ {
+ return $this->db
+ ->table(self::TABLE)
+ ->eq('user_id', $user_id)
+ ->findAll();
+ }
+
+ /**
* Log start time
*
* @access public
@@ -140,9 +155,17 @@ class SubtaskTimeTracking extends Base
->eq('end', 0)
->findOneColumn('start');
+ $time_spent = $this->db
+ ->table(Subtask::TABLE)
+ ->eq('id', $subtask_id)
+ ->findOneColumn('time_spent');
+
return $start_time &&
$this->db
- ->getConnection()
- ->exec('UPDATE '.Subtask::TABLE.' SET time_spent=time_spent+'.round((time() - $start_time) / 3600, 1).' WHERE id='.$subtask_id);
+ ->table(Subtask::TABLE)
+ ->eq('id', $subtask_id)
+ ->update(array(
+ 'time_spent' => $time_spent + round((time() - $start_time) / 3600, 1)
+ ));
}
}
diff --git a/composer.lock b/composer.lock
index c37550be..8b0388d8 100644
--- a/composer.lock
+++ b/composer.lock
@@ -88,12 +88,12 @@
"source": {
"type": "git",
"url": "https://github.com/fguillot/picoDb.git",
- "reference": "b3ef5f79e7e5e33729fdbf9c02c8a252a3d76b6b"
+ "reference": "3c7875c22195a6bc3937e8cb646a8589872521d1"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/fguillot/picoDb/zipball/b3ef5f79e7e5e33729fdbf9c02c8a252a3d76b6b",
- "reference": "b3ef5f79e7e5e33729fdbf9c02c8a252a3d76b6b",
+ "url": "https://api.github.com/repos/fguillot/picoDb/zipball/3c7875c22195a6bc3937e8cb646a8589872521d1",
+ "reference": "3c7875c22195a6bc3937e8cb646a8589872521d1",
"shasum": ""
},
"require": {
@@ -117,7 +117,7 @@
],
"description": "Minimalist database query builder",
"homepage": "https://github.com/fguillot/picoDb",
- "time": "2015-01-25 16:20:14"
+ "time": "2015-02-08 04:15:39"
},
{
"name": "fguillot/simple-validator",
diff --git a/tests/units/Base.php b/tests/units/Base.php
index 311d3cdf..bce65f2d 100644
--- a/tests/units/Base.php
+++ b/tests/units/Base.php
@@ -36,6 +36,8 @@ abstract class Base extends PHPUnit_Framework_TestCase
new EventDispatcher,
new Stopwatch
);
+
+ $this->container['db']->log_queries = true;
}
public function tearDown()
diff --git a/tests/units/SubtaskTimeTrackingTest.php b/tests/units/SubtaskTimeTrackingTest.php
new file mode 100644
index 00000000..a68d8ee1
--- /dev/null
+++ b/tests/units/SubtaskTimeTrackingTest.php
@@ -0,0 +1,154 @@
+<?php
+
+require_once __DIR__.'/Base.php';
+
+use Model\TaskFinder;
+use Model\TaskCreation;
+use Model\Subtask;
+use Model\SubtaskTimeTracking;
+use Model\Project;
+use Model\Category;
+use Model\User;
+
+class SubtaskTimeTrackingTest extends Base
+{
+ public function testLogStartTime()
+ {
+ $tc = new TaskCreation($this->container);
+ $s = new Subtask($this->container);
+ $st = new SubtaskTimeTracking($this->container);
+ $p = new Project($this->container);
+
+ $this->assertEquals(1, $p->create(array('name' => 'test1')));
+ $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1)));
+ $this->assertEquals(1, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'user_id' => 1)));
+
+ $this->assertTrue($st->logStartTime(1, 1));
+
+ $timesheet = $st->getUserTimesheet(1);
+ $this->assertNotEmpty($timesheet);
+ $this->assertCount(1, $timesheet);
+ $this->assertNotEmpty($timesheet[0]['start']);
+ $this->assertEmpty($timesheet[0]['end']);
+ $this->assertEquals(1, $timesheet[0]['user_id']);
+ $this->assertEquals(1, $timesheet[0]['subtask_id']);
+ }
+
+ public function testLogStartEnd()
+ {
+ $tc = new TaskCreation($this->container);
+ $s = new Subtask($this->container);
+ $st = new SubtaskTimeTracking($this->container);
+ $p = new Project($this->container);
+
+ $this->assertEquals(1, $p->create(array('name' => 'test1')));
+ $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1)));
+ $this->assertEquals(1, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'user_id' => 1)));
+
+ // No start time
+ $this->assertTrue($st->logEndTime(1, 1));
+ $timesheet = $st->getUserTimesheet(1);
+ $this->assertEmpty($timesheet);
+
+ // Log start and end time
+ $this->assertTrue($st->logStartTime(1, 1));
+ sleep(1);
+ $this->assertTrue($st->logEndTime(1, 1));
+
+ $timesheet = $st->getUserTimesheet(1);
+ $this->assertNotEmpty($timesheet);
+ $this->assertCount(1, $timesheet);
+ $this->assertNotEmpty($timesheet[0]['start']);
+ $this->assertNotEmpty($timesheet[0]['end']);
+ $this->assertEquals(1, $timesheet[0]['user_id']);
+ $this->assertEquals(1, $timesheet[0]['subtask_id']);
+ $this->assertNotEquals($timesheet[0]['start'], $timesheet[0]['end']);
+ }
+
+ public function testCalculateSubtaskTime()
+ {
+ $tc = new TaskCreation($this->container);
+ $s = new Subtask($this->container);
+ $st = new SubtaskTimeTracking($this->container);
+ $p = new Project($this->container);
+
+ $this->assertEquals(1, $p->create(array('name' => 'test1')));
+ $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1)));
+ $this->assertEquals(1, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_spent' => 2.2, 'time_estimated' => 3.3)));
+ $this->assertEquals(2, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_spent' => 1.1, 'time_estimated' => 4.4)));
+
+ $time = $st->calculateSubtaskTime(1);
+ $this->assertNotempty($time);
+ $this->assertCount(2, $time);
+ $this->assertEquals(3.3, $time['total_spent'], 'Total spent', 0.01);
+ $this->assertEquals(7.7, $time['total_estimated'], 'Total estimated', 0.01);
+ }
+
+ public function testUpdateSubtaskTimeSpent()
+ {
+ $tc = new TaskCreation($this->container);
+ $s = new Subtask($this->container);
+ $st = new SubtaskTimeTracking($this->container);
+ $p = new Project($this->container);
+
+ $this->assertEquals(1, $p->create(array('name' => 'test1')));
+ $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1)));
+ $this->assertEquals(1, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_spent' => 2.2)));
+ $this->assertEquals(2, $s->create(array('title' => 'subtask #2', 'task_id' => 1)));
+
+ $this->assertTrue($st->logStartTime(1, 1));
+ $this->assertTrue($st->logStartTime(2, 1));
+
+ // Fake start time
+ $this->container['db']->table(SubtaskTimeTracking::TABLE)->update(array('start' => time() - 3600));
+
+ $this->assertTrue($st->logEndTime(1, 1));
+ $this->assertTrue($st->logEndTime(2, 1));
+
+ $timesheet = $st->getUserTimesheet(1);
+ $this->assertNotEmpty($timesheet);
+ $this->assertCount(2, $timesheet);
+ $this->assertEquals(3600, $timesheet[0]['end'] - $timesheet[0]['start'], 'Wrong timestamps', 1);
+ $this->assertEquals(3600, $timesheet[1]['end'] - $timesheet[1]['start'], 'Wrong timestamps', 1);
+
+ $time = $st->calculateSubtaskTime(1);
+ $this->assertNotempty($time);
+ $this->assertEquals(4.2, $time['total_spent'], 'Total spent', 0.01);
+ $this->assertEquals(0, $time['total_estimated'], 'Total estimated', 0.01);
+
+ $time = $st->calculateSubtaskTime(2);
+ $this->assertNotempty($time);
+ $this->assertEquals(0, $time['total_spent'], 'Total spent', 0.01);
+ $this->assertEquals(0, $time['total_estimated'], 'Total estimated', 0.01);
+ }
+
+ public function testUpdateTaskTimeTracking()
+ {
+ $tf = new TaskFinder($this->container);
+ $tc = new TaskCreation($this->container);
+ $s = new Subtask($this->container);
+ $st = new SubtaskTimeTracking($this->container);
+ $p = new Project($this->container);
+
+ $this->assertEquals(1, $p->create(array('name' => 'test1')));
+
+ $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1)));
+ $this->assertEquals(2, $tc->create(array('title' => 'test 2', 'project_id' => 1, 'time_estimated' => 1.5, 'time_spent' => 0.5)));
+
+ $this->assertEquals(1, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_spent' => 2.2)));
+ $this->assertEquals(2, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_estimated' => 1)));
+
+ $st->updateTaskTimeTracking(1);
+ $st->updateTaskTimeTracking(2);
+
+ $task = $tf->getById(1);
+ $this->assertNotEmpty($task);
+ $this->assertEquals(2.2, $task['time_spent'], 'Total spent', 0.01);
+ $this->assertEquals(1, $task['time_estimated'], 'Total estimated', 0.01);
+
+ $task = $tf->getById(2);
+ $this->assertNotEmpty($task);
+ $this->assertEquals(0.5, $task['time_spent'], 'Total spent', 0.01);
+ $this->assertEquals(1.5, $task['time_estimated'], 'Total estimated', 0.01);
+ }
+}