Completed
Branch master (51b807)
by Frank
04:17 queued 02:02
created

GerritHookControllerTest::processChangeMergedWithInvalidToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * T3Bot.
4
 *
5
 * @author Frank Nägler <[email protected]>
6
 *
7
 * @link http://www.t3bot.de
8
 * @link http://wiki.typo3.org/T3Bot
9
 */
10
namespace T3Bot\Tests\Unit\Controller;
11
12
use Doctrine\DBAL\Configuration;
13
use Doctrine\DBAL\DriverManager;
14
use T3Bot\Controller\GerritHookController;
15
use T3Bot\Slack\Message;
16
use T3Bot\Tests\Unit\BaseTestCase;
17
18
/** @noinspection LongInheritanceChainInspection */
19
class GerritHookControllerTest extends BaseTestCase
20
{
21
    public function setUp()
22
    {
23
        $GLOBALS['config']['gerrit']['webhookToken'] = 'unit-test-token';
24
        $GLOBALS['config']['gerrit']['change-merged'] = ['channels' => ['#change-merged']];
25
        $GLOBALS['config']['gerrit']['rst-merged'] = ['channels' => ['#rst-channel']];
26
        $GLOBALS['config']['gerrit']['patchset-created'] = ['channels' => ['#atchset-created']];
27
        $GLOBALS['config']['slack']['botAvatar'] = 'botty';
28
    }
29
30
    /**
31
     * @test
32
     */
33
    public function processChangeMergedWithValidJson()
34
    {
35
        $controller = $this->getMock(GerritHookController::class, ['postToSlack'], [$GLOBALS['config']]);
36
        $controller
37
            ->expects(static::once())
38
            ->method('postToSlack');
39
        $controller->process('change-merged', __DIR__ . '/../Fixtures/Valid/change-merged.json');
40
    }
41
42
    /**
43
     * @test
44
     */
45
    public function processChangeMergedWithAddedRstFile()
46
    {
47
        $mergeChannel = $GLOBALS['config']['gerrit']['change-merged']['channels'];
48
        $GLOBALS['config']['gerrit']['change-merged']['channels'] = [];
49
        $controller = $this->getMock(GerritHookController::class, ['postToSlack'], [$GLOBALS['config']]);
50
        $controller
51
            ->expects(static::any())
52
            ->method('postToSlack')
53
            ->with(
54
                static::isInstanceOf(Message::class),
55
                static::equalTo('#rst-channel')
56
            );
57
        $controller->process('change-merged', __DIR__ . '/../Fixtures/Valid/change-merged-with-added-rst.json');
58
        $GLOBALS['config']['gerrit']['change-merged']['channels'] = $mergeChannel;
59
    }
60
61
    /**
62
     * @test
63
     */
64
    public function processChangeMergedWithDeletedRstFile()
65
    {
66
        $mergeChannel = $GLOBALS['config']['gerrit']['change-merged']['channels'];
67
        $GLOBALS['config']['gerrit']['change-merged']['channels'] = [];
68
        $controller = $this->getMock(GerritHookController::class, ['postToSlack'], [$GLOBALS['config']]);
69
        $controller
70
            ->expects(static::any())
71
            ->method('postToSlack')
72
            ->with(
73
                static::isInstanceOf(Message::class),
74
                static::equalTo('#rst-channel')
75
            );
76
        $controller->process('change-merged', __DIR__ . '/../Fixtures/Valid/change-merged-with-deleted-rst.json');
77
        $GLOBALS['config']['gerrit']['change-merged']['channels'] = $mergeChannel;
78
    }
79
80
    /**
81
     * @test
82
     */
83
    public function processChangeMergedWithChangedRstFile()
84
    {
85
        $mergeChannel = $GLOBALS['config']['gerrit']['change-merged']['channels'];
86
        $GLOBALS['config']['gerrit']['change-merged']['channels'] = [];
87
        $controller = $this->getMock(GerritHookController::class, ['postToSlack'], [$GLOBALS['config']]);
88
        $controller
89
            ->expects(static::any())
90
            ->method('postToSlack')
91
            ->with(
92
                static::isInstanceOf(Message::class),
93
                static::equalTo('#rst-channel')
94
            );
95
        $controller->process('change-merged', __DIR__ . '/../Fixtures/Valid/change-merged-with-changed-rst.json');
96
        $GLOBALS['config']['gerrit']['change-merged']['channels'] = $mergeChannel;
97
    }
98
99
    /**
100
     * @test
101
     */
102
    public function processPatchsetCreatedWithValidJson()
103
    {
104
        $controller = $this->getMock(GerritHookController::class, ['postToSlack'], [$GLOBALS['config']]);
105
        $controller
106
            ->expects(static::once())
107
            ->method('postToSlack');
108
        $controller->process('patchset-created', __DIR__ . '/../Fixtures/Valid/patchset-created.json');
109
    }
110
111
    /**
112
     * @test
113
     */
114
    public function processChangeMergedWithInvalidJson()
115
    {
116
        $controller = $this->getMock(GerritHookController::class, ['postToSlack'], [$GLOBALS['config']]);
117
        $controller
118
            ->expects(static::never())
119
            ->method('postToSlack');
120
        $controller->process('change-merged', __DIR__ . '/../Fixtures/Invalid/change-merged.json');
121
    }
122
123
    /**
124
     * @test
125
     */
126
    public function processPatchsetCreatedWithInvalidJson()
127
    {
128
        $controller = $this->getMock(GerritHookController::class, ['postToSlack'], [$GLOBALS['config']]);
129
        $controller
130
            ->expects(static::never())
131
            ->method('postToSlack');
132
        $controller->process('patchset-created', __DIR__ . '/../Fixtures/Invalid/patchset-created.json');
133
    }
134
135
    /**
136
     * @test
137
     */
138
    public function processChangeMergedWithInvalidToken()
139
    {
140
        $controller = $this->getMock(GerritHookController::class, ['postToSlack'], [$GLOBALS['config']]);
141
        $controller
142
            ->expects(static::never())
143
            ->method('postToSlack');
144
        $controller->process('change-merged', __DIR__ . '/../Fixtures/Invalid/change-merged-invalid-token.json');
145
    }
146
147
    /**
148
     * @test
149
     */
150
    public function processPatchsetCreatedWithInvalidToken()
151
    {
152
        $controller = $this->getMock(GerritHookController::class, ['postToSlack'], [$GLOBALS['config']]);
153
        $controller
154
            ->expects(static::never())
155
            ->method('postToSlack');
156
        $controller->process('patchset-created', __DIR__ . '/../Fixtures/Invalid/patchset-created-invalid-token.json');
157
    }
158
159
    /**
160
     * @test
161
     */
162
    public function processChangeMergedWithValidJsonAddEntryToMessageQueue()
163
    {
164
        $controller = $this->getMock(GerritHookController::class, ['addMessageToQueue'], [$GLOBALS['config']]);
165
        $controller
166
            ->expects(static::once())
167
            ->method('addMessageToQueue');
168
        $controller->process('change-merged', __DIR__ . '/../Fixtures/Valid/change-merged.json');
169
    }
170
171
    /**
172
     * @test
173
     */
174
    public function processPatchsetCreatedWithValidJsonAddEntryToMessageQueue()
175
    {
176
        $controller = $this->getMock(GerritHookController::class, ['addMessageToQueue'], [$GLOBALS['config']]);
177
        $controller
178
            ->expects(static::once())
179
            ->method('addMessageToQueue');
180
        $controller->process('patchset-created', __DIR__ . '/../Fixtures/Valid/patchset-created.json');
181
    }
182
183
    /**
184
     * @test
185
     */
186
    public function addMessageToQueueCreatesEntryInDatabase()
187
    {
188
        $controller = $this->getMock(GerritHookController::class, [], [$GLOBALS['config']]);
189
        $testMessage = [
190
            'message' => 'addMessageToQueueCreatesEntryInDatabase-test',
191
            'test-id' => uniqid('addMessageToQueueCreatesEntryInDatabase-test', true),
192
        ];
193
        $result = json_encode($testMessage);
194
        $this->getDatabaseConnection()->delete('messages', ['message' => $result]);
195
196
        $this->invokeMethod($controller, 'addMessageToQueue', [$testMessage]);
197
198
        $records = $this->getDatabaseConnection()->fetchAll('SELECT * FROM messages WHERE message = ?', [$result]);
199
200
        static::assertGreaterThan(0, count($records));
201
        $this->getDatabaseConnection()->delete('messages', ['message' => $result]);
202
    }
203
204
    /**
205
     * @return \Doctrine\DBAL\Connection
206
     *
207
     * @throws \Doctrine\DBAL\DBALException
208
     */
209
    public function getDatabaseConnection()
210
    {
211
        /* @noinspection PhpInternalEntityUsedInspection */
212
        $config = new Configuration();
213
214
        return DriverManager::getConnection($GLOBALS['config']['db'], $config);
215
    }
216
}
217