Skip to content

Commit db5ac2d

Browse files
committed
fix(tests): Simplify testRestoreWithIgnorePlatformReq and testRestoreWithIgnorePlatformReqs by extracting common setup logic.
1 parent 9483f28 commit db5ac2d

File tree

1 file changed

+71
-115
lines changed

1 file changed

+71
-115
lines changed

tests/Fallback/ComposerFallbackTest.php

Lines changed: 71 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -156,69 +156,19 @@ public function testRestoreWithDisableOption(): void
156156
*/
157157
public function testRestoreWithIgnorePlatformReq(string $optionName, mixed $optionValue): void
158158
{
159-
$composerFile = 'composer.json';
160-
$composerContent = '{}';
161-
$lockFile = 'composer.lock';
162-
$vendorDir = $this->cwd . '/vendor/';
163159
$packages = [['name' => 'foo/bar', 'version' => '1.0.0.0']];
164160

165-
file_put_contents($this->cwd . '/' . $composerFile, $composerContent);
166-
file_put_contents(
167-
$this->cwd . '/' . $lockFile,
168-
json_encode(
169-
[
170-
'content-hash' => 'HASH_VALUE',
171-
'packages' => $packages,
172-
'packages-dev' => [],
173-
'prefer-stable' => true,
174-
],
175-
JSON_THROW_ON_ERROR,
176-
),
161+
$this->setupRestoreEnvironment(
162+
$packages,
163+
fn($option): mixed => match ($option) {
164+
'ignore-platform-reqs' => null,
165+
$optionName => $optionValue,
166+
'verbose' => false,
167+
default => null,
168+
},
177169
);
178170

179-
$this->input
180-
->expects(self::any())
181-
->method('getOption')
182-
->willReturnCallback(
183-
fn($option): mixed => match ($option) {
184-
'ignore-platform-reqs' => null,
185-
$optionName => $optionValue,
186-
'verbose' => false,
187-
default => null,
188-
},
189-
);
190-
191-
$ed = $this->createMock(EventDispatcher::class);
192-
193-
$this->composer->expects(self::any())->method('getEventDispatcher')->willReturn($ed);
194-
195-
$rm = $this->createMock(RepositoryManager::class);
196-
197-
$this->composer->expects(self::any())->method('getRepositoryManager')->willReturn($rm);
198-
199-
$im = $this->createMock(InstallationManager::class);
200-
201-
$this->composer->expects(self::any())->method('getInstallationManager')->willReturn($im);
202-
$this->io->expects(self::once())->method('write');
203-
204-
$locker = LockerUtil::getLocker($this->io, $im, $composerFile);
205-
206-
$this->composer->expects(self::atLeastOnce())->method('getLocker')->willReturn($locker);
207-
208-
$config = $this->getMockBuilder(\Composer\Config::class)
209-
->disableOriginalConstructor()
210-
->onlyMethods(['get'])
211-
->getMock();
212-
213-
$this->composer->expects(self::atLeastOnce())->method('getConfig')->willReturn($config);
214-
215-
$config
216-
->expects(self::atLeastOnce())
217-
->method('get')
218-
->willReturnCallback(fn($key, $default = null) => 'vendor-dir' === $key ? $vendorDir : $default);
219-
220171
$this->installer->expects(self::once())->method('run');
221-
222172
$this->composerFallback->save();
223173
$this->composerFallback->restore();
224174
}
@@ -230,68 +180,18 @@ public function testRestoreWithIgnorePlatformReq(string $optionName, mixed $opti
230180
*/
231181
public function testRestoreWithIgnorePlatformReqs(string $optionName, mixed $optionValue): void
232182
{
233-
$composerFile = 'composer.json';
234-
$composerContent = '{}';
235-
$lockFile = 'composer.lock';
236-
$vendorDir = $this->cwd . '/vendor/';
237183
$packages = [['name' => 'foo/bar', 'version' => '1.0.0.0']];
238184

239-
file_put_contents($this->cwd . '/' . $composerFile, $composerContent);
240-
file_put_contents(
241-
$this->cwd . '/' . $lockFile,
242-
json_encode(
243-
[
244-
'content-hash' => 'HASH_VALUE',
245-
'packages' => $packages,
246-
'packages-dev' => [],
247-
'prefer-stable' => true,
248-
],
249-
JSON_THROW_ON_ERROR,
250-
),
185+
$this->setupRestoreEnvironment(
186+
$packages,
187+
fn($option): mixed => match ($option) {
188+
$optionName => $optionValue,
189+
'verbose' => false,
190+
default => null,
191+
},
251192
);
252193

253-
$this->input
254-
->expects(self::any())
255-
->method('getOption')
256-
->willReturnCallback(
257-
fn($option): mixed => match ($option) {
258-
$optionName => $optionValue,
259-
'verbose' => false,
260-
default => null,
261-
},
262-
);
263-
264-
$ed = $this->createMock(EventDispatcher::class);
265-
266-
$this->composer->expects(self::any())->method('getEventDispatcher')->willReturn($ed);
267-
268-
$rm = $this->createMock(RepositoryManager::class);
269-
270-
$this->composer->expects(self::any())->method('getRepositoryManager')->willReturn($rm);
271-
272-
$im = $this->createMock(InstallationManager::class);
273-
274-
$this->composer->expects(self::any())->method('getInstallationManager')->willReturn($im);
275-
$this->io->expects(self::once())->method('write');
276-
277-
$locker = LockerUtil::getLocker($this->io, $im, $composerFile);
278-
279-
$this->composer->expects(self::atLeastOnce())->method('getLocker')->willReturn($locker);
280-
281-
$config = $this->getMockBuilder(\Composer\Config::class)
282-
->disableOriginalConstructor()
283-
->onlyMethods(['get'])
284-
->getMock();
285-
286-
$this->composer->expects(self::atLeastOnce())->method('getConfig')->willReturn($config);
287-
288-
$config
289-
->expects(self::atLeastOnce())
290-
->method('get')
291-
->willReturnCallback(fn($key, $default = null) => 'vendor-dir' === $key ? $vendorDir : $default);
292-
293194
$this->installer->expects(self::once())->method('run');
294-
295195
$this->composerFallback->save();
296196
$this->composerFallback->restore();
297197
}
@@ -372,4 +272,60 @@ protected function tearDown(): void
372272
$this->oldCwd = null;
373273
$this->cwd = null;
374274
}
275+
276+
private function setupRestoreEnvironment(
277+
array $packages,
278+
callable $optionCallback,
279+
): void {
280+
$composerFile = 'composer.json';
281+
$composerContent = '{}';
282+
$lockFile = 'composer.lock';
283+
$vendorDir = $this->cwd . '/vendor/';
284+
285+
file_put_contents($this->cwd . '/' . $composerFile, $composerContent);
286+
file_put_contents(
287+
$this->cwd . '/' . $lockFile,
288+
json_encode(
289+
[
290+
'content-hash' => 'HASH_VALUE',
291+
'packages' => $packages,
292+
'packages-dev' => [],
293+
'prefer-stable' => true,
294+
],
295+
JSON_THROW_ON_ERROR,
296+
),
297+
);
298+
299+
$this->input
300+
->expects(self::any())
301+
->method('getOption')
302+
->willReturnCallback($optionCallback);
303+
304+
$eventDispatcher = $this->createMock(EventDispatcher::class);
305+
$this->composer->expects(self::any())->method('getEventDispatcher')->willReturn($eventDispatcher);
306+
307+
$repositoryManager = $this->createMock(RepositoryManager::class);
308+
$this->composer->expects(self::any())->method('getRepositoryManager')->willReturn($repositoryManager);
309+
310+
$installationManager = $this->createMock(InstallationManager::class);
311+
$this->composer->expects(self::any())->method('getInstallationManager')->willReturn($installationManager);
312+
313+
$this->io->expects(self::once())->method('write');
314+
315+
$locker = LockerUtil::getLocker($this->io, $installationManager, $composerFile);
316+
317+
$this->composer->expects(self::atLeastOnce())->method('getLocker')->willReturn($locker);
318+
319+
$config = $this->getMockBuilder(\Composer\Config::class)
320+
->disableOriginalConstructor()
321+
->onlyMethods(['get'])
322+
->getMock();
323+
324+
$this->composer->expects(self::atLeastOnce())->method('getConfig')->willReturn($config);
325+
326+
$config
327+
->expects(self::atLeastOnce())
328+
->method('get')
329+
->willReturnCallback(fn($key, $default = null) => 'vendor-dir' === $key ? $vendorDir : $default);
330+
}
375331
}

0 commit comments

Comments
 (0)