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
10 changes: 9 additions & 1 deletion packages/runtime-playground/src/phpunit-command-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ interface PhpunitConfigDiscoveryPhpOptions {
basePathExpression: string
uniqueReturnValues: boolean
replaceDefaultMatchers: boolean
allowMissingImplicitConfig: boolean
}

interface PhpunitChangedTestFilterPhpOptions {
Expand All @@ -77,6 +78,11 @@ function phpunitConfigDiscoveryPhp(options: PhpunitConfigDiscoveryPhpOptions): s
$xml_path = $alternate;
}
}` : ""
const missingImplicitConfig = options.allowMissingImplicitConfig ? `
if (!is_readable($xml_path)) {
${options.logFunction}('NOTICE:using managed PHPUnit discovery defaults; no implicit config found at ' . $xml_path);
return $return_values();
}` : ""
const directoryRestriction = options.restrictDirectoriesToTests ? `
$normalized = trim(str_replace('\\\\', '/', $raw), '/');
if ($raw === '' || ($normalized !== 'tests' && strpos($normalized, 'tests/') !== 0)) {
Expand All @@ -99,7 +105,7 @@ function phpunitConfigDiscoveryPhp(options: PhpunitConfigDiscoveryPhpOptions): s
$files = array();
$return_values = static function() use (&$directories, &$suffixes, &$prefixes, &$excludes, &$files) {
return ${returnValues};
};${fallbackXmlDist}
};${fallbackXmlDist}${missingImplicitConfig}
if (!is_readable($xml_path)) {
throw new RuntimeException('PHPUnit config is not readable: ' . $xml_path);
}
Expand Down Expand Up @@ -1208,6 +1214,7 @@ ${phpunitConfigDiscoveryPhp({
basePathExpression: "dirname($xml_path)",
uniqueReturnValues: false,
replaceDefaultMatchers: true,
allowMissingImplicitConfig: options.phpunitXmlIsDefault,
})}

${phpunitDiscoveryPhp("wp_codebox_phpunit_discover", "pg_log")}
Expand Down Expand Up @@ -1468,6 +1475,7 @@ ${phpunitConfigDiscoveryPhp({
basePathExpression: "dirname($xml_path)",
uniqueReturnValues: true,
replaceDefaultMatchers: false,
allowMissingImplicitConfig: false,
})}

${phpunitDiscoveryPhp("core_pg_discover_tests", "core_pg_log")}
Expand Down
23 changes: 23 additions & 0 deletions tests/phpunit-project-autoload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ function assert_phpunit_error($callback, $expected, $label) {
}
throw new RuntimeException($label . ' unexpectedly succeeded');
}

${!supportsImplicitFallback ? `assert_phpunit_error(function() { ${functionName}(${phpString(explicitMissingXml)}, ${phpString(join(tempDir, "tests"))}); }, 'PHPUnit config is not readable', 'explicit missing config');` : ""}
assert_phpunit_error(function() { ${functionName}(${phpString(malformedXml)}, ${phpString(join(tempDir, "tests"))}); }, 'PHPUnit config could not be parsed', 'malformed config');
assert_phpunit_error(function() { ${discoveryFunctionName}(array(${phpString(join(tempDir, "missing-tests"))}), array('Test.php'), array('test-'), array()); }, 'configured PHPUnit test directory is not a readable directory', 'missing configured directory');
Expand All @@ -114,6 +115,27 @@ echo "ok\n";
assert.equal(execFileSync("php", [scriptPath], { encoding: "utf8" }), "ok\n")
}

function assertConfiglessPluginUsesManagedDiscovery(source: string): void {
const tempDir = mkdtempSync(join(tmpdir(), "wp-codebox-configless-plugin-"))
const testsDir = join(tempDir, "tests")
const testFile = join(testsDir, "ExampleTest.php")
const scriptPath = join(tempDir, "assert-configless-discovery.php")
mkdirSync(testsDir)
writeFileSync(testFile, "<?php final class ExampleTest extends WP_UnitTestCase {}\n")
writeFileSync(scriptPath, `<?php
function pg_log($message) {}
${extractPhpFunction(source, "wp_codebox_phpunit_parse_config")}
${extractPhpFunction(source, "wp_codebox_phpunit_discover")}
list($directories, $suffixes, $prefixes, $excludes, $files) = wp_codebox_phpunit_parse_config(${phpString(join(tempDir, "phpunit.xml.dist"))}, ${phpString(testsDir)});
$discovered = wp_codebox_phpunit_discover($directories, $suffixes, $prefixes, $excludes, $files);
if ($discovered !== array(${phpString(testFile)})) {
throw new RuntimeException('managed discovery did not find the configless plugin test: ' . json_encode($discovered));
}
echo "ok\n";
`)
assert.equal(execFileSync("php", [scriptPath], { encoding: "utf8" }), "ok\n")
}

function assertChangedScopeNoOp(source: string, filterFunctionName: string, relativeFunctionName: string): void {
const tempDir = mkdtempSync(join(tmpdir(), "wp-codebox-changed-scope-"))
const scriptPath = join(tempDir, "assert-changed-scope.php")
Expand Down Expand Up @@ -437,6 +459,7 @@ const implicitProjectConfigCode = phpunitRunCode({
projectBootstrap: "",
multisite: false,
})
assertConfiglessPluginUsesManagedDiscovery(implicitProjectConfigCode)

const bootIndex = projectModeCode.indexOf("$config_path = pg_run_boot_stage")
const projectBootstrapIndex = projectModeCode.indexOf("pg_run_project_bootstrap_stage", bootIndex)
Expand Down
Loading