summaryrefslogtreecommitdiff
path: root/tests/units/ActionTest.php
blob: 67957a2625b969bea95c1e8b75d2965b6f3638bc (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php

require_once __DIR__.'/Base.php';

use Model\Action;
use Model\Project;
use Model\Board;
use Model\Task;
use Model\TaskPosition;
use Model\TaskCreation;
use Model\TaskFinder;
use Model\Category;
use Integration\GithubWebhook;

class ActionTest extends Base
{
    public function testSingleAction()
    {
        $tp = new TaskPosition($this->container);
        $tc = new TaskCreation($this->container);
        $tf = new TaskFinder($this->container);
        $board = new Board($this->container);
        $project = new Project($this->container);
        $action = new Action($this->container);

        // We create a project
        $this->assertEquals(1, $project->create(array('name' => 'unit_test')));

        // We create a new action
        $this->assertEquals(1, $action->create(array(
            'project_id' => 1,
            'event_name' => Task::EVENT_MOVE_COLUMN,
            'action_name' => 'TaskClose',
            'params' => array(
                'column_id' => 4,
            )
        )));

        // We create a task
        $this->assertEquals(1, $tc->create(array(
            'title' => 'unit_test',
            'project_id' => 1,
            'owner_id' => 1,
            'color_id' => 'red',
            'column_id' => 1,
        )));

        // We attach events
        $action->attachEvents();

        // Our task should be open
        $t1 = $tf->getById(1);
        $this->assertEquals(1, $t1['is_active']);
        $this->assertEquals(1, $t1['column_id']);

        // We move our task
        $tp->movePosition(1, 1, 4, 1);

        // Our task should be closed
        $t1 = $tf->getById(1);
        $this->assertEquals(4, $t1['column_id']);
        $this->assertEquals(0, $t1['is_active']);
    }

    public function testMultipleActions()
    {
        $tp = new TaskPosition($this->container);
        $tc = new TaskCreation($this->container);
        $tf = new TaskFinder($this->container);
        $b = new Board($this->container);
        $p = new Project($this->container);
        $a = new Action($this->container);
        $c = new Category($this->container);
        $g = new GithubWebhook($this->container);

        // We create a project
        $this->assertEquals(1, $p->create(array('name' => 'unit_test')));
        $this->assertEquals(1, $c->create(array('name' => 'unit_test')));

        // We create a new action
        $this->assertEquals(1, $a->create(array(
            'project_id' => 1,
            'event_name' => GithubWebhook::EVENT_ISSUE_OPENED,
            'action_name' => 'TaskCreation',
            'params' => array()
        )));

        $this->assertEquals(2, $a->create(array(
            'project_id' => 1,
            'event_name' => GithubWebhook::EVENT_ISSUE_LABEL_CHANGE,
            'action_name' => 'TaskAssignCategoryLabel',
            'params' => array(
                'label' => 'bug',
                'category_id' => 1,
            )
        )));

        $this->assertEquals(3, $a->create(array(
            'project_id' => 1,
            'event_name' => Task::EVENT_CREATE_UPDATE,
            'action_name' => 'TaskAssignColorCategory',
            'params' => array(
                'color_id' => 'red',
                'category_id' => 1,
            )
        )));

        // We attach events
        $a->attachEvents();
        $g->setProjectId(1);

        // We create a Github issue
        $issue = array(
            'number' => 123,
            'title' => 'Bugs everywhere',
            'body' => 'There is a bug!',
            'html_url' => 'http://localhost/',
        );

        $this->assertTrue($g->handleIssueOpened($issue));

        $task = $tf->getById(1);
        $this->assertNotEmpty($task);
        $this->assertEquals(1, $task['is_active']);
        $this->assertEquals(0, $task['category_id']);
        $this->assertEquals('yellow', $task['color_id']);

        // We assign a label to our issue
        $label = array(
            'name' => 'bug',
        );

        $this->assertTrue($g->handleIssueLabeled($issue, $label));

        $task = $tf->getById(1);
        $this->assertNotEmpty($task);
        $this->assertEquals(1, $task['is_active']);
        $this->assertEquals(1, $task['category_id']);
        $this->assertEquals('red', $task['color_id']);
    }
}