Passed
Pull Request — master (#48)
by Marco
02:59
created

testExecuteWhenRevisionsAreProvidedAsOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 9.125
c 0
b 0
f 0
cc 1
eloc 38
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\ApiCompare\Command;
6
7
use Assert\InvalidArgumentException;
8
use PHPUnit\Framework\MockObject\MockObject;
9
use PHPUnit\Framework\TestCase;
10
use Roave\ApiCompare\Change;
11
use Roave\ApiCompare\Changes;
12
use Roave\ApiCompare\Command\ApiCompare;
13
use Roave\ApiCompare\Comparator;
14
use Roave\ApiCompare\Factory\DirectoryReflectorFactory;
15
use Roave\ApiCompare\Git\CheckedOutRepository;
16
use Roave\ApiCompare\Git\GetVersionCollection;
17
use Roave\ApiCompare\Git\ParseRevision;
18
use Roave\ApiCompare\Git\PerformCheckoutOfRevision;
19
use Roave\ApiCompare\Git\PickVersionFromVersionCollection;
20
use Roave\ApiCompare\Git\Revision;
21
use Roave\ApiCompare\LocateDependencies\LocateDependencies;
22
use Roave\BetterReflection\BetterReflection;
23
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
24
use Symfony\Component\Console\Input\InputInterface;
25
use Symfony\Component\Console\Output\ConsoleOutputInterface;
26
use Symfony\Component\Console\Output\OutputInterface;
27
use Version\Version;
28
use Version\VersionsCollection;
29
use function chdir;
30
use function realpath;
31
use function sha1;
32
use function uniqid;
33
34
/**
35
 * @covers \Roave\ApiCompare\Command\ApiCompare
36
 */
37
final class ApiCompareTest extends TestCase
38
{
39
    /** @var CheckedOutRepository */
40
    private $sourceRepository;
41
42
    /** @var InputInterface|MockObject */
43
    private $input;
44
45
    /** @var ConsoleOutputInterface|MockObject */
46
    private $output;
47
48
    /** @var OutputInterface|MockObject */
49
    private $stdErr;
50
51
    /** @var PerformCheckoutOfRevision|MockObject */
52
    private $performCheckout;
53
54
    /** @var ParseRevision|MockObject */
55
    private $parseRevision;
56
57
    /** @var GetVersionCollection|MockObject */
58
    private $getVersions;
59
60
    /** @var PickVersionFromVersionCollection|MockObject */
61
    private $pickVersion;
62
63
    /** @var LocateDependencies|MockObject */
64
    private $locateDependencies;
65
66
    /** @var Comparator|MockObject */
67
    private $comparator;
68
69
    /** @var AggregateSourceLocator */
70
    private $dependencies;
71
72
    /** @var ApiCompare */
73
    private $compare;
74
75
    public function setUp() : void
76
    {
77
        $this->sourceRepository = CheckedOutRepository::fromPath(realpath(__DIR__ . '/../../../'));
78
        chdir((string) $this->sourceRepository);
79
80
        $this->input              = $this->createMock(InputInterface::class);
81
        $this->output             = $this->createMock(ConsoleOutputInterface::class);
82
        $this->stdErr             = $this->createMock(OutputInterface::class);
83
        $this->performCheckout    = $this->createMock(PerformCheckoutOfRevision::class);
84
        $this->parseRevision      = $this->createMock(ParseRevision::class);
85
        $this->getVersions        = $this->createMock(GetVersionCollection::class);
86
        $this->pickVersion        = $this->createMock(PickVersionFromVersionCollection::class);
87
        $this->locateDependencies = $this->createMock(LocateDependencies::class);
88
        $this->dependencies       = new AggregateSourceLocator();
89
        $this->comparator         = $this->createMock(Comparator::class);
90
        $this->compare            = new ApiCompare(
91
            $this->performCheckout,
92
            new DirectoryReflectorFactory((new BetterReflection())->astLocator()),
93
            $this->parseRevision,
94
            $this->getVersions,
95
            $this->pickVersion,
96
            $this->locateDependencies,
97
            $this->comparator
98
        );
99
100
        $this
101
            ->output
102
            ->expects(self::any())
103
            ->method('getErrorOutput')
104
            ->willReturn($this->stdErr);
105
    }
106
107
    public function testExecuteWhenRevisionsAreProvidedAsOptions() : void
108
    {
109
        $fromSha = sha1('fromRevision', false);
110
        $toSha   = sha1('toRevision', false);
111
112
        $this->input->expects(self::any())->method('hasOption')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Symfony\Component\Console\Input\InputInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

112
        $this->input->/** @scrutinizer ignore-call */ 
113
                      expects(self::any())->method('hasOption')->willReturn(true);

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.

Loading history...
113
        $this->input->expects(self::any())->method('getOption')->willReturnMap([
114
            ['from', $fromSha],
115
            ['to', $toSha],
116
        ]);
117
        $this->input->expects(self::any())->method('getArgument')->willReturnMap([
118
            ['sources-path', 'src'],
119
        ]);
120
121
        $this->performCheckout->expects(self::at(0))
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Roave\ApiCompare\Git\PerformCheckoutOfRevision. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
        $this->performCheckout->/** @scrutinizer ignore-call */ 
122
                                expects(self::at(0))

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.

Loading history...
122
            ->method('checkout')
123
            ->with($this->sourceRepository, $fromSha)
124
            ->willReturn($this->sourceRepository);
125
        $this->performCheckout->expects(self::at(1))
126
            ->method('checkout')
127
            ->with($this->sourceRepository, $toSha)
128
            ->willReturn($this->sourceRepository);
129
        $this->performCheckout->expects(self::at(2))
130
            ->method('remove')
131
            ->with($this->sourceRepository);
132
        $this->performCheckout->expects(self::at(3))
133
            ->method('remove')
134
            ->with($this->sourceRepository);
135
136
        $this->parseRevision->expects(self::at(0))
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Roave\ApiCompare\Git\ParseRevision. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

136
        $this->parseRevision->/** @scrutinizer ignore-call */ 
137
                              expects(self::at(0))

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.

Loading history...
137
            ->method('fromStringForRepository')
138
            ->with($fromSha)
139
            ->willReturn(Revision::fromSha1($fromSha));
140
        $this->parseRevision->expects(self::at(1))
141
            ->method('fromStringForRepository')
142
            ->with($toSha)
143
            ->willReturn(Revision::fromSha1($toSha));
144
145
        $this
146
            ->locateDependencies
147
            ->expects(self::any())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Roave\ApiCompare\LocateD...cies\LocateDependencies. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

147
            ->/** @scrutinizer ignore-call */ 
148
              expects(self::any())

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.

Loading history...
148
            ->method('__invoke')
149
            ->with((string) $this->sourceRepository)
150
            ->willReturn($this->dependencies);
151
152
        $this->comparator->expects(self::once())->method('compare')->willReturn(Changes::new());
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Roave\ApiCompare\Comparator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

152
        $this->comparator->/** @scrutinizer ignore-call */ 
153
                           expects(self::once())->method('compare')->willReturn(Changes::new());

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.

Loading history...
153
154
        self::assertSame(0, $this->compare->execute($this->input, $this->output));
155
    }
156
157
    public function testExecuteReturnsNonZeroExitCodeWhenChangesAreDetected() : void
158
    {
159
        $fromSha = sha1('fromRevision', false);
160
        $toSha   = sha1('toRevision', false);
161
162
        $this->input->expects(self::any())->method('hasOption')->willReturn(true);
163
        $this->input->expects(self::any())->method('getOption')->willReturnMap([
164
            ['from', $fromSha],
165
            ['to', $toSha],
166
        ]);
167
        $this->input->expects(self::any())->method('getArgument')->willReturnMap([
168
            ['sources-path', 'src'],
169
        ]);
170
171
        $this->performCheckout->expects(self::at(0))
172
            ->method('checkout')
173
            ->with($this->sourceRepository, $fromSha)
174
            ->willReturn($this->sourceRepository);
175
        $this->performCheckout->expects(self::at(1))
176
            ->method('checkout')
177
            ->with($this->sourceRepository, $toSha)
178
            ->willReturn($this->sourceRepository);
179
        $this->performCheckout->expects(self::at(2))
180
            ->method('remove')
181
            ->with($this->sourceRepository);
182
        $this->performCheckout->expects(self::at(3))
183
            ->method('remove')
184
            ->with($this->sourceRepository);
185
186
        $this->parseRevision->expects(self::at(0))
187
            ->method('fromStringForRepository')
188
            ->with($fromSha)
189
            ->willReturn(Revision::fromSha1($fromSha));
190
        $this->parseRevision->expects(self::at(1))
191
            ->method('fromStringForRepository')
192
            ->with($toSha)
193
            ->willReturn(Revision::fromSha1($toSha));
194
195
        $this
196
            ->locateDependencies
197
            ->expects(self::any())
198
            ->method('__invoke')
199
            ->with((string) $this->sourceRepository)
200
            ->willReturn($this->dependencies);
201
202
        $this->comparator->expects(self::once())->method('compare')->willReturn(Changes::fromArray([
203
            Change::added(uniqid('added', true), true),
204
        ]));
205
206
        self::assertSame(2, $this->compare->execute($this->input, $this->output));
207
    }
208
209
    public function testProvidingMarkdownOptionWritesMarkdownOutput() : void
210
    {
211
        $fromSha = sha1('fromRevision', false);
212
        $toSha   = sha1('toRevision', false);
213
214
        $this->input->expects(self::any())->method('hasOption')->willReturn(true);
215
        $this->input->expects(self::any())->method('getOption')->willReturnMap([
216
            ['from', $fromSha],
217
            ['to', $toSha],
218
            ['format', ['markdown']],
219
        ]);
220
        $this->input->expects(self::any())->method('getArgument')->willReturnMap([
221
            ['sources-path', 'src'],
222
        ]);
223
224
        $this->performCheckout->expects(self::at(0))
225
            ->method('checkout')
226
            ->with($this->sourceRepository, $fromSha)
227
            ->willReturn($this->sourceRepository);
228
        $this->performCheckout->expects(self::at(1))
229
            ->method('checkout')
230
            ->with($this->sourceRepository, $toSha)
231
            ->willReturn($this->sourceRepository);
232
        $this->performCheckout->expects(self::at(2))
233
            ->method('remove')
234
            ->with($this->sourceRepository);
235
        $this->performCheckout->expects(self::at(3))
236
            ->method('remove')
237
            ->with($this->sourceRepository);
238
239
        $this->parseRevision->expects(self::at(0))
240
            ->method('fromStringForRepository')
241
            ->with($fromSha)
242
            ->willReturn(Revision::fromSha1($fromSha));
243
        $this->parseRevision->expects(self::at(1))
244
            ->method('fromStringForRepository')
245
            ->with($toSha)
246
            ->willReturn(Revision::fromSha1($toSha));
247
248
        $this
249
            ->locateDependencies
250
            ->expects(self::any())
251
            ->method('__invoke')
252
            ->with((string) $this->sourceRepository)
253
            ->willReturn($this->dependencies);
254
255
        $changeToExpect = uniqid('changeToExpect', true);
256
        $this->comparator->expects(self::once())->method('compare')->willReturn(Changes::fromArray([
257
            Change::removed($changeToExpect, true),
258
        ]));
259
260
        $this->compare->execute($this->input, $this->output);
261
262
        $this->output->expects(self::any())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Symfony\Component\Consol...\ConsoleOutputInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

262
        $this->output->/** @scrutinizer ignore-call */ 
263
                       expects(self::any())

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.

Loading history...
263
            ->method('writeln')
264
            ->willReturnCallback(function (string $output) use ($changeToExpect) : void {
265
                self::assertContains($changeToExpect, $output);
266
            });
267
    }
268
269
    public function testExecuteWithDefaultRevisionsNotProvided() : void
270
    {
271
        $fromSha       = sha1('fromRevision', false);
272
        $toSha         = sha1('toRevision', false);
273
        $versions      = VersionsCollection::fromArray(['1.0.0', '1.0.1']);
274
        $pickedVersion = Version::fromString('1.0.0');
275
276
        $this->input->expects(self::any())->method('hasOption')->willReturn(false);
277
        $this->input->expects(self::any())->method('getOption')->willReturnMap([
278
            ['from', null],
279
            ['to', 'HEAD'],
280
        ]);
281
        $this->input->expects(self::any())->method('getArgument')->willReturnMap([
282
            ['sources-path', 'src'],
283
        ]);
284
285
        $this->performCheckout->expects(self::at(0))
286
            ->method('checkout')
287
            ->with($this->sourceRepository, $fromSha)
288
            ->willReturn($this->sourceRepository);
289
        $this->performCheckout->expects(self::at(1))
290
            ->method('checkout')
291
            ->with($this->sourceRepository, $toSha)
292
            ->willReturn($this->sourceRepository);
293
        $this->performCheckout->expects(self::at(2))
294
            ->method('remove')
295
            ->with($this->sourceRepository);
296
        $this->performCheckout->expects(self::at(3))
297
            ->method('remove')
298
            ->with($this->sourceRepository);
299
300
        $this->parseRevision->expects(self::at(0))
301
            ->method('fromStringForRepository')
302
            ->with((string) $pickedVersion)
303
            ->willReturn(Revision::fromSha1($fromSha));
304
        $this->parseRevision->expects(self::at(1))
305
            ->method('fromStringForRepository')
306
            ->with('HEAD')
307
            ->willReturn(Revision::fromSha1($toSha));
308
309
        $this->getVersions->expects(self::once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Roave\ApiCompare\Git\GetVersionCollection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

309
        $this->getVersions->/** @scrutinizer ignore-call */ 
310
                            expects(self::once())

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.

Loading history...
310
            ->method('fromRepository')
311
            ->with(self::callback(function (CheckedOutRepository $checkedOutRepository) : bool {
312
                self::assertEquals($this->sourceRepository, $checkedOutRepository);
313
                return true;
314
            }))
315
            ->willReturn($versions);
316
        $this->pickVersion->expects(self::once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Roave\ApiCompare\Git\Pic...onFromVersionCollection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

316
        $this->pickVersion->/** @scrutinizer ignore-call */ 
317
                            expects(self::once())

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.

Loading history...
317
            ->method('forVersions')
318
            ->with($versions)
319
            ->willReturn($pickedVersion);
320
321
        $this->comparator->expects(self::once())->method('compare')->willReturn(Changes::new());
322
323
        self::assertSame(0, $this->compare->execute($this->input, $this->output));
324
    }
325
326
    public function testExecuteFailsIfCheckedOutRepositoryDoesNotExist() : void
327
    {
328
        $fromSha = sha1('fromRevision', false);
329
        $toSha   = sha1('toRevision', false);
330
331
        $this->input->expects(self::any())->method('hasOption')->willReturn(true);
332
        $this->input->expects(self::any())->method('getOption')->willReturnMap([
333
            ['from', $fromSha],
334
            ['to', $toSha],
335
        ]);
336
        $this->input->expects(self::any())->method('getArgument')->willReturnMap([
337
            ['sources-path', uniqid('src', true)],
338
        ]);
339
340
        $this->performCheckout->expects(self::at(0))
341
            ->method('checkout')
342
            ->with($this->sourceRepository, $fromSha)
343
            ->willReturn($this->sourceRepository);
344
        $this->performCheckout->expects(self::at(1))
345
            ->method('checkout')
346
            ->with($this->sourceRepository, $toSha)
347
            ->willReturn($this->sourceRepository);
348
        $this->performCheckout->expects(self::at(2))
349
            ->method('remove')
350
            ->with($this->sourceRepository);
351
        $this->performCheckout->expects(self::at(3))
352
            ->method('remove')
353
            ->with($this->sourceRepository);
354
355
        $this->parseRevision->expects(self::at(0))
356
            ->method('fromStringForRepository')
357
            ->with($fromSha)
358
            ->willReturn(Revision::fromSha1($fromSha));
359
        $this->parseRevision->expects(self::at(1))
360
            ->method('fromStringForRepository')
361
            ->with($toSha)
362
            ->willReturn(Revision::fromSha1($toSha));
363
364
        $this->comparator->expects(self::never())->method('compare');
365
366
        $this->expectException(InvalidArgumentException::class);
367
        $this->compare->execute($this->input, $this->output);
368
    }
369
}
370