Passed
Push — main ( 25f4a8...799be8 )
by Sebastian
03:52
created

testSubjectWithEmptyPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 14
c 2
b 0
f 1
nc 1
nop 0
dl 0
loc 21
rs 9.7998
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Hook\Message\Action;
13
14
use CaptainHook\App\Config\Mockery as ConfigMockery;
15
use CaptainHook\App\Config\Options;
16
use CaptainHook\App\Console\IO\Mockery as IOMockery;
17
use CaptainHook\App\Exception\ActionFailed;
18
use CaptainHook\App\Mockery as CHMockery;
19
use CaptainHook\App\RepoMock;
20
use PHPUnit\Framework\TestCase;
21
use SebastianFeldmann\Git\CommitMessage;
22
23
class InjectIssueKeyFromBranchTest extends TestCase
24
{
25
    use ConfigMockery;
26
    use CHMockery;
27
    use IOMockery;
28
29
    public function testConstraint(): void
30
    {
31
        $this->assertTrue(InjectIssueKeyFromBranch::getRestriction()->isApplicableFor('prepare-commit-msg'));
32
        $this->assertFalse(InjectIssueKeyFromBranch::getRestriction()->isApplicableFor('pre-push'));
33
    }
34
35
    public function testPrependSubject(): void
36
    {
37
        $repo = new RepoMock();
38
        $info = $this->createGitInfoOperator('5.0.0', 'feature/ABCD-12345-foo-bar-baz');
39
40
        $repo->setCommitMsg(new CommitMessage('foo' . PHP_EOL . PHP_EOL . 'bar'));
41
        $repo->setInfoOperator($info);
42
43
        $io      = $this->createIOMock();
44
        $config  = $this->createConfigMock();
45
        $action  = $this->createActionConfigMock();
46
        $action->method('getOptions')->willReturn(new Options([
47
            'into' => 'subject',
48
            'mode' => 'prepend',
49
        ]));
50
51
        $hook = new InjectIssueKeyFromBranch();
52
        $hook->execute($config, $io, $repo, $action);
53
54
        $this->assertEquals('ABCD-12345 foo', $repo->getCommitMsg()->getSubject());
55
    }
56
57
    public function testModify(): void
58
    {
59
        $repo = new RepoMock();
60
        $info = $this->createGitInfoOperator('5.0.0', 'feature/ABC-12345-foo-bar-baz');
61
62
        $repo->setCommitMsg(new CommitMessage('foo' . PHP_EOL . PHP_EOL . 'bar'));
63
        $repo->setInfoOperator($info);
64
65
        $io      = $this->createIOMock();
66
        $config  = $this->createConfigMock();
67
        $action  = $this->createActionConfigMock();
68
        $action->method('getOptions')->willReturn(new Options([
69
            'into'   => 'subject',
70
            'mode'   => 'prepend',
71
            'modify' => 'lowercase'
72
        ]));
73
74
        $hook = new InjectIssueKeyFromBranch();
75
        $hook->execute($config, $io, $repo, $action);
76
77
        $this->assertEquals('abc-12345 foo', $repo->getCommitMsg()->getSubject());
78
    }
79
80
    public function testAppendSubject(): void
81
    {
82
        $repo = new RepoMock();
83
        $info = $this->createGitInfoOperator('5.0.0', 'feature/ABCD-12345-foo-bar-baz');
84
85
        $repo->setCommitMsg(new CommitMessage('foo' . PHP_EOL . PHP_EOL . 'bar'));
86
        $repo->setInfoOperator($info);
87
88
        $io      = $this->createIOMock();
89
        $config  = $this->createConfigMock();
90
        $action  = $this->createActionConfigMock();
91
        $action->method('getOptions')->willReturn(new Options([
92
            'into' => 'subject',
93
            'mode' => 'append',
94
        ]));
95
96
        $hook = new InjectIssueKeyFromBranch();
97
        $hook->execute($config, $io, $repo, $action);
98
99
        $this->assertEquals('foo ABCD-12345', $repo->getCommitMsg()->getSubject());
100
    }
101
102
    public function testAppendBodyWithPrefix(): void
103
    {
104
        $repo = new RepoMock();
105
        $info = $this->createGitInfoOperator('5.0.0', 'feature/ABCD-12345-foo-bar-baz');
106
107
        $repo->setCommitMsg(new CommitMessage('foo' . PHP_EOL . PHP_EOL . 'bar'));
108
        $repo->setInfoOperator($info);
109
110
        $io      = $this->createIOMock();
111
        $config  = $this->createConfigMock();
112
        $action  = $this->createActionConfigMock();
113
        $action->method('getOptions')->willReturn(new Options([
114
            'into'   => 'body',
115
            'mode'   => 'append',
116
            'prefix' => PHP_EOL . PHP_EOL . 'issue: '
117
        ]));
118
119
        $hook = new InjectIssueKeyFromBranch();
120
        $hook->execute($config, $io, $repo, $action);
121
122
        $this->assertEquals(
123
            'bar' . PHP_EOL . PHP_EOL . 'issue: ABCD-12345' . PHP_EOL,
124
            $repo->getCommitMsg()->getBody()
125
        );
126
    }
127
128
    public function testAppendBodyWithPrefixWithComments(): void
129
    {
130
        $repo = new RepoMock();
131
        $info = $this->createGitInfoOperator('5.0.0', 'feature/ABCD-12345-foo-bar-baz');
132
133
        $repo->setCommitMsg(
134
            new CommitMessage(
135
                implode(PHP_EOL, ['foo', '', 'bar', '# some comment', '# some comment'])
136
            )
137
        );
138
        $repo->setInfoOperator($info);
139
140
        $io      = $this->createIOMock();
141
        $config  = $this->createConfigMock();
142
        $action  = $this->createActionConfigMock();
143
        $action->method('getOptions')->willReturn(new Options([
144
            'into'   => 'body',
145
            'mode'   => 'append',
146
            'prefix' => PHP_EOL . PHP_EOL . 'issue: '
147
        ]));
148
149
        $expected = 'bar' . PHP_EOL . PHP_EOL . 'issue: ABCD-12345';
150
        $hook     = new InjectIssueKeyFromBranch();
151
        $hook->execute($config, $io, $repo, $action);
152
153
        $this->assertEquals(
154
            $expected,
155
            $repo->getCommitMsg()->getBody()
156
        );
157
        $this->assertStringContainsString('# some comment', $repo->getCommitMsg()->getRawContent());
158
    }
159
160
    public function testIgnoreIssueKeyNotFound(): void
161
    {
162
        $repo = new RepoMock();
163
        $info = $this->createGitInfoOperator('5.0.0', 'foo');
164
165
        $repo->setCommitMsg(new CommitMessage('foo' . PHP_EOL . PHP_EOL . 'bar'));
166
        $repo->setInfoOperator($info);
167
168
        $io      = $this->createIOMock();
169
        $config  = $this->createConfigMock();
170
        $action  = $this->createActionConfigMock();
171
        $action->method('getOptions')->willReturn(new Options([
172
            'into'   => 'body',
173
            'mode'   => 'append',
174
            'prefix' => PHP_EOL . PHP_EOL . 'issue: '
175
        ]));
176
177
        $hook = new InjectIssueKeyFromBranch();
178
        $hook->execute($config, $io, $repo, $action);
179
180
        $this->assertEquals('bar', $repo->getCommitMsg()->getBody());
181
    }
182
183
    public function testFailIssueKeyNotFound(): void
184
    {
185
        $this->expectException(ActionFailed::class);
186
187
        $repo = new RepoMock();
188
        $info = $this->createGitInfoOperator('5.0.0', 'foo');
189
190
        $repo->setCommitMsg(new CommitMessage('foo' . PHP_EOL . PHP_EOL . 'bar'));
191
        $repo->setInfoOperator($info);
192
193
        $io      = $this->createIOMock();
194
        $config  = $this->createConfigMock();
195
        $action  = $this->createActionConfigMock();
196
        $action->method('getOptions')->willReturn(new Options([
197
            'force' => true,
198
        ]));
199
200
        $hook = new InjectIssueKeyFromBranch();
201
        $hook->execute($config, $io, $repo, $action);
202
    }
203
204
    public function testIssueKeyAlreadyInMSG(): void
205
    {
206
        $repo = new RepoMock();
207
        $info = $this->createGitInfoOperator('5.0.0', 'feature/ABCD-12345-foo-bar-baz');
208
209
        $repo->setCommitMsg(new CommitMessage('ABCD-12345 foo' . PHP_EOL . PHP_EOL . 'bar'));
210
        $repo->setInfoOperator($info);
211
212
        $io      = $this->createIOMock();
213
        $config  = $this->createConfigMock();
214
        $action  = $this->createActionConfigMock();
215
        $action->method('getOptions')->willReturn(new Options([
216
            'into' => 'subject',
217
        ]));
218
219
        $hook = new InjectIssueKeyFromBranch();
220
        $hook->execute($config, $io, $repo, $action);
221
222
        $this->assertEquals('ABCD-12345 foo', $repo->getCommitMsg()->getSubject());
223
    }
224
225
    public function testSubjectWithPattern(): void
226
    {
227
        $repo = new RepoMock();
228
        $info = $this->createGitInfoOperator('5.0.0', 'feature/ABCD-12345-foo-bar-baz');
229
230
        $repo->setCommitMsg(new CommitMessage('foo' . PHP_EOL . PHP_EOL . 'bar'));
231
        $repo->setInfoOperator($info);
232
233
        $io      = $this->createIOMock();
234
        $config  = $this->createConfigMock();
235
        $action  = $this->createActionConfigMock();
236
        $action->method('getOptions')->willReturn(new Options([
237
            'into'    => 'subject',
238
            'pattern' => '$1: ',
239
            'mode'    => 'prepend',
240
        ]));
241
242
        $hook = new InjectIssueKeyFromBranch();
243
        $hook->execute($config, $io, $repo, $action);
244
245
        $this->assertEquals('ABCD-12345: foo', $repo->getCommitMsg()->getSubject());
246
    }
247
248
    public function testSubjectWithEmptyPattern(): void
249
    {
250
        $repo = new RepoMock();
251
        $info = $this->createGitInfoOperator('5.0.0', 'feature/ABCD-12345-foo-bar-baz');
252
253
        $repo->setCommitMsg(new CommitMessage('foo' . PHP_EOL . PHP_EOL . 'bar'));
254
        $repo->setInfoOperator($info);
255
256
        $io      = $this->createIOMock();
257
        $config  = $this->createConfigMock();
258
        $action  = $this->createActionConfigMock();
259
        $action->method('getOptions')->willReturn(new Options([
260
            'into'    => 'subject',
261
            'pattern' => '',
262
            'mode'    => 'prepend',
263
        ]));
264
265
        $hook = new InjectIssueKeyFromBranch();
266
        $hook->execute($config, $io, $repo, $action);
267
268
        $this->assertEquals('ABCD-12345 foo', $repo->getCommitMsg()->getSubject());
269
    }
270
}
271