1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ChangelogGenerator\Tests; |
6
|
|
|
|
7
|
|
|
use ChangelogGenerator\ChangelogConfig; |
8
|
|
|
use ChangelogGenerator\ChangelogGenerator; |
9
|
|
|
use ChangelogGenerator\Issue; |
10
|
|
|
use ChangelogGenerator\IssueGroup; |
11
|
|
|
use ChangelogGenerator\IssueGrouper; |
12
|
|
|
use ChangelogGenerator\IssueRepository; |
13
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Symfony\Component\Console\Output\StreamOutput; |
16
|
|
|
|
17
|
|
|
use function fopen; |
18
|
|
|
use function rewind; |
19
|
|
|
use function stream_get_contents; |
20
|
|
|
|
21
|
|
|
final class ChangelogGeneratorTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
/** @var MockObject&IssueRepository */ |
24
|
|
|
private $issueRepository; |
25
|
|
|
|
26
|
|
|
/** @var MockObject&IssueGrouper */ |
27
|
|
|
private $issueGrouper; |
28
|
|
|
|
29
|
|
|
private ChangelogGenerator $changelogGenerator; |
30
|
|
|
|
31
|
|
|
public function testGenerate(): void |
32
|
|
|
{ |
33
|
|
|
$user = 'jwage'; |
34
|
|
|
$repository = 'changelog-generator'; |
35
|
|
|
$milestone = '1.0'; |
36
|
|
|
|
37
|
|
|
$outputStream = fopen('php://memory', 'rwb+'); |
38
|
|
|
|
39
|
|
|
self::assertNotFalse($outputStream); |
40
|
|
|
|
41
|
|
|
$output = new StreamOutput($outputStream); |
42
|
|
|
|
43
|
|
|
$pullRequest1 = new Issue(3, 'Issue #3', 'Test Body', 'https://example.com/3', 'Ocramius', [], true); |
44
|
|
|
$pullRequest2 = new Issue(4, 'Issue #4', 'Test Body', 'https://example.com/4', 'romanb', [], true); |
45
|
|
|
|
46
|
|
|
$issue1 = new Issue(1, 'Issue #1', 'Test Body', 'https://example.com/1', 'jwage', [], false); |
47
|
|
|
$issue1->setLinkedPullRequest($pullRequest1); |
48
|
|
|
|
49
|
|
|
$issue2 = new Issue(2, 'Issue #2', 'Test Body', 'https://example.com/2', 'jwage', [], false); |
50
|
|
|
$issue2->setLinkedPullRequest($pullRequest2); |
51
|
|
|
|
52
|
|
|
$pullRequest1->setLinkedIssue($issue1); |
53
|
|
|
$pullRequest2->setLinkedIssue($issue2); |
54
|
|
|
|
55
|
|
|
$issueGroup = new IssueGroup('Enhancement', [$pullRequest1, $pullRequest2]); |
56
|
|
|
|
57
|
|
|
$milestoneIssues = [$issue1, $issue2, $pullRequest1, $pullRequest2]; |
58
|
|
|
$issueGroups = [$issueGroup]; |
59
|
|
|
|
60
|
|
|
$changelogConfig = (new ChangelogConfig($user, $repository, $milestone, [])) |
61
|
|
|
->setShowContributors(true); |
62
|
|
|
|
63
|
|
|
$this->issueRepository->expects(self::once()) |
64
|
|
|
->method('getMilestoneIssues') |
65
|
|
|
->with($changelogConfig) |
66
|
|
|
->willReturn($milestoneIssues); |
67
|
|
|
|
68
|
|
|
$this->issueGrouper->expects(self::once()) |
69
|
|
|
->method('groupIssues') |
70
|
|
|
->with($milestoneIssues) |
71
|
|
|
->willReturn($issueGroups); |
72
|
|
|
|
73
|
|
|
$this->changelogGenerator->generate($changelogConfig, $output); |
74
|
|
|
|
75
|
|
|
rewind($outputStream); |
76
|
|
|
self::assertSame( |
77
|
|
|
<<<'EXPECTED_OUTPUT' |
78
|
|
|
1.0 |
79
|
|
|
=== |
80
|
|
|
|
81
|
|
|
- Total issues resolved: **2** |
82
|
|
|
- Total pull requests resolved: **2** |
83
|
|
|
- Total contributors: **3** |
84
|
|
|
|
85
|
|
|
Enhancement |
86
|
|
|
----------- |
87
|
|
|
|
88
|
|
|
- [3: Issue #3](https://example.com/3) thanks to @Ocramius and @jwage |
89
|
|
|
- [4: Issue #4](https://example.com/4) thanks to @romanb and @jwage |
90
|
|
|
|
91
|
|
|
Contributors |
92
|
|
|
------------ |
93
|
|
|
|
94
|
|
|
- [@jwage](https://github.com/jwage) |
95
|
|
|
- [@Ocramius](https://github.com/Ocramius) |
96
|
|
|
- [@romanb](https://github.com/romanb) |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
EXPECTED_OUTPUT |
100
|
|
|
, |
101
|
|
|
stream_get_contents($outputStream) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testGenerateNoGroups(): void |
106
|
|
|
{ |
107
|
|
|
$user = 'jwage'; |
108
|
|
|
$repository = 'changelog-generator'; |
109
|
|
|
$milestone = '1.0'; |
110
|
|
|
|
111
|
|
|
$outputStream = fopen('php://memory', 'rwb+'); |
112
|
|
|
|
113
|
|
|
self::assertNotFalse($outputStream); |
114
|
|
|
|
115
|
|
|
$output = new StreamOutput($outputStream); |
116
|
|
|
|
117
|
|
|
$pullRequest1 = new Issue(3, 'Issue #3', 'Test Body', 'https://example.com/3', 'Ocramius', [], true); |
118
|
|
|
$pullRequest2 = new Issue(4, 'Issue #4', 'Test Body', 'https://example.com/4', 'romanb', [], true); |
119
|
|
|
|
120
|
|
|
$issue1 = new Issue(1, 'Issue #1', 'Test Body', 'https://example.com/1', 'jwage', [], false); |
121
|
|
|
$issue1->setLinkedPullRequest($pullRequest1); |
122
|
|
|
|
123
|
|
|
$issue2 = new Issue(2, 'Issue #2', 'Test Body', 'https://example.com/2', 'jwage', [], false); |
124
|
|
|
$issue2->setLinkedPullRequest($pullRequest2); |
125
|
|
|
|
126
|
|
|
$pullRequest1->setLinkedIssue($issue1); |
127
|
|
|
$pullRequest2->setLinkedIssue($issue2); |
128
|
|
|
|
129
|
|
|
$issueGroup = new IssueGroup('', [$pullRequest1, $pullRequest2]); |
130
|
|
|
|
131
|
|
|
$milestoneIssues = [$issue1, $issue2, $pullRequest1, $pullRequest2]; |
132
|
|
|
$issueGroups = [$issueGroup]; |
133
|
|
|
|
134
|
|
|
$changelogConfig = (new ChangelogConfig($user, $repository, $milestone, [])) |
135
|
|
|
->setShowContributors(true); |
136
|
|
|
|
137
|
|
|
$this->issueRepository->expects(self::once()) |
138
|
|
|
->method('getMilestoneIssues') |
139
|
|
|
->with($changelogConfig) |
140
|
|
|
->willReturn($milestoneIssues); |
141
|
|
|
|
142
|
|
|
$this->issueGrouper->expects(self::once()) |
143
|
|
|
->method('groupIssues') |
144
|
|
|
->with($milestoneIssues) |
145
|
|
|
->willReturn($issueGroups); |
146
|
|
|
|
147
|
|
|
$this->changelogGenerator->generate($changelogConfig, $output); |
148
|
|
|
|
149
|
|
|
rewind($outputStream); |
150
|
|
|
self::assertSame( |
151
|
|
|
<<<'EXPECTED_OUTPUT' |
152
|
|
|
1.0 |
153
|
|
|
=== |
154
|
|
|
|
155
|
|
|
- Total issues resolved: **2** |
156
|
|
|
- Total pull requests resolved: **2** |
157
|
|
|
- Total contributors: **3** |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
|
161
|
|
|
|
162
|
|
|
- [3: Issue #3](https://example.com/3) thanks to @Ocramius and @jwage |
163
|
|
|
- [4: Issue #4](https://example.com/4) thanks to @romanb and @jwage |
164
|
|
|
|
165
|
|
|
Contributors |
166
|
|
|
------------ |
167
|
|
|
|
168
|
|
|
- [@jwage](https://github.com/jwage) |
169
|
|
|
- [@Ocramius](https://github.com/Ocramius) |
170
|
|
|
- [@romanb](https://github.com/romanb) |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
EXPECTED_OUTPUT |
174
|
|
|
, |
175
|
|
|
stream_get_contents($outputStream) |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function testGenerateNoGroupsAndGroups(): void |
180
|
|
|
{ |
181
|
|
|
$user = 'jwage'; |
182
|
|
|
$repository = 'changelog-generator'; |
183
|
|
|
$milestone = '1.0'; |
184
|
|
|
|
185
|
|
|
$outputStream = fopen('php://memory', 'rwb+'); |
186
|
|
|
|
187
|
|
|
self::assertNotFalse($outputStream); |
188
|
|
|
|
189
|
|
|
$output = new StreamOutput($outputStream); |
190
|
|
|
|
191
|
|
|
$pullRequest1 = new Issue(3, 'Issue #3', 'Test Body', 'https://example.com/3', 'Ocramius', [], true); |
192
|
|
|
$pullRequest2 = new Issue(4, 'Issue #4', 'Test Body', 'https://example.com/4', 'romanb', [], true); |
193
|
|
|
|
194
|
|
|
$issue1 = new Issue(1, 'Issue #1', 'Test Body', 'https://example.com/1', 'jwage', [], false); |
195
|
|
|
$issue1->setLinkedPullRequest($pullRequest1); |
196
|
|
|
|
197
|
|
|
$issue2 = new Issue(2, 'Issue #2', 'Test Body', 'https://example.com/2', 'jwage', [], false); |
198
|
|
|
$issue2->setLinkedPullRequest($pullRequest2); |
199
|
|
|
|
200
|
|
|
$pullRequest1->setLinkedIssue($issue1); |
201
|
|
|
$pullRequest2->setLinkedIssue($issue2); |
202
|
|
|
|
203
|
|
|
$issueGroup = new IssueGroup('', [$pullRequest1]); |
204
|
|
|
$issueGroupNamed = new IssueGroup('Enhancement', [$pullRequest2]); |
205
|
|
|
|
206
|
|
|
$milestoneIssues = [$issue1, $issue2, $pullRequest1, $pullRequest2]; |
207
|
|
|
$issueGroups = [$issueGroup, $issueGroupNamed]; |
208
|
|
|
|
209
|
|
|
$changelogConfig = (new ChangelogConfig($user, $repository, $milestone, [])) |
210
|
|
|
->setShowContributors(true); |
211
|
|
|
|
212
|
|
|
$this->issueRepository->expects(self::once()) |
213
|
|
|
->method('getMilestoneIssues') |
214
|
|
|
->with($changelogConfig) |
215
|
|
|
->willReturn($milestoneIssues); |
216
|
|
|
|
217
|
|
|
$this->issueGrouper->expects(self::once()) |
218
|
|
|
->method('groupIssues') |
219
|
|
|
->with($milestoneIssues) |
220
|
|
|
->willReturn($issueGroups); |
221
|
|
|
|
222
|
|
|
$this->changelogGenerator->generate($changelogConfig, $output); |
223
|
|
|
|
224
|
|
|
rewind($outputStream); |
225
|
|
|
self::assertSame( |
226
|
|
|
<<<'EXPECTED_OUTPUT' |
227
|
|
|
1.0 |
228
|
|
|
=== |
229
|
|
|
|
230
|
|
|
- Total issues resolved: **2** |
231
|
|
|
- Total pull requests resolved: **2** |
232
|
|
|
- Total contributors: **3** |
233
|
|
|
|
234
|
|
|
|
235
|
|
|
|
236
|
|
|
|
237
|
|
|
- [3: Issue #3](https://example.com/3) thanks to @Ocramius and @jwage |
238
|
|
|
|
239
|
|
|
Enhancement |
240
|
|
|
----------- |
241
|
|
|
|
242
|
|
|
- [4: Issue #4](https://example.com/4) thanks to @romanb and @jwage |
243
|
|
|
|
244
|
|
|
Contributors |
245
|
|
|
------------ |
246
|
|
|
|
247
|
|
|
- [@jwage](https://github.com/jwage) |
248
|
|
|
- [@Ocramius](https://github.com/Ocramius) |
249
|
|
|
- [@romanb](https://github.com/romanb) |
250
|
|
|
|
251
|
|
|
|
252
|
|
|
EXPECTED_OUTPUT |
253
|
|
|
, |
254
|
|
|
stream_get_contents($outputStream) |
255
|
|
|
); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
protected function setUp(): void |
259
|
|
|
{ |
260
|
|
|
$this->issueRepository = $this->createMock(IssueRepository::class); |
261
|
|
|
$this->issueGrouper = $this->createMock(IssueGrouper::class); |
262
|
|
|
|
263
|
|
|
$this->changelogGenerator = new ChangelogGenerator( |
264
|
|
|
$this->issueRepository, |
265
|
|
|
$this->issueGrouper |
266
|
|
|
); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|