Completed
Pull Request — master (#155)
by Deven
05:10
created

EventsControllerTest::testIndex()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 171
Code Lines 101

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 171
rs 8.2857
cc 1
eloc 101
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Test\TestCase\Controller;
4
5
use Cake\ORM\TableRegistry;
6
use Cake\TestSuite\IntegrationTestCase;
7
8
class EventsControllerTest extends IntegrationTestCase
9
{
10
    public $fixtures = array('app.reports');
11
12
    public function setUp()
13
    {
14
        $this->Reports = TableRegistry::get('Reports');
0 ignored issues
show
Bug introduced by
The property Reports does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
15
    }
16
17
    public function testIndex()
18
    {
19
20
        /* Test case 1 */
21
        // Invalid User Agent
22
        $this->configRequest([
23
            'headers' => ['User-Agent' => 'Invalid-GitHub-Hookshot-abcdef']
24
        ]);
25
        $this->post('/events');
26
        $this->assertResponseCode(403);
27
28
        /* Test case 2 */
29
        // Invalid Event Type
30
        $this->configRequest([
31
            'headers' => [
32
                'User-Agent' => 'GitHub-Hookshot-abcdef',
33
                'X-GitHub-Event' => 'anything-except-issues'
34
            ]
35
        ]);
36
        $this->post('/events');
37
        $this->assertResponseCode(400);
38
39
        /* Test case 3 */
40
        // Invalid Hash in headers
41
        $this->configRequest([
42
            'headers' => [
43
                'User-Agent' => 'GitHub-Hookshot-abcdef',
44
                'X-GitHub-Event' => 'issues',
45
                'X-Hub-Signature' => 'sha1=89db05030cf4c1fbcfb4d590deda30fa40a247ed'
46
            ]
47
        ]);
48
        $this->post(
49
            '/events',
50
            json_encode(
51
                array(
52
                    'action' => 'closed',
53
                    'issue' => array(
54
                        'number' => 4
55
                    )
56
                )
57
            )
58
        );
59
        $this->assertResponseCode(401);
60
61
        /* Test case 4 */
62
        // Invalid issues action
63
        $this->configRequest([
64
            'headers' => [
65
                'User-Agent' => 'GitHub-Hookshot-abcdef',
66
                'X-GitHub-Event' => 'issues',
67
                'X-Hub-Signature' => 'sha1=46811e277d924832a778361793c24810a16d4e77'
68
            ]
69
        ]);
70
        $this->post(
71
            '/events',
72
            json_encode(
73
                array(
74
                    'action' => 'anything-invalid',
75
                    'issue' => array(
76
                        'number' => 4
77
                    )
78
                )
79
            )
80
        );
81
        $this->assertResponseCode(204);
82
83
        /* Test case 5 */
84
        // Event for an unlinked issue
85
        $this->configRequest([
86
            'headers' => [
87
                'User-Agent' => 'GitHub-Hookshot-abcdef',
88
                'X-GitHub-Event' => 'issues',
89
                'X-Hub-Signature' => 'sha1=46811e277d924832a778361793c24810a16d4e77'
90
            ]
91
        ]);
92
        $this->post(
93
            '/events',
94
            json_encode(
95
                array(
96
                    'action' => 'closed',
97
                    'issue' => array(
98
                        'number' => 1234
99
                    )
100
                )
101
            )
102
        );
103
        $this->assertResponseCode(204);
104
105
        // Prepare for testcase
106
        $report = $this->Reports->get(4);
107
        $report->status = 'resolved';
108
        $this->Reports->save($report);
109
110
        /* Test case 6 */
111
        // Event 'opened' for a linked issue
112
        $this->configRequest([
113
            'headers' => [
114
                'User-Agent' => 'GitHub-Hookshot-abcdef',
115
                'X-GitHub-Event' => 'issues',
116
                'X-Hub-Signature' => 'sha1=46811e277d924832a778361793c24810a16d4e77'
117
            ]
118
        ]);
119
        $this->post(
120
            '/events',
121
            json_encode(
122
                array(
123
                    'action' => 'opened',
124
                    'issue' => array(
125
                        'number' => 4
126
                    )
127
                )
128
            )
129
        );
130
        $this->assertResponseCode(201);
131
        $report = $this->Reports->get(4);
132
        $this->assertEquals($report->status, 'forwarded');
133
134
135
        /* Test case 7 */
136
        // Event 'closed' for a linked issue
137
        $this->configRequest([
138
            'headers' => [
139
                'User-Agent' => 'GitHub-Hookshot-abcdef',
140
                'X-GitHub-Event' => 'issues',
141
                'X-Hub-Signature' => 'sha1=46811e277d924832a778361793c24810a16d4e77'
142
            ]
143
        ]);
144
        $this->post(
145
            '/events',
146
            json_encode(
147
                array(
148
                    'action' => 'closed',
149
                    'issue' => array(
150
                        'number' => 4
151
                    )
152
                )
153
            )
154
        );
155
        $this->assertResponseCode(201);
156
        $report = $this->Reports->get(4);
157
        $this->assertEquals($report->status, 'resolved');
158
159
        // Prepare for testcase
160
        $report = $this->Reports->get(4);
161
        $report->status = 'resolved';
162
        $this->Reports->save($report);
163
164
        /* Test case 8 */
165
        // Event 'reopened' for a linked issue
166
        $this->configRequest([
167
            'headers' => [
168
                'User-Agent' => 'GitHub-Hookshot-abcdef',
169
                'X-GitHub-Event' => 'issues',
170
                'X-Hub-Signature' => 'sha1=46811e277d924832a778361793c24810a16d4e77'
171
            ]
172
        ]);
173
        $this->post(
174
            '/events',
175
            json_encode(
176
                array(
177
                    'action' => 'reopened',
178
                    'issue' => array(
179
                        'number' => 4
180
                    )
181
                )
182
            )
183
        );
184
        $this->assertResponseCode(201);
185
        $report = $this->Reports->get(4);
186
        $this->assertEquals($report->status, 'forwarded');
187
    }
188
}
189