Skip to content

Commit 1d8d764

Browse files
committed
Merge remote-tracking branch 'origin/main' into fix/1838-cloudflare-bounded-browser
# Conflicts: # tests/playground-phpunit-readonly-cache.integration.test.ts
2 parents 0f144aa + 95a3778 commit 1d8d764

3 files changed

Lines changed: 33 additions & 2 deletions

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
}
@@ -1206,6 +1212,7 @@ ${phpunitConfigDiscoveryPhp({
12061212
basePathExpression: "dirname($xml_path)",
12071213
uniqueReturnValues: false,
12081214
replaceDefaultMatchers: true,
1215+
allowMissingImplicitConfig: options.phpunitXmlIsDefault,
12091216
})}
12101217
12111218
${phpunitDiscoveryPhp("wp_codebox_phpunit_discover", "pg_log")}
@@ -1466,6 +1473,7 @@ ${phpunitConfigDiscoveryPhp({
14661473
basePathExpression: "dirname($xml_path)",
14671474
uniqueReturnValues: true,
14681475
replaceDefaultMatchers: false,
1476+
allowMissingImplicitConfig: false,
14691477
})}
14701478
14711479
${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)

tests/playground-phpunit-readonly-cache.integration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ async function writeFixture(): Promise<void> {
6262
await mkdir(join(plugin, "tests"), { recursive: true })
6363
await mkdir(dependency, { recursive: true })
6464
await writeFile(join(plugin, "readonly-phpunit-fixture.php"), "<?php\n/**\n * Plugin Name: Readonly PHPUnit Fixture\n */\n")
65-
await writeFile(join(plugin, "phpunit.xml.dist"), "<?xml version=\"1.0\"?><phpunit><testsuites><testsuite name=\"readonly\"><directory>tests</directory></testsuite></testsuites></phpunit>\n")
65+
await writeFile(join(plugin, "phpunit.xml.dist"), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<phpunit><testsuites><testsuite name=\"readonly-cache\"><directory>tests</directory></testsuite></testsuites></phpunit>\n")
6666
await writeFile(join(plugin, "source-sentinel.bin"), sentinel)
6767
await writeFile(join(plugin, "tests", "ReadonlyCacheTest.php"), "<?php\nclass ReadonlyCacheTest extends WP_UnitTestCase { public function test_multisite_runtime_is_active(): void { $this->assertTrue(is_multisite()); } public function test_sentinel_is_available(): void { $this->assertGreaterThan(0, filesize(dirname(__DIR__) . \'/source-sentinel.bin\')); } public function test_dependency_activation_runs_after_install(): void { $this->assertGreaterThanOrEqual(1, get_option(\'wp_codebox_dependency_activation_users\')); } public function test_dependency_plugins_loaded_runs_once(): void { $this->assertSame(1, (int) get_option(\'wp_codebox_dependency_plugins_loaded_count\')); } public function test_wp_cli_namespaced_stdout_is_available(): void { $this->assertTrue(eval(\'namespace cli; return is_resource(STDOUT);\')); } }\n")
6868
await writeFile(join(dependency, "activation-dependency.php"), "<?php\n/**\n * Plugin Name: Activation Dependency\n */\nadd_action('plugins_loaded', static function (): void { update_option('wp_codebox_dependency_plugins_loaded_count', (int) get_option('wp_codebox_dependency_plugins_loaded_count', 0) + 1); });\nregister_activation_hook(__FILE__, static function (): void { update_option('wp_codebox_dependency_activation_users', count(get_users(array('number' => 1)))); });\n")

0 commit comments

Comments
 (0)