1 | <?php |
||
21 | class InstallerTest extends PHPUnit_Framework_TestCase |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * |
||
26 | * @return \PHPUnit_Framework_MockObject_MockObject |
||
27 | */ |
||
28 | private function getIO() |
||
29 | { |
||
30 | return $this->createMock(IOInterface::class); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * |
||
35 | * @return \PHPUnit_Framework_MockObject_MockObject |
||
36 | */ |
||
37 | private function getComposer() |
||
38 | { |
||
39 | $composer = $this->createMock(Composer::class); |
||
40 | |||
41 | /* |
||
42 | * $composer->getConfig() |
||
43 | */ |
||
44 | $config = $this->getMockBuilder(Config::class) |
||
45 | ->disableOriginalConstructor() |
||
46 | ->getMock(); |
||
47 | |||
48 | $composer->expects(self::any()) |
||
49 | ->method('getConfig') |
||
50 | ->willReturn($config); |
||
51 | |||
52 | /* |
||
53 | * $composer->getLocker() |
||
54 | */ |
||
55 | $locker = $this->getMockBuilder(Locker::class) |
||
56 | ->disableOriginalConstructor() |
||
57 | ->getMock(); |
||
58 | |||
59 | $composer->expects(self::any()) |
||
60 | ->method('getLocker') |
||
61 | ->willReturn($locker); |
||
62 | |||
63 | return $composer; |
||
64 | } |
||
65 | |||
66 | public function testActivate() |
||
67 | { |
||
68 | $installer = new Installer(); |
||
69 | $return = $installer->activate($this->getComposer(), $this->getIO()); |
||
70 | |||
71 | $this->assertNull($return); |
||
72 | } |
||
73 | |||
74 | public function testGetSubscribedEvents() |
||
75 | { |
||
76 | $events = Installer::getSubscribedEvents(); |
||
77 | |||
78 | self::assertSame([ |
||
79 | 'post-install-cmd' => 'dumpAll', |
||
80 | 'post-update-cmd' => 'dumpAll' |
||
81 | ], $events); |
||
82 | |||
83 | foreach ($events as $callback) { |
||
84 | self::assertInternalType('callable', [ |
||
85 | new Installer(), |
||
86 | $callback |
||
87 | ]); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @dataProvider dumpAllProvider |
||
93 | */ |
||
94 | public function testDumpAll(array $lockData, $expectedResult) |
||
95 | { |
||
96 | /* |
||
97 | * Mocks |
||
98 | */ |
||
99 | $composer = $this->getComposer(); |
||
100 | |||
101 | /* |
||
102 | * $composer->getLocker() |
||
103 | */ |
||
104 | $locker = $composer->getLocker(); |
||
105 | |||
106 | $locker->expects(self::any()) |
||
107 | ->method('getLockData') |
||
108 | ->willReturn($lockData); |
||
109 | |||
110 | /* |
||
111 | * $composer->getPackage() |
||
112 | */ |
||
113 | $rootPackage = $this->createMock(RootPackageInterface::class); |
||
114 | |||
115 | $rootPackage->expects(self::any()) |
||
116 | ->method('getName') |
||
117 | ->willReturn('root/package'); |
||
118 | $rootPackage->expects(self::any()) |
||
119 | ->method('getPrettyName') |
||
120 | ->willReturn('root/package'); |
||
121 | |||
122 | $rootPackage->expects(self::any()) |
||
123 | ->method('getVersion') |
||
124 | ->willReturn('1.3.5'); |
||
125 | $rootPackage->expects(self::any()) |
||
126 | ->method('getPrettyVersion') |
||
127 | ->willReturn('1.3.5'); |
||
128 | |||
129 | $rootPackage->expects(self::any()) |
||
130 | ->method('getSourceReference') |
||
131 | ->willReturn('aaabbbcccddd'); |
||
132 | |||
133 | $composer->expects(self::any()) |
||
134 | ->method('getPackage') |
||
135 | ->willReturn($rootPackage); |
||
136 | |||
137 | $io = $this->getIO(); |
||
138 | |||
139 | /* |
||
140 | * Paths |
||
141 | */ |
||
142 | $vendorDir = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true); |
||
143 | |||
144 | $expectedPath = $vendorDir . '/thadafinser/package-info/src'; |
||
145 | |||
146 | mkdir($expectedPath, 0777, true); |
||
147 | |||
148 | $config = $composer->getConfig(); |
||
149 | $config->expects(self::any()) |
||
150 | ->method('get') |
||
151 | ->with('vendor-dir') |
||
152 | ->willReturn($vendorDir); |
||
153 | |||
154 | /* |
||
155 | * Execute |
||
156 | */ |
||
157 | Installer::dumpAll(new Event('post-install-cmd', $composer, $io)); |
||
158 | |||
159 | /* |
||
160 | * Test |
||
161 | */ |
||
162 | self::assertSame($expectedResult, file_get_contents($expectedPath . '/Package.php')); |
||
163 | |||
164 | $this->rmDir($vendorDir); |
||
165 | } |
||
166 | |||
167 | public function dumpAllProvider() |
||
168 | { |
||
169 | $examples = []; |
||
170 | foreach (new \DirectoryIterator('tests/resources') as $fileInfo) { |
||
171 | if ($fileInfo->isDot()) { |
||
172 | continue; |
||
173 | } |
||
174 | |||
175 | $examples[$fileInfo->getFilename()] = include $fileInfo->getPathname(); |
||
176 | } |
||
177 | |||
178 | return $examples; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @dataProvider rootPackageProvider |
||
183 | * |
||
184 | * @param RootPackageInterface $rootPackage |
||
185 | * @param bool $inVendor |
||
186 | * |
||
187 | * @throws \RuntimeException |
||
188 | */ |
||
189 | public function testDumpsVersionsClassToSpecificLocation(RootPackageInterface $rootPackage, $inVendor) |
||
190 | { |
||
191 | /* |
||
192 | * Mocks |
||
193 | */ |
||
194 | $composer = $this->getComposer(); |
||
195 | |||
196 | /* |
||
197 | * root package |
||
198 | */ |
||
199 | $composer->expects(self::any()) |
||
200 | ->method('getPackage') |
||
201 | ->willReturn($rootPackage); |
||
202 | |||
203 | /* |
||
204 | * lock data |
||
205 | */ |
||
206 | $locker = $composer->getLocker(); |
||
207 | |||
208 | $locker->expects(self::any()) |
||
209 | ->method('getLockData') |
||
210 | ->willReturn([ |
||
211 | 'packages' => [], |
||
212 | 'packages-dev' => [] |
||
213 | ]); |
||
214 | |||
215 | $repository = $this->createMock(InstalledRepositoryInterface::class); |
||
216 | |||
217 | $repositoryManager = $this->getMockBuilder(RepositoryManager::class) |
||
218 | ->disableOriginalConstructor() |
||
219 | ->getMock(); |
||
220 | |||
221 | $repositoryManager->expects(self::any()) |
||
222 | ->method('getLocalRepository') |
||
223 | ->willReturn($repository); |
||
224 | |||
225 | $composer->expects(self::any()) |
||
226 | ->method('getRepositoryManager') |
||
227 | ->willReturn($repositoryManager); |
||
228 | |||
229 | $installManager = $this->getMockBuilder(InstallationManager::class) |
||
230 | ->disableOriginalConstructor() |
||
231 | ->getMock(); |
||
232 | |||
233 | $composer->expects(self::any()) |
||
234 | ->method('getInstallationManager') |
||
235 | ->willReturn($installManager); |
||
236 | |||
237 | /* |
||
238 | * Paths |
||
239 | */ |
||
240 | $tmpDir = sys_get_temp_dir() . '/' . uniqid('InstallerTest', true); |
||
241 | |||
242 | $vendorDir = $tmpDir . '/vendor'; |
||
243 | |||
244 | if ($inVendor) { |
||
245 | $expectedPath = $vendorDir . '/thadafinser/package-info/src'; |
||
246 | } else { |
||
247 | $expectedPath = $tmpDir . '/src'; |
||
248 | } |
||
249 | |||
250 | mkdir($expectedPath, 0777, true); |
||
251 | |||
252 | $config = $composer->getConfig(); |
||
253 | $config->expects(self::any()) |
||
254 | ->method('get') |
||
255 | ->with('vendor-dir') |
||
256 | ->willReturn($vendorDir); |
||
257 | |||
258 | Installer::dumpAll(new Event('post-install-cmd', $composer, $this->getIO())); |
||
259 | |||
260 | self::assertStringMatchesFormat('%Aclass Package%A1.2.3%A', file_get_contents($expectedPath . '/Package.php')); |
||
261 | |||
262 | $this->rmDir($tmpDir); |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * |
||
267 | * @return bool[][]|RootPackageInterface[][] the root package and whether the versions class is to be generated in |
||
268 | * the vendor dir or not |
||
269 | */ |
||
270 | public function rootPackageProvider() |
||
271 | { |
||
272 | $baseRootPackage = new RootPackage('root/package', '1.2.3', '1.2.3'); |
||
273 | $aliasRootPackage = new RootAliasPackage($baseRootPackage, '1.2.3', '1.2.3'); |
||
274 | $indirectAliasRootPackage = new RootAliasPackage($aliasRootPackage, '1.2.3', '1.2.3'); |
||
275 | $packageVersionsRootPackage = new RootPackage('thadafinser/package-info', '1.2.3', '1.2.3'); |
||
276 | $aliasPackageVersionsRootPackage = new RootAliasPackage($packageVersionsRootPackage, '1.2.3', '1.2.3'); |
||
277 | $indirectAliasPackageVersionsRootPackage = new RootAliasPackage($aliasPackageVersionsRootPackage, '1.2.3', '1.2.3'); |
||
278 | |||
279 | return [ |
||
280 | 'root package is not thadafinser/package-info' => [ |
||
281 | $baseRootPackage, |
||
282 | true |
||
283 | ], |
||
284 | 'alias root package is not othadafinser/package-info' => [ |
||
285 | $aliasRootPackage, |
||
286 | true |
||
287 | ], |
||
288 | 'indirect alias root package is not thadafinser/package-info' => [ |
||
289 | $indirectAliasRootPackage, |
||
290 | true |
||
291 | ], |
||
292 | |||
293 | 'root package is thadafinser/package-info' => [ |
||
294 | $packageVersionsRootPackage, |
||
295 | false |
||
296 | ], |
||
297 | 'alias root package is thadafinser/package-info' => [ |
||
298 | $aliasPackageVersionsRootPackage, |
||
299 | false |
||
300 | ], |
||
301 | 'indirect alias root package is thadafinser/package-info' => [ |
||
302 | $indirectAliasPackageVersionsRootPackage, |
||
303 | false |
||
304 | ] |
||
305 | ]; |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * |
||
310 | * @param string $directory |
||
311 | * |
||
312 | * @return void |
||
313 | */ |
||
314 | private function rmDir($directory) |
||
315 | { |
||
316 | if (! is_dir($directory)) { |
||
317 | unlink($directory); |
||
318 | return; |
||
319 | } |
||
320 | |||
321 | array_map(function ($item) use($directory) { |
||
322 | $this->rmDir($directory . '/' . $item); |
||
323 | }, array_filter(scandir($directory), function ($dirItem) { |
||
324 | return ! in_array($dirItem, [ |
||
325 | '.', |
||
326 | '..' |
||
327 | ], true); |
||
328 | })); |
||
329 | |||
330 | rmdir($directory); |
||
331 | } |
||
332 | } |
||
333 |