forked from oraoto/pib
-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathbasic.mjs
179 lines (120 loc) · 5.44 KB
/
basic.mjs
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
import { test } from 'node:test';
import { strict as assert } from 'node:assert';
import { PhpNode } from '../packages/php-wasm/PhpNode.mjs';
test('Can run PHP', async () => {
const php = new PhpNode({});
let stdOut = '', stdErr = '';
php.addEventListener('output', (event) => event.detail.forEach(line => void (stdOut += line)));
php.addEventListener('error', (event) => event.detail.forEach(line => void (stdErr += line)));
await php.binary;
const exitCode = await php.run(`<?php 2 + 2;`);
assert.equal(exitCode, 0);
assert.equal(stdOut, '');
assert.equal(stdErr, '');
});
test('Returns 0 as an exit code: User report', async () => {
const php = new PhpNode({ persist: {mountPath: '/host' , localPath: '/'} });
let stdOut = '', stdErr = '';
php.addEventListener('output', (event) => event.detail.forEach(line => void (stdOut += line)));
php.addEventListener('error', (event) => event.detail.forEach(line => void (stdErr += line)));
await php.binary;
const exitCode = await php.run(
'<?php function main(): int { echo "Hello World" . PHP_EOL; return 0;}; exit(main());',
);
assert.equal(stdOut, 'Hello World\n');
assert.equal(stdErr, '');
assert.equal(exitCode, 0);
});
test('Returns 0 as an exit code: Distilled', async () => {
const php = new PhpNode({ persist: {mountPath: '/host' , localPath: '/'} });
let stdOut = '', stdErr = '';
php.addEventListener('output', (event) => event.detail.forEach(line => void (stdOut += line)));
php.addEventListener('error', (event) => event.detail.forEach(line => void (stdErr += line)));
await php.binary;
const exitCode = await php.run(
'<?php exit(0);',
);
assert.equal(stdOut, '');
assert.equal(stdErr, '');
assert.equal(exitCode, 0);
});
test('Can print to STDOUT', async () => {
const php = new PhpNode();
let stdOut = '', stdErr = '';
php.addEventListener('output', (event) => event.detail.forEach(line => void (stdOut += line)));
php.addEventListener('error', (event) => event.detail.forEach(line => void (stdErr += line)));
await php.binary;
const exitCode = await php.run(`<?php echo "Hello, World!";`);
assert.equal(exitCode, 0);
assert.equal(stdOut, 'Hello, World!');
assert.equal(stdErr, '');
});
test('Can print to STDERR', async () => {
const php = new PhpNode();
let stdOut = '', stdErr = '';
php.addEventListener('output', (event) => event.detail.forEach(line => void (stdOut += line)));
php.addEventListener('error', (event) => event.detail.forEach(line => void (stdErr += line)));
await php.binary;
const exitCode = await php.run(`<?php file_put_contents("php://stderr", "Hello, World!");`);
assert.equal(exitCode, 0);
assert.equal(stdOut, '');
assert.equal(stdErr, 'Hello, World!');
});
test('Can take input on STDIN', async () => {
const php = new PhpNode();
let stdOut = '', stdErr = '', stdin = 'This is a string of data provided on STDIN.';
php.inputString(stdin);
php.addEventListener('output', (event) => event.detail.forEach(line => void (stdOut += line)));
php.addEventListener('error', (event) => event.detail.forEach(line => void (stdErr += line)));
await php.binary;
const exitCode = await php.run(`<?php echo file_get_contents('php://stdin');`);
assert.equal(exitCode, 0);
assert.equal(stdOut, stdin);
assert.equal(stdErr, '');
});
test('Can maintain memory between executions', async () => {
const php = new PhpNode();
let stdOut = '', stdErr = '';
php.addEventListener('output', (event) => event.detail.forEach(line => void (stdOut += line)));
php.addEventListener('error', (event) => event.detail.forEach(line => void (stdErr += line)));
await php.binary;
await php.run(`<?php $i = 100;`);
await php.run(`<?php $i++;`);
await php.run(`<?php echo $i . PHP_EOL;`);
assert.equal(stdOut, `101\n`);
assert.equal(stdErr, '');
});
test('Can refresh memory between executions', async () => {
const php = new PhpNode({});
let stdOut = '', stdErr = '';
php.addEventListener('output', (event) => event.detail.forEach(line => void (stdOut += line)));
php.addEventListener('error', (event) => event.detail.forEach(line => void (stdErr += line)));
await php.binary;
await php.run(`<?php $i = 100;`);
await php.run(`<?php $i++;`);
await php.refresh();
await php.run(`<?php ini_set('display_errors', 1); @error_reporting(E_ALL | E_STRICT); var_dump($i) . PHP_EOL;`);
assert.equal(stdOut, `\nWarning: Undefined variable $i in php-wasm run script on line 1\nNULL\n`);
assert.equal(stdErr, '');
});
test('Can read files from the local FS through PHP functions', async () => {
const php = new PhpNode( { persist: { mountPath: '/persist', localPath: process.cwd() + '/test/' } } );
let stdOut = '', stdErr = '';
php.addEventListener('output', (event) => event.detail.forEach(line => void (stdOut += line)));
php.addEventListener('error', (event) => event.detail.forEach(line => void (stdErr += line)));
await php.binary;
await php.run(`<?php echo file_get_contents('/persist/test-content.txt');`);
assert.equal(stdOut, `Hello, world!\n`);
assert.equal(stdErr, '');
});
test('PIB extension is enabled.', async () => {
const php = new PhpNode();
let stdOut = '', stdErr = '';
php.addEventListener('output', (event) => event.detail.forEach(line => void (stdOut += line)));
php.addEventListener('error', (event) => event.detail.forEach(line => void (stdErr += line)));
await php.binary;
const exitCode = await php.run(`<?php var_dump(extension_loaded('pib'));`);
assert.equal(exitCode, 0);
assert.equal(stdOut, `bool(true)\n`);
assert.equal(stdErr, '');
});