-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathUnixInstallTest.php
More file actions
243 lines (209 loc) · 9.32 KB
/
Copy pathUnixInstallTest.php
File metadata and controls
243 lines (209 loc) · 9.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
declare(strict_types=1);
namespace Php\PieIntegrationTest\Installing;
use Composer\IO\BufferIO;
use Composer\Package\CompletePackageInterface;
use Composer\Util\Filesystem;
use Composer\Util\Platform;
use Php\Pie\Building\UnixBuild;
use Php\Pie\DependencyResolver\Package;
use Php\Pie\Downloading\DownloadedPackage;
use Php\Pie\Downloading\DownloadUrlMethod;
use Php\Pie\ExtensionName;
use Php\Pie\ExtensionType;
use Php\Pie\File\BinaryFile;
use Php\Pie\Installing\Ini\PickBestSetupIniApproach;
use Php\Pie\Installing\SetupIniFile;
use Php\Pie\Installing\UnixInstall;
use Php\Pie\Platform\TargetPhp\PhpBinaryPath;
use Php\Pie\Platform\TargetPlatform;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;
use function array_combine;
use function array_filter;
use function array_map;
use function array_unshift;
use function assert;
use function file_exists;
use function getenv;
use function is_executable;
use function is_writable;
use function Safe\mkdir;
use function Safe\putenv;
use function Safe\rename;
use function uniqid;
use const DIRECTORY_SEPARATOR;
#[CoversClass(UnixInstall::class)]
final class UnixInstallTest extends TestCase
{
private const COMPOSER_PACKAGE_EXTRA_KEY = 'download-url-method';
private const TEST_EXTENSION_PATH = __DIR__ . '/../../assets/pie_test_ext';
private const TEST_PREBUILT_PATH = __DIR__ . '/../../assets/pre-packaged-binary-examples/install';
/** @return array<string, array{0: non-empty-string}> */
public static function phpPathProvider(): array
{
// data providers cannot return empty, even if the test is skipped
if (Platform::isWindows()) {
return ['skip' => ['skip']];
}
$possiblePhpConfigPaths = array_filter(
[
'/usr/bin/php-config',
'/usr/bin/php-config8.5',
'/usr/bin/php-config8.4',
'/usr/bin/php-config8.3',
'/usr/bin/php-config8.2',
'/usr/bin/php-config8.1',
'/usr/bin/php-config8.0',
'/usr/bin/php-config7.4',
'/usr/bin/php-config7.3',
'/usr/bin/php-config7.2',
'/usr/local/bin/php-config',
],
static fn (string $phpConfigPath) => file_exists($phpConfigPath)
&& is_executable($phpConfigPath),
);
return array_combine(
$possiblePhpConfigPaths,
array_map(static fn (string $phpConfigPath) => [$phpConfigPath], $possiblePhpConfigPaths),
);
}
#[DataProvider('phpPathProvider')]
public function testUnixInstallCanInstallExtensionBuiltFromSource(string $phpConfig): void
{
$installRoot = '/tmp/' . uniqid('pie-test-install-root-', true);
$oldInstallRoot = getenv('INSTALL_ROOT');
putenv('INSTALL_ROOT=' . $installRoot);
assert($phpConfig !== '');
if (Platform::isWindows()) {
self::markTestSkipped('Unix build test cannot be run on Windows');
}
$output = new BufferIO();
$targetPlatform = TargetPlatform::fromPhpBinaryPath(PhpBinaryPath::fromPhpConfigExecutable($phpConfig), null, null);
$extensionPath = $targetPlatform->phpBinaryPath->extensionPath($installRoot);
$composerPackage = $this->createMock(CompletePackageInterface::class);
$composerPackage
->method('getExtra')
->willReturn([self::COMPOSER_PACKAGE_EXTRA_KEY => DownloadUrlMethod::ComposerDefaultDownload->value]);
$downloadedPackage = DownloadedPackage::fromPackageAndExtractedPath(
new Package(
$composerPackage,
ExtensionType::PhpModule,
ExtensionName::normaliseFromString('pie_test_ext'),
'pie_test_ext',
'0.1.0',
null,
),
self::TEST_EXTENSION_PATH,
);
$built = (new UnixBuild())->__invoke(
$downloadedPackage,
$targetPlatform,
['--enable-pie_test_ext'],
$output,
);
$installedSharedObject = (new UnixInstall(new SetupIniFile(new PickBestSetupIniApproach([]))))->__invoke(
$downloadedPackage,
$targetPlatform,
$built,
$output,
true,
);
$outputString = $output->getOutput();
self::assertStringContainsString('Install complete: ' . $extensionPath . '/pie_test_ext.so', $outputString);
self::assertStringContainsString('You must now add "extension=pie_test_ext" to your php.ini', $outputString);
self::assertSame($extensionPath . '/pie_test_ext.so', $installedSharedObject->filePath);
self::assertFileExists($installedSharedObject->filePath);
$rmCommand = ['rm', $installedSharedObject->filePath];
if (! is_writable($installedSharedObject->filePath)) {
array_unshift($rmCommand, 'sudo');
}
(new Process($rmCommand))->mustRun();
(new Process(['make', 'clean'], $downloadedPackage->extractedSourcePath))->mustRun();
(new Process(['phpize', '--clean'], $downloadedPackage->extractedSourcePath))->mustRun();
(new Filesystem())->remove($installRoot);
putenv('INSTALL_ROOT=' . $oldInstallRoot);
}
#[DataProvider('phpPathProvider')]
public function testUnixInstallCanInstallPrePackagedBinary(string $phpConfig): void
{
$installRoot = '/tmp/' . uniqid('pie-test-install-root-', true);
$oldInstallRoot = getenv('INSTALL_ROOT');
putenv('INSTALL_ROOT=' . $installRoot);
assert($phpConfig !== '');
if (Platform::isWindows()) {
self::markTestSkipped('Unix build test cannot be run on Windows');
}
$output = new BufferIO();
$targetPlatform = TargetPlatform::fromPhpBinaryPath(PhpBinaryPath::fromPhpConfigExecutable($phpConfig), null, null);
$extensionPath = $installRoot . $targetPlatform->phpBinaryPath->extensionPath();
mkdir($extensionPath, 0777, true);
// First build it (otherwise the test assets would need to have a binary for every test platform...)
$composerPackage = $this->createMock(CompletePackageInterface::class);
$composerPackage
->method('getExtra')
->willReturn([self::COMPOSER_PACKAGE_EXTRA_KEY => DownloadUrlMethod::ComposerDefaultDownload->value]);
$built = (new UnixBuild())->__invoke(
DownloadedPackage::fromPackageAndExtractedPath(
new Package(
$composerPackage,
ExtensionType::PhpModule,
ExtensionName::normaliseFromString('pie_test_ext'),
'pie_test_ext',
'0.1.0',
null,
),
self::TEST_EXTENSION_PATH,
),
$targetPlatform,
['--enable-pie_test_ext'],
$output,
);
if (! file_exists(self::TEST_PREBUILT_PATH)) {
mkdir(self::TEST_PREBUILT_PATH, 0777, true);
}
/**
* Move the built .so into a new path; this simulates a pre-packaged binary, which would not have Makefile etc
* so this ensures we're not accidentally relying on any build mechanism (`make install` or otherwise)
*/
$prebuiltBinaryFilePath = self::TEST_PREBUILT_PATH . DIRECTORY_SEPARATOR . 'pie_test_ext.so';
rename($built->filePath, $prebuiltBinaryFilePath);
$prebuiltBinaryFile = BinaryFile::fromFileWithSha256Checksum($prebuiltBinaryFilePath);
$composerPackage = $this->createMock(CompletePackageInterface::class);
$composerPackage
->method('getExtra')
->willReturn([self::COMPOSER_PACKAGE_EXTRA_KEY => DownloadUrlMethod::PrePackagedBinary->value]);
$installedSharedObject = (new UnixInstall(new SetupIniFile(new PickBestSetupIniApproach([]))))->__invoke(
DownloadedPackage::fromPackageAndExtractedPath(
new Package(
$composerPackage,
ExtensionType::PhpModule,
ExtensionName::normaliseFromString('pie_test_ext'),
'pie_test_ext',
'0.1.0',
null,
),
self::TEST_PREBUILT_PATH,
),
$targetPlatform,
$prebuiltBinaryFile,
$output,
true,
);
$outputString = $output->getOutput();
self::assertStringContainsString('Install complete: ' . $extensionPath . '/pie_test_ext.so', $outputString);
self::assertStringContainsString('You must now add "extension=pie_test_ext" to your php.ini', $outputString);
self::assertSame($extensionPath . '/pie_test_ext.so', $installedSharedObject->filePath);
self::assertFileExists($installedSharedObject->filePath);
$rmCommand = ['rm', $installedSharedObject->filePath];
if (! is_writable($installedSharedObject->filePath)) {
array_unshift($rmCommand, 'sudo');
}
(new Process($rmCommand))->mustRun();
(new Filesystem())->remove($prebuiltBinaryFile->filePath);
(new Filesystem())->remove($installRoot);
putenv('INSTALL_ROOT=' . $oldInstallRoot);
}
}