Skip to content

Commit 9483f28

Browse files
committed
fix(tests): Refactor ignore platform requirements data provider and update restore tests.
1 parent 25a7ab5 commit 9483f28

File tree

2 files changed

+41
-42
lines changed

2 files changed

+41
-42
lines changed

src/Fallback/ComposerFallback.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ private function restorePreviousLockFile(): void
179179
$installer->setPlatformRequirementFilter(PlatformRequirementFilterFactory::fromBoolOrList($ignorePlatformReqs));
180180
$runScripts = $isOptionTrue($this->input->getOption('no-scripts')) === false;
181181
$dispatcher = $this->composer->getEventDispatcher();
182-
$dispatcher->setRunScripts(false);
183-
$installer->run();
184182
$dispatcher->setRunScripts($runScripts);
183+
$installer->run();
185184
}
186185
}

tests/Fallback/ComposerFallbackTest.php

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ final class ComposerFallbackTest extends TestCase
3939
private string|null $oldCwd = '';
4040
private \Symfony\Component\Filesystem\Filesystem|null $sfs = null;
4141

42-
public static function getIgnorePlatformReqsData(): array
42+
public static function getIgnorePlatformReqData(): array
4343
{
4444
return [
45-
'ignore-platform-reqs is true' => ['ignore-platform-reqs', true],
46-
'ignore-platform-reqs is array' => ['ignore-platform-reqs', ['php', 'ext-json']],
45+
'ignore-platform-req is true' => ['ignore-platform-req', true],
46+
'ignore-platform-req is array' => ['ignore-platform-req', ['php', 'ext-json']],
4747
];
4848
}
4949

50-
public static function getIgnorePlatformReqData(): array
50+
public static function getIgnorePlatformReqsData(): array
5151
{
5252
return [
53-
'ignore-platform-req is true' => ['ignore-platform-req', true],
54-
'ignore-platform-req is array' => ['ignore-platform-req', ['php', 'ext-json']],
53+
'ignore-platform-reqs is true' => ['ignore-platform-reqs', true],
54+
'ignore-platform-reqs is array' => ['ignore-platform-reqs', ['php', 'ext-json']],
5555
];
5656
}
5757

@@ -66,17 +66,16 @@ public static function getSaveData(): array
6666
}
6767

6868
/**
69-
* @dataProvider getIgnorePlatformReqsData
69+
* @dataProvider getRestoreData
7070
*
7171
* @throws Exception|JsonException
7272
*/
73-
public function testRestoreWithIgnorePlatformReqs(string $optionName, mixed $optionValue): void
73+
public function testRestore(array $packages): void
7474
{
7575
$composerFile = 'composer.json';
7676
$composerContent = '{}';
7777
$lockFile = 'composer.lock';
7878
$vendorDir = $this->cwd . '/vendor/';
79-
$packages = [['name' => 'foo/bar', 'version' => '1.0.0.0']];
8079

8180
file_put_contents($this->cwd . '/' . $composerFile, $composerContent);
8281
file_put_contents(
@@ -95,13 +94,7 @@ public function testRestoreWithIgnorePlatformReqs(string $optionName, mixed $opt
9594
$this->input
9695
->expects(self::any())
9796
->method('getOption')
98-
->willReturnCallback(
99-
fn($option): mixed => match ($option) {
100-
$optionName => $optionValue,
101-
'verbose' => false,
102-
default => null,
103-
}
104-
);
97+
->willReturnCallback(fn($option): bool|null => 'verbose' === $option ? false : null);
10598

10699
$ed = $this->createMock(EventDispatcher::class);
107100

@@ -132,12 +125,30 @@ public function testRestoreWithIgnorePlatformReqs(string $optionName, mixed $opt
132125
->method('get')
133126
->willReturnCallback(fn($key, $default = null) => 'vendor-dir' === $key ? $vendorDir : $default);
134127

135-
$this->installer->expects(self::once())->method('run');
128+
if (0 === count($packages)) {
129+
$this->fs->expects(self::once())->method('remove')->with($vendorDir);
130+
} else {
131+
$this->fs->expects(self::never())->method('remove');
132+
$this->installer->expects(self::once())->method('run');
133+
}
136134

137135
$this->composerFallback->save();
138136
$this->composerFallback->restore();
139137
}
140138

139+
/**
140+
* @throws Exception
141+
*/
142+
public function testRestoreWithDisableOption(): void
143+
{
144+
$config = new Config(['fallback-composer' => false]);
145+
$composerFallback = new ComposerFallback($this->composer, $this->io, $config, $this->input);
146+
147+
$this->io->expects(self::never())->method('write');
148+
149+
$composerFallback->restore();
150+
}
151+
141152
/**
142153
* @dataProvider getIgnorePlatformReqData
143154
*
@@ -174,7 +185,7 @@ public function testRestoreWithIgnorePlatformReq(string $optionName, mixed $opti
174185
$optionName => $optionValue,
175186
'verbose' => false,
176187
default => null,
177-
}
188+
},
178189
);
179190

180191
$ed = $this->createMock(EventDispatcher::class);
@@ -213,16 +224,17 @@ public function testRestoreWithIgnorePlatformReq(string $optionName, mixed $opti
213224
}
214225

215226
/**
216-
* @dataProvider getRestoreData
227+
* @dataProvider getIgnorePlatformReqsData
217228
*
218229
* @throws Exception|JsonException
219230
*/
220-
public function testRestore(array $packages): void
231+
public function testRestoreWithIgnorePlatformReqs(string $optionName, mixed $optionValue): void
221232
{
222233
$composerFile = 'composer.json';
223234
$composerContent = '{}';
224235
$lockFile = 'composer.lock';
225236
$vendorDir = $this->cwd . '/vendor/';
237+
$packages = [['name' => 'foo/bar', 'version' => '1.0.0.0']];
226238

227239
file_put_contents($this->cwd . '/' . $composerFile, $composerContent);
228240
file_put_contents(
@@ -241,7 +253,13 @@ public function testRestore(array $packages): void
241253
$this->input
242254
->expects(self::any())
243255
->method('getOption')
244-
->willReturnCallback(fn($option): bool|null => 'verbose' === $option ? false : null);
256+
->willReturnCallback(
257+
fn($option): mixed => match ($option) {
258+
$optionName => $optionValue,
259+
'verbose' => false,
260+
default => null,
261+
},
262+
);
245263

246264
$ed = $this->createMock(EventDispatcher::class);
247265

@@ -272,30 +290,12 @@ public function testRestore(array $packages): void
272290
->method('get')
273291
->willReturnCallback(fn($key, $default = null) => 'vendor-dir' === $key ? $vendorDir : $default);
274292

275-
if (0 === count($packages)) {
276-
$this->fs->expects(self::once())->method('remove')->with($vendorDir);
277-
} else {
278-
$this->fs->expects(self::never())->method('remove');
279-
$this->installer->expects(self::once())->method('run');
280-
}
293+
$this->installer->expects(self::once())->method('run');
281294

282295
$this->composerFallback->save();
283296
$this->composerFallback->restore();
284297
}
285298

286-
/**
287-
* @throws Exception
288-
*/
289-
public function testRestoreWithDisableOption(): void
290-
{
291-
$config = new Config(['fallback-composer' => false]);
292-
$composerFallback = new ComposerFallback($this->composer, $this->io, $config, $this->input);
293-
294-
$this->io->expects(self::never())->method('write');
295-
296-
$composerFallback->restore();
297-
}
298-
299299
/**
300300
* @dataProvider getSaveData
301301
*

0 commit comments

Comments
 (0)