-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathBinaryEntrypointTest.php
More file actions
189 lines (168 loc) · 6.7 KB
/
Copy pathBinaryEntrypointTest.php
File metadata and controls
189 lines (168 loc) · 6.7 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
<?php
/**
* This file is part of TypePHP(AOT).
*
* @link https://www.swoole.com/aot/
* @contact service@swoole.com
*/
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
final class BinaryEntrypointTest extends TestCase
{
public function testBootstrapConstantsUseTypePhpPrefix(): void
{
self::assertFalse(defined('ROOT_PATH'));
self::assertFalse(defined('DEBUG'));
self::assertFalse(defined('TYPEPHP_PHP_SCRIPT_ENTRY'));
self::assertFalse(defined('TYPEPHP_COMPILER_EXECUTABLE'));
self::assertSame(realpath(__DIR__ . '/..'), TYPEPHP_ROOT_PATH);
}
public function testStubGeneratorDoesNotPolluteTheGlobalSymbolTable(): void
{
self::assertTrue(function_exists('TypePhp\\StubGenerator\\generateStubFile'));
self::assertTrue(class_exists('TypePhp\\StubGenerator\\ClassInfo'));
self::assertFalse(function_exists('generateStubFile'));
self::assertFalse(class_exists('ClassInfo'));
}
public function testSourceCheckoutEntrypointUsesPackageAutoloader(): void
{
$temporaryRoot = sys_get_temp_dir() . '/typephp-source-bin-' . bin2hex(random_bytes(6));
self::assertTrue(mkdir($temporaryRoot, 0777, true));
try {
$result = $this->runCompiler(TYPEPHP_ROOT_PATH . '/bin/tpc.php', $temporaryRoot);
self::assertSame(0, $result['status'], $result['stderr']);
self::assertStringContainsString('TypePHP Compiler (AOT)', $result['stdout']);
} finally {
$this->removeDirectory($temporaryRoot);
}
}
public function testComposerVendorEntrypointUsesProjectAutoloader(): void
{
$temporaryRoot = sys_get_temp_dir() . '/typephp-composer-bin-' . bin2hex(random_bytes(6));
$packageRoot = $temporaryRoot . '/vendor/swoole/typephp';
try {
$proxy = $this->createComposerEntrypoint($temporaryRoot, $packageRoot);
self::assertDirectoryDoesNotExist($packageRoot . '/vendor');
$result = $this->runCompiler($proxy, $temporaryRoot);
self::assertSame(0, $result['status'], $result['stderr']);
self::assertStringContainsString('TypePHP Compiler (AOT)', $result['stdout']);
} finally {
$this->removeDirectory($temporaryRoot);
}
}
public function testComposerVendorEntrypointDryCompilesProject(): void
{
if (!$this->hasPhpxLibrary()) {
self::markTestSkipped('A built PHPX library is required for the Zend PHP compiler integration test');
}
$temporaryRoot = sys_get_temp_dir() . '/typephp-composer-project-' . bin2hex(random_bytes(6));
$packageRoot = $temporaryRoot . '/vendor/swoole/typephp';
try {
$proxy = $this->createComposerEntrypoint($temporaryRoot, $packageRoot);
self::assertNotFalse(file_put_contents(
$temporaryRoot . '/main.php',
"<?php\nfunction main(): void {}\n",
));
self::assertNotFalse(file_put_contents(
$temporaryRoot . '/project.yml',
"name: composer-entrypoint-test\nbuild-dir: build\nsources:\n - main.php\n",
));
$result = $this->runCompiler(
$proxy,
$temporaryRoot,
['project.yml', '--dry', '--no-color'],
);
self::assertSame(0, $result['status'], $result['stderr']);
self::assertStringContainsString('Dry run completed:', $result['stdout']);
} finally {
$this->removeDirectory($temporaryRoot);
}
}
/** @return array{status: int, stdout: string, stderr: string} */
private function runCompiler(
string $entrypoint,
string $workingDirectory,
array $arguments = ['--version', '--no-color'],
): array {
$process = proc_open([PHP_BINARY, $entrypoint, ...$arguments], [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
2 => ['pipe', 'w'],
], $pipes, $workingDirectory, null, ['suppress_errors' => true]);
self::assertIsResource($process);
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
return [
'status' => proc_close($process),
'stdout' => (string) $stdout,
'stderr' => (string) $stderr,
];
}
private function createComposerEntrypoint(string $temporaryRoot, string $packageRoot): string
{
$this->copyPackageEntrypoint($packageRoot);
$proxy = $temporaryRoot . '/vendor/bin/tpc.php';
$proxySource = sprintf(
"<?php\n\$GLOBALS['_composer_autoload_path'] = %s;\nrequire %s;\n",
var_export(TYPEPHP_ROOT_PATH . '/vendor/autoload.php', true),
var_export($packageRoot . '/bin/tpc.php', true),
);
self::assertNotFalse(file_put_contents($proxy, $proxySource));
return $proxy;
}
private function hasPhpxLibrary(): bool
{
$phpxHome = getenv('PHPX_HOME');
$roots = [TYPEPHP_ROOT_PATH . '/vendor/swoole/phpx'];
if (is_string($phpxHome) && $phpxHome !== '') {
array_unshift($roots, $phpxHome);
}
foreach ($roots as $root) {
foreach (['lib/libphpx.so', 'lib/libphpx.a', 'lib/phpx.lib', 'bin/phpx.dll'] as $library) {
if (is_file($root . '/' . $library)) {
return true;
}
}
}
return false;
}
private function copyPackageEntrypoint(string $packageRoot): void
{
$files = [
'bin/bootstrap.php',
'bin/tpc.php',
'src/compiler.php',
'src/gen_stub.php',
'src/polyfills.php',
];
foreach ($files as $file) {
$destination = $packageRoot . '/' . $file;
$directory = dirname($destination);
if (!is_dir($directory)) {
self::assertTrue(mkdir($directory, 0777, true));
}
self::assertTrue(copy(TYPEPHP_ROOT_PATH . '/' . $file, $destination));
}
self::assertTrue(mkdir(dirname($packageRoot, 2) . '/bin', 0777, true));
}
private function removeDirectory(string $directory): void
{
if (!is_dir($directory)) {
return;
}
$items = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST,
);
foreach ($items as $item) {
$item->isDir() ? rmdir($item->getPathname()) : unlink($item->getPathname());
}
rmdir($directory);
}
}