Repository navigation
Expand file tree
/
Copy pathnodejs.test.ts
More file actions
64 lines (49 loc) · 1.55 KB
/
Copy pathnodejs.test.ts
File metadata and controls
64 lines (49 loc) · 1.55 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
import { PHPEngine, NodeJSExtension } from "../../index.js";
describe("Node.js Extension Interop Tests", () => {
let engine: PHPEngine;
beforeEach(() => {
engine = new PHPEngine({
watch: false,
extensions: [new NodeJSExtension()],
});
});
afterEach(() => {
engine.close();
});
test("Requires built-in Node.js module 'path' and calls method", async () => {
let out = "";
const ctx = engine.createContext({ stdout: (d) => { out += d; } });
await ctx.eval(`
$path = nodejs_require("path");
echo $path->basename("usr/local/bin/file.txt") . ";";
`);
expect(out).toBe("file.txt;");
});
test("Accesses Node.js process global variable", async () => {
let out = "";
const ctx = engine.createContext({ stdout: (d) => { out += d; } });
await ctx.eval(`
$process = nodejs_global("process");
echo $process->arch . ";";
`);
expect(out).toBe(`${process.arch};`);
});
test("Evaluates JavaScript expression via nodejs_eval", async () => {
let out = "";
const ctx = engine.createContext({ stdout: (d) => { out += d; } });
await ctx.eval(`
$res = nodejs_eval("100 + 200");
echo $res . ";";
`);
expect(out).toBe("300;");
});
test("Requires 'fs' module and checks directory existence", async () => {
let out = "";
const ctx = engine.createContext({ stdout: (d) => { out += d; } });
await ctx.eval(`
$fs = nodejs_require("fs");
echo $fs->existsSync(__DIR__) ? "EXISTS;" : "NO;";
`);
expect(out).toBe("EXISTS;");
});
});