Skip to content

Commit f3bfe3d

Browse files
authored
Forward runtime environment to PHPUnit (#1861)
1 parent 0ecc975 commit f3bfe3d

4 files changed

Lines changed: 35 additions & 6 deletions

File tree

packages/runtime-playground/src/php-bootstrap.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,16 @@ function recipeActivePluginMetadata(spec: RuntimeCreateSpec): RecipePluginMetada
134134
return plugins.filter((plugin) => typeof plugin.pluginFile === "string")
135135
}
136136

137-
export async function phpCodeFromArgs(args: string[], command = "wordpress.run-php"): Promise<string> {
137+
export async function phpCodeFromArgs(args: string[], command = "wordpress.run-php", normalize = true): Promise<string> {
138138
const inlineCode = argValue(args, "code")
139139
if (inlineCode) {
140-
return normalizePhpCode(inlineCode)
140+
return normalize ? normalizePhpCode(inlineCode) : inlineCode
141141
}
142142

143143
const codeFile = argValue(args, "code-file")
144144
if (codeFile) {
145-
return normalizePhpCode(await readFile(resolveCommandPath(codeFile), "utf8"))
145+
const code = await readFile(resolveCommandPath(codeFile), "utf8")
146+
return normalize ? normalizePhpCode(code) : code
146147
}
147148

148149
throw new Error(`${command} requires code=<php> or code-file=<path>`)

packages/runtime-playground/src/playground-runtime.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,7 @@ class PlaygroundRuntime implements Runtime {
17251725
artifactRoot: this.artifactRoot,
17261726
mounts: this.mounts,
17271727
runPlaygroundCommand: (command, targetServer, options) => this.runPlaygroundCommand(command, targetServer, options),
1728+
runtimeSpec: this.spec,
17281729
server,
17291730
spec,
17301731
})

packages/runtime-playground/src/wordpress-command-runners.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -895,12 +895,14 @@ export async function runPhpunitCommand({
895895
artifactRoot,
896896
mounts,
897897
runPlaygroundCommand,
898+
runtimeSpec,
898899
server,
899900
spec,
900901
}: {
901902
artifactRoot: string
902903
mounts: MountSpec[]
903904
runPlaygroundCommand: RunPlaygroundCommand
905+
runtimeSpec: RuntimeCreateSpec
904906
server: PlaygroundCliServer
905907
spec: ExecutionSpec
906908
}): Promise<string> {
@@ -911,7 +913,7 @@ export async function runPhpunitCommand({
911913
const autoloadFile = argValue(args, "autoload-file")?.trim() || (bootstrapMode === "project" ? "" : "/wp-codebox-vendor/autoload.php")
912914
const autoloadFileRole = argValue(args, "autoload-file-role")?.trim() === "harness" ? "harness" : undefined
913915
const resultFile = PLUGIN_PHPUNIT_RESULT_FILE
914-
const code = explicitCode ? await phpCodeFromArgs(args, "wordpress.phpunit") : normalizePhpCode(phpunitRunCode({
916+
const code = explicitCode ? await phpCodeFromArgs(args, "wordpress.phpunit", false) : phpunitRunCode({
915917
pluginSlug,
916918
cwd: argValue(args, "cwd")?.trim() || `/wordpress/wp-content/plugins/${pluginSlug}`,
917919
autoloadFile,
@@ -932,13 +934,13 @@ export async function runPhpunitCommand({
932934
projectBootstrap: argValue(args, "project-bootstrap")?.trim() || "",
933935
multisite: booleanArg(args, "multisite"),
934936
resultFile,
935-
}))
937+
})
936938
if (!explicitCode && !pluginSlug) {
937939
throw new Error("wordpress.phpunit requires plugin-slug=<slug> when code/code-file is not provided")
938940
}
939941
let response: PlaygroundRunResponse
940942
try {
941-
response = await runPlaygroundCommand("wordpress.phpunit", server, { code })
943+
response = await runPlaygroundCommand("wordpress.phpunit", server, { code: bootstrapPhpCode(runtimeSpec, code, args) })
942944
} catch (error) {
943945
await persistPluginPhpunitResult(server, resultFile, artifactRoot)
944946
await persistVfsDiagnosticFileToHost(server, resultFile, `/wordpress/wp-content/plugins/${pluginSlug}/.pg-test-result.txt`, mounts)

tests/phpunit-project-autoload.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import { recipeExtraPluginSourceSubpath } from "../packages/cli/src/recipe-sourc
1212
import { recipeInputMountPathMap, rewriteInputMountPathArgs } from "../packages/cli/src/commands/recipe-runtime-setup.js"
1313

1414
const woocommerceAutoload = "/wordpress/wp-content/plugins/woocommerce/vendor/autoload_packages.php"
15+
const phpunitRuntimeSpec = {
16+
runtimeEnv: { TC_MYSQL_PORT: "3306" },
17+
} as never
1518

1619
function phpunitRecipeArgs(options: Omit<Parameters<typeof buildWordPressPhpunitRecipe>[0], "pluginSlug">): string[] {
1720
return buildWordPressPhpunitRecipe({ pluginSlug: "demo-plugin", ...options }).workflow.steps[0].args
@@ -332,6 +335,7 @@ await runPhpunitCommand({
332335
capturedCanonicalHarnessCode = input.code
333336
return { text: "ok", exitCode: 0 }
334337
},
338+
runtimeSpec: phpunitRuntimeSpec,
335339
server: { playground: {} } as never,
336340
spec: {
337341
command: "wordpress.phpunit",
@@ -347,6 +351,27 @@ await runPhpunitCommand({
347351
})
348352
assert.ok(capturedCanonicalHarnessCode.includes('$autoload_file = "/tmp/wp-codebox-inputs/0-wp-codebox-vendor-73845ca47d2f/autoload.php";'))
349353
assert.ok(capturedCanonicalHarnessCode.includes('$autoload_file_role = "harness";'))
354+
assert.ok(capturedCanonicalHarnessCode.includes('putenv("TC_MYSQL_PORT=3306");'), "runtime service environment is passed to the PHP executed by wordpress.phpunit")
355+
assert.ok(capturedCanonicalHarnessCode.indexOf('putenv("TC_MYSQL_PORT=3306");') < capturedCanonicalHarnessCode.indexOf("require_once '/wordpress/wp-load.php';"), "runtime environment is available to project bootstrap code")
356+
357+
let capturedExplicitCode = ""
358+
await runPhpunitCommand({
359+
artifactRoot: mkdtempSync(join(tmpdir(), "wp-codebox-phpunit-artifacts-")),
360+
mounts: [],
361+
runPlaygroundCommand: async (_command, _server, input) => {
362+
capturedExplicitCode = input.code
363+
return { text: "ok", exitCode: 0 }
364+
},
365+
runtimeSpec: phpunitRuntimeSpec,
366+
server: { playground: {} } as never,
367+
spec: {
368+
command: "wordpress.phpunit",
369+
args: ["code=<?php declare(strict_types=1); echo getenv('TC_MYSQL_PORT');", "env-json={\"TC_MYSQL_PORT\":\"3307\"}"],
370+
},
371+
})
372+
assert.equal((capturedExplicitCode.match(/declare\(strict_types=1\);/g) ?? []).length, 1, "explicit PHP is normalized once at the runtime bootstrap boundary")
373+
assert.ok(capturedExplicitCode.includes("echo getenv('TC_MYSQL_PORT');"), "explicit PHPUnit code receives the same runtime bootstrap")
374+
assert.ok(capturedExplicitCode.indexOf('putenv("TC_MYSQL_PORT=3306");') < capturedExplicitCode.lastIndexOf("TC_MYSQL_PORT"), "explicit env-json handling remains after runtime environment bootstrap")
350375

351376
const coreModeCode = corePhpunitRunCode({
352377
coreRoot: "/wordpress",

0 commit comments

Comments
 (0)