From 3fdd4ed549f7e802f4e3ac1a8893905fd71e66b6 Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Wed, 2 Dec 2026 21:14:56 +0000 Subject: [PATCH] fix(phpunit): load project-autoload-file before the project bootstrap --- packages/runtime-core/src/command-registry.ts | 2 +- .../src/phpunit-command-handlers.ts | 5 +++- tests/phpunit-project-autoload.test.ts | 7 +++--- ...yground-phpunit-modern.integration.test.ts | 24 ++++++++++++------- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/packages/runtime-core/src/command-registry.ts b/packages/runtime-core/src/command-registry.ts index c68220e7..c6180e43 100644 --- a/packages/runtime-core/src/command-registry.ts +++ b/packages/runtime-core/src/command-registry.ts @@ -995,7 +995,7 @@ export const commandRegistry = [ { name: "code", description: "Inline override PHP runner code.", format: "PHP string" }, { name: "code-file", description: "Path to override PHP runner code.", format: "path" }, { name: "autoload-file", description: "WP Codebox/PHPUnit harness autoload path inside the sandbox. When configured, it must be readable; clear it only when project bootstrap mode loads PHPUnit itself.", format: "sandbox path" }, - { name: "project-autoload-file", description: "Project/plugin autoload path loaded after the project bootstrap has provided WordPress functions.", format: "sandbox path" }, + { name: "project-autoload-file", description: "Project/plugin autoload path loaded before the project bootstrap in project mode.", format: "sandbox path" }, { name: "tests-dir", description: "WP PHPUnit tests directory inside the sandbox.", format: "sandbox path" }, { name: "phpunit-xml", description: "phpunit.xml path inside the plugin.", format: "path" }, { name: "phpunit-xml-default", description: "Marks phpunit-xml as the standard default path, allowing an adjacent phpunit.xml fallback only when phpunit.xml.dist is absent.", format: "boolean" }, diff --git a/packages/runtime-playground/src/phpunit-command-handlers.ts b/packages/runtime-playground/src/phpunit-command-handlers.ts index 65485ae3..6446e50e 100644 --- a/packages/runtime-playground/src/phpunit-command-handlers.ts +++ b/packages/runtime-playground/src/phpunit-command-handlers.ts @@ -1444,8 +1444,11 @@ $config_path = pg_run_boot_stage(array('extra_defines' => $wp_config_defines, 't if ($bootstrap_mode === 'project') { pg_prepare_project_bootstrap_environment($config_path); pg_skip_project_bootstrap_shell_install(); + // Match vendor/bin/phpunit: load the explicit Composer autoloader before bootstrap. + pg_run_project_autoload_stage($project_autoload_file); pg_run_project_bootstrap_stage(array('project_bootstrap' => $project_bootstrap, 'phpunit_xml' => ${JSON.stringify(options.phpunitXml)}, 'phpunit_xml_is_default' => ${JSON.stringify(options.phpunitXmlIsDefault)})); - pg_run_project_autoload_stage($project_autoload_file !== '' ? $project_autoload_file : $legacy_project_autoload_file); + // A legacy autoload-file reclassified as a project autoloader still runs after bootstrap. + pg_run_project_autoload_stage($legacy_project_autoload_file); } else { if ($bootstrap_mode !== 'managed') { pg_log('NOTICE:unknown bootstrap-mode ' . var_export($bootstrap_mode, true) . '; using managed'); diff --git a/tests/phpunit-project-autoload.test.ts b/tests/phpunit-project-autoload.test.ts index a69b9b3c..72fb83b6 100644 --- a/tests/phpunit-project-autoload.test.ts +++ b/tests/phpunit-project-autoload.test.ts @@ -588,12 +588,13 @@ assertConfiglessPluginUsesManagedDiscovery(implicitProjectConfigCode) const bootIndex = projectModeCode.indexOf("$config_path = pg_run_boot_stage") const projectBootstrapIndex = projectModeCode.indexOf("pg_run_project_bootstrap_stage", bootIndex) -const projectAutoloadIndex = projectModeCode.indexOf("pg_run_project_autoload_stage", projectBootstrapIndex) +const projectAutoloadIndex = projectModeCode.indexOf("pg_run_project_autoload_stage($project_autoload_file);", bootIndex) +const legacyAutoloadIndex = projectModeCode.indexOf("pg_run_project_autoload_stage($legacy_project_autoload_file);", bootIndex) const phpunitArgvIndex = projectModeCode.indexOf("$_SERVER['argv'] = $phpunit_argv;") assert.ok(bootIndex > 0) assert.ok(phpunitArgvIndex > 0 && phpunitArgvIndex < projectBootstrapIndex, "project bootstrap must receive forwarded PHPUnit arguments") -assert.ok(projectBootstrapIndex > bootIndex) -assert.ok(projectAutoloadIndex > projectBootstrapIndex) +assert.ok(projectAutoloadIndex > bootIndex && projectAutoloadIndex < projectBootstrapIndex, "explicit project autoload must run before project bootstrap") +assert.ok(legacyAutoloadIndex > projectBootstrapIndex, "legacy project autoload must still run after project bootstrap") assert.ok(projectModeCode.includes("'autoload_required' => $bootstrap_mode !== 'project' || $harness_autoload_file !== ''")) assert.ok(projectModeCode.includes("$legacy_project_autoload_file = $autoload_file")) assert.ok(projectModeCode.includes('$autoload_file_role = "";'), "direct callers without an explicit role retain the legacy compatibility path") diff --git a/tests/playground-phpunit-modern.integration.test.ts b/tests/playground-phpunit-modern.integration.test.ts index 8620ec97..533b7c60 100644 --- a/tests/playground-phpunit-modern.integration.test.ts +++ b/tests/playground-phpunit-modern.integration.test.ts @@ -6,6 +6,8 @@ import { join } from "node:path" import { test } from "node:test" const pluginSlug = "modern-phpunit" +const sandboxPlugin = `/wordpress/wp-content/plugins/${pluginSlug}` +const projectAutoloadFile = `${sandboxPlugin}/vendor/autoload.php` const cli = "packages/cli/dist/index.js" function run(command: string, args: string[], cwd?: string) { @@ -33,11 +35,11 @@ test("project recipes run and list tests with Composer-installed PHPUnit 11", (t assert.equal(install.status, 0, installOutput) writeFileSync(join(plugin, `${pluginSlug}.php`), " + writeFileSync(join(plugin, "phpunit.xml.dist"), ` tests `) @@ -65,7 +67,9 @@ final class FailingTest extends PHPUnit\\Framework\\TestCase { pluginSource: plugin, bootstrapMode: "project", phpVersion: "8.3", - testsDir: `/wordpress/wp-content/plugins/${pluginSlug}/vendor/wp-phpunit/wp-phpunit`, + autoloadFile: "", + projectAutoloadFile, + testsDir: `${sandboxPlugin}/vendor/wp-phpunit/wp-phpunit`, ...options, })) const build = run(process.execPath, [cli, "recipe", "build", "phpunit", "--options", optionsPath, "--output", recipe]) @@ -73,24 +77,26 @@ final class FailingTest extends PHPUnit\\Framework\\TestCase { assert.equal(build.status, 0, build.stderr) const result = run(process.execPath, [cli, "recipe-run", "--recipe", recipe, "--artifacts", artifacts, "--json"]) assert.ifError(result.error) - assert.equal(result.status, expectedExit, result.stderr) const latest = JSON.parse(readFileSync(join(artifacts, "latest-runtime.json"), "utf8")) const directory = join(artifacts, latest.paths.runtimeDirectory, "files", "phpunit") const diagnostics = readFileSync(join(directory, ".pg-test-result.txt"), "utf8") + assert.equal(result.status, expectedExit, diagnostics + result.stderr) assert.match(diagnostics, /^STAGE_OK:run_tests$/m, diagnostics) assert.doesNotMatch(diagnostics, /^STAGE_FAIL:/m) return { diagnostics, stdout: result.stdout, directory } } + const passing = runRecipe("passing", { phpunitArgs: ["--filter", "testPass", "--no-testdox"] }, 0) + assert.match(passing.diagnostics, /^STAGE_OK:project_autoload$[\s\S]*^STAGE_BEGIN:project_bootstrap$/m) + assert.ok(passing.diagnostics.includes(`PROJECT_AUTOLOAD:${projectAutoloadFile}\n`), passing.diagnostics) + assert.match(passing.diagnostics, /^ALL TESTS PASSED$/m) + assert.match(passing.diagnostics, /^TESTS: 1 ASSERTIONS: 1 FAILURES: 0 ERRORS: 0$/m) + const mixed = runRecipe("mixed", {}, 1) assert.match(mixed.diagnostics, /^SOME TESTS FAILED$/m) assert.match(mixed.diagnostics, /^TESTS: 2 ASSERTIONS: 2 FAILURES: 1 ERRORS: 0$/m) assert.match(readFileSync(join(mixed.directory, ".wp-codebox-junit.xml"), "utf8"), /