Completed
Push — master ( 7e0f99...47534f )
by Marco
10s
created

testExecuteWhenRevisionsAreProvidedAsOptions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 32
nc 1
nop 0
dl 0
loc 41
c 0
b 0
f 0
cc 1
rs 8.8571
1
<?php
2
declare(strict_types=1);
3
4
namespace RoaveTest\ApiCompare\Command;
5
6
use Assert\InvalidArgumentException;
7
use PHPUnit\Framework\MockObject\MockObject;
8
use Roave\ApiCompare\Change;
9
use Roave\ApiCompare\Changes;
10
use Roave\ApiCompare\Command\ApiCompare;
11
use PHPUnit\Framework\TestCase;
12
use Roave\ApiCompare\Comparator;
13
use Roave\ApiCompare\Factory\DirectoryReflectorFactory;
14
use Roave\ApiCompare\Git\CheckedOutRepository;
15
use Roave\ApiCompare\Git\GetVersionCollection;
16
use Roave\ApiCompare\Git\ParseRevision;
17
use Roave\ApiCompare\Git\PerformCheckoutOfRevision;
18
use Roave\ApiCompare\Git\PickVersionFromVersionCollection;
19
use Roave\ApiCompare\Git\Revision;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Version\Version;
23
use Version\VersionsCollection;
24
25
/**
26
 * @covers \Roave\ApiCompare\Command\ApiCompare
27
 */
28
final class ApiCompareTest extends TestCase
29
{
30
    /** @var CheckedOutRepository */
31
    private $sourceRepository;
32
33
    /** @var InputInterface|MockObject */
34
    private $input;
35
36
    /** @var OutputInterface|MockObject */
37
    private $output;
38
39
    /** @var PerformCheckoutOfRevision|MockObject */
40
    private $performCheckout;
41
42
    /** @var ParseRevision|MockObject */
43
    private $parseRevision;
44
45
    /** @var GetVersionCollection|MockObject */
46
    private $getVersions;
47
48
    /** @var PickVersionFromVersionCollection|MockObject */
49
    private $pickVersion;
50
51
    /** @var Comparator|MockObject */
52
    private $comparator;
53
54
    /** @var ApiCompare */
55
    private $compare;
56
57
    public function setUp() : void
58
    {
59
        $this->sourceRepository = CheckedOutRepository::fromPath(realpath(__DIR__ . '/../../../'));
60
        chdir((string)$this->sourceRepository);
61
62
        $this->input = $this->createMock(InputInterface::class);
63
        $this->output = $this->createMock(OutputInterface::class);
64
        $this->performCheckout = $this->createMock(PerformCheckoutOfRevision::class);
65
        $this->parseRevision = $this->createMock(ParseRevision::class);
66
        $this->getVersions = $this->createMock(GetVersionCollection::class);
67
        $this->pickVersion = $this->createMock(PickVersionFromVersionCollection::class);
68
        $this->comparator = $this->createMock(Comparator::class);
69
        $this->compare = new ApiCompare(
70
            $this->performCheckout,
71
            new DirectoryReflectorFactory(),
72
            $this->parseRevision,
73
            $this->getVersions,
74
            $this->pickVersion,
75
            $this->comparator
76
        );
77
    }
78
79
    public function testExecuteWhenRevisionsAreProvidedAsOptions() : void
80
    {
81
        $fromSha = sha1('fromRevision', false);
82
        $toSha = sha1('toRevision', false);
83
84
        $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

84
        $this->input->/** @scrutinizer ignore-call */ 
85
                      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...
85
        $this->input->expects(self::any())->method('getOption')->willReturnMap([
86
            ['from', $fromSha],
87
            ['to', $toSha],
88
        ]);
89
        $this->input->expects(self::any())->method('getArgument')->willReturnMap([
90
            ['sources-path', 'src'],
91
        ]);
92
93
        $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

93
        $this->performCheckout->/** @scrutinizer ignore-call */ 
94
                                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...
94
            ->method('checkout')
95
            ->with($this->sourceRepository, $fromSha)
96
            ->willReturn($this->sourceRepository);
97
        $this->performCheckout->expects(self::at(1))
98
            ->method('checkout')
99
            ->with($this->sourceRepository, $toSha)
100
            ->willReturn($this->sourceRepository);
101
        $this->performCheckout->expects(self::at(2))
102
            ->method('remove')
103
            ->with($this->sourceRepository);
104
        $this->performCheckout->expects(self::at(3))
105
            ->method('remove')
106
            ->with($this->sourceRepository);
107
108
        $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

108
        $this->parseRevision->/** @scrutinizer ignore-call */ 
109
                              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...
109
            ->method('fromStringForRepository')
110
            ->with($fromSha)
111
            ->willReturn(Revision::fromSha1($fromSha));
112
        $this->parseRevision->expects(self::at(1))
113
            ->method('fromStringForRepository')
114
            ->with($toSha)
115
            ->willReturn(Revision::fromSha1($toSha));
116
117
        $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

117
        $this->comparator->/** @scrutinizer ignore-call */ 
118
                           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...
118
119
        self::assertSame(0, $this->compare->execute($this->input, $this->output));
120
    }
121
122
    public function testExecuteReturnsNonZeroExitCodeWhenChangesAreDetected() : void
123
    {
124
        $fromSha = sha1('fromRevision', false);
125
        $toSha = sha1('toRevision', false);
126
127
        $this->input->expects(self::any())->method('hasOption')->willReturn(true);
128
        $this->input->expects(self::any())->method('getOption')->willReturnMap([
129
            ['from', $fromSha],
130
            ['to', $toSha],
131
        ]);
132
        $this->input->expects(self::any())->method('getArgument')->willReturnMap([
133
            ['sources-path', 'src'],
134
        ]);
135
136
        $this->performCheckout->expects(self::at(0))
137
            ->method('checkout')
138
            ->with($this->sourceRepository, $fromSha)
139
            ->willReturn($this->sourceRepository);
140
        $this->performCheckout->expects(self::at(1))
141
            ->method('checkout')
142
            ->with($this->sourceRepository, $toSha)
143
            ->willReturn($this->sourceRepository);
144
        $this->performCheckout->expects(self::at(2))
145
            ->method('remove')
146
            ->with($this->sourceRepository);
147
        $this->performCheckout->expects(self::at(3))
148
            ->method('remove')
149
            ->with($this->sourceRepository);
150
151
        $this->parseRevision->expects(self::at(0))
152
            ->method('fromStringForRepository')
153
            ->with($fromSha)
154
            ->willReturn(Revision::fromSha1($fromSha));
155
        $this->parseRevision->expects(self::at(1))
156
            ->method('fromStringForRepository')
157
            ->with($toSha)
158
            ->willReturn(Revision::fromSha1($toSha));
159
160
        $this->comparator->expects(self::once())->method('compare')->willReturn(Changes::fromArray([
161
            Change::added(uniqid('added', true), true),
162
            Change::removed(uniqid('removed', true), true)
163
        ]));
164
165
        self::assertSame(2, $this->compare->execute($this->input, $this->output));
166
    }
167
168
    public function testExecuteWithDefaultRevisionsNotProvided() : void
169
    {
170
        $fromSha = sha1('fromRevision', false);
171
        $toSha = sha1('toRevision', false);
172
        $versions = VersionsCollection::fromArray(['1.0.0', '1.0.1']);
173
        $pickedVersion = Version::fromString('1.0.0');
174
175
        $this->input->expects(self::any())->method('hasOption')->willReturn(false);
176
        $this->input->expects(self::any())->method('getOption')->willReturnMap([
177
            ['from', null],
178
            ['to', 'HEAD'],
179
        ]);
180
        $this->input->expects(self::any())->method('getArgument')->willReturnMap([
181
            ['sources-path', 'src'],
182
        ]);
183
184
        $this->performCheckout->expects(self::at(0))
185
            ->method('checkout')
186
            ->with($this->sourceRepository, $fromSha)
187
            ->willReturn($this->sourceRepository);
188
        $this->performCheckout->expects(self::at(1))
189
            ->method('checkout')
190
            ->with($this->sourceRepository, $toSha)
191
            ->willReturn($this->sourceRepository);
192
        $this->performCheckout->expects(self::at(2))
193
            ->method('remove')
194
            ->with($this->sourceRepository);
195
        $this->performCheckout->expects(self::at(3))
196
            ->method('remove')
197
            ->with($this->sourceRepository);
198
199
        $this->parseRevision->expects(self::at(0))
200
            ->method('fromStringForRepository')
201
            ->with((string)$pickedVersion)
202
            ->willReturn(Revision::fromSha1($fromSha));
203
        $this->parseRevision->expects(self::at(1))
204
            ->method('fromStringForRepository')
205
            ->with('HEAD')
206
            ->willReturn(Revision::fromSha1($toSha));
207
208
        $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

208
        $this->getVersions->/** @scrutinizer ignore-call */ 
209
                            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...
209
            ->method('fromRepository')
210
            ->with(self::callback(function (CheckedOutRepository $checkedOutRepository) : bool {
211
                self::assertEquals($this->sourceRepository, $checkedOutRepository);
212
                return true;
213
            }))
214
            ->willReturn($versions);
215
        $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

215
        $this->pickVersion->/** @scrutinizer ignore-call */ 
216
                            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...
216
            ->method('forVersions')
217
            ->with($versions)
218
            ->willReturn($pickedVersion);
219
220
        $this->comparator->expects(self::once())->method('compare')->willReturn(Changes::new());
221
222
        self::assertSame(0, $this->compare->execute($this->input, $this->output));
223
    }
224
225
    public function testExecuteFailsIfCheckedOutRepositoryDoesNotExist() : void
226
    {
227
        $fromSha = sha1('fromRevision', false);
228
        $toSha = sha1('toRevision', false);
229
230
        $this->input->expects(self::any())->method('hasOption')->willReturn(true);
231
        $this->input->expects(self::any())->method('getOption')->willReturnMap([
232
            ['from', $fromSha],
233
            ['to', $toSha],
234
        ]);
235
        $this->input->expects(self::any())->method('getArgument')->willReturnMap([
236
            ['sources-path', uniqid('src', true)],
237
        ]);
238
239
        $this->performCheckout->expects(self::at(0))
240
            ->method('checkout')
241
            ->with($this->sourceRepository, $fromSha)
242
            ->willReturn($this->sourceRepository);
243
        $this->performCheckout->expects(self::at(1))
244
            ->method('checkout')
245
            ->with($this->sourceRepository, $toSha)
246
            ->willReturn($this->sourceRepository);
247
        $this->performCheckout->expects(self::at(2))
248
            ->method('remove')
249
            ->with($this->sourceRepository);
250
        $this->performCheckout->expects(self::at(3))
251
            ->method('remove')
252
            ->with($this->sourceRepository);
253
254
        $this->parseRevision->expects(self::at(0))
255
            ->method('fromStringForRepository')
256
            ->with($fromSha)
257
            ->willReturn(Revision::fromSha1($fromSha));
258
        $this->parseRevision->expects(self::at(1))
259
            ->method('fromStringForRepository')
260
            ->with($toSha)
261
            ->willReturn(Revision::fromSha1($toSha));
262
263
        $this->comparator->expects(self::never())->method('compare');
264
265
        $this->expectException(InvalidArgumentException::class);
266
        $this->compare->execute($this->input, $this->output);
267
    }
268
}
269