Skip to content

Commit 671a1f1

Browse files
committed
Add ability for additional step before checking exts are enabled
1 parent 2d24f88 commit 671a1f1

6 files changed

+134
-4
lines changed

src/Installing/Ini/AddExtensionToTheIniFile.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@
2121
/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
2222
class AddExtensionToTheIniFile
2323
{
24-
public function __invoke(string $ini, Package $package, PhpBinaryPath $phpBinaryPath, OutputInterface $output): bool
25-
{
24+
/** @param callable():bool|null $additionalEnableStep */
25+
public function __invoke(
26+
string $ini,
27+
Package $package,
28+
PhpBinaryPath $phpBinaryPath,
29+
OutputInterface $output,
30+
callable|null $additionalEnableStep,
31+
): bool {
2632
if (! is_writable($ini)) {
2733
$output->writeln(
2834
sprintf(
@@ -63,6 +69,10 @@ public function __invoke(string $ini, Package $package, PhpBinaryPath $phpBinary
6369
OutputInterface::VERBOSITY_VERBOSE,
6470
);
6571

72+
if ($additionalEnableStep !== null && ! $additionalEnableStep()) {
73+
return false;
74+
}
75+
6676
$phpBinaryPath->assertExtensionIsLoadedInRuntime($package->extensionName, $output);
6777

6878
return true;

src/Installing/Ini/CheckAndAddExtensionToIniIfNeeded.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ public function __construct(
2222
) {
2323
}
2424

25-
/** @param non-empty-string $iniFile */
25+
/**
26+
* @param non-empty-string $iniFile
27+
* @param callable():bool|null $additionalEnableStep
28+
*/
2629
public function __invoke(
2730
string $iniFile,
2831
TargetPlatform $targetPlatform,
2932
DownloadedPackage $downloadedPackage,
3033
OutputInterface $output,
34+
callable|null $additionalEnableStep,
3135
): bool {
3236
if (! file_exists($iniFile) || ! is_readable($iniFile)) {
3337
$output->writeln(
@@ -50,6 +54,10 @@ public function __invoke(
5054
OutputInterface::VERBOSITY_VERBOSE,
5155
);
5256

57+
if ($additionalEnableStep !== null && ! $additionalEnableStep()) {
58+
return false;
59+
}
60+
5361
try {
5462
$targetPlatform->phpBinaryPath->assertExtensionIsLoadedInRuntime($downloadedPackage->package->extensionName, $output);
5563

@@ -65,6 +73,12 @@ public function __invoke(
6573
}
6674
}
6775

68-
return ($this->addExtensionToTheIniFile)($iniFile, $downloadedPackage->package, $targetPlatform->phpBinaryPath, $output);
76+
return ($this->addExtensionToTheIniFile)(
77+
$iniFile,
78+
$downloadedPackage->package,
79+
$targetPlatform->phpBinaryPath,
80+
$output,
81+
$additionalEnableStep,
82+
);
6983
}
7084
}

src/Installing/Ini/StandardAdditionalPhpIniDirectory.php

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function setup(
6969
$targetPlatform,
7070
$downloadedPackage,
7171
$output,
72+
null,
7273
);
7374

7475
if (! $addingExtensionWasSuccessful && $pieCreatedTheIniFile) {

src/Installing/Ini/StandardSinglePhpIni.php

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function setup(
4040
$targetPlatform,
4141
$downloadedPackage,
4242
$output,
43+
null,
4344
);
4445
}
4546
}

test/unit/Installing/Ini/AddExtensionToTheIniFileTest.php

+60
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public function testReturnsFalseWhenFileIsNotWritable(): void
7373
),
7474
$this->mockPhpBinary,
7575
$this->output,
76+
null,
7677
));
7778

7879
self::assertStringContainsString(
@@ -116,6 +117,7 @@ public function testReturnsFalseWhenExistingIniCouldNotBeRead(): void
116117
),
117118
$this->mockPhpBinary,
118119
$this->output,
120+
null,
119121
));
120122

121123
self::assertStringContainsString(
@@ -169,6 +171,7 @@ public function testReturnsFalseWhenExtensionWasAddedButPhpRuntimeDidNotLoadExte
169171
),
170172
$this->mockPhpBinary,
171173
$this->output,
174+
null,
172175
));
173176

174177
self::assertStringContainsString(
@@ -212,6 +215,7 @@ public function testReturnsTrueWhenExtensionAdded(): void
212215
),
213216
$this->mockPhpBinary,
214217
$this->output,
218+
null,
215219
));
216220

217221
$iniContent = file_get_contents($iniFile);
@@ -230,4 +234,60 @@ public function testReturnsTrueWhenExtensionAdded(): void
230234
unlink($iniFile);
231235
}
232236
}
237+
238+
public function testReturnsTrueWhenExtensionAddedWithAdditionalStep(): void
239+
{
240+
$iniFile = tempnam(sys_get_temp_dir(), 'PIE_ini_file');
241+
touch($iniFile);
242+
243+
$this->mockPhpBinary
244+
->expects(self::once())
245+
->method('assertExtensionIsLoadedInRuntime');
246+
247+
try {
248+
$additionalStepInvoked = false;
249+
self::assertTrue((new AddExtensionToTheIniFile())(
250+
$iniFile,
251+
new Package(
252+
$this->createMock(CompletePackage::class),
253+
ExtensionType::PhpModule,
254+
ExtensionName::normaliseFromString('foobar'),
255+
'foo/bar',
256+
'1.0.0',
257+
null,
258+
[],
259+
true,
260+
true,
261+
null,
262+
null,
263+
null,
264+
99,
265+
),
266+
$this->mockPhpBinary,
267+
$this->output,
268+
static function () use (&$additionalStepInvoked): bool {
269+
$additionalStepInvoked = true;
270+
271+
return true;
272+
},
273+
));
274+
275+
self::assertTrue($additionalStepInvoked, 'Failed asserting that the additional step was invoked');
276+
277+
$iniContent = file_get_contents($iniFile);
278+
self::assertSame(
279+
PHP_EOL . '; PIE automatically added this to enable the foo/bar extension' . PHP_EOL
280+
. '; priority=99' . PHP_EOL
281+
. 'extension=foobar' . PHP_EOL,
282+
$iniContent,
283+
);
284+
285+
self::assertStringContainsString(
286+
sprintf('Enabled extension foobar in the INI file %s', $iniFile),
287+
$this->output->fetch(),
288+
);
289+
} finally {
290+
unlink($iniFile);
291+
}
292+
}
233293
}

test/unit/Installing/Ini/CheckAndAddExtensionToIniIfNeededTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public function testReturnsFalseWhenIniFileDoesNotExist(): void
108108
$this->targetPlatform,
109109
$this->downloadedPackage,
110110
$this->output,
111+
null,
111112
));
112113

113114
self::assertStringContainsString(
@@ -142,6 +143,7 @@ public function testExtensionIsAlreadyEnabledButExtensionDoesNotLoad(): void
142143
$this->targetPlatform,
143144
$this->downloadedPackage,
144145
$this->output,
146+
null,
145147
));
146148

147149
$output = $this->output->fetch();
@@ -177,6 +179,7 @@ public function testExtensionIsAlreadyEnabledAndExtensionLoaded(): void
177179
$this->targetPlatform,
178180
$this->downloadedPackage,
179181
$this->output,
182+
null,
180183
));
181184

182185
$output = $this->output->fetch();
@@ -186,6 +189,45 @@ public function testExtensionIsAlreadyEnabledAndExtensionLoaded(): void
186189
);
187190
}
188191

192+
public function testExtensionIsAlreadyEnabledWithAdditionalStepAndExtensionLoaded(): void
193+
{
194+
$this->isExtensionAlreadyInTheIniFile
195+
->expects(self::once())
196+
->method('__invoke')
197+
->with(self::INI_FILE, $this->downloadedPackage->package->extensionName)
198+
->willReturn(true);
199+
200+
$this->mockPhpBinary
201+
->expects(self::once())
202+
->method('assertExtensionIsLoadedInRuntime')
203+
->with($this->downloadedPackage->package->extensionName, $this->output);
204+
205+
$this->addExtensionToTheIniFile
206+
->expects(self::never())
207+
->method('__invoke');
208+
209+
$additionalStepInvoked = false;
210+
self::assertTrue($this->checkAndAddExtensionToIniIfNeeded->__invoke(
211+
self::INI_FILE,
212+
$this->targetPlatform,
213+
$this->downloadedPackage,
214+
$this->output,
215+
static function () use (&$additionalStepInvoked): bool {
216+
$additionalStepInvoked = true;
217+
218+
return true;
219+
},
220+
));
221+
222+
self::assertTrue($additionalStepInvoked, 'Failed asserting that the additional step was invoked');
223+
224+
$output = $this->output->fetch();
225+
self::assertStringContainsString(
226+
'Extension is already enabled in the INI file',
227+
$output,
228+
);
229+
}
230+
189231
public function testExtensionIsNotYetAdded(): void
190232
{
191233
$this->isExtensionAlreadyInTheIniFile
@@ -214,6 +256,7 @@ public function testExtensionIsNotYetAdded(): void
214256
$this->targetPlatform,
215257
$this->downloadedPackage,
216258
$this->output,
259+
null,
217260
));
218261
}
219262

@@ -245,6 +288,7 @@ public function testExtensionIsNotYetAddedButFailsToBeAdded(): void
245288
$this->targetPlatform,
246289
$this->downloadedPackage,
247290
$this->output,
291+
null,
248292
));
249293
}
250294
}

0 commit comments

Comments
 (0)