1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace RoaveTest\BackwardCompatibility\Command; |
6
|
|
|
|
7
|
|
|
use Assert\AssertionFailedException; |
8
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use Roave\BackwardCompatibility\Change; |
11
|
|
|
use Roave\BackwardCompatibility\Changes; |
12
|
|
|
use Roave\BackwardCompatibility\Command\AssertBackwardsCompatible; |
13
|
|
|
use Roave\BackwardCompatibility\CompareApi; |
14
|
|
|
use Roave\BackwardCompatibility\Factory\ComposerInstallationReflectorFactory; |
15
|
|
|
use Roave\BackwardCompatibility\Git\CheckedOutRepository; |
16
|
|
|
use Roave\BackwardCompatibility\Git\GetVersionCollection; |
17
|
|
|
use Roave\BackwardCompatibility\Git\ParseRevision; |
18
|
|
|
use Roave\BackwardCompatibility\Git\PerformCheckoutOfRevision; |
19
|
|
|
use Roave\BackwardCompatibility\Git\PickVersionFromVersionCollection; |
20
|
|
|
use Roave\BackwardCompatibility\Git\Revision; |
21
|
|
|
use Roave\BackwardCompatibility\LocateDependencies\LocateDependencies; |
22
|
|
|
use Roave\BackwardCompatibility\LocateSources\LocateSourcesViaComposerJson; |
23
|
|
|
use Roave\BetterReflection\BetterReflection; |
24
|
|
|
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator; |
25
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
26
|
|
|
use Symfony\Component\Console\Output\ConsoleOutputInterface; |
27
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
28
|
|
|
use Version\Version; |
29
|
|
|
use Version\VersionsCollection; |
30
|
|
|
use function Safe\chdir; |
31
|
|
|
use function Safe\realpath; |
32
|
|
|
use function sha1; |
33
|
|
|
use function uniqid; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @covers \Roave\BackwardCompatibility\Command\AssertBackwardsCompatible |
37
|
|
|
*/ |
38
|
|
|
final class AssertBackwardsCompatibleTest extends TestCase |
39
|
|
|
{ |
40
|
|
|
/** @var CheckedOutRepository */ |
41
|
|
|
private $sourceRepository; |
42
|
|
|
|
43
|
|
|
/** @var InputInterface&MockObject */ |
44
|
|
|
private $input; |
45
|
|
|
|
46
|
|
|
/** @var ConsoleOutputInterface&MockObject */ |
47
|
|
|
private $output; |
48
|
|
|
|
49
|
|
|
/** @var OutputInterface&MockObject */ |
50
|
|
|
private $stdErr; |
51
|
|
|
|
52
|
|
|
/** @var PerformCheckoutOfRevision&MockObject */ |
53
|
|
|
private $performCheckout; |
54
|
|
|
|
55
|
|
|
/** @var ParseRevision&MockObject */ |
56
|
|
|
private $parseRevision; |
57
|
|
|
|
58
|
|
|
/** @var GetVersionCollection&MockObject */ |
59
|
|
|
private $getVersions; |
60
|
|
|
|
61
|
|
|
/** @var PickVersionFromVersionCollection&MockObject */ |
62
|
|
|
private $pickVersion; |
63
|
|
|
|
64
|
|
|
/** @var LocateDependencies&MockObject */ |
65
|
|
|
private $locateDependencies; |
66
|
|
|
|
67
|
|
|
/** @var CompareApi&MockObject */ |
68
|
|
|
private $compareApi; |
69
|
|
|
|
70
|
|
|
/** @var AggregateSourceLocator */ |
71
|
|
|
private $dependencies; |
72
|
|
|
|
73
|
|
|
/** @var AssertBackwardsCompatible */ |
74
|
|
|
private $compare; |
75
|
|
|
|
76
|
|
|
public function setUp() : void |
77
|
|
|
{ |
78
|
|
|
$repositoryPath = realpath(__DIR__ . '/../../../'); |
79
|
|
|
|
80
|
|
|
$this->sourceRepository = CheckedOutRepository::fromPath($repositoryPath); |
81
|
|
|
|
82
|
|
|
chdir($this->sourceRepository->__toString()); |
83
|
|
|
|
84
|
|
|
$this->input = $this->createMock(InputInterface::class); |
85
|
|
|
$this->output = $this->createMock(ConsoleOutputInterface::class); |
86
|
|
|
$this->stdErr = $this->createMock(OutputInterface::class); |
87
|
|
|
$this->performCheckout = $this->createMock(PerformCheckoutOfRevision::class); |
88
|
|
|
$this->parseRevision = $this->createMock(ParseRevision::class); |
89
|
|
|
$this->getVersions = $this->createMock(GetVersionCollection::class); |
90
|
|
|
$this->pickVersion = $this->createMock(PickVersionFromVersionCollection::class); |
91
|
|
|
$this->locateDependencies = $this->createMock(LocateDependencies::class); |
92
|
|
|
$this->dependencies = new AggregateSourceLocator(); |
93
|
|
|
$this->compareApi = $this->createMock(CompareApi::class); |
94
|
|
|
$this->compare = new AssertBackwardsCompatible( |
95
|
|
|
$this->performCheckout, |
96
|
|
|
new ComposerInstallationReflectorFactory(new LocateSourcesViaComposerJson((new BetterReflection())->astLocator())), |
97
|
|
|
$this->parseRevision, |
98
|
|
|
$this->getVersions, |
99
|
|
|
$this->pickVersion, |
100
|
|
|
$this->locateDependencies, |
101
|
|
|
$this->compareApi |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
$this |
105
|
|
|
->output |
106
|
|
|
->expects(self::any()) |
|
|
|
|
107
|
|
|
->method('getErrorOutput') |
108
|
|
|
->willReturn($this->stdErr); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testDefinition() : void |
112
|
|
|
{ |
113
|
|
|
$usages = $this->compare->getUsages(); |
114
|
|
|
|
115
|
|
|
self::assertCount(1, $usages); |
|
|
|
|
116
|
|
|
self::assertStringStartsWith( |
|
|
|
|
117
|
|
|
'roave-backwards-compatibility-check:assert-backwards-compatible', |
118
|
|
|
$usages[0] |
119
|
|
|
); |
120
|
|
|
self::assertStringContainsString('Without arguments, this command will attempt to detect', $usages[0]); |
|
|
|
|
121
|
|
|
self::assertStringStartsWith('Verifies that the revision being', $this->compare->getDescription()); |
122
|
|
|
|
123
|
|
|
self::assertSame( |
|
|
|
|
124
|
|
|
'[--from [FROM]] [--to TO] [--format [FORMAT]]', |
|
|
|
|
125
|
|
|
$this->compare |
126
|
|
|
->getDefinition() |
127
|
|
|
->getSynopsis() |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testExecuteWhenRevisionsAreProvidedAsOptions() : void |
132
|
|
|
{ |
133
|
|
|
$fromSha = sha1('fromRevision', false); |
134
|
|
|
$toSha = sha1('toRevision', false); |
135
|
|
|
|
136
|
|
|
$this->input->expects(self::any())->method('getOption')->willReturnMap([ |
|
|
|
|
137
|
|
|
['from', $fromSha], |
138
|
|
|
['to', $toSha], |
139
|
|
|
]); |
140
|
|
|
$this->input->expects(self::any())->method('getArgument')->willReturnMap([ |
141
|
|
|
['sources-path', 'src'], |
142
|
|
|
]); |
143
|
|
|
|
144
|
|
|
$this->performCheckout->expects(self::at(0)) |
|
|
|
|
145
|
|
|
->method('checkout') |
146
|
|
|
->with($this->sourceRepository, $fromSha) |
147
|
|
|
->willReturn($this->sourceRepository); |
148
|
|
|
$this->performCheckout->expects(self::at(1)) |
149
|
|
|
->method('checkout') |
150
|
|
|
->with($this->sourceRepository, $toSha) |
151
|
|
|
->willReturn($this->sourceRepository); |
152
|
|
|
$this->performCheckout->expects(self::at(2)) |
153
|
|
|
->method('remove') |
154
|
|
|
->with($this->sourceRepository); |
155
|
|
|
$this->performCheckout->expects(self::at(3)) |
156
|
|
|
->method('remove') |
157
|
|
|
->with($this->sourceRepository); |
158
|
|
|
|
159
|
|
|
$this->parseRevision->expects(self::at(0)) |
|
|
|
|
160
|
|
|
->method('fromStringForRepository') |
161
|
|
|
->with($fromSha) |
162
|
|
|
->willReturn(Revision::fromSha1($fromSha)); |
163
|
|
|
$this->parseRevision->expects(self::at(1)) |
164
|
|
|
->method('fromStringForRepository') |
165
|
|
|
->with($toSha) |
166
|
|
|
->willReturn(Revision::fromSha1($toSha)); |
167
|
|
|
|
168
|
|
|
$this |
169
|
|
|
->locateDependencies |
170
|
|
|
->expects(self::any()) |
|
|
|
|
171
|
|
|
->method('__invoke') |
172
|
|
|
->with((string) $this->sourceRepository) |
173
|
|
|
->willReturn($this->dependencies); |
174
|
|
|
|
175
|
|
|
$this->compareApi->expects(self::once())->method('__invoke')->willReturn(Changes::empty()); |
|
|
|
|
176
|
|
|
|
177
|
|
|
self::assertSame(0, $this->compare->execute($this->input, $this->output)); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function testExecuteReturnsNonZeroExitCodeWhenChangesAreDetected() : void |
181
|
|
|
{ |
182
|
|
|
$fromSha = sha1('fromRevision', false); |
183
|
|
|
$toSha = sha1('toRevision', false); |
184
|
|
|
|
185
|
|
|
$this->input->expects(self::any())->method('getOption')->willReturnMap([ |
186
|
|
|
['from', $fromSha], |
187
|
|
|
['to', $toSha], |
188
|
|
|
]); |
189
|
|
|
$this->input->expects(self::any())->method('getArgument')->willReturnMap([ |
190
|
|
|
['sources-path', 'src'], |
191
|
|
|
]); |
192
|
|
|
|
193
|
|
|
$this->performCheckout->expects(self::at(0)) |
194
|
|
|
->method('checkout') |
195
|
|
|
->with($this->sourceRepository, $fromSha) |
196
|
|
|
->willReturn($this->sourceRepository); |
197
|
|
|
$this->performCheckout->expects(self::at(1)) |
198
|
|
|
->method('checkout') |
199
|
|
|
->with($this->sourceRepository, $toSha) |
200
|
|
|
->willReturn($this->sourceRepository); |
201
|
|
|
$this->performCheckout->expects(self::at(2)) |
202
|
|
|
->method('remove') |
203
|
|
|
->with($this->sourceRepository); |
204
|
|
|
$this->performCheckout->expects(self::at(3)) |
205
|
|
|
->method('remove') |
206
|
|
|
->with($this->sourceRepository); |
207
|
|
|
|
208
|
|
|
$this->parseRevision->expects(self::at(0)) |
209
|
|
|
->method('fromStringForRepository') |
210
|
|
|
->with($fromSha) |
211
|
|
|
->willReturn(Revision::fromSha1($fromSha)); |
212
|
|
|
$this->parseRevision->expects(self::at(1)) |
213
|
|
|
->method('fromStringForRepository') |
214
|
|
|
->with($toSha) |
215
|
|
|
->willReturn(Revision::fromSha1($toSha)); |
216
|
|
|
|
217
|
|
|
$this |
218
|
|
|
->locateDependencies |
219
|
|
|
->expects(self::any()) |
220
|
|
|
->method('__invoke') |
221
|
|
|
->with((string) $this->sourceRepository) |
222
|
|
|
->willReturn($this->dependencies); |
223
|
|
|
|
224
|
|
|
$this->compareApi->expects(self::once())->method('__invoke')->willReturn(Changes::fromList( |
225
|
|
|
Change::added(uniqid('added', true), true) |
226
|
|
|
)); |
227
|
|
|
|
228
|
|
|
$this |
229
|
|
|
->stdErr |
230
|
|
|
->expects(self::exactly(3)) |
|
|
|
|
231
|
|
|
->method('writeln') |
232
|
|
|
->with(self::logicalOr( |
|
|
|
|
233
|
|
|
self::matches('Comparing from %a to %a...'), |
|
|
|
|
234
|
|
|
self::matches('[BC] ADDED: added%a'), |
235
|
|
|
self::matches('<error>1 backwards-incompatible changes detected</error>') |
236
|
|
|
)); |
237
|
|
|
|
238
|
|
|
self::assertSame(3, $this->compare->execute($this->input, $this->output)); |
|
|
|
|
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
public function testProvidingMarkdownOptionWritesMarkdownOutput() : void |
242
|
|
|
{ |
243
|
|
|
$fromSha = sha1('fromRevision', false); |
244
|
|
|
$toSha = sha1('toRevision', false); |
245
|
|
|
|
246
|
|
|
$this->input->expects(self::any())->method('getOption')->willReturnMap([ |
247
|
|
|
['from', $fromSha], |
248
|
|
|
['to', $toSha], |
249
|
|
|
['format', ['markdown']], |
250
|
|
|
]); |
251
|
|
|
$this->input->expects(self::any())->method('getArgument')->willReturnMap([ |
252
|
|
|
['sources-path', 'src'], |
253
|
|
|
]); |
254
|
|
|
|
255
|
|
|
$this->performCheckout->expects(self::at(0)) |
256
|
|
|
->method('checkout') |
257
|
|
|
->with($this->sourceRepository, $fromSha) |
258
|
|
|
->willReturn($this->sourceRepository); |
259
|
|
|
$this->performCheckout->expects(self::at(1)) |
260
|
|
|
->method('checkout') |
261
|
|
|
->with($this->sourceRepository, $toSha) |
262
|
|
|
->willReturn($this->sourceRepository); |
263
|
|
|
$this->performCheckout->expects(self::at(2)) |
264
|
|
|
->method('remove') |
265
|
|
|
->with($this->sourceRepository); |
266
|
|
|
$this->performCheckout->expects(self::at(3)) |
267
|
|
|
->method('remove') |
268
|
|
|
->with($this->sourceRepository); |
269
|
|
|
|
270
|
|
|
$this->parseRevision->expects(self::at(0)) |
271
|
|
|
->method('fromStringForRepository') |
272
|
|
|
->with($fromSha) |
273
|
|
|
->willReturn(Revision::fromSha1($fromSha)); |
274
|
|
|
$this->parseRevision->expects(self::at(1)) |
275
|
|
|
->method('fromStringForRepository') |
276
|
|
|
->with($toSha) |
277
|
|
|
->willReturn(Revision::fromSha1($toSha)); |
278
|
|
|
|
279
|
|
|
$this |
280
|
|
|
->locateDependencies |
281
|
|
|
->method('__invoke') |
282
|
|
|
->with((string) $this->sourceRepository) |
283
|
|
|
->willReturn($this->dependencies); |
|
|
|
|
284
|
|
|
|
285
|
|
|
$changeToExpect = uniqid('changeToExpect', true); |
286
|
|
|
$this->compareApi->expects(self::once())->method('__invoke')->willReturn(Changes::fromList( |
287
|
|
|
Change::removed($changeToExpect, true) |
288
|
|
|
)); |
289
|
|
|
|
290
|
|
|
$this->output |
291
|
|
|
->expects(self::once()) |
|
|
|
|
292
|
|
|
->method('writeln') |
293
|
|
|
->willReturnCallback(static function (string $output) use ($changeToExpect) : void { |
294
|
|
|
self::assertStringContainsString(' [BC] ' . $changeToExpect, $output); |
295
|
|
|
}); |
296
|
|
|
|
297
|
|
|
$this->compare->execute($this->input, $this->output); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
public function testExecuteWithDefaultRevisionsNotProvidedAndNoDetectedTags() : void |
301
|
|
|
{ |
302
|
|
|
$this->input->expects(self::any())->method('getOption')->willReturnMap([ |
303
|
|
|
['from', null], |
304
|
|
|
['to', 'HEAD'], |
305
|
|
|
]); |
306
|
|
|
$this->input->expects(self::any())->method('getArgument')->willReturnMap([ |
307
|
|
|
['sources-path', 'src'], |
308
|
|
|
]); |
309
|
|
|
|
310
|
|
|
$this |
311
|
|
|
->performCheckout |
312
|
|
|
->expects(self::never()) |
|
|
|
|
313
|
|
|
->method('checkout'); |
314
|
|
|
$this |
315
|
|
|
->parseRevision |
316
|
|
|
->expects(self::never()) |
317
|
|
|
->method('fromStringForRepository'); |
318
|
|
|
|
319
|
|
|
$this |
320
|
|
|
->getVersions |
321
|
|
|
->expects(self::once()) |
|
|
|
|
322
|
|
|
->method('fromRepository') |
323
|
|
|
->willReturn(new VersionsCollection()); |
324
|
|
|
$this |
325
|
|
|
->pickVersion |
326
|
|
|
->expects(self::never()) |
|
|
|
|
327
|
|
|
->method('forVersions'); |
328
|
|
|
$this |
329
|
|
|
->compareApi |
330
|
|
|
->expects(self::never()) |
331
|
|
|
->method('__invoke'); |
332
|
|
|
|
333
|
|
|
$this->expectException(AssertionFailedException::class); |
334
|
|
|
|
335
|
|
|
$this->compare->execute($this->input, $this->output); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* @dataProvider validVersionsCollections |
340
|
|
|
*/ |
341
|
|
|
public function testExecuteWithDefaultRevisionsNotProvided(VersionsCollection $versions) : void |
342
|
|
|
{ |
343
|
|
|
$fromSha = sha1('fromRevision', false); |
344
|
|
|
$toSha = sha1('toRevision', false); |
345
|
|
|
$pickedVersion = Version::fromString('1.0.0'); |
346
|
|
|
|
347
|
|
|
$this->input->expects(self::any())->method('getOption')->willReturnMap([ |
348
|
|
|
['from', null], |
349
|
|
|
['to', 'HEAD'], |
350
|
|
|
]); |
351
|
|
|
$this->input->expects(self::any())->method('getArgument')->willReturnMap([ |
352
|
|
|
['sources-path', 'src'], |
353
|
|
|
]); |
354
|
|
|
|
355
|
|
|
$this->performCheckout->expects(self::at(0)) |
356
|
|
|
->method('checkout') |
357
|
|
|
->with($this->sourceRepository, $fromSha) |
358
|
|
|
->willReturn($this->sourceRepository); |
359
|
|
|
$this->performCheckout->expects(self::at(1)) |
360
|
|
|
->method('checkout') |
361
|
|
|
->with($this->sourceRepository, $toSha) |
362
|
|
|
->willReturn($this->sourceRepository); |
363
|
|
|
$this->performCheckout->expects(self::at(2)) |
364
|
|
|
->method('remove') |
365
|
|
|
->with($this->sourceRepository); |
366
|
|
|
$this->performCheckout->expects(self::at(3)) |
367
|
|
|
->method('remove') |
368
|
|
|
->with($this->sourceRepository); |
369
|
|
|
|
370
|
|
|
$this->parseRevision->expects(self::at(0)) |
371
|
|
|
->method('fromStringForRepository') |
372
|
|
|
->with((string) $pickedVersion) |
373
|
|
|
->willReturn(Revision::fromSha1($fromSha)); |
374
|
|
|
$this->parseRevision->expects(self::at(1)) |
375
|
|
|
->method('fromStringForRepository') |
376
|
|
|
->with('HEAD') |
377
|
|
|
->willReturn(Revision::fromSha1($toSha)); |
378
|
|
|
|
379
|
|
|
$this->getVersions->expects(self::once()) |
380
|
|
|
->method('fromRepository') |
381
|
|
|
->with(self::callback(function (CheckedOutRepository $checkedOutRepository) : bool { |
|
|
|
|
382
|
|
|
self::assertEquals($this->sourceRepository, $checkedOutRepository); |
|
|
|
|
383
|
|
|
|
384
|
|
|
return true; |
385
|
|
|
})) |
386
|
|
|
->willReturn($versions); |
387
|
|
|
$this->pickVersion->expects(self::once()) |
388
|
|
|
->method('forVersions') |
389
|
|
|
->with($versions) |
390
|
|
|
->willReturn($pickedVersion); |
391
|
|
|
|
392
|
|
|
$this |
393
|
|
|
->stdErr |
394
|
|
|
->expects(self::exactly(3)) |
395
|
|
|
->method('writeln') |
396
|
|
|
->with(self::logicalOr( |
397
|
|
|
'Detected last minor version: 1.0.0', |
398
|
|
|
self::matches('Comparing from %a to %a...'), |
399
|
|
|
self::matches('<info>No backwards-incompatible changes detected</info>') |
400
|
|
|
)); |
401
|
|
|
|
402
|
|
|
$this->compareApi->expects(self::once())->method('__invoke')->willReturn(Changes::empty()); |
403
|
|
|
|
404
|
|
|
self::assertSame(0, $this->compare->execute($this->input, $this->output)); |
|
|
|
|
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** @return VersionsCollection[][] */ |
408
|
|
|
public function validVersionsCollections() : array |
409
|
|
|
{ |
410
|
|
|
return [ |
411
|
|
|
[new VersionsCollection( |
412
|
|
|
Version::fromString('1.0.0'), |
413
|
|
|
Version::fromString('1.0.1'), |
414
|
|
|
Version::fromString('1.0.2') |
415
|
|
|
), |
416
|
|
|
], |
417
|
|
|
[new VersionsCollection( |
418
|
|
|
Version::fromString('1.0.0'), |
419
|
|
|
Version::fromString('1.0.1') |
420
|
|
|
), |
421
|
|
|
], |
422
|
|
|
[new VersionsCollection(Version::fromString('1.0.0'))], |
423
|
|
|
]; |
424
|
|
|
} |
425
|
|
|
} |
426
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.