summaryrefslogtreecommitdiff
path: root/tests/units/Analytic/AverageTimeSpentColumnAnalyticTest.php
blob: 8ad6d1e764e15507255b041778feb51dd28261ca (plain)
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
<?php

require_once __DIR__.'/../Base.php';

use Kanboard\Model\TaskCreation;
use Kanboard\Model\Project;
use Kanboard\Model\Transition;
use Kanboard\Model\Task;
use Kanboard\Model\TaskFinder;
use Kanboard\Analytic\AverageTimeSpentColumnAnalytic;

class AverageTimeSpentColumnAnalyticTest extends Base
{
    public function testAverageWithNoTransitions()
    {
        $taskCreationModel = new TaskCreation($this->container);
        $projectModel = new Project($this->container);
        $averageLeadCycleTimeAnalytic = new AverageTimeSpentColumnAnalytic($this->container);

        $this->assertEquals(1, $projectModel->create(array('name' => 'test1')));

        $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test')));
        $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test')));

        $now = time();

        $this->container['db']->table(Task::TABLE)->eq('id', 1)->update(array('date_completed' => $now + 3600));
        $this->container['db']->table(Task::TABLE)->eq('id', 2)->update(array('date_completed' => $now + 1800));

        $stats = $averageLeadCycleTimeAnalytic->build(1);
        $expected = array(
            1 => array(
                'count' => 2,
                'time_spent' => 3600+1800,
                'average' => (int) ((3600+1800)/2),
                'title' => 'Backlog',
            ),
            2 => array(
                'count' => 0,
                'time_spent' => 0,
                'average' => 0,
                'title' => 'Ready',
            ),
            3 => array(
                'count' => 0,
                'time_spent' => 0,
                'average' => 0,
                'title' => 'Work in progress',
            ),
            4 => array(
                'count' => 0,
                'time_spent' => 0,
                'average' => 0,
                'title' => 'Done',
            )
        );

        $this->assertEquals($expected, $stats);
    }

    public function testAverageWithTransitions()
    {
        $transitionModel = new Transition($this->container);
        $taskFinderModel = new TaskFinder($this->container);
        $taskCreationModel = new TaskCreation($this->container);
        $projectModel = new Project($this->container);
        $averageLeadCycleTimeAnalytic = new AverageTimeSpentColumnAnalytic($this->container);

        $this->assertEquals(1, $projectModel->create(array('name' => 'test1')));

        $this->assertEquals(1, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test')));
        $this->assertEquals(2, $taskCreationModel->create(array('project_id' => 1, 'title' => 'test')));

        $now = time();
        $this->container['db']->table(Task::TABLE)->eq('id', 1)->update(array('date_completed' => $now + 3600));
        $this->container['db']->table(Task::TABLE)->eq('id', 2)->update(array('date_completed' => $now + 1800));

        foreach (array(1, 2) as $task_id) {
            $task = $taskFinderModel->getById($task_id);
            $task['task_id'] = $task['id'];
            $task['date_moved'] = $now - 900;
            $task['src_column_id'] = 3;
            $task['dst_column_id'] = 1;
            $this->assertTrue($transitionModel->save(1, $task));
        }

        $stats = $averageLeadCycleTimeAnalytic->build(1);
        $expected = array(
            1 => array(
                'count' => 2,
                'time_spent' => 3600+1800,
                'average' => (int) ((3600+1800)/2),
                'title' => 'Backlog',
            ),
            2 => array(
                'count' => 0,
                'time_spent' => 0,
                'average' => 0,
                'title' => 'Ready',
            ),
            3 => array(
                'count' => 2,
                'time_spent' => 1800,
                'average' => 900,
                'title' => 'Work in progress',
            ),
            4 => array(
                'count' => 0,
                'time_spent' => 0,
                'average' => 0,
                'title' => 'Done',
            )
        );

        $this->assertEquals($expected, $stats);
    }
}