Passed
Pull Request — master (#49)
by Marco
02:55
created

testWillLocateDependenciesEvenWithoutAutoloadFiles()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 8.8571
c 0
b 0
f 0
eloc 29
nc 1
nop 0
cc 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\ApiCompare\LocateDependencies;
6
7
use Composer\Installer;
8
use PHPUnit\Framework\MockObject\MockObject;
9
use PHPUnit\Framework\TestCase;
10
use Roave\ApiCompare\LocateDependencies\LocateDependenciesViaComposer;
11
use Roave\ApiCompare\SourceLocator\StaticClassMapSourceLocator;
12
use Roave\BetterReflection\BetterReflection;
13
use Roave\BetterReflection\SourceLocator\Ast\Locator;
14
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
15
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
16
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator;
17
use stdClass;
18
use function getcwd;
19
use function realpath;
20
21
/**
22
 * @covers \Roave\ApiCompare\LocateDependencies\LocateDependenciesViaComposer
23
 */
24
final class LocateDependenciesViaComposerTest extends TestCase
25
{
26
    /** @var string */
27
    private $originalCwd;
28
29
    /** @var callable|MockObject */
30
    private $makeInstaller;
31
32
    /** @var Installer|MockObject */
33
    private $composerInstaller;
34
35
    /** @var Locator */
36
    private $astLocator;
37
38
    /** @var LocateDependenciesViaComposer */
39
    private $locateDependencies;
40
41
    protected function setUp() : void
42
    {
43
        parent::setUp();
44
45
        $this->originalCwd       = getcwd();
46
        $this->composerInstaller = $this->createMock(Installer::class);
47
        $this->astLocator        = (new BetterReflection())->astLocator();
48
        $this->makeInstaller     = $this
49
            ->getMockBuilder(stdClass::class)
50
            ->setMethods(['__invoke'])
51
            ->getMock();
52
53
        $this
54
            ->composerInstaller
55
            ->expects(self::atLeastOnce())
56
            ->method('setDevMode')
57
            ->with(false);
58
        $this
59
            ->composerInstaller
60
            ->expects(self::atLeastOnce())
61
            ->method('setDumpAutoloader')
62
            ->with(true);
63
        $this
64
            ->composerInstaller
65
            ->expects(self::atLeastOnce())
66
            ->method('setRunScripts')
67
            ->with(false);
68
        $this
69
            ->composerInstaller
70
            ->expects(self::atLeastOnce())
71
            ->method('setOptimizeAutoloader')
72
            ->with(true);
73
        $this
74
            ->composerInstaller
75
            ->expects(self::atLeastOnce())
76
            ->method('setClassMapAuthoritative')
77
            ->with(true);
78
        $this
79
            ->composerInstaller
80
            ->expects(self::atLeastOnce())
81
            ->method('setIgnorePlatformRequirements')
82
            ->with(true);
83
84
        $this->locateDependencies = new LocateDependenciesViaComposer($this->makeInstaller, $this->astLocator);
0 ignored issues
show
Bug introduced by
$this->makeInstaller of type PHPUnit\Framework\MockObject\MockObject is incompatible with the type callable expected by parameter $makeComposerInstaller of Roave\ApiCompare\LocateD...Composer::__construct(). ( Ignorable by Annotation )

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

84
        $this->locateDependencies = new LocateDependenciesViaComposer(/** @scrutinizer ignore-type */ $this->makeInstaller, $this->astLocator);
Loading history...
85
    }
86
87
    protected function tearDown() : void
88
    {
89
        self::assertSame($this->originalCwd, getcwd());
90
91
        parent::tearDown();
92
    }
93
94
    public function testWillLocateDependencies() : void
95
    {
96
        $composerInstallationStructure = realpath(__DIR__ . '/../../asset/composer-installation-structure');
97
98
        $this
99
            ->makeInstaller
100
            ->expects(self::any())
101
            ->method('__invoke')
102
            ->with($composerInstallationStructure)
103
            ->willReturn($this->composerInstaller);
104
105
        $this
106
            ->composerInstaller
107
            ->expects(self::once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Composer\Installer. ( Ignorable by Annotation )

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

107
            ->/** @scrutinizer ignore-call */ 
108
              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...
108
            ->method('run')
109
            ->willReturnCallback(function () use ($composerInstallationStructure) : void {
110
                self::assertSame($composerInstallationStructure, getcwd());
111
            });
112
113
        $locator = $this
114
            ->locateDependencies
115
            ->__invoke($composerInstallationStructure);
116
117
        self::assertInstanceOf(AggregateSourceLocator::class, $locator);
118
119
        $reflectionLocators = new \ReflectionProperty(AggregateSourceLocator::class, 'sourceLocators');
120
121
        $reflectionLocators->setAccessible(true);
122
123
        $locators = $reflectionLocators->getValue($locator);
124
125
        self::assertCount(3, $locators);
126
        self::assertEquals(
127
            new StaticClassMapSourceLocator(
128
                [
129
                    'A\\ClassName' => realpath(__DIR__ . '/../../asset/composer-installation-structure/AClassName.php'),
130
                    'B\\ClassName' => realpath(__DIR__ . '/../../asset/composer-installation-structure/BClassName.php'),
131
                ],
132
                $this->astLocator
133
            ),
134
            $locators[0]
135
        );
136
        self::assertEquals(
137
            new AggregateSourceLocator([
138
                new SingleFileSourceLocator(
139
                    realpath(__DIR__ . '/../../asset/composer-installation-structure/included-file-1.php'),
140
                    $this->astLocator
141
                ),
142
                new SingleFileSourceLocator(
143
                    realpath(__DIR__ . '/../../asset/composer-installation-structure/included-file-2.php'),
144
                    $this->astLocator
145
                ),
146
            ]),
147
            $locators[1]
148
        );
149
        self::assertInstanceOf(PhpInternalSourceLocator::class, $locators[2]);
150
    }
151
152
    public function testWillLocateDependenciesEvenWithoutAutoloadFiles() : void
153
    {
154
        $composerInstallationStructure = realpath(__DIR__ . '/../../asset/composer-installation-structure-without-autoload-files');
155
156
        $this
157
            ->makeInstaller
158
            ->expects(self::any())
159
            ->method('__invoke')
160
            ->with($composerInstallationStructure)
161
            ->willReturn($this->composerInstaller);
162
163
        $this
164
            ->composerInstaller
165
            ->expects(self::once())
166
            ->method('run')
167
            ->willReturnCallback(function () use ($composerInstallationStructure) : void {
168
                self::assertSame($composerInstallationStructure, getcwd());
169
            });
170
171
        $locator = $this
172
            ->locateDependencies
173
            ->__invoke($composerInstallationStructure);
174
175
        self::assertInstanceOf(AggregateSourceLocator::class, $locator);
176
177
        $reflectionLocators = new \ReflectionProperty(AggregateSourceLocator::class, 'sourceLocators');
178
179
        $reflectionLocators->setAccessible(true);
180
181
        $locators = $reflectionLocators->getValue($locator);
182
183
        self::assertCount(3, $locators);
184
        self::assertEquals(
185
            new StaticClassMapSourceLocator(
186
                [
187
                    'A\\ClassName' => realpath(__DIR__ . '/../../asset/composer-installation-structure-without-autoload-files/AClassName.php'),
188
                    'B\\ClassName' => realpath(__DIR__ . '/../../asset/composer-installation-structure-without-autoload-files/BClassName.php'),
189
                ],
190
                $this->astLocator
191
            ),
192
            $locators[0]
193
        );
194
        self::assertEquals(new AggregateSourceLocator(), $locators[1]);
195
        self::assertInstanceOf(PhpInternalSourceLocator::class, $locators[2]);
196
    }
197
}
198