Skip to content

Commit c3e4a5a

Browse files
authored
Allow managed PHPUnit discovery without config (#1944)
1 parent a9edf31 commit c3e4a5a

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

packages/runtime-playground/src/phpunit-command-handlers.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ interface PhpunitConfigDiscoveryPhpOptions {
5959
basePathExpression: string
6060
uniqueReturnValues: boolean
6161
replaceDefaultMatchers: boolean
62+
allowMissingImplicitConfig: boolean
6263
}
6364

6465
interface PhpunitChangedTestFilterPhpOptions {
@@ -77,6 +78,11 @@ function phpunitConfigDiscoveryPhp(options: PhpunitConfigDiscoveryPhpOptions): s
7778
$xml_path = $alternate;
7879
}
7980
}` : ""
81+
const missingImplicitConfig = options.allowMissingImplicitConfig ? `
82+
if (!is_readable($xml_path)) {
83+
${options.logFunction}('NOTICE:using managed PHPUnit discovery defaults; no implicit config found at ' . $xml_path);
84+
return $return_values();
85+
}` : ""
8086
const directoryRestriction = options.restrictDirectoriesToTests ? `
8187
$normalized = trim(str_replace('\\\\', '/', $raw), '/');
8288
if ($raw === '' || ($normalized !== 'tests' && strpos($normalized, 'tests/') !== 0)) {
@@ -99,7 +105,7 @@ function phpunitConfigDiscoveryPhp(options: PhpunitConfigDiscoveryPhpOptions): s
99105
$files = array();
100106
$return_values = static function() use (&$directories, &$suffixes, &$prefixes, &$excludes, &$files) {
101107
return ${returnValues};
102-
};${fallbackXmlDist}
108+
};${fallbackXmlDist}${missingImplicitConfig}
103109
if (!is_readable($xml_path)) {
104110
throw new RuntimeException('PHPUnit config is not readable: ' . $xml_path);
105111
}
@@ -1208,6 +1214,7 @@ ${phpunitConfigDiscoveryPhp({
12081214
basePathExpression: "dirname($xml_path)",
12091215
uniqueReturnValues: false,
12101216
replaceDefaultMatchers: true,
1217+
allowMissingImplicitConfig: options.phpunitXmlIsDefault,
12111218
})}
12121219
12131220
${phpunitDiscoveryPhp("wp_codebox_phpunit_discover", "pg_log")}
@@ -1468,6 +1475,7 @@ ${phpunitConfigDiscoveryPhp({
14681475
basePathExpression: "dirname($xml_path)",
14691476
uniqueReturnValues: true,
14701477
replaceDefaultMatchers: false,
1478+
allowMissingImplicitConfig: false,
14711479
})}
14721480
14731481
${phpunitDiscoveryPhp("core_pg_discover_tests", "core_pg_log")}

tests/phpunit-project-autoload.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ function assert_phpunit_error($callback, $expected, $label) {
9797
}
9898
throw new RuntimeException($label . ' unexpectedly succeeded');
9999
}
100+
100101
${!supportsImplicitFallback ? `assert_phpunit_error(function() { ${functionName}(${phpString(explicitMissingXml)}, ${phpString(join(tempDir, "tests"))}); }, 'PHPUnit config is not readable', 'explicit missing config');` : ""}
101102
assert_phpunit_error(function() { ${functionName}(${phpString(malformedXml)}, ${phpString(join(tempDir, "tests"))}); }, 'PHPUnit config could not be parsed', 'malformed config');
102103
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');
@@ -114,6 +115,27 @@ echo "ok\n";
114115
assert.equal(execFileSync("php", [scriptPath], { encoding: "utf8" }), "ok\n")
115116
}
116117

118+
function assertConfiglessPluginUsesManagedDiscovery(source: string): void {
119+
const tempDir = mkdtempSync(join(tmpdir(), "wp-codebox-configless-plugin-"))
120+
const testsDir = join(tempDir, "tests")
121+
const testFile = join(testsDir, "ExampleTest.php")
122+
const scriptPath = join(tempDir, "assert-configless-discovery.php")
123+
mkdirSync(testsDir)
124+
writeFileSync(testFile, "<?php final class ExampleTest extends WP_UnitTestCase {}\n")
125+
writeFileSync(scriptPath, `<?php
126+
function pg_log($message) {}
127+
${extractPhpFunction(source, "wp_codebox_phpunit_parse_config")}
128+
${extractPhpFunction(source, "wp_codebox_phpunit_discover")}
129+
list($directories, $suffixes, $prefixes, $excludes, $files) = wp_codebox_phpunit_parse_config(${phpString(join(tempDir, "phpunit.xml.dist"))}, ${phpString(testsDir)});
130+
$discovered = wp_codebox_phpunit_discover($directories, $suffixes, $prefixes, $excludes, $files);
131+
if ($discovered !== array(${phpString(testFile)})) {
132+
throw new RuntimeException('managed discovery did not find the configless plugin test: ' . json_encode($discovered));
133+
}
134+
echo "ok\n";
135+
`)
136+
assert.equal(execFileSync("php", [scriptPath], { encoding: "utf8" }), "ok\n")
137+
}
138+
117139
function assertChangedScopeNoOp(source: string, filterFunctionName: string, relativeFunctionName: string): void {
118140
const tempDir = mkdtempSync(join(tmpdir(), "wp-codebox-changed-scope-"))
119141
const scriptPath = join(tempDir, "assert-changed-scope.php")
@@ -437,6 +459,7 @@ const implicitProjectConfigCode = phpunitRunCode({
437459
projectBootstrap: "",
438460
multisite: false,
439461
})
462+
assertConfiglessPluginUsesManagedDiscovery(implicitProjectConfigCode)
440463

441464
const bootIndex = projectModeCode.indexOf("$config_path = pg_run_boot_stage")
442465
const projectBootstrapIndex = projectModeCode.indexOf("pg_run_project_bootstrap_stage", bootIndex)

0 commit comments

Comments
 (0)