GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ProjectVersionServiceTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace KamiYang\ProjectVersion\Tests\Unit\Service;
5
6
/*
7
 * This file is part of the ProjectVersion project.
8
 *
9
 * It is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * For the full copyright and license information, please read
15
 * LICENSE file that was distributed with this source code.
16
 */
17
18
use KamiYang\ProjectVersion\Configuration\ExtensionConfiguration;
19
use KamiYang\ProjectVersion\Enumeration\GitCommandEnumeration;
20
use KamiYang\ProjectVersion\Enumeration\ProjectVersionModeEnumeration;
21
use KamiYang\ProjectVersion\Facade\CommandUtilityFacade;
22
use KamiYang\ProjectVersion\Facade\SystemEnvironmentBuilderFacade;
23
use KamiYang\ProjectVersion\Service\ProjectVersion;
24
use KamiYang\ProjectVersion\Service\ProjectVersionService;
25
use Nimut\TestingFramework\TestCase\UnitTestCase;
26
use Prophecy\Argument;
27
use TYPO3\CMS\Core\Utility\GeneralUtility;
28
29
/**
30
 * Class ProjectVersionServiceTest
31
 */
32
class ProjectVersionServiceTest extends UnitTestCase
33
{
34
    /**
35
     * @var \KamiYang\ProjectVersion\Service\ProjectVersionService
36
     */
37
    private $subject;
38
39
    private $extensionConfiguration = [
40
        'gitFormat' => GitCommandEnumeration::FORMAT_REVISION_BRANCH,
41
        'mode' => ProjectVersionModeEnumeration::FILE,
42
        'staticVersion' => '',
43
        'versionFilePath' => 'VERSION'
44
    ];
45
46
    /**
47
     * @var \KamiYang\ProjectVersion\Facade\SystemEnvironmentBuilderFacade|\Prophecy\Prophecy\ObjectProphecy
48
     */
49
    private $systemEnvironmentBuilderFacadeProphecy;
50
51
    /**
52
     * @var \KamiYang\ProjectVersion\Facade\CommandUtilityFacade
53
     */
54
    private $commandUtilityFacadeProphecy;
55
56
    /**
57
     * @test
58
     */
59
    public function getProjectVersionShouldNotSetProjectVersionIfVersionFileIsNotFound()
60
    {
61
        $this->extensionConfiguration['versionFilePath'] = '/some/not/existing/path';
62
        $this->setUpExtensionConfiguration();
63
64
        $projectVersionProphecy = $this->prophesize(ProjectVersion::class);
65
        GeneralUtility::setSingletonInstance(ProjectVersion::class, $projectVersionProphecy->reveal());
66
67
        $this->subject->getProjectVersion();
68
69
        $projectVersionProphecy->setVersion(Argument::any())
70
            ->shouldNotHaveBeenCalled();
71
    }
72
73
    /**
74
     * @test
75
     * @param string $versionFilePath
76
     * @dataProvider versionFilePathDataProvider
77
     */
78
    public function getProjectVersionShouldSetVersionFromVersionFileIfFileExists(string $versionFilePath)
79
    {
80
        $this->extensionConfiguration['versionFilePath'] = $versionFilePath;
81
        $this->setUpExtensionConfiguration();
82
83
        $projectVersionProphecy = $this->prophesize(ProjectVersion::class);
84
        GeneralUtility::setSingletonInstance(ProjectVersion::class, $projectVersionProphecy->reveal());
85
86
        $this->subject->getProjectVersion();
87
88
        $projectVersionProphecy->setVersion(Argument::containingString('1.0.1'))
89
            ->shouldHaveBeenCalledTimes(1);
90
    }
91
92
    /**
93
     * @return array
94
     */
95
    public function versionFilePathDataProvider(): array
96
    {
97
        return [
98
            'version file with EXT shortcut' => [
99
                'EXT:project_version/Tests/Fixture/VERSION'
100
            ],
101
            'directory with EXT shortcut' => [
102
                'EXT:project_version/Tests/Fixture/'
103
            ],
104
            'Version file with EXT shortcut and different filename' => [
105
                'EXT:project_version/Tests/Fixture/VersionFileWithDifferentName'
106
            ]
107
        ];
108
    }
109
110
    /**
111
     * @test
112
     */
113
    public function getProjectVersionShouldNotSetVersionFromGitIfCommandExecIsNotAvailable()
114
    {
115
        $this->extensionConfiguration['mode'] = ProjectVersionModeEnumeration::GIT;
116
        $this->setUpExtensionConfiguration();
117
118
        $projectVersionProphecy = $this->prophesize(ProjectVersion::class);
119
        GeneralUtility::setSingletonInstance(ProjectVersion::class, $projectVersionProphecy->reveal());
120
121
        $this->systemEnvironmentBuilderFacadeProphecy->isFunctionDisabled('exec')
122
            ->willReturn(true);
123
124
        $this->subject->getProjectVersion();
125
126
        $projectVersionProphecy->setVersion(Argument::any())
127
            ->shouldNotHaveBeenCalled();
128
    }
129
130
    /**
131
     * @test
132
     *
133
     * @param string $format
134
     * @param string $branch
135
     * @param string $revision
136
     * @param string $tag
137
     * @param string $expected
138
     *
139
     * @dataProvider gitFormatDataProvider
140
     */
141
    public function getProjectVersionShouldReturnSpecifiedVersionBasedOnConfiguredGitFormat(
142
        string $format,
143
        string $branch,
144
        string $revision,
145
        string $tag,
146
        string $expected
147
    ) {
148
        // Arrange
149
        $this->extensionConfiguration['mode'] = ProjectVersionModeEnumeration::GIT;
150
        $this->extensionConfiguration['gitFormat'] = $format;
151
        $this->setUpExtensionConfiguration();
152
153
        $projectVersionProphecy = $this->prophesize(ProjectVersion::class);
154
        GeneralUtility::setSingletonInstance(ProjectVersion::class, $projectVersionProphecy->reveal());
155
156
        $this->commandUtilityFacadeProphecy->exec(GitCommandEnumeration::CMD_BRANCH)->willReturn($branch);
157
        $this->commandUtilityFacadeProphecy->exec(GitCommandEnumeration::CMD_REVISION)->willReturn($revision);
158
        $this->commandUtilityFacadeProphecy->exec(GitCommandEnumeration::CMD_TAG)->willReturn($tag);
159
160
        /** @var \KamiYang\ProjectVersion\Service\ProjectVersionService|\PHPUnit\Framework\MockObject\MockObject $subject */
161
        $subject = $this->createPartialMock(ProjectVersionService::class, ['isGitAvailable']);
162
        $subject->method('isGitAvailable')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

162
        $subject->/** @scrutinizer ignore-call */ 
163
                  method('isGitAvailable')->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...
163
        $subject->injectCommandUtilityFacade($this->commandUtilityFacadeProphecy->reveal());
0 ignored issues
show
Bug introduced by
The method injectCommandUtilityFacade() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

163
        $subject->/** @scrutinizer ignore-call */ 
164
                  injectCommandUtilityFacade($this->commandUtilityFacadeProphecy->reveal());

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...
Bug introduced by
The method reveal() does not exist on KamiYang\ProjectVersion\...de\CommandUtilityFacade. ( Ignorable by Annotation )

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

163
        $subject->injectCommandUtilityFacade($this->commandUtilityFacadeProphecy->/** @scrutinizer ignore-call */ reveal());

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...
164
        $subject->injectSystemEnvironmentBuilderFacade($this->systemEnvironmentBuilderFacadeProphecy->reveal());
0 ignored issues
show
Bug introduced by
The method injectSystemEnvironmentBuilderFacade() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

164
        $subject->/** @scrutinizer ignore-call */ 
165
                  injectSystemEnvironmentBuilderFacade($this->systemEnvironmentBuilderFacadeProphecy->reveal());

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...
165
166
        // Act
167
        $actual = $subject->getProjectVersion();
0 ignored issues
show
Unused Code introduced by
The assignment to $actual is dead and can be removed.
Loading history...
Bug introduced by
The method getProjectVersion() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

167
        /** @scrutinizer ignore-call */ 
168
        $actual = $subject->getProjectVersion();

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...
168
169
        // Assert
170
        $projectVersionProphecy->setVersion($expected)->shouldHaveBeenCalled();
171
172
        $this->commandUtilityFacadeProphecy->exec(GitCommandEnumeration::CMD_BRANCH)->shouldHaveBeenCalledTimes(1);
173
        $this->commandUtilityFacadeProphecy->exec(GitCommandEnumeration::CMD_REVISION)->shouldHaveBeenCalledTimes(1);
174
        $this->commandUtilityFacadeProphecy->exec(GitCommandEnumeration::CMD_TAG)->shouldHaveBeenCalledTimes(1);
175
    }
176
177
    /**
178
     * @test
179
     */
180
    public function getProjectVersionShouldTryToFetchVersionFromFileIfResolvingUsingGitErrored()
181
    {
182
        //Arrange
183
        $this->extensionConfiguration['versionFilePath'] = 'EXT:project_version/Tests/Fixture/VERSION';
184
        $this->extensionConfiguration['mode'] = ProjectVersionModeEnumeration::GIT_FILE_FALLBACK;
185
        $this->extensionConfiguration['gitFormat'] = GitCommandEnumeration::FORMAT_REVISION_BRANCH;
186
        $this->setUpExtensionConfiguration();
187
        $branch = '';
188
        $revision = '';
189
        $tag = '';
190
        $absoluteVersionFilename = GeneralUtility::getFileAbsFileName($this->extensionConfiguration['versionFilePath']);
191
        $expected = \file_get_contents($absoluteVersionFilename);
192
193
        $projectVersion = new ProjectVersion();
194
        GeneralUtility::setSingletonInstance(ProjectVersion::class, $projectVersion);
195
196
        $this->commandUtilityFacadeProphecy->exec(GitCommandEnumeration::CMD_BRANCH)->willReturn($branch);
197
        $this->commandUtilityFacadeProphecy->exec(GitCommandEnumeration::CMD_REVISION)->willReturn($revision);
198
        $this->commandUtilityFacadeProphecy->exec(GitCommandEnumeration::CMD_TAG)->willReturn($tag);
199
200
        /** @var \KamiYang\ProjectVersion\Service\ProjectVersionService $subject */
201
        $subject = $this->createPartialMock(ProjectVersionService::class, ['isGitAvailable']);
202
        $subject->method('isGitAvailable')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method method() does not exist on KamiYang\ProjectVersion\...e\ProjectVersionService. ( Ignorable by Annotation )

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

202
        $subject->/** @scrutinizer ignore-call */ 
203
                  method('isGitAvailable')->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...
203
        $subject->injectCommandUtilityFacade($this->commandUtilityFacadeProphecy->reveal());
204
        $subject->injectSystemEnvironmentBuilderFacade($this->systemEnvironmentBuilderFacadeProphecy->reveal());
205
206
        // Act
207
        $subject->getProjectVersion();
208
209
        // Assert
210
        static::assertSame($expected, $projectVersion->getVersion());
211
    }
212
213
    /**
214
     * @return array
215
     */
216
    public function gitFormatDataProvider(): array
217
    {
218
        $branch = 'master';
219
        $revision = 'abcdefg';
220
        $tag = '9.0.42-rc.2';
221
222
        return [
223
            'default git format' => [
224
                'format' => GitCommandEnumeration::FORMAT_REVISION_BRANCH,
225
                'branch' => $branch,
226
                'revision' => $revision,
227
                'tag' => $tag,
228
                'expected' => "[{$revision}] {$branch}"
229
            ],
230
            'git format: revision' => [
231
                'format' => GitCommandEnumeration::FORMAT_REVISION,
232
                'branch' => $branch,
233
                'revision' => $revision,
234
                'tag' => $tag,
235
                'expected' => "{$revision}"
236
            ],
237
            'git format: [revision] branch' => [
238
                'format' => GitCommandEnumeration::FORMAT_REVISION_BRANCH,
239
                'branch' => $branch,
240
                'revision' => $revision,
241
                'tag' => $tag,
242
                'expected' => "[{$revision}] {$branch}"
243
            ],
244
            'git format: [revision] tag' => [
245
                'format' => GitCommandEnumeration::FORMAT_REVISION_TAG,
246
                'branch' => $branch,
247
                'revision' => $revision,
248
                'tag' => $tag,
249
                'expected' => "[{$revision}] {$tag}"
250
            ],
251
            'git format: branch' => [
252
                'format' => GitCommandEnumeration::FORMAT_BRANCH,
253
                'branch' => $branch,
254
                'revision' => $revision,
255
                'tag' => $tag,
256
                'expected' => "{$branch}"
257
            ],
258
            'git format: tag' => [
259
                'format' => GitCommandEnumeration::FORMAT_TAG,
260
                'branch' => $branch,
261
                'revision' => $revision,
262
                'tag' => $tag,
263
                'expected' => "{$tag}"
264
            ],
265
        ];
266
    }
267
268
    /**
269
     * @test
270
     */
271
    public function getProjectVersionShouldAlwaysSetStaticVersionIfSelected()
272
    {
273
        $this->extensionConfiguration['mode'] = ProjectVersionModeEnumeration::STATIC_VERSION;
274
        $this->setUpExtensionConfiguration();
275
276
        $projectVersionProphecy = $this->prophesize(ProjectVersion::class);
277
        GeneralUtility::setSingletonInstance(ProjectVersion::class, $projectVersionProphecy->reveal());
278
279
        $this->subject->getProjectVersion();
280
281
        $projectVersionProphecy->setVersion('')->shouldHaveBeenCalledTimes(1);
282
    }
283
284
    /**
285
     * @test
286
     * @param string $staticVersion
287
     * @dataProvider staticVersionDataProvider
288
     */
289
    public function getProjectVersionShouldSetStaticVersionFromExtensionConfigurationIfSelected(string $staticVersion)
290
    {
291
        $this->extensionConfiguration['mode'] = ProjectVersionModeEnumeration::STATIC_VERSION;
292
        $this->extensionConfiguration['staticVersion'] = $staticVersion;
293
        $this->setUpExtensionConfiguration();
294
295
        $projectVersionProphecy = $this->prophesize(ProjectVersion::class);
296
        GeneralUtility::setSingletonInstance(ProjectVersion::class, $projectVersionProphecy->reveal());
297
298
        $this->subject->getProjectVersion();
299
300
        $projectVersionProphecy->setVersion($staticVersion)->shouldHaveBeenCalledTimes(1);
301
    }
302
303
    public function staticVersionDataProvider(): array
304
    {
305
        return [
306
            'empty static version (default value)' => [
307
                'staticVersion' => ''
308
            ],
309
            'some value' => [
310
                'staticVersion' => 'some value'
311
            ],
312
            'some extreme long value' => [
313
                'staticVersion' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eos hic ipsa labore molestiae nesciunt quo repellendus similique tenetur vitae voluptatem! Dicta dolor minus nostrum ratione voluptas? Ad animi iste sunt!'
314
            ]
315
        ];
316
    }
317
318
    protected function setUp()
319
    {
320
        $this->systemEnvironmentBuilderFacadeProphecy = $this->prophesize(SystemEnvironmentBuilderFacade::class);
321
        $this->systemEnvironmentBuilderFacadeProphecy->isFunctionDisabled('exec')
322
            ->willReturn(false);
323
324
        $this->commandUtilityFacadeProphecy = $this->prophesize(CommandUtilityFacade::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(KamiYa...ndUtilityFacade::class) of type Prophecy\Prophecy\ObjectProphecy is incompatible with the declared type KamiYang\ProjectVersion\...de\CommandUtilityFacade of property $commandUtilityFacadeProphecy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
325
326
        $this->subject = new ProjectVersionService();
327
        $this->subject->injectCommandUtilityFacade($this->commandUtilityFacadeProphecy->reveal());
328
        $this->subject->injectSystemEnvironmentBuilderFacade($this->systemEnvironmentBuilderFacadeProphecy->reveal());
329
    }
330
331
    protected function tearDown()
332
    {
333
        GeneralUtility::purgeInstances();
334
335
        parent::tearDown();
336
    }
337
338
    protected function setUpExtensionConfiguration()
339
    {
340
        $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['project_version'] = serialize($this->extensionConfiguration);
341
342
        GeneralUtility::makeInstance(ExtensionConfiguration::class);
343
    }
344
}
345