Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/runtime-core/src/command-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
5 changes: 4 additions & 1 deletion packages/runtime-playground/src/phpunit-command-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
7 changes: 4 additions & 3 deletions tests/phpunit-project-autoload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
24 changes: 15 additions & 9 deletions tests/playground-phpunit-modern.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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`), "<?php\n/** Plugin Name: PHPUnit 11 Fixture */\n")
writeFileSync(join(plugin, "bootstrap.php"), `<?php
require __DIR__ . '/vendor/autoload.php';
writeFileSync(join(plugin, "tests", "bootstrap.php"), `<?php
PHPUnit\\Runner\\Version::id();
require getenv('WP_TESTS_DIR') . '/includes/functions.php';
`)
writeFileSync(join(plugin, "phpunit.xml.dist"), `<phpunit bootstrap="bootstrap.php">
writeFileSync(join(plugin, "phpunit.xml.dist"), `<phpunit bootstrap="tests/bootstrap.php">
<testsuites><testsuite name="fixture"><directory>tests</directory></testsuite></testsuites>
</phpunit>
`)
Expand Down Expand Up @@ -65,32 +67,36 @@ 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])
assert.ifError(build.error)
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"), /<failure\b/)

const passing = runRecipe("passing", { phpunitArgs: ["--filter", "testPass", "--no-testdox"] }, 0)
assert.match(passing.diagnostics, /^ALL TESTS PASSED$/m)
assert.match(passing.diagnostics, /^TESTS: 1 ASSERTIONS: 1 FAILURES: 0 ERRORS: 0$/m)

const selected = runRecipe("selected", { selectedTestFile: "PassingTest.php" }, 0)
assert.match(selected.diagnostics, /^TESTS: 1 ASSERTIONS: 1 FAILURES: 0 ERRORS: 0$/m)

Expand Down
Loading