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); |
|
|
|
|
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()) |
|
|
|
|
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
|
|
|
|