Skip to content

Commit 8d49e91

Browse files
authored
Fix project-mode PHPUnit builder autoload default (#1836)
1 parent 486ea59 commit 8d49e91

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

packages/runtime-core/src/recipe-builders.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export function normalizeRecipeMounts(mounts: readonly WorkspaceRecipeMount[] =
6767
export function buildWordPressPhpunitRecipe(options: WordPressPhpunitRecipeOptions): WorkspaceRecipe {
6868
const pluginSlug = requiredPluginSlug(options.pluginSlug, "buildWordPressPhpunitRecipe")
6969
const pluginTarget = `/wordpress/wp-content/plugins/${pluginSlug}`
70+
const autoloadFile = options.autoloadFile ?? (options.bootstrapMode === "project" ? "" : "/wp-codebox-vendor/autoload.php")
7071

7172
return {
7273
schema: "wp-codebox/workspace-recipe/v1",
@@ -93,8 +94,8 @@ export function buildWordPressPhpunitRecipe(options: WordPressPhpunitRecipeOptio
9394
commandJsonArg("changed-tests-json", options.changedTestFiles ?? []),
9495
commandJsonArg("env-json", options.env ?? {}),
9596
commandJsonArg("wp-config-defines-json", options.wpConfigDefines ?? {}),
96-
commandArg("autoload-file", options.autoloadFile ?? "/wp-codebox-vendor/autoload.php"),
97-
commandArg("autoload-file-role", "harness"),
97+
commandArg("autoload-file", autoloadFile),
98+
commandArg("autoload-file-role", autoloadFile ? "harness" : ""),
9899
commandArg("project-autoload-file", options.projectAutoloadFile ?? ""),
99100
commandArg("tests-dir", options.testsDir ?? "/wp-codebox-vendor/wp-phpunit/wp-phpunit"),
100101
commandArg("test-root", options.testRoot ?? `${pluginTarget}/tests`),

tests/phpunit-project-autoload.test.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,27 @@ import { recipeInputMountPathMap, rewriteInputMountPathArgs } from "../packages/
1313

1414
const woocommerceAutoload = "/wordpress/wp-content/plugins/woocommerce/vendor/autoload_packages.php"
1515

16+
function phpunitRecipeArgs(options: Omit<Parameters<typeof buildWordPressPhpunitRecipe>[0], "pluginSlug">): string[] {
17+
return buildWordPressPhpunitRecipe({ pluginSlug: "demo-plugin", ...options }).workflow.steps[0].args
18+
}
19+
20+
const projectRecipeWithoutAutoload = phpunitRecipeArgs({ bootstrapMode: "project" })
21+
assert.ok(projectRecipeWithoutAutoload.includes("autoload-file="))
22+
assert.ok(!projectRecipeWithoutAutoload.some((arg) => arg === "autoload-file-role=harness"), "project mode without an autoload file must not require the harness")
23+
24+
const managedRecipe = phpunitRecipeArgs({})
25+
assert.ok(managedRecipe.includes("autoload-file=/wp-codebox-vendor/autoload.php"))
26+
assert.ok(managedRecipe.includes("autoload-file-role=harness"))
27+
28+
const explicitAutoloadRecipe = phpunitRecipeArgs({
29+
bootstrapMode: "project",
30+
autoloadFile: "/wp-codebox-vendor/autoload.php",
31+
projectAutoloadFile: "/workspace/project/vendor/autoload.php",
32+
})
33+
assert.ok(explicitAutoloadRecipe.includes("autoload-file=/wp-codebox-vendor/autoload.php"))
34+
assert.ok(explicitAutoloadRecipe.includes("autoload-file-role=harness"))
35+
assert.ok(explicitAutoloadRecipe.includes("project-autoload-file=/workspace/project/vendor/autoload.php"))
36+
1637
function extractPhpFunction(source: string, functionName: string): string {
1738
const start = source.indexOf(`function ${functionName}(`)
1839
assert.notEqual(start, -1)
@@ -198,15 +219,15 @@ assert.deepEqual(recipe.inputs.mounts?.filter((mount) => mount.target === "/wp-c
198219
])
199220

200221
assert.deepEqual(recipe.workflow.steps[0].args.filter((arg) => arg.includes("autoload-file=")), [
201-
"autoload-file=/wp-codebox-vendor/autoload.php",
222+
"autoload-file=",
202223
`project-autoload-file=${woocommerceAutoload}`,
203224
])
204-
assert.ok(recipe.workflow.steps[0].args.includes("autoload-file-role=harness"), "modern PHPUnit recipes explicitly preserve harness autoload intent")
225+
assert.ok(!recipe.workflow.steps[0].args.includes("autoload-file-role=harness"), "project mode without an explicit autoload file preserves project-owned harness setup")
205226
assert.deepEqual(rewriteInputMountPathArgs(recipe.workflow.steps[0].args, recipeInputMountPathMap(recipe)).filter((arg) => arg.includes("autoload-file=")), [
206-
"autoload-file=/tmp/wp-codebox-inputs/0-wp-codebox-vendor-73845ca47d2f/autoload.php",
227+
"autoload-file=",
207228
`project-autoload-file=${woocommerceAutoload}`,
208229
])
209-
assert.ok(rewriteInputMountPathArgs(recipe.workflow.steps[0].args, recipeInputMountPathMap(recipe)).includes("autoload-file-role=harness"), "CLI path canonicalization preserves autoload intent")
230+
assert.ok(!rewriteInputMountPathArgs(recipe.workflow.steps[0].args, recipeInputMountPathMap(recipe)).includes("autoload-file-role=harness"), "CLI path canonicalization preserves absent harness autoload intent")
210231
assert.ok(recipe.workflow.steps[0].args.includes("cwd=/home/example/public_html"))
211232
assert.ok(recipe.workflow.steps[0].args.includes("test-root=/home/example/public_html/bin/tests/phpunit"))
212233
assert.ok(recipe.workflow.steps[0].args.includes("phpunit-xml=/home/example/public_html/bin/tests/phpunit/phpunit.xml.dist"))

0 commit comments

Comments
 (0)