|
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
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Tests Beams::getRestriction |
|
32
|
|
|
*/ |
|
33
|
|
|
public function testConstraint(): void |
|
34
|
|
|
{ |
|
35
|
|
|
$this->assertTrue(InjectIssueKeyFromBranch::getRestriction()->isApplicableFor('prepare-commit-msg')); |
|
36
|
|
|
$this->assertFalse(InjectIssueKeyFromBranch::getRestriction()->isApplicableFor('pre-push')); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Tests InjectIssueKeyFromBranch::execute |
|
41
|
|
|
* |
|
42
|
|
|
* @throws \Exception |
|
43
|
|
|
*/ |
|
44
|
|
|
public function testPrependSubject(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$repo = new RepoMock(); |
|
47
|
|
|
$info = $this->createGitInfoOperator('5.0.0', 'feature/ABCD-12345-foo-bar-baz'); |
|
48
|
|
|
|
|
49
|
|
|
$repo->setCommitMsg(new CommitMessage('foo' . PHP_EOL . PHP_EOL . 'bar')); |
|
50
|
|
|
$repo->setInfoOperator($info); |
|
51
|
|
|
|
|
52
|
|
|
$io = $this->createIOMock(); |
|
53
|
|
|
$config = $this->createConfigMock(); |
|
54
|
|
|
$action = $this->createActionConfigMock(); |
|
55
|
|
|
$action->method('getOptions')->willReturn(new Options([ |
|
56
|
|
|
'into' => 'subject', |
|
57
|
|
|
'mode' => 'prepend', |
|
58
|
|
|
])); |
|
59
|
|
|
|
|
60
|
|
|
$hook = new InjectIssueKeyFromBranch(); |
|
61
|
|
|
$hook->execute($config, $io, $repo, $action); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertEquals('ABCD-12345 foo', $repo->getCommitMsg()->getSubject()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Tests InjectIssueKeyFromBranch::execute |
|
68
|
|
|
* |
|
69
|
|
|
* @throws \Exception |
|
70
|
|
|
*/ |
|
71
|
|
|
public function testAppendSubject(): void |
|
72
|
|
|
{ |
|
73
|
|
|
$repo = new RepoMock(); |
|
74
|
|
|
$info = $this->createGitInfoOperator('5.0.0', 'feature/ABCD-12345-foo-bar-baz'); |
|
75
|
|
|
|
|
76
|
|
|
$repo->setCommitMsg(new CommitMessage('foo' . PHP_EOL . PHP_EOL . 'bar')); |
|
77
|
|
|
$repo->setInfoOperator($info); |
|
78
|
|
|
|
|
79
|
|
|
$io = $this->createIOMock(); |
|
80
|
|
|
$config = $this->createConfigMock(); |
|
81
|
|
|
$action = $this->createActionConfigMock(); |
|
82
|
|
|
$action->method('getOptions')->willReturn(new Options([ |
|
83
|
|
|
'into' => 'subject', |
|
84
|
|
|
'mode' => 'append', |
|
85
|
|
|
])); |
|
86
|
|
|
|
|
87
|
|
|
$hook = new InjectIssueKeyFromBranch(); |
|
88
|
|
|
$hook->execute($config, $io, $repo, $action); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertEquals('foo ABCD-12345', $repo->getCommitMsg()->getSubject()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Tests InjectIssueKeyFromBranch::execute |
|
95
|
|
|
* |
|
96
|
|
|
* @throws \Exception |
|
97
|
|
|
*/ |
|
98
|
|
|
public function testAppendBodyWithPrefix(): void |
|
99
|
|
|
{ |
|
100
|
|
|
$repo = new RepoMock(); |
|
101
|
|
|
$info = $this->createGitInfoOperator('5.0.0', 'feature/ABCD-12345-foo-bar-baz'); |
|
102
|
|
|
|
|
103
|
|
|
$repo->setCommitMsg(new CommitMessage('foo' . PHP_EOL . PHP_EOL . 'bar')); |
|
104
|
|
|
$repo->setInfoOperator($info); |
|
105
|
|
|
|
|
106
|
|
|
$io = $this->createIOMock(); |
|
107
|
|
|
$config = $this->createConfigMock(); |
|
108
|
|
|
$action = $this->createActionConfigMock(); |
|
109
|
|
|
$action->method('getOptions')->willReturn(new Options([ |
|
110
|
|
|
'into' => 'body', |
|
111
|
|
|
'mode' => 'append', |
|
112
|
|
|
'prefix' => PHP_EOL . PHP_EOL . 'issue: ' |
|
113
|
|
|
])); |
|
114
|
|
|
|
|
115
|
|
|
$hook = new InjectIssueKeyFromBranch(); |
|
116
|
|
|
$hook->execute($config, $io, $repo, $action); |
|
117
|
|
|
|
|
118
|
|
|
$this->assertEquals( |
|
119
|
|
|
'bar' . PHP_EOL . PHP_EOL . 'issue: ABCD-12345' . PHP_EOL, |
|
120
|
|
|
$repo->getCommitMsg()->getBody() |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Tests InjectIssueKeyFromBranch::execute |
|
127
|
|
|
* |
|
128
|
|
|
* @throws \Exception |
|
129
|
|
|
*/ |
|
130
|
|
|
public function testAppendBodyWithPrefixWithComments(): void |
|
131
|
|
|
{ |
|
132
|
|
|
$repo = new RepoMock(); |
|
133
|
|
|
$info = $this->createGitInfoOperator('5.0.0', 'feature/ABCD-12345-foo-bar-baz'); |
|
134
|
|
|
|
|
135
|
|
|
$repo->setCommitMsg( |
|
136
|
|
|
new CommitMessage( |
|
137
|
|
|
implode(PHP_EOL, ['foo', '', 'bar', '# some comment', '# some comment']) |
|
138
|
|
|
) |
|
139
|
|
|
); |
|
140
|
|
|
$repo->setInfoOperator($info); |
|
141
|
|
|
|
|
142
|
|
|
$io = $this->createIOMock(); |
|
143
|
|
|
$config = $this->createConfigMock(); |
|
144
|
|
|
$action = $this->createActionConfigMock(); |
|
145
|
|
|
$action->method('getOptions')->willReturn(new Options([ |
|
146
|
|
|
'into' => 'body', |
|
147
|
|
|
'mode' => 'append', |
|
148
|
|
|
'prefix' => PHP_EOL . PHP_EOL . 'issue: ' |
|
149
|
|
|
])); |
|
150
|
|
|
|
|
151
|
|
|
$expected = 'bar' . PHP_EOL . PHP_EOL . 'issue: ABCD-12345' . PHP_EOL; |
|
152
|
|
|
$hook = new InjectIssueKeyFromBranch(); |
|
153
|
|
|
$hook->execute($config, $io, $repo, $action); |
|
154
|
|
|
|
|
155
|
|
|
$this->assertEquals( |
|
156
|
|
|
$expected, |
|
157
|
|
|
$repo->getCommitMsg()->getBody() |
|
158
|
|
|
); |
|
159
|
|
|
$this->assertStringContainsString('# some comment', $repo->getCommitMsg()->getRawContent()); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Tests InjectIssueKeyFromBranch::execute |
|
164
|
|
|
* |
|
165
|
|
|
* @throws \Exception |
|
166
|
|
|
*/ |
|
167
|
|
|
public function testIgnoreIssueKeyNotFound(): void |
|
168
|
|
|
{ |
|
169
|
|
|
$repo = new RepoMock(); |
|
170
|
|
|
$info = $this->createGitInfoOperator('5.0.0', 'foo'); |
|
171
|
|
|
|
|
172
|
|
|
$repo->setCommitMsg(new CommitMessage('foo' . PHP_EOL . PHP_EOL . 'bar')); |
|
173
|
|
|
$repo->setInfoOperator($info); |
|
174
|
|
|
|
|
175
|
|
|
$io = $this->createIOMock(); |
|
176
|
|
|
$config = $this->createConfigMock(); |
|
177
|
|
|
$action = $this->createActionConfigMock(); |
|
178
|
|
|
$action->method('getOptions')->willReturn(new Options([ |
|
179
|
|
|
'into' => 'body', |
|
180
|
|
|
'mode' => 'append', |
|
181
|
|
|
'prefix' => PHP_EOL . PHP_EOL . 'issue: ' |
|
182
|
|
|
])); |
|
183
|
|
|
|
|
184
|
|
|
$hook = new InjectIssueKeyFromBranch(); |
|
185
|
|
|
$hook->execute($config, $io, $repo, $action); |
|
186
|
|
|
|
|
187
|
|
|
$this->assertEquals('bar', $repo->getCommitMsg()->getBody()); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Tests InjectIssueKeyFromBranch::execute |
|
192
|
|
|
* |
|
193
|
|
|
* @throws \Exception |
|
194
|
|
|
*/ |
|
195
|
|
|
public function testFailIssueKeyNotFound(): void |
|
196
|
|
|
{ |
|
197
|
|
|
$this->expectException(ActionFailed::class); |
|
198
|
|
|
|
|
199
|
|
|
$repo = new RepoMock(); |
|
200
|
|
|
$info = $this->createGitInfoOperator('5.0.0', 'foo'); |
|
201
|
|
|
|
|
202
|
|
|
$repo->setCommitMsg(new CommitMessage('foo' . PHP_EOL . PHP_EOL . 'bar')); |
|
203
|
|
|
$repo->setInfoOperator($info); |
|
204
|
|
|
|
|
205
|
|
|
$io = $this->createIOMock(); |
|
206
|
|
|
$config = $this->createConfigMock(); |
|
207
|
|
|
$action = $this->createActionConfigMock(); |
|
208
|
|
|
$action->method('getOptions')->willReturn(new Options([ |
|
209
|
|
|
'force' => true, |
|
210
|
|
|
])); |
|
211
|
|
|
|
|
212
|
|
|
$hook = new InjectIssueKeyFromBranch(); |
|
213
|
|
|
$hook->execute($config, $io, $repo, $action); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Tests InjectIssueKeyFromBranch::execute |
|
218
|
|
|
* |
|
219
|
|
|
* @throws \Exception |
|
220
|
|
|
*/ |
|
221
|
|
|
public function testIssueKeyAlreadyInMSG(): void |
|
222
|
|
|
{ |
|
223
|
|
|
$repo = new RepoMock(); |
|
224
|
|
|
$info = $this->createGitInfoOperator('5.0.0', 'feature/ABCD-12345-foo-bar-baz'); |
|
225
|
|
|
|
|
226
|
|
|
$repo->setCommitMsg(new CommitMessage('ABCD-12345 foo' . PHP_EOL . PHP_EOL . 'bar')); |
|
227
|
|
|
$repo->setInfoOperator($info); |
|
228
|
|
|
|
|
229
|
|
|
$io = $this->createIOMock(); |
|
230
|
|
|
$config = $this->createConfigMock(); |
|
231
|
|
|
$action = $this->createActionConfigMock(); |
|
232
|
|
|
$action->method('getOptions')->willReturn(new Options([ |
|
233
|
|
|
'into' => 'subject', |
|
234
|
|
|
])); |
|
235
|
|
|
|
|
236
|
|
|
$hook = new InjectIssueKeyFromBranch(); |
|
237
|
|
|
$hook->execute($config, $io, $repo, $action); |
|
238
|
|
|
|
|
239
|
|
|
$this->assertEquals('ABCD-12345 foo', $repo->getCommitMsg()->getSubject()); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Tests InjectIssueKeyFromBranch::execute |
|
244
|
|
|
* |
|
245
|
|
|
* @throws \Exception |
|
246
|
|
|
*/ |
|
247
|
|
|
public function testSubjectWithPattern(): void |
|
248
|
|
|
{ |
|
249
|
|
|
$repo = new RepoMock(); |
|
250
|
|
|
$info = $this->createGitInfoOperator('5.0.0', 'feature/ABCD-12345-foo-bar-baz'); |
|
251
|
|
|
|
|
252
|
|
|
$repo->setCommitMsg(new CommitMessage('foo' . PHP_EOL . PHP_EOL . 'bar')); |
|
253
|
|
|
$repo->setInfoOperator($info); |
|
254
|
|
|
|
|
255
|
|
|
$io = $this->createIOMock(); |
|
256
|
|
|
$config = $this->createConfigMock(); |
|
257
|
|
|
$action = $this->createActionConfigMock(); |
|
258
|
|
|
$action->method('getOptions')->willReturn(new Options([ |
|
259
|
|
|
'into' => 'subject', |
|
260
|
|
|
'pattern' => '$1:', |
|
261
|
|
|
'mode' => 'prepend', |
|
262
|
|
|
])); |
|
263
|
|
|
|
|
264
|
|
|
$hook = new InjectIssueKeyFromBranch(); |
|
265
|
|
|
$hook->execute($config, $io, $repo, $action); |
|
266
|
|
|
|
|
267
|
|
|
$this->assertEquals('ABCD-12345: foo', $repo->getCommitMsg()->getSubject()); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Tests InjectIssueKeyFromBranch::execute |
|
272
|
|
|
* |
|
273
|
|
|
* @throws \Exception |
|
274
|
|
|
*/ |
|
275
|
|
|
public function testSubjectWithEmptyPattern(): void |
|
276
|
|
|
{ |
|
277
|
|
|
$repo = new RepoMock(); |
|
278
|
|
|
$info = $this->createGitInfoOperator('5.0.0', 'feature/ABCD-12345-foo-bar-baz'); |
|
279
|
|
|
|
|
280
|
|
|
$repo->setCommitMsg(new CommitMessage('foo' . PHP_EOL . PHP_EOL . 'bar')); |
|
281
|
|
|
$repo->setInfoOperator($info); |
|
282
|
|
|
|
|
283
|
|
|
$io = $this->createIOMock(); |
|
284
|
|
|
$config = $this->createConfigMock(); |
|
285
|
|
|
$action = $this->createActionConfigMock(); |
|
286
|
|
|
$action->method('getOptions')->willReturn(new Options([ |
|
287
|
|
|
'into' => 'subject', |
|
288
|
|
|
'pattern' => '', |
|
289
|
|
|
'mode' => 'prepend', |
|
290
|
|
|
])); |
|
291
|
|
|
|
|
292
|
|
|
$hook = new InjectIssueKeyFromBranch(); |
|
293
|
|
|
$hook->execute($config, $io, $repo, $action); |
|
294
|
|
|
|
|
295
|
|
|
$this->assertEquals('ABCD-12345 foo', $repo->getCommitMsg()->getSubject()); |
|
296
|
|
|
} |
|
297
|
|
|
} |
|
298
|
|
|
|