Completed
Push — master ( d83206...aa4419 )
by Marco
10s
created

testExecuteWithDefaultRevisionsNotProvided()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 55
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 44
nc 1
nop 0
dl 0
loc 55
c 0
b 0
f 0
cc 1
rs 9.7692

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Command\ApiCompare;
9
use PHPUnit\Framework\TestCase;
10
use Roave\ApiCompare\Factory\DirectoryReflectorFactory;
11
use Roave\ApiCompare\Git\CheckedOutRepository;
12
use Roave\ApiCompare\Git\GetVersionCollection;
13
use Roave\ApiCompare\Git\ParseRevision;
14
use Roave\ApiCompare\Git\PerformCheckoutOfRevision;
15
use Roave\ApiCompare\Git\PickVersionFromVersionCollection;
16
use Roave\ApiCompare\Git\Revision;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Version\Version;
20
use Version\VersionsCollection;
21
22
/**
23
 * @covers \Roave\ApiCompare\Command\ApiCompare
24
 */
25
final class ApiCompareTest extends TestCase
26
{
27
    /** @var CheckedOutRepository */
28
    private $sourceRepository;
29
30
    /** @var InputInterface|MockObject */
31
    private $input;
32
33
    /** @var OutputInterface|MockObject */
34
    private $output;
35
36
    /** @var PerformCheckoutOfRevision|MockObject */
37
    private $performCheckout;
38
39
    /** @var ParseRevision|MockObject */
40
    private $parseRevision;
41
42
    /** @var GetVersionCollection|MockObject */
43
    private $getVersions;
44
45
    /** @var PickVersionFromVersionCollection|MockObject */
46
    private $pickVersion;
47
48
    /** @var ApiCompare */
49
    private $compare;
50
51
    public function setUp() : void
52
    {
53
        $this->sourceRepository = CheckedOutRepository::fromPath(realpath(__DIR__ . '/../../../'));
54
        $this->input = $this->createMock(InputInterface::class);
55
        $this->output = $this->createMock(OutputInterface::class);
56
        $this->performCheckout = $this->createMock(PerformCheckoutOfRevision::class);
57
        $this->parseRevision = $this->createMock(ParseRevision::class);
58
        $this->getVersions = $this->createMock(GetVersionCollection::class);
59
        $this->pickVersion = $this->createMock(PickVersionFromVersionCollection::class);
60
        $this->compare = new ApiCompare(
61
            $this->performCheckout,
62
            new DirectoryReflectorFactory(),
63
            $this->parseRevision,
64
            $this->getVersions,
65
            $this->pickVersion
66
        );
67
    }
68
69
    public function testExecuteWhenRevisionsAreProvidedAsOptions() : void
70
    {
71
        $fromSha = sha1('fromRevision', false);
72
        $toSha = sha1('toRevision', false);
73
74
        $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

74
        $this->input->/** @scrutinizer ignore-call */ 
75
                      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...
75
        $this->input->expects(self::any())->method('getOption')->willReturnMap([
76
            ['from', $fromSha],
77
            ['to', $toSha],
78
        ]);
79
        $this->input->expects(self::any())->method('getArgument')->willReturnMap([
80
            ['sources-path', 'src'],
81
        ]);
82
83
        $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

83
        $this->performCheckout->/** @scrutinizer ignore-call */ 
84
                                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...
84
            ->method('checkout')
85
            ->with($this->sourceRepository, $fromSha)
86
            ->willReturn($this->sourceRepository);
87
        $this->performCheckout->expects(self::at(1))
88
            ->method('checkout')
89
            ->with($this->sourceRepository, $toSha)
90
            ->willReturn($this->sourceRepository);
91
        $this->performCheckout->expects(self::at(2))
92
            ->method('remove')
93
            ->with($this->sourceRepository);
94
        $this->performCheckout->expects(self::at(3))
95
            ->method('remove')
96
            ->with($this->sourceRepository);
97
98
        $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

98
        $this->parseRevision->/** @scrutinizer ignore-call */ 
99
                              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...
99
            ->method('fromStringForRepository')
100
            ->with($fromSha)
101
            ->willReturn(Revision::fromSha1($fromSha));
102
        $this->parseRevision->expects(self::at(1))
103
            ->method('fromStringForRepository')
104
            ->with($toSha)
105
            ->willReturn(Revision::fromSha1($toSha));
106
107
        chdir((string)$this->sourceRepository);
108
109
        $this->compare->execute($this->input, $this->output);
110
    }
111
112
    public function testExecuteWithDefaultRevisionsNotProvided() : void
113
    {
114
        $fromSha = sha1('fromRevision', false);
115
        $toSha = sha1('toRevision', false);
116
        $versions = VersionsCollection::fromArray(['1.0.0', '1.0.1']);
117
        $pickedVersion = Version::fromString('1.0.0');
118
119
        $this->input->expects(self::any())->method('hasOption')->willReturn(false);
120
        $this->input->expects(self::any())->method('getOption')->willReturnMap([
121
            ['from', null],
122
            ['to', 'HEAD'],
123
        ]);
124
        $this->input->expects(self::any())->method('getArgument')->willReturnMap([
125
            ['sources-path', 'src'],
126
        ]);
127
128
        $this->performCheckout->expects(self::at(0))
129
            ->method('checkout')
130
            ->with($this->sourceRepository, $fromSha)
131
            ->willReturn($this->sourceRepository);
132
        $this->performCheckout->expects(self::at(1))
133
            ->method('checkout')
134
            ->with($this->sourceRepository, $toSha)
135
            ->willReturn($this->sourceRepository);
136
        $this->performCheckout->expects(self::at(2))
137
            ->method('remove')
138
            ->with($this->sourceRepository);
139
        $this->performCheckout->expects(self::at(3))
140
            ->method('remove')
141
            ->with($this->sourceRepository);
142
143
        $this->parseRevision->expects(self::at(0))
144
            ->method('fromStringForRepository')
145
            ->with((string)$pickedVersion)
146
            ->willReturn(Revision::fromSha1($fromSha));
147
        $this->parseRevision->expects(self::at(1))
148
            ->method('fromStringForRepository')
149
            ->with('HEAD')
150
            ->willReturn(Revision::fromSha1($toSha));
151
152
        $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

152
        $this->getVersions->/** @scrutinizer ignore-call */ 
153
                            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...
153
            ->method('fromRepository')
154
            ->with(self::callback(function (CheckedOutRepository $checkedOutRepository) : bool {
155
                self::assertEquals($this->sourceRepository, $checkedOutRepository);
156
                return true;
157
            }))
158
            ->willReturn($versions);
159
        $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

159
        $this->pickVersion->/** @scrutinizer ignore-call */ 
160
                            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...
160
            ->method('forVersions')
161
            ->with($versions)
162
            ->willReturn($pickedVersion);
163
164
        chdir((string)$this->sourceRepository);
165
166
        $this->compare->execute($this->input, $this->output);
167
    }
168
169
    public function testExecuteFailsIfCheckedOutRepositoryDoesNotExist() : void
170
    {
171
        $fromSha = sha1('fromRevision', false);
172
        $toSha = sha1('toRevision', false);
173
174
        $this->input->expects(self::any())->method('hasOption')->willReturn(true);
175
        $this->input->expects(self::any())->method('getOption')->willReturnMap([
176
            ['from', $fromSha],
177
            ['to', $toSha],
178
        ]);
179
        $this->input->expects(self::any())->method('getArgument')->willReturnMap([
180
            ['sources-path', uniqid('src', true)],
181
        ]);
182
183
        $this->performCheckout->expects(self::at(0))
184
            ->method('checkout')
185
            ->with($this->sourceRepository, $fromSha)
186
            ->willReturn($this->sourceRepository);
187
        $this->performCheckout->expects(self::at(1))
188
            ->method('checkout')
189
            ->with($this->sourceRepository, $toSha)
190
            ->willReturn($this->sourceRepository);
191
        $this->performCheckout->expects(self::at(2))
192
            ->method('remove')
193
            ->with($this->sourceRepository);
194
        $this->performCheckout->expects(self::at(3))
195
            ->method('remove')
196
            ->with($this->sourceRepository);
197
198
        $this->parseRevision->expects(self::at(0))
199
            ->method('fromStringForRepository')
200
            ->with($fromSha)
201
            ->willReturn(Revision::fromSha1($fromSha));
202
        $this->parseRevision->expects(self::at(1))
203
            ->method('fromStringForRepository')
204
            ->with($toSha)
205
            ->willReturn(Revision::fromSha1($toSha));
206
207
        chdir((string)$this->sourceRepository);
208
209
        $this->expectException(InvalidArgumentException::class);
210
        $this->compare->execute($this->input, $this->output);
211
    }
212
}
213