diff --git a/packages/runtime-playground/src/phpunit-command-handlers.ts b/packages/runtime-playground/src/phpunit-command-handlers.ts index 961c2b61..e3679e57 100644 --- a/packages/runtime-playground/src/phpunit-command-handlers.ts +++ b/packages/runtime-playground/src/phpunit-command-handlers.ts @@ -59,6 +59,7 @@ interface PhpunitConfigDiscoveryPhpOptions { basePathExpression: string uniqueReturnValues: boolean replaceDefaultMatchers: boolean + allowMissingImplicitConfig: boolean } interface PhpunitChangedTestFilterPhpOptions { @@ -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)) { @@ -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); } @@ -1208,6 +1214,7 @@ ${phpunitConfigDiscoveryPhp({ basePathExpression: "dirname($xml_path)", uniqueReturnValues: false, replaceDefaultMatchers: true, + allowMissingImplicitConfig: options.phpunitXmlIsDefault, })} ${phpunitDiscoveryPhp("wp_codebox_phpunit_discover", "pg_log")} @@ -1468,6 +1475,7 @@ ${phpunitConfigDiscoveryPhp({ basePathExpression: "dirname($xml_path)", uniqueReturnValues: true, replaceDefaultMatchers: false, + allowMissingImplicitConfig: false, })} ${phpunitDiscoveryPhp("core_pg_discover_tests", "core_pg_log")} diff --git a/tests/phpunit-project-autoload.test.ts b/tests/phpunit-project-autoload.test.ts index d51f53d8..71ff862e 100644 --- a/tests/phpunit-project-autoload.test.ts +++ b/tests/phpunit-project-autoload.test.ts @@ -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'); @@ -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, "