1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
<?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 #1', '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);
}
public function testGetCalendarEvents()
{
$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(2, $p->create(array('name' => 'test2')));
$this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1)));
$this->assertEquals(2, $tc->create(array('title' => 'test 1', 'project_id' => 2)));
$this->assertEquals(1, $s->create(array('title' => 'subtask #1', 'task_id' => 1)));
$this->assertEquals(2, $s->create(array('title' => 'subtask #2', 'task_id' => 1)));
$this->assertEquals(3, $s->create(array('title' => 'subtask #3', 'task_id' => 1)));
$this->assertEquals(4, $s->create(array('title' => 'subtask #4', 'task_id' => 2)));
$this->assertEquals(5, $s->create(array('title' => 'subtask #5', 'task_id' => 2)));
$this->assertEquals(6, $s->create(array('title' => 'subtask #6', '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));
// 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));
// 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));
// 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));
// 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));
// 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));
$timesheet = $st->getUserTimesheet(1);
$this->assertNotEmpty($timesheet);
$this->assertCount(6, $timesheet);
$events = $st->getUserCalendarEvents(1, date('Y-m-d', $now), date('Y-m-d', $now + 86400));
$this->assertNotEmpty($events);
$this->assertCount(4, $events);
$this->assertEquals(1, $events[0]['subtask_id']);
$this->assertEquals(2, $events[1]['subtask_id']);
$this->assertEquals(3, $events[2]['subtask_id']);
$this->assertEquals(4, $events[3]['subtask_id']);
$events = $st->getProjectCalendarEvents(1, date('Y-m-d', $now), date('Y-m-d', $now + 86400));
$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));
$this->assertNotEmpty($events);
$this->assertCount(1, $events);
$this->assertEquals(4, $events[0]['subtask_id']);
}
}
|