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\OutputInterface; |
16
|
|
|
|
17
|
|
|
final class ChangelogGeneratorTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
private const USER = 'jwage'; |
20
|
|
|
private const REPOSITORY = 'changelog-generator'; |
21
|
|
|
private const MILESTONE = '1.0'; |
22
|
|
|
|
23
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|IssueRepository */ |
24
|
|
|
private $issueRepository; |
25
|
|
|
|
26
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|IssueGrouper */ |
27
|
|
|
private $issueGrouper; |
28
|
|
|
|
29
|
|
|
/** @var ChangelogGenerator */ |
30
|
|
|
private $changelogGenerator; |
31
|
|
|
|
32
|
|
|
public function testGenerate() : void |
33
|
|
|
{ |
34
|
|
|
[$issue1, $issue2, $pullRequest1, $pullRequest2, $issueGroup, $milestoneIssues, $issueGroups] = $this->arrangeIssues(); |
35
|
|
|
|
36
|
|
|
$changelogConfig = new ChangelogConfig(self::USER, self::REPOSITORY, self::MILESTONE, []); |
37
|
|
|
|
38
|
|
|
$this->issueRepository->expects(self::once()) |
39
|
|
|
->method('getMilestoneIssues') |
40
|
|
|
->with($changelogConfig) |
41
|
|
|
->willReturn($milestoneIssues); |
42
|
|
|
|
43
|
|
|
$this->issueGrouper->expects(self::once()) |
44
|
|
|
->method('groupIssues') |
45
|
|
|
->with($milestoneIssues) |
46
|
|
|
->willReturn($issueGroups); |
47
|
|
|
|
48
|
|
|
$issueGroup->expects(self::once()) |
49
|
|
|
->method('getName') |
50
|
|
|
->willReturn('Enhancement'); |
51
|
|
|
|
52
|
|
|
$issueGroup->expects(self::once()) |
53
|
|
|
->method('getIssues') |
54
|
|
|
->willReturn([$issue1, $issue2]); |
55
|
|
|
|
56
|
|
|
$issue1->expects(self::once()) |
57
|
|
|
->method('render') |
58
|
|
|
->willReturn('Issue #1'); |
59
|
|
|
|
60
|
|
|
$issue1->expects(self::once()) |
61
|
|
|
->method('getUser') |
62
|
|
|
->willReturn('jwage'); |
63
|
|
|
|
64
|
|
|
$issue2->expects(self::once()) |
65
|
|
|
->method('render') |
66
|
|
|
->willReturn('Issue #2'); |
67
|
|
|
|
68
|
|
|
$issue2->expects(self::once()) |
69
|
|
|
->method('getUser') |
70
|
|
|
->willReturn('jwage'); |
71
|
|
|
|
72
|
|
|
$pullRequest1->expects(self::any()) |
73
|
|
|
->method('isPullRequest') |
74
|
|
|
->willReturn(true); |
75
|
|
|
|
76
|
|
|
$pullRequest1->expects(self::once()) |
77
|
|
|
->method('getUser') |
78
|
|
|
->willReturn('Ocramius'); |
79
|
|
|
|
80
|
|
|
$pullRequest2->expects(self::any()) |
81
|
|
|
->method('isPullRequest') |
82
|
|
|
->willReturn(true); |
83
|
|
|
|
84
|
|
|
$pullRequest2->expects(self::once()) |
85
|
|
|
->method('getUser') |
86
|
|
|
->willReturn('romanb'); |
87
|
|
|
|
88
|
|
|
$output = $this->arrangeConsoleOutput(); |
89
|
|
|
|
90
|
|
|
$output->expects(self::at(0)) |
91
|
|
|
->method('writeln') |
92
|
|
|
->with([ |
93
|
|
|
'## 1.0', |
94
|
|
|
'', |
95
|
|
|
'- Total issues resolved: **2**', |
96
|
|
|
'- Total pull requests resolved: **2**', |
97
|
|
|
'- Total contributors: **3**', |
98
|
|
|
]); |
99
|
|
|
|
100
|
|
|
$this->changelogGenerator->generate($changelogConfig, $output); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testGenerateWithDate() : void |
104
|
|
|
{ |
105
|
|
|
[$issue1, $issue2, $pullRequest1, $pullRequest2, $issueGroup, $milestoneIssues, $issueGroups] = $this->arrangeIssues(); |
106
|
|
|
|
107
|
|
|
$changelogConfig = new ChangelogConfig(self::USER, self::REPOSITORY, self::MILESTONE, []); |
108
|
|
|
$changelogConfig->setIncludeDate(true); |
109
|
|
|
|
110
|
|
|
$this->issueRepository->expects(self::once()) |
111
|
|
|
->method('getMilestoneIssues') |
112
|
|
|
->with($changelogConfig) |
113
|
|
|
->willReturn($milestoneIssues); |
114
|
|
|
|
115
|
|
|
$this->issueGrouper->expects(self::once()) |
116
|
|
|
->method('groupIssues') |
117
|
|
|
->with($milestoneIssues) |
118
|
|
|
->willReturn($issueGroups); |
119
|
|
|
|
120
|
|
|
$issueGroup->expects(self::once()) |
121
|
|
|
->method('getName') |
122
|
|
|
->willReturn('Enhancement'); |
123
|
|
|
|
124
|
|
|
$issueGroup->expects(self::once()) |
125
|
|
|
->method('getIssues') |
126
|
|
|
->willReturn([$issue1, $issue2]); |
127
|
|
|
|
128
|
|
|
$issue1->expects(self::once()) |
129
|
|
|
->method('render') |
130
|
|
|
->willReturn('Issue #1'); |
131
|
|
|
|
132
|
|
|
$issue1->expects(self::once()) |
133
|
|
|
->method('getUser') |
134
|
|
|
->willReturn('jwage'); |
135
|
|
|
|
136
|
|
|
$issue2->expects(self::once()) |
137
|
|
|
->method('render') |
138
|
|
|
->willReturn('Issue #2'); |
139
|
|
|
|
140
|
|
|
$issue2->expects(self::once()) |
141
|
|
|
->method('getUser') |
142
|
|
|
->willReturn('jwage'); |
143
|
|
|
|
144
|
|
|
$pullRequest1->expects(self::any()) |
145
|
|
|
->method('isPullRequest') |
146
|
|
|
->willReturn(true); |
147
|
|
|
|
148
|
|
|
$pullRequest1->expects(self::once()) |
149
|
|
|
->method('getUser') |
150
|
|
|
->willReturn('Ocramius'); |
151
|
|
|
|
152
|
|
|
$pullRequest2->expects(self::any()) |
153
|
|
|
->method('isPullRequest') |
154
|
|
|
->willReturn(true); |
155
|
|
|
|
156
|
|
|
$pullRequest2->expects(self::once()) |
157
|
|
|
->method('getUser') |
158
|
|
|
->willReturn('romanb'); |
159
|
|
|
|
160
|
|
|
$output = $this->arrangeConsoleOutput(); |
161
|
|
|
|
162
|
|
|
$output->expects(self::at(0)) |
163
|
|
|
->method('writeln') |
164
|
|
|
->with([ |
165
|
|
|
'## 1.0 - [' . (new \DateTime('now'))->format($changelogConfig->getOption('dateFormat')) . ']', |
166
|
|
|
'', |
167
|
|
|
'- Total issues resolved: **2**', |
168
|
|
|
'- Total pull requests resolved: **2**', |
169
|
|
|
'- Total contributors: **3**', |
170
|
|
|
]); |
171
|
|
|
|
172
|
|
|
$this->changelogGenerator->generate($changelogConfig, $output); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
protected function setUp() : void |
176
|
|
|
{ |
177
|
|
|
$this->issueRepository = $this->createMock(IssueRepository::class); |
178
|
|
|
$this->issueGrouper = $this->createMock(IssueGrouper::class); |
179
|
|
|
|
180
|
|
|
$this->changelogGenerator = new ChangelogGenerator( |
181
|
|
|
$this->issueRepository, |
182
|
|
|
$this->issueGrouper |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return mixed[] |
188
|
|
|
*/ |
189
|
|
|
private function arrangeIssues() : array |
190
|
|
|
{ |
191
|
|
|
$issue1 = $this->createMock(Issue::class); |
192
|
|
|
$issue2 = $this->createMock(Issue::class); |
193
|
|
|
$pullRequest1 = $this->createMock(Issue::class); |
194
|
|
|
$pullRequest2 = $this->createMock(Issue::class); |
195
|
|
|
|
196
|
|
|
$issueGroup = $this->createMock(IssueGroup::class); |
197
|
|
|
|
198
|
|
|
$milestoneIssues = [$issue1, $issue2, $pullRequest1, $pullRequest2]; |
199
|
|
|
$issueGroups = [$issueGroup]; |
200
|
|
|
|
201
|
|
|
return [$issue1, $issue2, $pullRequest1, $pullRequest2, $issueGroup, $milestoneIssues, $issueGroups]; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return MockObject|OutputInterface |
206
|
|
|
*/ |
207
|
|
|
private function arrangeConsoleOutput() |
208
|
|
|
{ |
209
|
|
|
$output = $this->createMock(OutputInterface::class); |
210
|
|
|
|
211
|
|
|
$output->expects(self::at(1)) |
212
|
|
|
->method('writeln') |
213
|
|
|
->with([ |
214
|
|
|
'', |
215
|
|
|
'### Enhancement', |
216
|
|
|
'', |
217
|
|
|
]); |
218
|
|
|
|
219
|
|
|
$output->expects(self::at(2)) |
220
|
|
|
->method('writeln') |
221
|
|
|
->with('Issue #1'); |
222
|
|
|
return $output; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|